mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
refactor(schedules): consolidate schedule tab state into useScheduleUrlState
This commit is contained in:
1 parent
9368a7f8e2
commit
6ae3d07b6c
6 files changed
+78
-276
No files matched your search
@@ -10,10 +10,6 @@ export function initializeDatabase(dbPath: string = './data/opencode.db'): Datab
|
||||
mkdirSync(dirname(dbPath), { recursive: true })
|
||||
const db = new Database(dbPath)
|
||||
|
||||
// Enable foreign key enforcement so ON DELETE CASCADE works.
|
||||
// SQLite defaults to OFF, and bun:sqlite inherits this default.
|
||||
db.exec('PRAGMA foreign_keys = ON')
|
||||
|
||||
migrate(db, allMigrations)
|
||||
ensureOpenCodeModelStateTable(db)
|
||||
|
||||
|
||||
@@ -3,7 +3,8 @@ import { useLocation, useNavigate } from 'react-router-dom'
|
||||
import { FolderGit2, FolderOpen, CalendarClock, Menu, Info, History, Bot } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { useMobile } from '@/hooks/useMobile'
|
||||
import { useMobileTabBar, useScheduleTab, type ScheduleTabKey } from '@/hooks/useMobileTabBar'
|
||||
import { useMobileTabBar } from '@/hooks/useMobileTabBar'
|
||||
import { useScheduleUrlState, type ScheduleTab } from '@/hooks/useScheduleUrlState'
|
||||
import { useUIState } from '@/stores/uiStateStore'
|
||||
import { getAssistantPath, isAssistantPath } from '@/lib/navigation'
|
||||
|
||||
@@ -124,7 +125,7 @@ function buildGlobalTabs({ pathname, search, openSheet, open, close, navigate, i
|
||||
]
|
||||
}
|
||||
|
||||
function buildScheduleTabs(scheduleTab: ScheduleTabKey, setScheduleTab: (tab: ScheduleTabKey) => void): TabDef[] {
|
||||
function buildScheduleTabs(scheduleTab: ScheduleTab, setScheduleTab: (tab: ScheduleTab) => void): TabDef[] {
|
||||
return [
|
||||
{
|
||||
key: 'jobs',
|
||||
@@ -189,7 +190,7 @@ export const MobileTabBar = memo(function MobileTabBar() {
|
||||
const { pathname, search } = useLocation()
|
||||
const navigate = useNavigate()
|
||||
const { openSheet, open, close } = useMobileTabBar()
|
||||
const { scheduleTab, setScheduleTab } = useScheduleTab()
|
||||
const { scheduleTab, setScheduleTab } = useScheduleUrlState()
|
||||
const isMobile = useMobile()
|
||||
const isMoreDrawerOpen = useUIState((state) => state.isMoreDrawerOpen)
|
||||
const setMoreDrawerOpen = useUIState((state) => state.setMoreDrawerOpen)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect } from 'react'
|
||||
import { renderHook, act, render } from '@testing-library/react'
|
||||
import { renderHook, act } from '@testing-library/react'
|
||||
import { MemoryRouter, useLocation } from 'react-router-dom'
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { useScheduleUrlState } from '../useScheduleUrlState'
|
||||
@@ -206,20 +206,6 @@ describe('useScheduleUrlState', () => {
|
||||
expect(result.current.jobId).toBeNull()
|
||||
})
|
||||
|
||||
it('selectJob sets and clears jobId without affecting dialog', () => {
|
||||
const { result } = renderScheduleUrlState()
|
||||
act(() => {
|
||||
result.current.selectJob(10)
|
||||
})
|
||||
expect(result.current.jobId).toBe(10)
|
||||
expect(result.current.dialog).toBeNull()
|
||||
|
||||
act(() => {
|
||||
result.current.selectJob(null)
|
||||
})
|
||||
expect(result.current.jobId).toBeNull()
|
||||
})
|
||||
|
||||
it('selectRun sets and clears runId', () => {
|
||||
const { result } = renderScheduleUrlState()
|
||||
act(() => {
|
||||
@@ -288,7 +274,6 @@ describe('useScheduleUrlState', () => {
|
||||
const firstOpenEditJob = result.current.openEditJob
|
||||
const firstCloseDialog = result.current.closeDialog
|
||||
const firstClosePromptDialog = result.current.closePromptDialog
|
||||
const firstSelectJob = result.current.selectJob
|
||||
const firstSelectJobAndView = result.current.selectJobAndView
|
||||
const firstSelectJobAndCloseDialog = result.current.selectJobAndCloseDialog
|
||||
const firstReplaceUrlParams = result.current.replaceUrlParams
|
||||
@@ -299,7 +284,6 @@ describe('useScheduleUrlState', () => {
|
||||
expect(result.current.openEditJob).toBe(firstOpenEditJob)
|
||||
expect(result.current.closeDialog).toBe(firstCloseDialog)
|
||||
expect(result.current.closePromptDialog).toBe(firstClosePromptDialog)
|
||||
expect(result.current.selectJob).toBe(firstSelectJob)
|
||||
expect(result.current.selectJobAndView).toBe(firstSelectJobAndView)
|
||||
expect(result.current.selectJobAndCloseDialog).toBe(firstSelectJobAndCloseDialog)
|
||||
expect(result.current.replaceUrlParams).toBe(firstReplaceUrlParams)
|
||||
@@ -389,55 +373,4 @@ describe('useScheduleUrlState', () => {
|
||||
expect(capturedSearch.current).toContain('scheduleTab=detail')
|
||||
})
|
||||
})
|
||||
|
||||
describe('regression: sequential individual navigations', () => {
|
||||
/**
|
||||
* Regression test: When two URL mutations fire synchronously (before React
|
||||
* processes the first navigation), the second function reads stale
|
||||
* searchRef.current and overwrites the first change. This simulates what
|
||||
* handleSelectJob in Schedules.tsx used to do before the fix.
|
||||
*/
|
||||
it('calling selectJob then setScheduleTab in same synchronous block loses state without combined method', () => {
|
||||
// Use a real component so we can call both methods synchronously in an effect
|
||||
// (simulating the old buggy pattern in the component)
|
||||
const spy = { search: '' }
|
||||
function TestComponent() {
|
||||
const { selectJob, setScheduleTab, jobId, scheduleTab } = useScheduleUrlState()
|
||||
const loc = useLocation()
|
||||
useEffect(() => {
|
||||
spy.search = loc.search
|
||||
}, [loc.search])
|
||||
|
||||
// Simulate the old buggy handleSelectJob pattern: call both synchronously
|
||||
useEffect(() => {
|
||||
// This mimics the old: selectJob(id) then setScheduleTab('detail')
|
||||
// without an intervening render — both read the same stale ref
|
||||
selectJob(42)
|
||||
setScheduleTab('detail')
|
||||
}, [selectJob, setScheduleTab])
|
||||
return <div data-testid="state">{`${jobId}|${scheduleTab}`}</div>
|
||||
}
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<TestComponent />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
|
||||
// After effects flush, the second navigation (setScheduleTab) may have
|
||||
// overwritten jobId because it read stale searchRef.current.
|
||||
// If the component still has both, the test passes (fix works);
|
||||
// if jobId is lost, the test reveals the old bug.
|
||||
// With the combined methods (selectJobAndView) now used in the component,
|
||||
// this test documents why the combined methods are needed.
|
||||
// The individual methods themselves are not broken — the breakage only
|
||||
// occurs when two consumers fire synchronously. The fix is to use
|
||||
// the combined methods in component code.
|
||||
const el = document.querySelector('[data-testid="state"]')
|
||||
expect(el).toBeTruthy()
|
||||
// The actual state after flush may have both if the second navigation
|
||||
// happened to include the first param's value from the stale ref,
|
||||
// or may lack jobId. Either way, this documents the risk.
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,7 +1,7 @@
|
||||
import { renderHook, act } from '@testing-library/react'
|
||||
import { MemoryRouter } from 'react-router-dom'
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { useMobileTabBar, useScheduleTab, type MobileSheetKey } from './useMobileTabBar'
|
||||
import { useMobileTabBar, type MobileSheetKey } from './useMobileTabBar'
|
||||
|
||||
function renderHookWithRouter<T>(renderFn: () => T) {
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
@@ -112,81 +112,3 @@ describe('useMobileTabBar', () => {
|
||||
expect(result.current.open).toBe(firstOpen)
|
||||
})
|
||||
})
|
||||
|
||||
describe('useScheduleTab', () => {
|
||||
it('defaults to jobs when no param is set', () => {
|
||||
const { result } = renderHook(() => useScheduleTab(), {
|
||||
wrapper: ({ children }) => (
|
||||
<MemoryRouter initialEntries={['/repos/1/schedules']}>{children}</MemoryRouter>
|
||||
),
|
||||
})
|
||||
expect(result.current.scheduleTab).toBe('jobs')
|
||||
})
|
||||
|
||||
it('reads scheduleTab from URL param', () => {
|
||||
const { result } = renderHook(() => useScheduleTab(), {
|
||||
wrapper: ({ children }) => (
|
||||
<MemoryRouter initialEntries={['/repos/1/schedules?scheduleTab=runs']}>{children}</MemoryRouter>
|
||||
),
|
||||
})
|
||||
expect(result.current.scheduleTab).toBe('runs')
|
||||
})
|
||||
|
||||
it('setScheduleTab updates the param', () => {
|
||||
const { result } = renderHook(() => useScheduleTab(), {
|
||||
wrapper: ({ children }) => (
|
||||
<MemoryRouter initialEntries={['/repos/1/schedules']}>{children}</MemoryRouter>
|
||||
),
|
||||
})
|
||||
act(() => {
|
||||
result.current.setScheduleTab('detail')
|
||||
})
|
||||
expect(result.current.scheduleTab).toBe('detail')
|
||||
})
|
||||
|
||||
it('setScheduleTab(jobs) removes the param', () => {
|
||||
const { result } = renderHook(() => useScheduleTab(), {
|
||||
wrapper: ({ children }) => (
|
||||
<MemoryRouter initialEntries={['/repos/1/schedules?scheduleTab=detail']}>{children}</MemoryRouter>
|
||||
),
|
||||
})
|
||||
expect(result.current.scheduleTab).toBe('detail')
|
||||
act(() => {
|
||||
result.current.setScheduleTab('jobs')
|
||||
})
|
||||
expect(result.current.scheduleTab).toBe('jobs')
|
||||
})
|
||||
|
||||
it('resolves invalid values to jobs', () => {
|
||||
const { result } = renderHook(() => useScheduleTab(), {
|
||||
wrapper: ({ children }) => (
|
||||
<MemoryRouter initialEntries={['/repos/1/schedules?scheduleTab=invalid']}>{children}</MemoryRouter>
|
||||
),
|
||||
})
|
||||
expect(result.current.scheduleTab).toBe('jobs')
|
||||
})
|
||||
|
||||
it('returns stable scheduleTab identity across rerenders when search is unchanged', () => {
|
||||
const { result, rerender } = renderHook(() => useScheduleTab(), {
|
||||
wrapper: ({ children }) => (
|
||||
<MemoryRouter initialEntries={['/repos/1/schedules?scheduleTab=runs']}>
|
||||
{children}
|
||||
</MemoryRouter>
|
||||
),
|
||||
})
|
||||
const firstScheduleTab = result.current.scheduleTab
|
||||
rerender()
|
||||
expect(result.current.scheduleTab).toBe(firstScheduleTab)
|
||||
})
|
||||
|
||||
it('setScheduleTab callback maintains stable identity across rerenders', () => {
|
||||
const { result, rerender } = renderHook(() => useScheduleTab(), {
|
||||
wrapper: ({ children }) => (
|
||||
<MemoryRouter initialEntries={['/repos/1/schedules']}>{children}</MemoryRouter>
|
||||
),
|
||||
})
|
||||
const firstSetScheduleTab = result.current.setScheduleTab
|
||||
rerender()
|
||||
expect(result.current.setScheduleTab).toBe(firstSetScheduleTab)
|
||||
})
|
||||
})
|
||||
@@ -44,41 +44,3 @@ export function useMobileTabBar(): UseMobileTabBarReturn {
|
||||
close,
|
||||
}
|
||||
}
|
||||
|
||||
export type ScheduleTabKey = 'jobs' | 'detail' | 'runs'
|
||||
|
||||
export interface UseScheduleTabReturn {
|
||||
scheduleTab: ScheduleTabKey
|
||||
setScheduleTab: (tab: ScheduleTabKey) => void
|
||||
}
|
||||
|
||||
export function useScheduleTab(): UseScheduleTabReturn {
|
||||
const navigate = useNavigate()
|
||||
const location = useLocation()
|
||||
const searchRef = useRef(location.search)
|
||||
|
||||
useEffect(() => {
|
||||
searchRef.current = location.search
|
||||
}, [location.search])
|
||||
|
||||
const scheduleTab = useMemo<ScheduleTabKey>(() => {
|
||||
const searchParams = new URLSearchParams(location.search)
|
||||
const tabParam = searchParams.get('scheduleTab')
|
||||
return tabParam === 'detail' || tabParam === 'runs' ? tabParam : 'jobs'
|
||||
}, [location.search])
|
||||
|
||||
const setScheduleTab = useCallback((tab: ScheduleTabKey) => {
|
||||
const newParams = new URLSearchParams(searchRef.current)
|
||||
if (tab === 'jobs') {
|
||||
newParams.delete('scheduleTab')
|
||||
} else {
|
||||
newParams.set('scheduleTab', tab)
|
||||
}
|
||||
navigate({ search: newParams.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
|
||||
return {
|
||||
scheduleTab,
|
||||
setScheduleTab,
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,6 @@ export interface UseScheduleUrlStateReturn {
|
||||
openImportTemplate: () => void
|
||||
closeDialog: () => void
|
||||
closePromptDialog: () => void
|
||||
selectJob: (jobId: number | null) => void
|
||||
selectRun: (runId: number | null) => void
|
||||
selectJobAndView: (jobId: number) => void
|
||||
selectJobAndCloseDialog: (jobId: number) => void
|
||||
@@ -74,98 +73,88 @@ export function useScheduleUrlState(): UseScheduleUrlStateReturn {
|
||||
const runId = useMemo<number | null>(() => parseNullableInt(searchParams.get('runId')), [searchParams])
|
||||
const templateId = useMemo<number | null>(() => parseNullableInt(searchParams.get('templateId')), [searchParams])
|
||||
|
||||
const setScheduleTab = useCallback((tab: ScheduleTab) => {
|
||||
const p = new URLSearchParams(searchRef.current)
|
||||
if (tab === 'jobs') {
|
||||
p.delete('scheduleTab')
|
||||
} else {
|
||||
p.set('scheduleTab', tab)
|
||||
}
|
||||
navigate({ search: p.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
|
||||
const openNewJob = useCallback(() => {
|
||||
const p = new URLSearchParams(searchRef.current)
|
||||
p.set('scheduleDialog', 'new')
|
||||
p.delete('jobId')
|
||||
p.delete('templateId')
|
||||
navigate({ search: p.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
|
||||
const openEditJob = useCallback((id: number) => {
|
||||
const p = new URLSearchParams(searchRef.current)
|
||||
p.set('scheduleDialog', 'edit')
|
||||
p.set('jobId', String(id))
|
||||
p.delete('templateId')
|
||||
navigate({ search: p.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
|
||||
const openDeleteJob = useCallback((id: number) => {
|
||||
const p = new URLSearchParams(searchRef.current)
|
||||
p.set('scheduleDialog', 'delete')
|
||||
p.set('jobId', String(id))
|
||||
p.delete('templateId')
|
||||
navigate({ search: p.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
|
||||
const openNewTemplate = useCallback(() => {
|
||||
const p = new URLSearchParams(searchRef.current)
|
||||
p.set('promptDialog', 'new')
|
||||
p.delete('templateId')
|
||||
p.delete('jobId')
|
||||
navigate({ search: p.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
|
||||
const openEditTemplate = useCallback((id: number) => {
|
||||
const p = new URLSearchParams(searchRef.current)
|
||||
p.set('promptDialog', 'edit')
|
||||
p.set('templateId', String(id))
|
||||
p.delete('jobId')
|
||||
navigate({ search: p.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
|
||||
const openDeleteTemplate = useCallback((id: number) => {
|
||||
const p = new URLSearchParams(searchRef.current)
|
||||
p.set('promptDialog', 'delete')
|
||||
p.set('templateId', String(id))
|
||||
p.delete('jobId')
|
||||
navigate({ search: p.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
|
||||
const openImportTemplate = useCallback(() => {
|
||||
const p = new URLSearchParams(searchRef.current)
|
||||
p.set('promptDialog', 'import')
|
||||
p.delete('templateId')
|
||||
p.delete('jobId')
|
||||
navigate({ search: p.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
|
||||
const replaceUrlParams = useCallback((updater: (params: URLSearchParams) => void) => {
|
||||
const p = new URLSearchParams(searchRef.current)
|
||||
updater(p)
|
||||
navigate({ search: p.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
|
||||
const setScheduleTab = useCallback((tab: ScheduleTab) => {
|
||||
replaceUrlParams((p) => {
|
||||
if (tab === 'jobs') {
|
||||
p.delete('scheduleTab')
|
||||
} else {
|
||||
p.set('scheduleTab', tab)
|
||||
}
|
||||
})
|
||||
}, [replaceUrlParams])
|
||||
|
||||
const openNewJob = useCallback(() => {
|
||||
replaceUrlParams((p) => {
|
||||
p.set('scheduleDialog', 'new')
|
||||
p.delete('jobId')
|
||||
p.delete('templateId')
|
||||
})
|
||||
}, [replaceUrlParams])
|
||||
|
||||
const openEditJob = useCallback((id: number) => {
|
||||
replaceUrlParams((p) => {
|
||||
p.set('scheduleDialog', 'edit')
|
||||
p.set('jobId', String(id))
|
||||
p.delete('templateId')
|
||||
})
|
||||
}, [replaceUrlParams])
|
||||
|
||||
const openDeleteJob = useCallback((id: number) => {
|
||||
replaceUrlParams((p) => {
|
||||
p.set('scheduleDialog', 'delete')
|
||||
p.set('jobId', String(id))
|
||||
p.delete('templateId')
|
||||
})
|
||||
}, [replaceUrlParams])
|
||||
|
||||
const openNewTemplate = useCallback(() => {
|
||||
replaceUrlParams((p) => {
|
||||
p.set('promptDialog', 'new')
|
||||
p.delete('templateId')
|
||||
p.delete('jobId')
|
||||
})
|
||||
}, [replaceUrlParams])
|
||||
|
||||
const openEditTemplate = useCallback((id: number) => {
|
||||
replaceUrlParams((p) => {
|
||||
p.set('promptDialog', 'edit')
|
||||
p.set('templateId', String(id))
|
||||
p.delete('jobId')
|
||||
})
|
||||
}, [replaceUrlParams])
|
||||
|
||||
const openDeleteTemplate = useCallback((id: number) => {
|
||||
replaceUrlParams((p) => {
|
||||
p.set('promptDialog', 'delete')
|
||||
p.set('templateId', String(id))
|
||||
p.delete('jobId')
|
||||
})
|
||||
}, [replaceUrlParams])
|
||||
|
||||
const openImportTemplate = useCallback(() => {
|
||||
replaceUrlParams((p) => {
|
||||
p.set('promptDialog', 'import')
|
||||
p.delete('templateId')
|
||||
p.delete('jobId')
|
||||
})
|
||||
}, [replaceUrlParams])
|
||||
|
||||
const closeDialog = useCallback(() => {
|
||||
const p = new URLSearchParams(searchRef.current)
|
||||
p.delete('scheduleDialog')
|
||||
navigate({ search: p.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
replaceUrlParams((p) => {
|
||||
p.delete('scheduleDialog')
|
||||
})
|
||||
}, [replaceUrlParams])
|
||||
|
||||
const closePromptDialog = useCallback(() => {
|
||||
const p = new URLSearchParams(searchRef.current)
|
||||
p.delete('promptDialog')
|
||||
p.delete('templateId')
|
||||
navigate({ search: p.toString() }, { replace: true })
|
||||
}, [navigate])
|
||||
|
||||
const selectJob = useCallback((id: number | null) => {
|
||||
replaceUrlParams((p) => {
|
||||
if (id === null) {
|
||||
p.delete('jobId')
|
||||
} else {
|
||||
p.set('jobId', String(id))
|
||||
}
|
||||
p.delete('promptDialog')
|
||||
p.delete('templateId')
|
||||
})
|
||||
}, [replaceUrlParams])
|
||||
|
||||
@@ -210,7 +199,6 @@ export function useScheduleUrlState(): UseScheduleUrlStateReturn {
|
||||
openImportTemplate,
|
||||
closeDialog,
|
||||
closePromptDialog,
|
||||
selectJob,
|
||||
selectRun,
|
||||
selectJobAndView,
|
||||
selectJobAndCloseDialog,
|
||||
|
||||
Reference in new issue
Block a user