mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
section 13: Phase 13 — Source picker in the walkthrough panel
This commit is contained in:
1 parent
d5f56fc061
commit
aeea96b466
6 files changed
+311
-29
No files matched your search
@@ -1,16 +1,22 @@
|
||||
import { fetchWrapper } from './fetchWrapper'
|
||||
import { API_BASE_URL } from '@/config'
|
||||
import { walkthroughSourceKey } from '@opencode-manager/shared/schemas'
|
||||
import type {
|
||||
ChangeWalkthroughState,
|
||||
GenerateChangeWalkthroughRequest,
|
||||
WalkthroughSource,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
|
||||
function walkthroughUrl(sessionId: string): string {
|
||||
return `${API_BASE_URL}/api/change-walkthroughs/${encodeURIComponent(sessionId)}`
|
||||
}
|
||||
|
||||
export async function getChangeWalkthrough(sessionId: string): Promise<ChangeWalkthroughState> {
|
||||
return fetchWrapper<ChangeWalkthroughState>(walkthroughUrl(sessionId))
|
||||
export async function getChangeWalkthrough(
|
||||
sessionId: string,
|
||||
source: WalkthroughSource,
|
||||
): Promise<ChangeWalkthroughState> {
|
||||
const query = `?source=${encodeURIComponent(walkthroughSourceKey(source))}`
|
||||
return fetchWrapper<ChangeWalkthroughState>(`${walkthroughUrl(sessionId)}${query}`)
|
||||
}
|
||||
|
||||
export async function generateChangeWalkthrough(
|
||||
|
||||
@@ -500,7 +500,9 @@ describe('MultiRunSheet', () => {
|
||||
await user.click(within(card).getByRole('button', { name: /walkthrough/i }))
|
||||
|
||||
expect(await screen.findByText('Change walkthrough')).toBeInTheDocument()
|
||||
await waitFor(() => expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_11'))
|
||||
await waitFor(() =>
|
||||
expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_11', { kind: 'session' }),
|
||||
)
|
||||
})
|
||||
|
||||
it('lists fusions with truncation and opens the fusion session', async () => {
|
||||
|
||||
@@ -111,7 +111,7 @@ describe('ChangesWalkthroughSheet', () => {
|
||||
await user.click(generate)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', {})
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { source: { kind: 'session' } })
|
||||
})
|
||||
expect(await screen.findByText('This change adds a greeting.')).toBeInTheDocument()
|
||||
expect(screen.getByText('Stop 1 of 2')).toBeInTheDocument()
|
||||
@@ -156,7 +156,7 @@ describe('ChangesWalkthroughSheet', () => {
|
||||
await user.click(screen.getByRole('button', { name: 'Update walkthrough' }))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', {})
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { source: { kind: 'session' } })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -170,10 +170,106 @@ describe('ChangesWalkthroughSheet', () => {
|
||||
await user.click(screen.getByRole('button', { name: 'Regenerate walkthrough' }))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { regenerate: true })
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', {
|
||||
regenerate: true,
|
||||
source: { kind: 'session' },
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('switches to staged changes', async () => {
|
||||
const user = userEvent.setup()
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null }))
|
||||
mocks.generateChangeWalkthrough.mockResolvedValue(state())
|
||||
renderSheet()
|
||||
|
||||
await user.click(screen.getByRole('combobox', { name: 'Changes to walk through' }))
|
||||
await user.click(await screen.findByRole('option', { name: 'Staged' }))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_1', { kind: 'staged' })
|
||||
})
|
||||
|
||||
await user.click(await screen.findByRole('button', { name: 'Generate walkthrough' }))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { source: { kind: 'staged' } })
|
||||
})
|
||||
})
|
||||
|
||||
it('sends the branch base', async () => {
|
||||
const user = userEvent.setup()
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null }))
|
||||
mocks.generateChangeWalkthrough.mockResolvedValue(state())
|
||||
renderSheet()
|
||||
|
||||
await user.click(screen.getByRole('combobox', { name: 'Changes to walk through' }))
|
||||
await user.click(await screen.findByRole('option', { name: 'Branch vs base' }))
|
||||
|
||||
const base = await screen.findByLabelText('Base branch')
|
||||
await user.type(base, 'develop')
|
||||
await user.tab()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_1', { kind: 'branch', base: 'develop' })
|
||||
})
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Generate walkthrough' }))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', {
|
||||
source: { kind: 'branch', base: 'develop' },
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('ignores an invalid pull request number', async () => {
|
||||
const user = userEvent.setup()
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null }))
|
||||
mocks.generateChangeWalkthrough.mockResolvedValue(state())
|
||||
renderSheet()
|
||||
|
||||
await user.click(screen.getByRole('combobox', { name: 'Changes to walk through' }))
|
||||
await user.click(await screen.findByRole('option', { name: 'Pull request' }))
|
||||
|
||||
const number = await screen.findByLabelText('Pull request number')
|
||||
await user.type(number, '0')
|
||||
await user.tab()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Generate walkthrough' }))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { source: { kind: 'session' } })
|
||||
})
|
||||
expect(mocks.getChangeWalkthrough).not.toHaveBeenCalledWith(
|
||||
'ses_1',
|
||||
expect.objectContaining({ kind: 'pullRequest' }),
|
||||
)
|
||||
})
|
||||
|
||||
it('clears a generation error when the source changes', async () => {
|
||||
const user = userEvent.setup()
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null }))
|
||||
mocks.generateChangeWalkthrough.mockRejectedValue(
|
||||
new FetchError('Not a git repository', 409, 'WALKTHROUGH_NOT_A_REPO'),
|
||||
)
|
||||
renderSheet()
|
||||
|
||||
await user.click(screen.getByRole('combobox', { name: 'Changes to walk through' }))
|
||||
await user.click(await screen.findByRole('option', { name: 'Branch vs base' }))
|
||||
|
||||
await user.click(await screen.findByRole('button', { name: 'Generate walkthrough' }))
|
||||
expect(await screen.findByText('Not a git repository')).toBeInTheDocument()
|
||||
|
||||
await user.click(screen.getByRole('combobox', { name: 'Changes to walk through' }))
|
||||
await user.click(await screen.findByRole('option', { name: 'Staged' }))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByText('Not a git repository')).not.toBeInTheDocument()
|
||||
})
|
||||
expect(await screen.findByRole('button', { name: 'Generate walkthrough' })).toBeVisible()
|
||||
})
|
||||
|
||||
it('shows progress while the server is generating instead of the generate button', async () => {
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null, generating: true }))
|
||||
renderSheet()
|
||||
@@ -222,7 +318,7 @@ describe('ChangesWalkthroughSheet', () => {
|
||||
await user.click(screen.getByRole('button', { name: 'Retry unexplained stops' }))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', {})
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { source: { kind: 'session' } })
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -4,16 +4,25 @@ import { ChevronLeft, ChevronRight, Loader2, RefreshCw } from 'lucide-react'
|
||||
import { SideDrawer, SideDrawerHeader } from '@/components/ui/side-drawer'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
import { DiffLines } from '@/components/file-browser/DiffLines'
|
||||
import { ScheduleRunMarkdown } from '@/components/schedules/ScheduleRunMarkdown'
|
||||
import { useChangeWalkthrough, useGenerateChangeWalkthrough } from '@/hooks/useChangeWalkthrough'
|
||||
import { GIT_STATUS_COLORS, GIT_STATUS_LABELS } from '@/lib/git-status-styles'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { WalkthroughOmittedFileSchema } from '@opencode-manager/shared/schemas'
|
||||
import {
|
||||
DEFAULT_WALKTHROUGH_SOURCE,
|
||||
describeWalkthroughSource,
|
||||
walkthroughSourceKey,
|
||||
WalkthroughOmittedFileSchema,
|
||||
WalkthroughSourceSchema,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
import type {
|
||||
ChangeWalkthrough,
|
||||
WalkthroughHunk,
|
||||
WalkthroughOmittedFile,
|
||||
WalkthroughSource,
|
||||
WalkthroughStop,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
|
||||
@@ -86,6 +95,8 @@ interface ChangesWalkthroughContextValue {
|
||||
selectStop: (index: number) => void
|
||||
detailRef: RefObject<HTMLDivElement | null>
|
||||
failedStopCount: number
|
||||
source: WalkthroughSource
|
||||
setSource: (source: WalkthroughSource) => void
|
||||
}
|
||||
|
||||
const ChangesWalkthroughContext = createContext<ChangesWalkthroughContextValue | null>(null)
|
||||
@@ -96,6 +107,128 @@ function useChangesWalkthrough(): ChangesWalkthroughContextValue {
|
||||
return value
|
||||
}
|
||||
|
||||
const WALKTHROUGH_SOURCE_OPTIONS: { value: WalkthroughSource['kind']; label: string }[] = [
|
||||
{ value: 'session', label: 'This session' },
|
||||
{ value: 'uncommitted', label: 'Uncommitted' },
|
||||
{ value: 'staged', label: 'Staged' },
|
||||
{ value: 'unstaged', label: 'Unstaged' },
|
||||
{ value: 'branch', label: 'Branch vs base' },
|
||||
{ value: 'pullRequest', label: 'Pull request' },
|
||||
]
|
||||
|
||||
function walkthroughSourceLabel(source: WalkthroughSource): string {
|
||||
return source.kind === 'session' ? 'the changes in this session' : describeWalkthroughSource(source)
|
||||
}
|
||||
|
||||
function sourceBase(source: WalkthroughSource): string {
|
||||
return source.kind === 'branch' || source.kind === 'pullRequest' ? source.base ?? '' : ''
|
||||
}
|
||||
|
||||
function sourceNumber(source: WalkthroughSource): string {
|
||||
return source.kind === 'pullRequest' ? String(source.number) : ''
|
||||
}
|
||||
|
||||
/** Chooses which set of changes the walkthrough covers; git sources commit as soon as they are valid. */
|
||||
const WalkthroughSourcePicker = memo(function WalkthroughSourcePicker() {
|
||||
const { source, setSource } = useChangesWalkthrough()
|
||||
const [kind, setKind] = useState<WalkthroughSource['kind']>(source.kind)
|
||||
const [baseInput, setBaseInput] = useState(sourceBase(source))
|
||||
const [numberInput, setNumberInput] = useState(sourceNumber(source))
|
||||
|
||||
useEffect(() => {
|
||||
setKind(source.kind)
|
||||
setBaseInput(sourceBase(source))
|
||||
setNumberInput(sourceNumber(source))
|
||||
}, [source])
|
||||
|
||||
const commit = useCallback(
|
||||
(candidate: unknown) => {
|
||||
const parsed = WalkthroughSourceSchema.safeParse(candidate)
|
||||
if (parsed.success) setSource(parsed.data)
|
||||
},
|
||||
[setSource],
|
||||
)
|
||||
|
||||
const branchSource = useCallback((): WalkthroughSource => {
|
||||
const base = baseInput.trim()
|
||||
return base ? { kind: 'branch', base } : { kind: 'branch' }
|
||||
}, [baseInput])
|
||||
|
||||
const pullRequestSource = useCallback((): WalkthroughSource => {
|
||||
const base = baseInput.trim()
|
||||
return base
|
||||
? { kind: 'pullRequest', number: Number(numberInput), base }
|
||||
: { kind: 'pullRequest', number: Number(numberInput) }
|
||||
}, [baseInput, numberInput])
|
||||
|
||||
const commitBase = useCallback(() => {
|
||||
if (kind === 'branch') {
|
||||
commit(branchSource())
|
||||
} else if (kind === 'pullRequest') {
|
||||
commit(pullRequestSource())
|
||||
}
|
||||
}, [kind, commit, branchSource, pullRequestSource])
|
||||
|
||||
const commitNumber = useCallback(() => {
|
||||
commit(pullRequestSource())
|
||||
}, [commit, pullRequestSource])
|
||||
|
||||
const handleKindChange = useCallback(
|
||||
(next: WalkthroughSource['kind']) => {
|
||||
setKind(next)
|
||||
if (next === 'pullRequest') return
|
||||
commit(next === 'branch' ? branchSource() : { kind: next })
|
||||
},
|
||||
[commit, branchSource],
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<Select
|
||||
value={kind}
|
||||
onValueChange={(value) => handleKindChange(value as WalkthroughSource['kind'])}
|
||||
>
|
||||
<SelectTrigger aria-label="Changes to walk through">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{WALKTHROUGH_SOURCE_OPTIONS.map((option) => (
|
||||
<SelectItem key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
|
||||
{kind === 'branch' || kind === 'pullRequest' ? (
|
||||
<Input
|
||||
aria-label="Base branch"
|
||||
placeholder="Default branch"
|
||||
value={baseInput}
|
||||
onChange={(event) => setBaseInput(event.target.value)}
|
||||
onBlur={commitBase}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === 'Enter') commitBase()
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
|
||||
{kind === 'pullRequest' ? (
|
||||
<Input
|
||||
type="number"
|
||||
aria-label="Pull request number"
|
||||
value={numberInput}
|
||||
onChange={(event) => setNumberInput(event.target.value)}
|
||||
onBlur={commitNumber}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === 'Enter') commitNumber()
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
})
|
||||
|
||||
interface ChangesWalkthroughProviderProps {
|
||||
sessionId: string
|
||||
active: boolean
|
||||
@@ -104,8 +237,10 @@ interface ChangesWalkthroughProviderProps {
|
||||
|
||||
/** Loads a session's change walkthrough and shares its state with the surrounding chrome and body. */
|
||||
export function ChangesWalkthroughProvider({ sessionId, active, children }: ChangesWalkthroughProviderProps) {
|
||||
const stateQuery = useChangeWalkthrough(sessionId, active)
|
||||
const generate = useGenerateChangeWalkthrough(sessionId)
|
||||
const [source, setSource] = useState<WalkthroughSource>(DEFAULT_WALKTHROUGH_SOURCE)
|
||||
const sourceKey = walkthroughSourceKey(source)
|
||||
const stateQuery = useChangeWalkthrough(sessionId, active, source)
|
||||
const generate = useGenerateChangeWalkthrough(sessionId, source)
|
||||
const resetGenerate = generate.reset
|
||||
const [stopIndex, setStopIndex] = useState(0)
|
||||
const detailRef = useRef<HTMLDivElement>(null)
|
||||
@@ -119,11 +254,15 @@ export function ChangesWalkthroughProvider({ sessionId, active, children }: Chan
|
||||
|
||||
useEffect(() => {
|
||||
resetGenerate()
|
||||
}, [active, sessionId, resetGenerate])
|
||||
}, [active, sessionId, sourceKey, resetGenerate])
|
||||
|
||||
useEffect(() => {
|
||||
setSource(DEFAULT_WALKTHROUGH_SOURCE)
|
||||
}, [sessionId])
|
||||
|
||||
useEffect(() => {
|
||||
setStopIndex(0)
|
||||
}, [active, sessionId, walkthrough?.createdAt])
|
||||
}, [active, sessionId, sourceKey, walkthrough?.createdAt])
|
||||
|
||||
const hunksById = new Map(walkthrough?.hunks.map((hunk) => [hunk.id, hunk]) ?? [])
|
||||
const stops = walkthrough?.stops ?? []
|
||||
@@ -158,6 +297,8 @@ export function ChangesWalkthroughProvider({ sessionId, active, children }: Chan
|
||||
selectStop,
|
||||
detailRef,
|
||||
failedStopCount,
|
||||
source,
|
||||
setSource,
|
||||
}
|
||||
|
||||
return <ChangesWalkthroughContext.Provider value={value}>{children}</ChangesWalkthroughContext.Provider>
|
||||
@@ -232,12 +373,15 @@ export const ChangesWalkthroughView = memo(function ChangesWalkthroughView() {
|
||||
selectStop,
|
||||
detailRef,
|
||||
failedStopCount,
|
||||
source,
|
||||
} = useChangesWalkthrough()
|
||||
|
||||
const readyStopCount = stops.filter((stop) => stop.status === 'ready').length
|
||||
|
||||
return (
|
||||
<div className="min-h-0 flex-1 space-y-4 overflow-y-auto p-4">
|
||||
<WalkthroughSourcePicker />
|
||||
|
||||
{error ? (
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm text-destructive">{walkthroughErrorMessage(error)}</p>
|
||||
@@ -264,7 +408,7 @@ export const ChangesWalkthroughView = memo(function ChangesWalkthroughView() {
|
||||
generating ? null : (
|
||||
<div className="space-y-4">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Generate a step-by-step walkthrough of the changes in this session.
|
||||
Generate a step-by-step walkthrough of {walkthroughSourceLabel(source)}.
|
||||
</p>
|
||||
<Button onClick={generate}>Generate walkthrough</Button>
|
||||
</div>
|
||||
|
||||
@@ -3,6 +3,7 @@ import { act, renderHook, waitFor } from '@testing-library/react'
|
||||
import type { ReactNode } from 'react'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { changeWalkthroughQueryKey, useChangeWalkthrough, useGenerateChangeWalkthrough } from './useChangeWalkthrough'
|
||||
import { DEFAULT_WALKTHROUGH_SOURCE } from '@opencode-manager/shared/schemas'
|
||||
import type { ChangeWalkthrough, ChangeWalkthroughState } from '@opencode-manager/shared/schemas'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
@@ -55,7 +56,7 @@ describe('useGenerateChangeWalkthrough', () => {
|
||||
mocks.generateChangeWalkthrough.mockReturnValue(pending.promise)
|
||||
|
||||
const { result, rerender } = renderHook(
|
||||
({ sessionId }: { sessionId: string }) => useGenerateChangeWalkthrough(sessionId),
|
||||
({ sessionId }: { sessionId: string }) => useGenerateChangeWalkthrough(sessionId, DEFAULT_WALKTHROUGH_SOURCE),
|
||||
{ initialProps: { sessionId: 'ses_A' }, wrapper: createWrapper(queryClient) },
|
||||
)
|
||||
|
||||
@@ -63,7 +64,7 @@ describe('useGenerateChangeWalkthrough', () => {
|
||||
result.current.mutate({})
|
||||
})
|
||||
await waitFor(() => {
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_A', {})
|
||||
expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_A', { source: { kind: 'session' } })
|
||||
})
|
||||
|
||||
rerender({ sessionId: 'ses_B' })
|
||||
@@ -73,9 +74,11 @@ describe('useGenerateChangeWalkthrough', () => {
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(queryClient.getQueryData(changeWalkthroughQueryKey('ses_A'))).toEqual(state({ generating: true }))
|
||||
expect(queryClient.getQueryData(changeWalkthroughQueryKey('ses_A', 'session'))).toEqual(
|
||||
state({ generating: true }),
|
||||
)
|
||||
})
|
||||
expect(queryClient.getQueryData(changeWalkthroughQueryKey('ses_B'))).toBeUndefined()
|
||||
expect(queryClient.getQueryData(changeWalkthroughQueryKey('ses_B', 'session'))).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -92,7 +95,9 @@ describe('useChangeWalkthrough', () => {
|
||||
.mockResolvedValueOnce(state({ generating: true }))
|
||||
.mockResolvedValueOnce(state({ walkthrough: walkthroughForA }))
|
||||
|
||||
const { result } = renderHook(() => useChangeWalkthrough('ses_A', true), { wrapper: createWrapper(queryClient) })
|
||||
const { result } = renderHook(() => useChangeWalkthrough('ses_A', true, DEFAULT_WALKTHROUGH_SOURCE), {
|
||||
wrapper: createWrapper(queryClient),
|
||||
})
|
||||
|
||||
await waitFor(() => expect(result.current.data?.generating).toBe(true))
|
||||
|
||||
@@ -110,4 +115,24 @@ describe('useChangeWalkthrough', () => {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('invalidating the session key refetches every source', async () => {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state())
|
||||
|
||||
renderHook(() => useChangeWalkthrough('ses_A', true, { kind: 'staged' }), {
|
||||
wrapper: createWrapper(queryClient),
|
||||
})
|
||||
renderHook(() => useChangeWalkthrough('ses_A', true, { kind: 'unstaged' }), {
|
||||
wrapper: createWrapper(queryClient),
|
||||
})
|
||||
|
||||
await waitFor(() => expect(mocks.getChangeWalkthrough).toHaveBeenCalledTimes(2))
|
||||
|
||||
await act(async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: changeWalkthroughQueryKey('ses_A') })
|
||||
})
|
||||
|
||||
await waitFor(() => expect(mocks.getChangeWalkthrough).toHaveBeenCalledTimes(4))
|
||||
})
|
||||
})
|
||||
@@ -1,34 +1,43 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { generateChangeWalkthrough, getChangeWalkthrough } from '@/api/changeWalkthroughs'
|
||||
import type { GenerateChangeWalkthroughRequest } from '@opencode-manager/shared/schemas'
|
||||
import { walkthroughSourceKey } from '@opencode-manager/shared/schemas'
|
||||
import type { GenerateChangeWalkthroughRequest, WalkthroughSource } from '@opencode-manager/shared/schemas'
|
||||
|
||||
const GENERATING_POLL_INTERVAL_MS = 2_000
|
||||
|
||||
export function changeWalkthroughQueryKey(sessionId: string) {
|
||||
return ['change-walkthrough', sessionId] as const
|
||||
export function changeWalkthroughQueryKey(sessionId: string, sourceKey?: string) {
|
||||
return sourceKey === undefined
|
||||
? (['change-walkthrough', sessionId] as const)
|
||||
: (['change-walkthrough', sessionId, sourceKey] as const)
|
||||
}
|
||||
|
||||
export function useChangeWalkthrough(sessionId: string | undefined, enabled: boolean) {
|
||||
export function useChangeWalkthrough(
|
||||
sessionId: string | undefined,
|
||||
enabled: boolean,
|
||||
source: WalkthroughSource,
|
||||
) {
|
||||
const sourceKey = walkthroughSourceKey(source)
|
||||
return useQuery({
|
||||
queryKey: changeWalkthroughQueryKey(sessionId ?? ''),
|
||||
queryFn: () => getChangeWalkthrough(sessionId!),
|
||||
queryKey: changeWalkthroughQueryKey(sessionId ?? '', sourceKey),
|
||||
queryFn: () => getChangeWalkthrough(sessionId!, source),
|
||||
enabled: enabled && !!sessionId,
|
||||
staleTime: 30_000,
|
||||
refetchInterval: (query) => (query.state.data?.generating ? GENERATING_POLL_INTERVAL_MS : false),
|
||||
})
|
||||
}
|
||||
|
||||
export function useGenerateChangeWalkthrough(sessionId: string | undefined) {
|
||||
export function useGenerateChangeWalkthrough(sessionId: string | undefined, source: WalkthroughSource) {
|
||||
const queryClient = useQueryClient()
|
||||
const sourceKey = walkthroughSourceKey(source)
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (request: GenerateChangeWalkthroughRequest) => {
|
||||
const targetSessionId = sessionId!
|
||||
const state = await generateChangeWalkthrough(targetSessionId, request)
|
||||
return { sessionId: targetSessionId, state }
|
||||
const state = await generateChangeWalkthrough(targetSessionId, { ...request, source })
|
||||
return { sessionId: targetSessionId, sourceKey, state }
|
||||
},
|
||||
onSuccess: ({ sessionId: targetSessionId, state }) => {
|
||||
queryClient.setQueryData(changeWalkthroughQueryKey(targetSessionId), state)
|
||||
onSuccess: ({ sessionId: targetSessionId, sourceKey: targetSourceKey, state }) => {
|
||||
queryClient.setQueryData(changeWalkthroughQueryKey(targetSessionId, targetSourceKey), state)
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user