Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(web): start re-created columns expanded

Tin558d8307c3e8
diff --git a/apps/web/src/hooks/mutations/column/use-create-column.test.tsx b/apps/web/src/hooks/mutations/column/use-create-column.test.tsx
new file mode 100644
index 000000000..0a1f67784
--- /dev/null
+++ b/apps/web/src/hooks/mutations/column/use-create-column.test.tsx
@@ -0,0 +1,46 @@
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { act, renderHook } from "@testing-library/react";
+import type { ReactNode } from "react";
+import { afterEach, expect, it, vi } from "vite-plus/test";
+import { useCollapsedSections } from "@/hooks/use-collapsed-sections";
+import { useCreateColumn } from "./use-create-column";
+
+vi.mock("@/fetchers/column/create-column", () => ({
+ default: vi.fn(async (projectId: string, data: { name: string }) => ({
+ id: "column-new",
+ projectId,
+ name: data.name,
+ slug: data.name.toLowerCase(),
+ })),
+}));
+
+function Providers({ children }: { children: ReactNode }) {
+ return (
+ <QueryClientProvider client={new QueryClient()}>
+ {children}
+ </QueryClientProvider>
+ );
+}
+
+afterEach(() => window.localStorage.clear());
+
+it("starts a re-created column expanded after the old one was collapsed", async () => {
+ const before = renderHook(() => useCollapsedSections("p", "list"));
+ act(() => before.result.current.toggleSection("qa"));
+ act(() => before.result.current.toggleSection("done"));
+ // The old "QA" column is deleted in project settings, where the list view
+ // is not mounted, so only the create mutation sees the slug come back.
+ before.unmount();
+
+ const create = renderHook(() => useCreateColumn(), { wrapper: Providers });
+ await act(() =>
+ create.result.current.mutateAsync({
+ projectId: "p",
+ data: { name: "QA" },
+ }),
+ );
+
+ const after = renderHook(() => useCollapsedSections("p", "list"));
+ expect(after.result.current.isExpanded("qa")).toBe(true);
+ expect(after.result.current.isExpanded("done")).toBe(false);
+});
diff --git a/apps/web/src/hooks/mutations/column/use-create-column.ts b/apps/web/src/hooks/mutations/column/use-create-column.ts
index 02e8217fb..bd706cdb9 100644
--- a/apps/web/src/hooks/mutations/column/use-create-column.ts
+++ b/apps/web/src/hooks/mutations/column/use-create-column.ts
@@ -1,5 +1,6 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import createColumn from "@/fetchers/column/create-column";
+import { forgetCollapsedColumn } from "@/lib/collapsed-sections";
export function useCreateColumn() {
const queryClient = useQueryClient();
@@ -12,7 +13,8 @@ export function useCreateColumn() {
projectId: string;
data: { name: string; icon?: string; color?: string; isFinal?: boolean };
}) => createColumn(projectId, data),
- onSuccess: async () => {
+ onSuccess: async (column) => {
+ forgetCollapsedColumn(column.projectId, column.slug);
await queryClient.invalidateQueries({ refetchType: "all" });
},
});
diff --git a/apps/web/src/hooks/use-collapsed-sections.ts b/apps/web/src/hooks/use-collapsed-sections.ts
index eff62db0c..2e485ace3 100644
--- a/apps/web/src/hooks/use-collapsed-sections.ts
+++ b/apps/web/src/hooks/use-collapsed-sections.ts
@@ -1,14 +1,11 @@
import { useCallback } from "react";
+import {
+ collapsedSectionsKey,
+ NONE_COLLAPSED,
+ normalizeCollapsed,
+} from "@/lib/collapsed-sections";
import { useStoredState } from "./use-stored-state";
-const NONE_COLLAPSED: string[] = [];
-
-function normalizeCollapsed(value: unknown): string[] {
- return Array.isArray(value)
- ? value.filter((id): id is string => typeof id === "string")
- : NONE_COLLAPSED;
-}
-
// Stores collapsed ids rather than expanded ones so sections the user has
// never touched, such as a newly added column, start expanded.
export function useCollapsedSections(
@@ -16,7 +13,7 @@ export function useCollapsedSections(
scope: "list" | "backlog",
) {
const [collapsed, setCollapsed] = useStoredState(
- projectId ? `kaneo:${scope}-collapsed:${projectId}` : null,
+ projectId ? collapsedSectionsKey(projectId, scope) : null,
normalizeCollapsed,
NONE_COLLAPSED,
);
diff --git a/apps/web/src/lib/collapsed-sections.ts b/apps/web/src/lib/collapsed-sections.ts
new file mode 100644
index 000000000..99aecb60b
--- /dev/null
+++ b/apps/web/src/lib/collapsed-sections.ts
@@ -0,0 +1,31 @@
+export const NONE_COLLAPSED: string[] = [];
+
+export function collapsedSectionsKey(
+ projectId: string,
+ scope: "list" | "backlog",
+) {
+ return `kaneo:${scope}-collapsed:${projectId}`;
+}
+
+export function normalizeCollapsed(value: unknown): string[] {
+ return Array.isArray(value)
+ ? value.filter((id): id is string => typeof id === "string")
+ : NONE_COLLAPSED;
+}
+
+// List sections are keyed by column slug, which a new column can reuse after
+// the old one was deleted or renamed. Forget it so the new column starts
+// expanded instead of inheriting the old column's state.
+export function forgetCollapsedColumn(projectId: string, slug: string) {
+ const key = collapsedSectionsKey(projectId, "list");
+ try {
+ const raw = window.localStorage.getItem(key);
+ if (raw === null) return;
+ const collapsed = normalizeCollapsed(JSON.parse(raw));
+ if (!collapsed.includes(slug)) return;
+ window.localStorage.setItem(
+ key,
+ JSON.stringify(collapsed.filter((id) => id !== slug)),
+ );
+ } catch {}
+}