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.<user ID>`) 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 |
|---|---|
| 
| 
|
This commit is contained in:
1 parent
357e81b470
commit
c5bf97b463
6 files changed
+223
-38
No files matched your search
@@ -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 <ChatsSidebar {...args} />;
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
await within(canvasElement).findByRole("button", { name: "Retry" });
|
||||
},
|
||||
};
|
||||
|
||||
const mockEmojiProject: TypesGen.ChatProject = {
|
||||
...MockChatProject,
|
||||
icon: "/emojis/1f680.png",
|
||||
|
||||
@@ -263,6 +263,7 @@ export const ChatsSidebar: React.FC<ChatsSidebarProps> = (props) => {
|
||||
organizations={organizations}
|
||||
isProjectsLoading={projectsQuery.isLoading}
|
||||
projectsError={projectsQuery.error}
|
||||
isProjectsLoadError={projectsQuery.isLoadingError}
|
||||
onRetryProjects={() => void projectsQuery.refetch()}
|
||||
onOpenProjectDialog={openProjectDialog}
|
||||
onDeleteProject={openDeleteProjectDialog}
|
||||
|
||||
@@ -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<ChatsPanelProps> = ({
|
||||
organizations,
|
||||
isProjectsLoading,
|
||||
projectsError,
|
||||
isProjectsLoadError,
|
||||
onRetryProjects,
|
||||
onOpenProjectDialog,
|
||||
onDeleteProject,
|
||||
@@ -569,6 +572,7 @@ export const ChatsPanel: React.FC<ChatsPanelProps> = ({
|
||||
{chatProjectsEnabled && (
|
||||
<ChatTreeContext value={chatTreeCtx}>
|
||||
<ProjectFolders
|
||||
currentUserId={currentUserId}
|
||||
projects={projects}
|
||||
organizations={organizations}
|
||||
chatsByProjectId={chatsByProjectId}
|
||||
@@ -584,6 +588,7 @@ export const ChatsPanel: React.FC<ChatsPanelProps> = ({
|
||||
onDelete={onDeleteProject}
|
||||
isLoading={isProjectsLoading}
|
||||
error={projectsError}
|
||||
isLoadError={isProjectsLoadError}
|
||||
onRetry={onRetryProjects}
|
||||
emptyMessage={emptyStateMessage}
|
||||
/>
|
||||
|
||||
@@ -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 = {},
|
||||
) => (
|
||||
<MemoryRouter>
|
||||
<ProjectFolders
|
||||
currentUserId={currentUserId}
|
||||
projects={projects}
|
||||
organizations={[MockDefaultOrganization]}
|
||||
chatsByProjectId={new Map()}
|
||||
expandedProjectIds={{}}
|
||||
onToggle={vi.fn()}
|
||||
onOpenProjectDialog={vi.fn()}
|
||||
onDelete={vi.fn()}
|
||||
emptyMessage="No agents yet"
|
||||
isLoading={false}
|
||||
error={error}
|
||||
isLoadError={false}
|
||||
onRetry={vi.fn()}
|
||||
/>
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
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(
|
||||
<MemoryRouter>
|
||||
<ProjectFolders
|
||||
currentUserId={MockUserOwner.id}
|
||||
projects={[MockChatProject]}
|
||||
organizations={[MockDefaultOrganization]}
|
||||
chatsByProjectId={new Map()}
|
||||
@@ -28,6 +72,7 @@ describe("ProjectFolders", () => {
|
||||
emptyMessage="No agents yet"
|
||||
isLoading={false}
|
||||
error={new Error("Projects unavailable")}
|
||||
isLoadError={false}
|
||||
onRetry={onRetry}
|
||||
/>
|
||||
</MemoryRouter>,
|
||||
@@ -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(
|
||||
<MemoryRouter>
|
||||
<ProjectFolders
|
||||
projects={[MockChatProject]}
|
||||
organizations={[MockDefaultOrganization]}
|
||||
chatsByProjectId={new Map()}
|
||||
expandedProjectIds={{}}
|
||||
onToggle={vi.fn()}
|
||||
onOpenProjectDialog={vi.fn()}
|
||||
onDelete={vi.fn()}
|
||||
emptyMessage="No agents yet"
|
||||
isLoading={false}
|
||||
error={undefined}
|
||||
onRetry={vi.fn()}
|
||||
/>
|
||||
</MemoryRouter>,
|
||||
);
|
||||
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(
|
||||
<MemoryRouter>
|
||||
<ProjectFolders
|
||||
currentUserId={MockUserOwner.id}
|
||||
projects={[MockChatProject, otherProject]}
|
||||
organizations={[MockDefaultOrganization, MockOrganization2]}
|
||||
chatsByProjectId={new Map()}
|
||||
@@ -100,6 +174,7 @@ describe("ProjectFolders", () => {
|
||||
emptyMessage="No agents yet"
|
||||
isLoading={false}
|
||||
error={undefined}
|
||||
isLoadError={false}
|
||||
onRetry={vi.fn()}
|
||||
/>
|
||||
</MemoryRouter>,
|
||||
|
||||
@@ -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<string, readonly Chat[]>;
|
||||
@@ -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<ProjectFoldersProps> = ({
|
||||
currentUserId,
|
||||
projects,
|
||||
organizations,
|
||||
chatsByProjectId,
|
||||
@@ -58,12 +69,28 @@ export const ProjectFolders: React.FC<ProjectFoldersProps> = ({
|
||||
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 (
|
||||
<div>
|
||||
@@ -71,18 +98,20 @@ export const ProjectFolders: React.FC<ProjectFoldersProps> = ({
|
||||
title="Projects"
|
||||
collapsible={{
|
||||
expanded: isExpanded,
|
||||
onToggle: () => setIsExpanded((expanded) => !expanded),
|
||||
onToggle: toggleExpanded,
|
||||
}}
|
||||
actions={
|
||||
<Button
|
||||
variant="subtle"
|
||||
size="icon"
|
||||
className="size-7"
|
||||
aria-label="New project"
|
||||
onClick={() => onOpenProjectDialog({ mode: "create" })}
|
||||
>
|
||||
<PlusIcon />
|
||||
</Button>
|
||||
isShowingHeaderNewProject && (
|
||||
<Button
|
||||
variant="subtle"
|
||||
size="icon"
|
||||
className="size-7"
|
||||
aria-label="New project"
|
||||
onClick={openCreateDialog}
|
||||
>
|
||||
<PlusIcon />
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
/>
|
||||
{isExpanded && (
|
||||
@@ -99,10 +128,16 @@ export const ProjectFolders: React.FC<ProjectFoldersProps> = ({
|
||||
/>
|
||||
)}
|
||||
{isLoading && <Skeleton className="ml-2.5 h-3.5 w-20" />}
|
||||
{!isLoading && !error && projects.length === 0 && (
|
||||
<p className="m-0 px-2.5 py-1 text-xs text-content-secondary">
|
||||
No projects yet
|
||||
</p>
|
||||
{isShowingEmptyState && (
|
||||
<div className="flex flex-col items-center gap-3 rounded-lg border border-dashed border-border mx-1 px-8 py-5 text-center">
|
||||
<p className="m-0 text-sm text-content-secondary">
|
||||
Group related chats together, with shared members and history.
|
||||
</p>
|
||||
<Button variant="outline" size="sm" onClick={openCreateDialog}>
|
||||
<PlusIcon />
|
||||
New project
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
{projects.length > 0 && (
|
||||
<div className="flex flex-col gap-0.5">
|
||||
|
||||
@@ -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 {}
|
||||
}
|
||||
Reference in new issue
Block a user