-
Notifications
You must be signed in to change notification settings - Fork 67
fix(web): select pull request on review surface #1857
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
092116b
fix(web): select pull request on review surface
zeval 26acf71
Merge remote-tracking branch 'origin/main' into feature/review-surfac…
zeval 6fe6d8d
fix(web): address review feedback
zeval 25fbe2d
test(web): stabilize tablet review target assertion
zeval 4181170
fix(web): address late review feedback
zeval 160126f
test(web): cover PR review boundary branches
zeval 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
82 changes: 82 additions & 0 deletions
82
apps/web/components/review/review-dialog-pr-state.test.tsx
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,82 @@ | ||
| import { act, cleanup, fireEvent, render, renderHook, screen } from "@testing-library/react"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import type { TaskPR } from "@/lib/types/github"; | ||
| import { | ||
| ReviewPRDiffBoundary, | ||
| shouldBlockReviewForPR, | ||
| useReviewDialogTransientState, | ||
| } from "./review-dialog-pr-state"; | ||
|
|
||
| afterEach(cleanup); | ||
|
|
||
| const selectedPR = { | ||
| repo: "widgets", | ||
| pr_number: 42, | ||
| } as TaskPR; | ||
|
|
||
| describe("ReviewPRDiffBoundary", () => { | ||
| it("keeps expanded Review usable when local files exist during a PR failure", () => { | ||
| expect( | ||
| shouldBlockReviewForPR([ | ||
| { | ||
| path: "src/local.ts", | ||
| diff: "@@ -1 +1 @@", | ||
| status: "modified", | ||
| additions: 1, | ||
| deletions: 1, | ||
| staged: false, | ||
| source: "uncommitted", | ||
| }, | ||
| ]), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it("retries a failed selected PR without rendering stale children", () => { | ||
| const onRetry = vi.fn(); | ||
| render( | ||
| <ReviewPRDiffBoundary | ||
| selectedPR={selectedPR} | ||
| loading={false} | ||
| error="Could not load PR changes" | ||
| onRetry={onRetry} | ||
| > | ||
| <span>stale diff</span> | ||
| </ReviewPRDiffBoundary>, | ||
| ); | ||
|
|
||
| expect(screen.queryByText("stale diff")).toBeNull(); | ||
| fireEvent.click(screen.getByRole("button", { name: "Retry" })); | ||
| expect(onRetry).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("ignores PR fetch state for a local-only review source", () => { | ||
| render( | ||
| <ReviewPRDiffBoundary selectedPR={null} loading error="Could not load PR changes"> | ||
| <span>local diff</span> | ||
| </ReviewPRDiffBoundary>, | ||
| ); | ||
|
|
||
| expect(screen.getByText("local diff")).toBeTruthy(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("useReviewDialogTransientState", () => { | ||
| it("derives cleared state for a new source and keeps subsequent edits on that source", () => { | ||
| const { result, rerender } = renderHook( | ||
| ({ sourceKey }) => useReviewDialogTransientState(sourceKey), | ||
| { initialProps: { sourceKey: "session-a:pr-1" } }, | ||
| ); | ||
|
|
||
| act(() => { | ||
| result.current.setSelectedFile("src/old.ts"); | ||
| result.current.setFilter("old"); | ||
| }); | ||
| rerender({ sourceKey: "session-b:pr-1" }); | ||
|
|
||
| expect(result.current.selectedFile).toBeNull(); | ||
| expect(result.current.filter).toBe(""); | ||
|
|
||
| act(() => result.current.setFilter("new")); | ||
| expect(result.current.filter).toBe("new"); | ||
| }); | ||
| }); | ||
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,156 @@ | ||
| "use client"; | ||
|
|
||
| import { useCallback, useEffect, useRef, useState, type ReactNode } from "react"; | ||
| import { IconLoader2, IconRefresh } from "@tabler/icons-react"; | ||
| import { Button } from "@kandev/ui/button"; | ||
| import type { TaskPR } from "@/lib/types/github"; | ||
| import type { ReviewFile } from "./types"; | ||
|
|
||
| type AutoCloseReviewDialogInput = { | ||
| open: boolean; | ||
| previousFileCount: number | null; | ||
| fileCount: number; | ||
| prDiffLoading: boolean; | ||
| }; | ||
|
|
||
| export function shouldAutoCloseReviewDialog({ | ||
| open, | ||
| previousFileCount, | ||
| fileCount, | ||
| prDiffLoading, | ||
| }: AutoCloseReviewDialogInput): boolean { | ||
| return ( | ||
| open && !prDiffLoading && previousFileCount !== null && previousFileCount > 0 && fileCount === 0 | ||
| ); | ||
| } | ||
|
|
||
| export function useReviewDialogAutoClose(opts: { | ||
| open: boolean; | ||
| fileCount: number; | ||
| prDiffLoading: boolean; | ||
| onOpenChange: (open: boolean) => void; | ||
| }) { | ||
| const previousFileCountRef = useRef<number | null>(null); | ||
| useEffect(() => { | ||
| if ( | ||
| shouldAutoCloseReviewDialog({ | ||
| open: opts.open, | ||
| previousFileCount: previousFileCountRef.current, | ||
| fileCount: opts.fileCount, | ||
| prDiffLoading: opts.prDiffLoading, | ||
| }) | ||
| ) { | ||
| opts.onOpenChange(false); | ||
| } | ||
| previousFileCountRef.current = opts.fileCount; | ||
| }, [opts.open, opts.fileCount, opts.prDiffLoading, opts.onOpenChange]); | ||
| } | ||
|
|
||
| export type ReviewTransientState = { | ||
| sourceKey: string; | ||
| selectedFile: string | null; | ||
| filter: string; | ||
| }; | ||
|
|
||
| export function reviewDialogSourceKey(sessionId: string, selectedPRKey: string | null): string { | ||
| return `${sessionId}\u0000${selectedPRKey ?? "review-without-pr"}`; | ||
| } | ||
|
|
||
| export function shouldBlockReviewForPR(files: ReviewFile[]): boolean { | ||
| return !files.some((file) => file.source !== "pr"); | ||
| } | ||
|
|
||
| export function resolveReviewTransientState( | ||
| state: ReviewTransientState, | ||
| sourceKey: string, | ||
| ): ReviewTransientState { | ||
| if (state.sourceKey === sourceKey) return state; | ||
| return { sourceKey, selectedFile: null, filter: "" }; | ||
| } | ||
|
|
||
| export function useReviewDialogTransientState(sourceKey: string) { | ||
| const [state, setState] = useState<ReviewTransientState>(() => ({ | ||
| sourceKey, | ||
| selectedFile: null, | ||
| filter: "", | ||
| })); | ||
| const resolvedState = resolveReviewTransientState(state, sourceKey); | ||
| useEffect(() => { | ||
| setState((current) => resolveReviewTransientState(current, sourceKey)); | ||
| }, [sourceKey]); | ||
|
|
||
|
zeval marked this conversation as resolved.
|
||
| const setSelectedFile = useCallback( | ||
| (value: string | null) => | ||
| setState((current) => ({ | ||
| ...resolveReviewTransientState(current, sourceKey), | ||
| selectedFile: value, | ||
| })), | ||
| [sourceKey], | ||
| ); | ||
| const setFilter = useCallback( | ||
| (value: string) => | ||
| setState((current) => ({ | ||
| ...resolveReviewTransientState(current, sourceKey), | ||
| filter: value, | ||
| })), | ||
| [sourceKey], | ||
| ); | ||
|
|
||
| return { | ||
| selectedFile: resolvedState.selectedFile, | ||
| filter: resolvedState.filter, | ||
| setSelectedFile, | ||
| setFilter, | ||
| }; | ||
| } | ||
|
|
||
| export function usePRKeyedReviewFileSelection( | ||
| selectFile: (path: string, setSelectedFile: (value: string | null) => void) => void, | ||
| setSelectedFile: (value: string | null) => void, | ||
| ) { | ||
| return useCallback( | ||
| (path: string) => selectFile(path, setSelectedFile), | ||
| [selectFile, setSelectedFile], | ||
| ); | ||
| } | ||
|
|
||
| type ReviewPRDiffBoundaryProps = { | ||
| selectedPR: TaskPR | null; | ||
| loading: boolean; | ||
| error: string | null; | ||
| onRetry?: () => void; | ||
| children: ReactNode; | ||
| }; | ||
|
|
||
| export function ReviewPRDiffBoundary({ | ||
| selectedPR, | ||
| loading, | ||
| error, | ||
| onRetry, | ||
| children, | ||
| }: ReviewPRDiffBoundaryProps) { | ||
| if (selectedPR && loading) { | ||
| return ( | ||
| <div className="flex h-full flex-col items-center justify-center gap-2 px-6 text-center text-sm text-muted-foreground"> | ||
| <IconLoader2 className="h-5 w-5 animate-spin" /> | ||
| <span> | ||
| Loading {selectedPR.repo} #{selectedPR.pr_number} changes… | ||
| </span> | ||
| </div> | ||
| ); | ||
| } | ||
| if (selectedPR && error) { | ||
| return ( | ||
| <div className="flex h-full flex-col items-center justify-center gap-3 px-6 text-center text-sm text-muted-foreground"> | ||
| <span>{error}</span> | ||
| {onRetry && ( | ||
| <Button className="min-h-11" variant="outline" size="sm" onClick={onRetry}> | ||
| <IconRefresh className="h-4 w-4" /> | ||
| Retry | ||
| </Button> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
| return children; | ||
| } | ||
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.