An open-source, faster Devin alternative
TypeScript
dashboard: archive several automation projects at once
Mohamed Habib via Computera78653472c4f
diff --git a/web/src/managed-agents/Home.archive.test.tsx b/web/src/managed-agents/Home.archive.test.tsxnew file mode 100644index 00000000..b123cbbf--- /dev/null+++ b/web/src/managed-agents/Home.archive.test.tsx@@ -0,0 +1,103 @@+// @vitest-environment happy-dom++import { act } from 'react'+import { createRoot, type Root } from 'react-dom/client'+import { QueryClient, QueryClientProvider } from '@tanstack/react-query'+import { MemoryRouter } from 'react-router-dom'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++const projectsApi = vi.hoisted(() => ({+ getManagedProjects: vi.fn(),+ archiveManagedProject: vi.fn(),+ restoreManagedProject: vi.fn(),+}))+vi.mock('./api', () => projectsApi)++const { default: ProjectsHome } = await import('./Home')++const now = new Date().toISOString()+const project = (id: string, name: string) => ({+ id,+ slug: id,+ name,+ environments: [{ name: 'development', updatedAt: now }],+ agents: [],+ createdAt: now,+ updatedAt: now,+})++let container: HTMLDivElement+let root: Root++beforeEach(() => {+ ;(+ globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }+ ).IS_REACT_ACT_ENVIRONMENT = true+ projectsApi.getManagedProjects.mockImplementation(+ (options: { archived?: boolean } = {}) =>+ Promise.resolve(+ options.archived+ ? []+ : [+ project('p1', 'Support'),+ project('p2', 'Billing'),+ project('p3', 'Growth'),+ ],+ ),+ )+ projectsApi.archiveManagedProject.mockResolvedValue(project('p1', 'x'))+ container = document.createElement('div')+ document.body.appendChild(container)+ root = createRoot(container)+})++afterEach(() => {+ act(() => root.unmount())+ container.remove()+ document.body.innerHTML = ''+ vi.clearAllMocks()+})++const settle = () => act(() => new Promise((resolve) => setTimeout(resolve, 0)))+function button(text: string) {+ const match = [...document.querySelectorAll('button')].find(+ (candidate) =>+ candidate.textContent?.trim() === text ||+ candidate.getAttribute('aria-label') === text,+ )+ if (!match) throw new Error(`No button ${text}`)+ return match+}+const click = (text: string) => act(() => button(text).click())++describe('archiving many projects at once', () => {+ it('archives every selected project after one confirmation', async () => {+ act(() =>+ root.render(+ <QueryClientProvider client={new QueryClient()}>+ <MemoryRouter>+ <ProjectsHome list />+ </MemoryRouter>+ </QueryClientProvider>,+ ),+ )+ await settle()++ click('Select')+ click('Select Support')+ click('Select Growth')+ expect(button('Select Support').getAttribute('aria-pressed')).toBe('true')+ expect(document.body.textContent).toContain('2 selected')++ click('Archive 2')+ await settle()+ expect(document.body.textContent).toContain('Archive 2 projects?')++ click('Archive 2 projects')+ await settle()++ expect(+ projectsApi.archiveManagedProject.mock.calls.map(([id]) => id as string),+ ).toEqual(['p1', 'p3'])+ })+})diff --git a/web/src/managed-agents/Home.tsx b/web/src/managed-agents/Home.tsxindex cba35600..e46228a8 100644--- a/web/src/managed-agents/Home.tsx+++ b/web/src/managed-agents/Home.tsx@@ -3,6 +3,8 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'import { Link, Navigate } from 'react-router-dom'import {Archive,+ Check,+ CheckSquare,ChevronRight,FolderKanban,Loader2,@@ -47,21 +49,62 @@ export default function ProjectsHome({ list = false }: { list?: boolean }) {})const items = projects.data ?? []const archivedItems = archivedProjects.data ?? []- // The project the confirm dialog is open for, held whole so the dialog can- // name it after the list behind it has already been invalidated.- const [pendingArchive, setPendingArchive] = useState<{- id: string- name: string- } | null>(null)+ // The projects the confirm dialog is open for, held whole so the dialog can+ // name them after the list behind it has already been invalidated.+ const [pendingArchive, setPendingArchive] = useState<+ { id: string; name: string }[] | null+ >(null)+ // Selection mode turns the cards into checkboxes for archiving many at once.+ const [selecting, setSelecting] = useState(false)+ const [selected, setSelected] = useState<Set<string>>(new Set())+ const stopSelecting = () => {+ setSelecting(false)+ setSelected(new Set())+ }const archive = useMutation({- mutationFn: archiveManagedProject,- onSuccess: async () => {+ // One request per project; each is archived or fails on its own.+ mutationFn: async (ids: string[]) => {+ const results = await Promise.allSettled(ids.map(archiveManagedProject))+ const failed = ids.filter((_, i) => results[i].status === 'rejected')+ const rejection = results.find(+ (result): result is PromiseRejectedResult =>+ result.status === 'rejected',+ )+ return { failed, error: rejection?.reason as unknown }+ },+ onSuccess: async ({ failed, error }, ids) => {setPendingArchive(null)await queryClient.invalidateQueries({ queryKey: ['managed-projects'] })- notifySuccess('Project archived.', 'You can restore it at any time.')+ const archived = ids.length - failed.length+ if (failed.length === 0) {+ stopSelecting()+ notifySuccess(+ archived === 1+ ? 'Project archived.'+ : `${archived} projects archived.`,+ 'You can restore them at any time.',+ )+ return+ }+ // Keep what failed selected so it can be retried.+ setSelected(new Set(failed))+ notifyError(+ ids.length === 1+ ? "Couldn't archive the project."+ : `Couldn't archive ${failed.length} of ${ids.length} projects.`,+ error,+ )},onError: (error) => notifyError("Couldn't archive the project.", error),})+ const toggleSelected = (id: string) =>+ setSelected((current) => {+ const next = new Set(current)+ if (next.has(id)) next.delete(id)+ else next.add(id)+ return next+ })+ const selectedProjects = items.filter((project) => selected.has(project.id))const restore = useMutation({mutationFn: restoreManagedProject,onSuccess: async () => {@@ -146,58 +189,146 @@ export default function ProjectsHome({ list = false }: { list?: boolean }) {title="Automation projects"description="Open one to see its agent playground, deployments, sessions, and resources."actions={- <Button asChild>- <Link to="/new">- <Plus /> New project- </Link>- </Button>+ <>+ {items.length > 1 && !selecting && (+ <Button variant="outline" onClick={() => setSelecting(true)}>+ <CheckSquare /> Select+ </Button>+ )}+ <Button asChild>+ <Link to="/new">+ <Plus /> New project+ </Link>+ </Button>+ </>}/><section className="space-y-3">- <div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">- {items.map((project) => (- <Link- key={project.id}- to={`/projects/${encodeURIComponent(project.id)}`}- className="group focus-visible:ring-ring/50 rounded-lg outline-none focus-visible:ring-3"+ {selecting && (+ <div className="flex flex-wrap items-center gap-2">+ <p className="text-muted-foreground mr-auto text-sm">+ {selected.size} selected+ </p>+ <Button+ variant="ghost"+ size="sm"+ onClick={() =>+ setSelected(+ selected.size === items.length+ ? new Set()+ : new Set(items.map((project) => project.id)),+ )+ }+ >+ {selected.size === items.length ? 'Select none' : 'Select all'}+ </Button>+ <Button variant="ghost" size="sm" onClick={stopSelecting}>+ Cancel+ </Button>+ <Button+ size="sm"+ disabled={selected.size === 0}+ onClick={() =>+ setPendingArchive(+ selectedProjects.map(({ id, name }) => ({ id, name })),+ )+ }>- <Panel className="group-hover:border-foreground/20 group-hover:bg-muted/25 h-full transition-colors">- <PanelContent className="flex items-center gap-3">- <div className="bg-primary/10 text-primary flex size-10 shrink-0 items-center justify-center rounded-lg">- <FolderKanban className="size-4" aria-hidden />- </div>- <div className="min-w-0 flex-1">- <p className="truncate text-sm font-medium">- {project.name}- </p>- <p className="text-muted-foreground mt-0.5 text-xs">- {project.agents.length}{' '}- {project.agents.length === 1 ? 'agent' : 'agents'} ·{' '}- {project.environments.length === 1- ? 'single environment'- : `${project.environments.length} environments`}- </p>- </div>- <Button- variant="ghost"- size="icon"- aria-label={`Archive ${project.name}`}- className="text-muted-foreground size-8 shrink-0"- onClick={(event) => {- // The whole card is a link; archiving must not navigate.- event.preventDefault()- event.stopPropagation()- setPendingArchive({ id: project.id, name: project.name })- }}- >- <Archive className="size-4" aria-hidden />- </Button>- <ChevronRight className="text-muted-foreground size-4 transition-transform group-hover:translate-x-0.5" />- </PanelContent>- </Panel>- </Link>- ))}+ <Archive aria-hidden />+ Archive+ {selected.size > 0 ? ` ${selected.size}` : ''}+ </Button>+ </div>+ )}+ <div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">+ {items.map((project) =>+ selecting ? (+ <button+ key={project.id}+ type="button"+ aria-pressed={selected.has(project.id)}+ aria-label={`Select ${project.name}`}+ onClick={() => toggleSelected(project.id)}+ className="focus-visible:ring-ring/50 rounded-lg text-left outline-none focus-visible:ring-3"+ >+ <Panel+ className={+ selected.has(project.id)+ ? 'border-primary/60 bg-primary/5 h-full transition-colors'+ : 'hover:border-foreground/20 hover:bg-muted/25 h-full transition-colors'+ }+ >+ <PanelContent className="flex items-center gap-3">+ <span+ aria-hidden+ className={+ selected.has(project.id)+ ? 'border-primary bg-primary text-primary-foreground flex size-4 shrink-0 items-center justify-center rounded-[4px] border'+ : 'border-input flex size-4 shrink-0 items-center justify-center rounded-[4px] border'+ }+ >+ {selected.has(project.id) && (+ <Check className="size-3.5" />+ )}+ </span>+ <div className="min-w-0 flex-1">+ <p className="truncate text-sm font-medium">+ {project.name}+ </p>+ <p className="text-muted-foreground mt-0.5 text-xs">+ {project.agents.length}{' '}+ {project.agents.length === 1 ? 'agent' : 'agents'}+ </p>+ </div>+ </PanelContent>+ </Panel>+ </button>+ ) : (+ <Link+ key={project.id}+ to={`/projects/${encodeURIComponent(project.id)}`}+ className="group focus-visible:ring-ring/50 rounded-lg outline-none focus-visible:ring-3"+ >+ <Panel className="group-hover:border-foreground/20 group-hover:bg-muted/25 h-full transition-colors">+ <PanelContent className="flex items-center gap-3">+ <div className="bg-primary/10 text-primary flex size-10 shrink-0 items-center justify-center rounded-lg">+ <FolderKanban className="size-4" aria-hidden />+ </div>+ <div className="min-w-0 flex-1">+ <p className="truncate text-sm font-medium">+ {project.name}+ </p>+ <p className="text-muted-foreground mt-0.5 text-xs">+ {project.agents.length}{' '}+ {project.agents.length === 1 ? 'agent' : 'agents'} ·{' '}+ {project.environments.length === 1+ ? 'single environment'+ : `${project.environments.length} environments`}+ </p>+ </div>+ <Button+ variant="ghost"+ size="icon"+ aria-label={`Archive ${project.name}`}+ className="text-muted-foreground size-8 shrink-0"+ onClick={(event) => {+ // The whole card is a link; archiving must not navigate.+ event.preventDefault()+ event.stopPropagation()+ setPendingArchive([+ { id: project.id, name: project.name },+ ])+ }}+ >+ <Archive className="size-4" aria-hidden />+ </Button>+ <ChevronRight className="text-muted-foreground size-4 transition-transform group-hover:translate-x-0.5" />+ </PanelContent>+ </Panel>+ </Link>+ ),+ )}</div></section>@@ -248,12 +379,24 @@ export default function ProjectsHome({ list = false }: { list?: boolean }) {onOpenChange={(open) => {if (!open) setPendingArchive(null)}}- title={`Archive ${pendingArchive?.name ?? 'this project'}?`}- description="This hides the project and stops its running sessions. Its agents, deployments, secrets and connections are preserved, and you can restore it later."- confirmLabel="Archive project"+ title={+ pendingArchive && pendingArchive.length > 1+ ? `Archive ${pendingArchive.length} projects?`+ : `Archive ${pendingArchive?.[0]?.name ?? 'this project'}?`+ }+ description={+ pendingArchive && pendingArchive.length > 1+ ? `This hides ${pendingArchive.map((p) => p.name).join(', ')} and stops their running sessions. Their agents, deployments, secrets and connections are preserved, and you can restore them later.`+ : 'This hides the project and stops its running sessions. Its agents, deployments, secrets and connections are preserved, and you can restore it later.'+ }+ confirmLabel={+ pendingArchive && pendingArchive.length > 1+ ? `Archive ${pendingArchive.length} projects`+ : 'Archive project'+ }pending={archive.isPending}onConfirm={() => {- if (pendingArchive) archive.mutate(pendingArchive.id)+ if (pendingArchive) archive.mutate(pendingArchive.map((p) => p.id))}}/></div>