From 7612ee31e5fc16d8366723547cd0fe65a142e217 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Tue, 6 Oct 2026 11:21:51 -0400 Subject: [PATCH] feat(schedules): switch repository from the schedules header - add ScheduleRepoSwitcher: the header title opens a picker with All repos and every repository, on desktop (dropdown) and mobile (sheet with search) - wire it into the per-repo schedules header and the all-repos header so the scope can be changed from either page - mark the current scope active and navigate on selection --- .../schedules/ScheduleRepoSwitcher.test.tsx | 143 ++++++++++++++ .../schedules/ScheduleRepoSwitcher.tsx | 176 ++++++++++++++++++ frontend/src/components/schedules/index.ts | 1 + frontend/src/pages/GlobalSchedules.tsx | 7 +- frontend/src/pages/Schedules.tsx | 4 +- .../pages/__tests__/GlobalSchedules.test.tsx | 1 + .../src/pages/__tests__/Schedules.test.tsx | 1 + 7 files changed, 329 insertions(+), 4 deletions(-) create mode 100644 frontend/src/components/schedules/ScheduleRepoSwitcher.test.tsx create mode 100644 frontend/src/components/schedules/ScheduleRepoSwitcher.tsx diff --git a/frontend/src/components/schedules/ScheduleRepoSwitcher.test.tsx b/frontend/src/components/schedules/ScheduleRepoSwitcher.test.tsx new file mode 100644 index 00000000..da5a3c39 --- /dev/null +++ b/frontend/src/components/schedules/ScheduleRepoSwitcher.test.tsx @@ -0,0 +1,143 @@ +import { render, screen, waitFor, fireEvent, within } from '@testing-library/react' +import userEvent from '@testing-library/user-event' +import { beforeEach, describe, it, expect, vi } from 'vitest' +import { MemoryRouter, useLocation } from 'react-router-dom' +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { ScheduleRepoSwitcher } from './ScheduleRepoSwitcher' +import { listRepos } from '@/api/repos' +import { ASSISTANT_REPO_ID } from '@opencode-manager/shared/utils' + +const mobileState = { current: false } + +vi.mock('@/hooks/useMobile', async (importOriginal) => { + const actual = await importOriginal() + return { ...actual, useMobile: () => mobileState.current } +}) + +vi.mock('@/api/repos') + +function createRepo(id: number, name: string) { + return { + id, + name, + repoUrl: `https://github.com/test/${name}.git`, + localPath: `${name}-path`, + fullPath: `/repos/${name}-path`, + sourcePath: null, + currentBranch: 'main', + defaultBranch: 'main', + cloneStatus: 'ready' as const, + clonedAt: 0, + lastAccessedAt: 0, + isLocal: false, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + } +} + +function LocationSpy() { + const { pathname } = useLocation() + return
{pathname}
+} + +function renderSwitcher(repoId: number | undefined, name: string) { + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) + return render( + + + + + + , + ) +} + +describe('ScheduleRepoSwitcher', () => { + beforeEach(() => { + vi.clearAllMocks() + mobileState.current = false + vi.mocked(listRepos).mockResolvedValue([ + createRepo(1, 'repo1'), + createRepo(2, 'repo2'), + createRepo(ASSISTANT_REPO_ID, 'Assistant'), + ]) + }) + + it('shows the current repository and switches to another repo (desktop)', async () => { + const user = userEvent.setup() + renderSwitcher(1, 'repo1') + + await user.click(screen.getByRole('button', { name: 'Switch repository (repo1)' })) + + await waitFor(() => expect(screen.getByText('All repos')).toBeInTheDocument()) + expect(screen.getByText('repo2')).toBeInTheDocument() + + await user.click(screen.getByText('repo2')) + + await waitFor(() => expect(screen.getByTestId('location')).toHaveTextContent('/repos/2/schedules')) + }) + + it('switches to all repos', async () => { + const user = userEvent.setup() + renderSwitcher(1, 'repo1') + + await user.click(screen.getByRole('button', { name: 'Switch repository (repo1)' })) + await waitFor(() => expect(screen.getByText('All repos')).toBeInTheDocument()) + + await user.click(screen.getByText('All repos')) + + await waitFor(() => expect(screen.getByTestId('location')).toHaveTextContent('/schedules')) + }) + + it('marks the current repository as active', async () => { + const user = userEvent.setup() + renderSwitcher(1, 'repo1') + + await user.click(screen.getByRole('button', { name: 'Switch repository (repo1)' })) + const menu = await screen.findByRole('menu') + + expect(within(menu).getByText('repo1').closest('[aria-current="true"]')).not.toBeNull() + expect(within(menu).getByText('repo2').closest('[aria-current="true"]')).toBeNull() + }) + + it('navigates to the assistant schedules for repo 0', async () => { + const user = userEvent.setup() + renderSwitcher(ASSISTANT_REPO_ID, 'Assistant') + + await user.click(screen.getByRole('button', { name: 'Switch repository (Assistant)' })) + await waitFor(() => expect(screen.getByText('repo2')).toBeInTheDocument()) + + await user.click(screen.getByText('repo2')) + + await waitFor(() => expect(screen.getByTestId('location')).toHaveTextContent('/repos/2/schedules')) + }) + + it('filters repositories by search on mobile', async () => { + mobileState.current = true + renderSwitcher(1, 'repo1') + + fireEvent.click(screen.getByRole('button', { name: 'Switch repository (repo1)' })) + + const search = await screen.findByPlaceholderText('Search repositories...') + fireEvent.change(search, { target: { value: 'repo2' } }) + + const sheet = screen.getByRole('dialog', { name: 'Switch repository' }) + await waitFor(() => { + expect(within(sheet).getByText('repo2')).toBeInTheDocument() + expect(within(sheet).queryByText('repo1')).not.toBeInTheDocument() + }) + }) + + it('switches repositories from the mobile sheet', async () => { + mobileState.current = true + renderSwitcher(1, 'repo1') + + fireEvent.click(screen.getByRole('button', { name: 'Switch repository (repo1)' })) + await screen.findByPlaceholderText('Search repositories...') + await waitFor(() => expect(screen.getByText('repo2')).toBeInTheDocument()) + + fireEvent.click(screen.getByText('repo2')) + + await waitFor(() => expect(screen.getByTestId('location')).toHaveTextContent('/repos/2/schedules')) + }) +}) diff --git a/frontend/src/components/schedules/ScheduleRepoSwitcher.tsx b/frontend/src/components/schedules/ScheduleRepoSwitcher.tsx new file mode 100644 index 00000000..93f107e6 --- /dev/null +++ b/frontend/src/components/schedules/ScheduleRepoSwitcher.tsx @@ -0,0 +1,176 @@ +import { Fragment, useMemo, useState } from 'react' +import { useNavigate } from 'react-router-dom' +import { Check, ChevronsUpDown, FolderGit2, LayoutGrid } from 'lucide-react' +import { getRepoDisplayName } from '@/lib/utils' +import { useMobile } from '@/hooks/useMobile' +import { useNavigableRepos } from '@/hooks/useSidebarRepoGroups' +import { Input } from '@/components/ui/input' +import { BottomSheet, BottomSheetContent, BottomSheetHeader } from '@/components/ui/bottom-sheet' +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuLabel, + DropdownMenuSeparator, + DropdownMenuTrigger, +} from '@/components/ui/dropdown-menu' + +const ALL_REPOS_PATH = '/schedules' + +interface ScheduleRepoSwitcherProps { + repoId?: number + name: string +} + +interface RepoOption { + key: string + label: string + description: string + path: string + active: boolean + isAllRepos: boolean +} + +function schedulePath(repoId: number): string { + return `/repos/${repoId}/schedules` +} + +export function ScheduleRepoSwitcher({ repoId, name }: ScheduleRepoSwitcherProps) { + const navigate = useNavigate() + const isMobile = useMobile() + const [open, setOpen] = useState(false) + const [search, setSearch] = useState('') + const { repos } = useNavigableRepos() + + const options = useMemo(() => { + const allRepos: RepoOption = { + key: 'all', + label: 'All repos', + description: 'Every repository', + path: ALL_REPOS_PATH, + active: repoId === undefined, + isAllRepos: true, + } + const repoOptions = repos.map((repo): RepoOption => { + const label = getRepoDisplayName(repo) + const localPath = repo.localPath ?? '' + return { + key: String(repo.id), + label, + description: localPath && localPath !== label ? localPath : '', + path: schedulePath(repo.id), + active: repo.id === repoId, + isAllRepos: false, + } + }) + return [allRepos, ...repoOptions] + }, [repos, repoId]) + + const filteredOptions = useMemo(() => { + const term = search.trim().toLowerCase() + if (!term) return options + return options.filter((option) => ( + option.label.toLowerCase().includes(term) || option.description.toLowerCase().includes(term) + )) + }, [options, search]) + + const close = () => { + setOpen(false) + setSearch('') + } + + const select = (option: RepoOption) => { + close() + if (!option.active) navigate(option.path) + } + + const renderOptionContent = (option: RepoOption) => ( + <> + {option.isAllRepos ? ( + + ) : ( + + )} + + {option.label} + {option.description && {option.description}} + + {option.active && } + + ) + + const trigger = (onClick?: () => void) => ( + + ) + + if (isMobile) { + return ( + <> + {trigger(() => setOpen(true))} + + + setSearch(event.target.value)} + autoComplete="off" + name="schedule-repo-switch" + className="mt-2" + /> + + + {filteredOptions.length === 0 ? ( +
+ +

No repositories found

+
+ ) : ( +
+ {filteredOptions.map((option) => ( + + ))} +
+ )} +