-
Notifications
You must be signed in to change notification settings - Fork 21
feat: add ChapterContext and per-chapter line counts #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eca5f58
refactor: extract LineCounts shared component
dastratakos 0540ca0
feat: add ChapterContext with provider and line counts
dastratakos a6bb855
feat: add line counts to chapter navigator dropdown
dastratakos d79e68b
feat: add line counts to chapter side panel header
dastratakos 91f7bea
feat: add line counts to chapters index page
dastratakos 1282050
refactor: simplify ChapterDetailContent props via context
dastratakos d619d79
fix: handle zero line counts spacing in side panel header
dastratakos ea62866
fix: use context chapters instead of duplicated local sort
dastratakos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { cn } from "@/lib/utils"; | ||
|
|
||
| interface LineCountsProps { | ||
| additions: number; | ||
| deletions: number; | ||
| className?: string; | ||
| } | ||
|
|
||
| export function LineCounts({ additions, deletions, className }: LineCountsProps) { | ||
| if (additions === 0 && deletions === 0) return null; | ||
| return ( | ||
| <div className={cn("flex items-center gap-1 font-medium text-[10px] tabular-nums", className)}> | ||
| {additions > 0 && <span className="text-green-600 dark:text-green-500">+{additions}</span>} | ||
| {deletions > 0 && <span className="text-red-600 dark:text-red-500">-{deletions}</span>} | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import type { Chapter } from "@stage-cli/types/chapters"; | ||
| import { createContext, type ReactNode, use, useMemo } from "react"; | ||
| import { filterFilesForChapter } from "./filter-files-for-chapter"; | ||
| import { useChapters } from "./use-chapters"; | ||
| import { useDiffPatch } from "./use-diff-patch"; | ||
|
|
||
| export interface ChapterLineCounts { | ||
| linesAdded: number; | ||
| linesDeleted: number; | ||
| } | ||
|
|
||
| interface ChapterContextValue { | ||
| runId: string; | ||
| chapters: readonly Chapter[]; | ||
| chapterLineCountsMap: ReadonlyMap<string, ChapterLineCounts>; | ||
| } | ||
|
|
||
| const ChapterContext = createContext<ChapterContextValue | null>(null); | ||
|
|
||
| function buildChapterLineCountsMap( | ||
| chapters: readonly Chapter[], | ||
| patch: string | undefined, | ||
| ): ReadonlyMap<string, ChapterLineCounts> { | ||
| const map = new Map<string, ChapterLineCounts>(); | ||
| if (!patch) return map; | ||
| for (const chapter of chapters) { | ||
| const entries = filterFilesForChapter(patch, chapter.hunkRefs); | ||
| let linesAdded = 0; | ||
| let linesDeleted = 0; | ||
| for (const entry of entries) { | ||
| linesAdded += entry.file.additions; | ||
| linesDeleted += entry.file.deletions; | ||
|
dastratakos marked this conversation as resolved.
|
||
| } | ||
| map.set(chapter.id, { linesAdded, linesDeleted }); | ||
| } | ||
|
dastratakos marked this conversation as resolved.
|
||
| return map; | ||
|
dastratakos marked this conversation as resolved.
|
||
| } | ||
|
|
||
| export function ChapterProvider({ runId, children }: { runId: string; children: ReactNode }) { | ||
| const { data: chaptersData } = useChapters(runId); | ||
| const { data: patch } = useDiffPatch(runId); | ||
|
|
||
| const chapters = useMemo<readonly Chapter[]>(() => { | ||
| if (!chaptersData?.chapters) return []; | ||
| return [...chaptersData.chapters].sort((a, b) => a.order - b.order); | ||
| }, [chaptersData?.chapters]); | ||
|
|
||
| const chapterLineCountsMap = useMemo( | ||
| () => buildChapterLineCountsMap(chapters, patch), | ||
| [chapters, patch], | ||
| ); | ||
|
|
||
| const value = useMemo<ChapterContextValue>( | ||
| () => ({ runId, chapters, chapterLineCountsMap }), | ||
| [runId, chapters, chapterLineCountsMap], | ||
| ); | ||
|
|
||
| return <ChapterContext value={value}>{children}</ChapterContext>; | ||
| } | ||
|
|
||
| export function useChapterContext(): ChapterContextValue { | ||
| const ctx = use(ChapterContext); | ||
| if (!ctx) { | ||
| throw new Error("useChapterContext must be used within a ChapterProvider"); | ||
| } | ||
| return ctx; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.