Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions apps/dashboard/src/lib/tab-store.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// @vitest-environment jsdom

import { afterEach, describe, expect, it } from "vitest";
import { readStoredTabs, TABS_STORAGE_KEY, type Tab } from "./tab-store";

describe("readStoredTabs", () => {
afterEach(() => {
localStorage.clear();
});

it("keeps persisted repo tabs", () => {
const storedTabs: Tab[] = [
{
id: "repo:diffkit/app",
type: "repo",
title: "diffkit/app",
url: "/diffkit/app",
repo: "diffkit/app",
iconColor: "text-muted-foreground",
avatarUrl: "https://example.com/avatar.png",
},
];

localStorage.setItem(TABS_STORAGE_KEY, JSON.stringify(storedTabs));

expect(readStoredTabs()).toEqual(storedTabs);
});

it("clears storage when a tab type is unsupported", () => {
localStorage.setItem(
TABS_STORAGE_KEY,
JSON.stringify([
{
id: "project:diffkit/app",
type: "project",
},
]),
);

expect(readStoredTabs()).toEqual([]);
expect(localStorage.getItem(TABS_STORAGE_KEY)).toBeNull();
});
});
13 changes: 11 additions & 2 deletions apps/dashboard/src/lib/tab-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ export interface Tab {

export const TABS_STORAGE_KEY = "diffkit:tabs";

const VALID_TAB_TYPES = new Set<string>(["pull", "issue", "review"]);
const VALID_TAB_TYPES = {
pull: true,
issue: true,
review: true,
repo: true,
} satisfies Record<TabType, true>;

function isValidTabType(type: unknown): type is TabType {
return typeof type === "string" && type in VALID_TAB_TYPES;
}

function isValidTab(t: unknown): t is Tab {
return (
t !== null &&
typeof t === "object" &&
typeof (t as Tab).id === "string" &&
VALID_TAB_TYPES.has((t as Tab).type)
isValidTabType((t as Tab).type)
);
}

Expand Down
Loading