From c5bf97b4630bf960bd339934514c06b7c3eef82d Mon Sep 17 00:00:00 2001 From: Jaayden Halko Date: Fri, 16 Oct 2026 15:18:38 +0800 Subject: [PATCH] feat(site): add Projects empty state and persist section collapse (#30741) When a user has no projects, the Agents sidebar's Projects section now shows a dashed empty-state box ("Group related chats together, with shared members and history.") with a **New project** button, replacing the "No projects yet" text. The box has no dismiss control. The Projects section's collapsed state is now saved per user in localStorage (`agents.sidebar.projects-collapsed.`) and restored on load. Collapsing the section hides the empty-state box. The header's **+** button is hidden while the box is visible, so the section never shows two New project buttons, and while an expanded section is loading, so an empty result never unmounts a focused button. It shows when the section is collapsed or projects exist. | Empty state | Collapsed | |---|---| | ![Projects empty state](https://raw.githubusercontent.com/coder/coder/assets/pr-30741-projects-empty-state/pr/projects-empty.png) | ![Projects section collapsed](https://raw.githubusercontent.com/coder/coder/assets/pr-30741-projects-empty-state/pr/projects-collapsed.png) | --- .../ChatsSidebar/ChatsSidebar.stories.tsx | 43 ++++++- .../components/ChatsSidebar/ChatsSidebar.tsx | 1 + .../ChatsSidebar/chats/ChatsPanel.tsx | 5 + .../chats/ProjectFolders.test.tsx | 113 +++++++++++++++--- .../ChatsSidebar/chats/ProjectFolders.tsx | 65 +++++++--- .../utils/projectsSectionStorage.ts | 34 ++++++ 6 files changed, 223 insertions(+), 38 deletions(-) create mode 100644 site/src/pages/AgentsPage/utils/projectsSectionStorage.ts diff --git a/site/src/pages/AgentsPage/components/ChatsSidebar/ChatsSidebar.stories.tsx b/site/src/pages/AgentsPage/components/ChatsSidebar/ChatsSidebar.stories.tsx index 693a4f16fd9..49470748937 100644 --- a/site/src/pages/AgentsPage/components/ChatsSidebar/ChatsSidebar.stories.tsx +++ b/site/src/pages/AgentsPage/components/ChatsSidebar/ChatsSidebar.stories.tsx @@ -1,5 +1,6 @@ import type { Meta, StoryObj } from "@storybook/react-vite"; import { useEffect, useState } from "react"; +import { useQueryClient } from "react-query"; import { useLocation } from "react-router"; import { expect, @@ -28,6 +29,7 @@ import { } from "#/testHelpers/storybook"; import { useAgentsPageKeybindings } from "../../hooks/useAgentsPageKeybindings"; import { DEFAULT_AGENT_SIDEBAR_FILTERS as defaultSidebarFilters } from "../../utils/agentSidebarFilters"; +import { projectsSectionCollapsedStorageKey } from "../../utils/projectsSectionStorage"; import { ChatsSidebar } from "./ChatsSidebar"; // Probe element used by the archived-filter preservation story to surface the @@ -2936,10 +2938,18 @@ export const ProjectsSectionCollapsed: Story = { }, ], }, - play: async ({ canvasElement }) => { - await userEvent.click( - within(canvasElement).getByRole("button", { name: "Projects" }), - ); + beforeEach: () => { + const key = projectsSectionCollapsedStorageKey(MockUserOwner.id); + localStorage.setItem(key, "true"); + return () => localStorage.removeItem(key); + }, +}; + +export const ProjectsEmpty: Story = { + args: { chats: [buildChat({ id: "loose-chat", title: "Loose chat" })] }, + parameters: { + experiments: ["chat-projects"], + queries: [{ key: chatProjectsKey, data: [] }], }, }; @@ -2988,6 +2998,31 @@ export const ProjectsLoadError: Story = { }, }; +export const ProjectsEmptyRefetchError: Story = { + args: { chats: [buildChat({ id: "loose-chat", title: "Loose chat" })] }, + parameters: { + experiments: ["chat-projects"], + queries: [{ key: chatProjectsKey, data: [] }], + }, + beforeEach: () => { + spyOn(API.experimental, "getChatProjects").mockRejectedValue( + mockApiError({ message: "Failed to refresh projects." }), + ); + }, + render: function ProjectsEmptyRefetchErrorRender(args) { + const queryClient = useQueryClient(); + // Seeded data never goes stale in Storybook, so refetch explicitly + // to fail on top of the cached empty list. + useEffect(() => { + void queryClient.refetchQueries({ queryKey: chatProjectsKey }); + }, [queryClient]); + return ; + }, + play: async ({ canvasElement }) => { + await within(canvasElement).findByRole("button", { name: "Retry" }); + }, +}; + const mockEmojiProject: TypesGen.ChatProject = { ...MockChatProject, icon: "/emojis/1f680.png", diff --git a/site/src/pages/AgentsPage/components/ChatsSidebar/ChatsSidebar.tsx b/site/src/pages/AgentsPage/components/ChatsSidebar/ChatsSidebar.tsx index 46940cd2f31..e6b2cb45ab3 100644 --- a/site/src/pages/AgentsPage/components/ChatsSidebar/ChatsSidebar.tsx +++ b/site/src/pages/AgentsPage/components/ChatsSidebar/ChatsSidebar.tsx @@ -263,6 +263,7 @@ export const ChatsSidebar: React.FC = (props) => { organizations={organizations} isProjectsLoading={projectsQuery.isLoading} projectsError={projectsQuery.error} + isProjectsLoadError={projectsQuery.isLoadingError} onRetryProjects={() => void projectsQuery.refetch()} onOpenProjectDialog={openProjectDialog} onDeleteProject={openDeleteProjectDialog} diff --git a/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ChatsPanel.tsx b/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ChatsPanel.tsx index e49422280e2..a62f43f01ec 100644 --- a/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ChatsPanel.tsx +++ b/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ChatsPanel.tsx @@ -92,6 +92,8 @@ type ChatsPanelProps = { readonly organizations: readonly Organization[]; readonly isProjectsLoading: boolean; readonly projectsError: unknown; + /** True when the first projects load failed and no data is cached. */ + readonly isProjectsLoadError: boolean; readonly onRetryProjects: () => void; readonly onOpenProjectDialog: OpenProjectDialog; readonly onDeleteProject: DeleteProject; @@ -128,6 +130,7 @@ export const ChatsPanel: React.FC = ({ organizations, isProjectsLoading, projectsError, + isProjectsLoadError, onRetryProjects, onOpenProjectDialog, onDeleteProject, @@ -569,6 +572,7 @@ export const ChatsPanel: React.FC = ({ {chatProjectsEnabled && ( = ({ onDelete={onDeleteProject} isLoading={isProjectsLoading} error={projectsError} + isLoadError={isProjectsLoadError} onRetry={onRetryProjects} emptyMessage={emptyStateMessage} /> diff --git a/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ProjectFolders.test.tsx b/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ProjectFolders.test.tsx index 50e3a162d6b..6836b097bb3 100644 --- a/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ProjectFolders.test.tsx +++ b/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ProjectFolders.test.tsx @@ -1,15 +1,58 @@ import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { MemoryRouter } from "react-router"; -import { describe, expect, it, vi } from "vitest"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import type { ChatProject } from "#/api/typesGenerated"; import { MockChatProject, MockDefaultOrganization, MockOrganization2, + MockUserMember, + MockUserOwner, } from "#/testHelpers/entities"; +import { projectsSectionCollapsedStorageKey } from "../../../utils/projectsSectionStorage"; import { ProjectFolders } from "./ProjectFolders"; +type ProjectFoldersOptions = { + readonly error?: unknown; + readonly currentUserId?: string; +}; + +const projectFoldersElement = ( + projects: readonly ChatProject[], + { error, currentUserId = MockUserOwner.id }: ProjectFoldersOptions = {}, +) => ( + + + +); + +const renderProjectFolders = ( + projects: readonly ChatProject[], + options?: ProjectFoldersOptions, +) => render(projectFoldersElement(projects, options)); + +const collapsedKey = projectsSectionCollapsedStorageKey(MockUserOwner.id); + describe("ProjectFolders", () => { + beforeEach(() => { + localStorage.clear(); + }); + it("keeps cached folders usable when a refetch fails", async () => { const user = userEvent.setup(); const onToggle = vi.fn(); @@ -18,6 +61,7 @@ describe("ProjectFolders", () => { render( { emptyMessage="No agents yet" isLoading={false} error={new Error("Projects unavailable")} + isLoadError={false} onRetry={onRetry} /> , @@ -42,26 +87,26 @@ describe("ProjectFolders", () => { expect(onRetry).toHaveBeenCalledTimes(1); }); + it("keeps focus on the empty state's button when a refetch fails", async () => { + const user = userEvent.setup(); + const { rerender } = renderProjectFolders([]); + + await user.tab(); + await user.tab(); + const newProjectButton = screen.getByRole("button", { + name: "New project", + }); + expect(newProjectButton).toHaveFocus(); + + rerender( + projectFoldersElement([], { error: new Error("Projects unavailable") }), + ); + expect(screen.getByRole("button", { name: "New project" })).toHaveFocus(); + }); + it("collapses and expands the whole Projects section", async () => { const user = userEvent.setup(); - - render( - - - , - ); + renderProjectFolders([MockChatProject]); const sectionToggle = screen.getByRole("button", { name: "Projects" }); await user.click(sectionToggle); @@ -78,6 +123,34 @@ describe("ProjectFolders", () => { ).toBeVisible(); }); + it("remembers the Projects section collapse state across mounts", async () => { + const user = userEvent.setup(); + const firstMount = renderProjectFolders([]); + + await user.click(screen.getByRole("button", { name: "Projects" })); + expect(localStorage.getItem(collapsedKey)).toBe("true"); + firstMount.unmount(); + + renderProjectFolders([]); + await user.click(screen.getByRole("button", { name: "Projects" })); + expect(localStorage.getItem(collapsedKey)).toBeNull(); + }); + + it("keeps the saved collapse state separate per user", async () => { + const user = userEvent.setup(); + const firstMount = renderProjectFolders([]); + await user.click(screen.getByRole("button", { name: "Projects" })); + firstMount.unmount(); + + renderProjectFolders([], { currentUserId: MockUserMember.id }); + await user.click(screen.getByRole("button", { name: "Projects" })); + expect( + localStorage.getItem( + projectsSectionCollapsedStorageKey(MockUserMember.id), + ), + ).toBe("true"); + }); + it("names same-named projects by their organization", async () => { const user = userEvent.setup(); const onToggle = vi.fn(); @@ -90,6 +163,7 @@ describe("ProjectFolders", () => { render( { emptyMessage="No agents yet" isLoading={false} error={undefined} + isLoadError={false} onRetry={vi.fn()} /> , diff --git a/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ProjectFolders.tsx b/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ProjectFolders.tsx index 272f66f0b74..a4e52a5d5a1 100644 --- a/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ProjectFolders.tsx +++ b/site/src/pages/AgentsPage/components/ChatsSidebar/chats/ProjectFolders.tsx @@ -21,6 +21,10 @@ import { } from "#/components/DropdownMenu/DropdownMenu"; import { Skeleton } from "#/components/Skeleton/Skeleton"; import { buildAgentProjectPath } from "../../../utils/navigation"; +import { + getPersistedProjectsSectionExpanded, + savePersistedProjectsSectionExpanded, +} from "../../../utils/projectsSectionStorage"; import { ChatProjectIcon } from "../../ChatProjectIcon"; import { ChatTreeNode } from "../tree/ChatTreeNode"; import { ProjectActionsMenuItems } from "./ProjectActionsMenuItems"; @@ -35,6 +39,7 @@ export type OpenProjectDialog = (dialog: ProjectDialogState) => void; export type DeleteProject = (project: ChatProject) => void; type ProjectFoldersProps = { + readonly currentUserId: string; readonly projects: readonly ChatProject[]; readonly organizations: readonly Organization[]; readonly chatsByProjectId: ReadonlyMap; @@ -44,11 +49,17 @@ type ProjectFoldersProps = { readonly onDelete: DeleteProject; readonly isLoading: boolean; readonly error: unknown; + /** + * True when the first load failed. A failed refetch keeps cached + * projects, including an empty list, so its empty state stays. + */ + readonly isLoadError: boolean; readonly onRetry: () => void; readonly emptyMessage: string; }; export const ProjectFolders: React.FC = ({ + currentUserId, projects, organizations, chatsByProjectId, @@ -58,12 +69,28 @@ export const ProjectFolders: React.FC = ({ onDelete, isLoading, error, + isLoadError, onRetry, emptyMessage, }) => { const location = useLocation(); const organizationLabels = getOrganizationLabels(projects, organizations); - const [isExpanded, setIsExpanded] = useState(true); + const [isExpanded, setIsExpanded] = useState(() => + getPersistedProjectsSectionExpanded(currentUserId), + ); + const toggleExpanded = () => { + const next = !isExpanded; + setIsExpanded(next); + savePersistedProjectsSectionExpanded(currentUserId, next); + }; + const openCreateDialog = () => onOpenProjectDialog({ mode: "create" }); + const isShowingEmptyState = + isExpanded && !isLoading && !isLoadError && projects.length === 0; + // Also hidden while an expanded section loads: if the load comes back + // empty, the empty state's button would replace a focused header button + // and drop keyboard focus. + const isShowingHeaderNewProject = + !isShowingEmptyState && !(isExpanded && isLoading); return (
@@ -71,18 +98,20 @@ export const ProjectFolders: React.FC = ({ title="Projects" collapsible={{ expanded: isExpanded, - onToggle: () => setIsExpanded((expanded) => !expanded), + onToggle: toggleExpanded, }} actions={ - + isShowingHeaderNewProject && ( + + ) } /> {isExpanded && ( @@ -99,10 +128,16 @@ export const ProjectFolders: React.FC = ({ /> )} {isLoading && } - {!isLoading && !error && projects.length === 0 && ( -

- No projects yet -

+ {isShowingEmptyState && ( +
+

+ Group related chats together, with shared members and history. +

+ +
)} {projects.length > 0 && (
diff --git a/site/src/pages/AgentsPage/utils/projectsSectionStorage.ts b/site/src/pages/AgentsPage/utils/projectsSectionStorage.ts new file mode 100644 index 00000000000..1add8e28ec0 --- /dev/null +++ b/site/src/pages/AgentsPage/utils/projectsSectionStorage.ts @@ -0,0 +1,34 @@ +/** Per user, because the preference outlives sign-out in a shared browser. */ +export function projectsSectionCollapsedStorageKey(userId: string): string { + return `agents.sidebar.projects-collapsed.${userId}`; +} + +/** Returns true when nothing is saved or storage is unavailable. */ +export function getPersistedProjectsSectionExpanded(userId: string): boolean { + try { + return ( + localStorage.getItem(projectsSectionCollapsedStorageKey(userId)) !== + "true" + ); + } catch { + return true; + } +} + +/** + * Best effort: storage failures are ignored, so the state then lasts only + * for the current page session. + */ +export function savePersistedProjectsSectionExpanded( + userId: string, + expanded: boolean, +): void { + const key = projectsSectionCollapsedStorageKey(userId); + try { + if (expanded) { + localStorage.removeItem(key); + } else { + localStorage.setItem(key, "true"); + } + } catch {} +}