Open History to see the last commit
1import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2import {
3 ChatFileAttachment,
4 INCOGNITO_BROWSER_PROFILE_ID,
5 PreviewSessionSnapshot,
6 ScopedThreadRef,
7} from "@t3tools/contracts";
8import * as Schema from "effect/Schema";
9import { create } from "zustand";
10import { createJSONStorage, persist } from "zustand/middleware";
11
12import { resolveStorage } from "./lib/storage";
13import { randomUUID } from "./lib/utils";
14import { type RightPanelSurface } from "./rightPanelStore";
15
16export type ClosedView =
17 | {
18 kind: "panel-tab";
19 threadRef: ScopedThreadRef;
20 surface: Exclude<RightPanelSurface, { kind: "terminal" }>;
21 }
22 | { kind: "browser"; threadRef: ScopedThreadRef; snapshot: PreviewSessionSnapshot };
23
24export type ClosedViewEntry = ClosedView & { id: string };
25
26interface ClosedViewStoreState {
27 entries: ClosedViewEntry[];
28 remember: (view: ClosedView) => string;
29 defer: (id: string) => void;
30 remove: (id: string) => void;
31}
32
33const sameTarget = (entry: ClosedViewEntry, view: ClosedView): boolean => {
34 if (
35 entry.kind !== view.kind ||
36 scopedThreadKey(entry.threadRef) !== scopedThreadKey(view.threadRef)
37 ) {
38 return false;
39 }
40 switch (entry.kind) {
41 case "panel-tab":
42 return view.kind === "panel-tab" && entry.surface.id === view.surface.id;
43 case "browser":
44 return view.kind === "browser" && entry.snapshot.tabId === view.snapshot.tabId;
45 }
46};
47
48const isPersistentView = (entry: ClosedViewEntry) =>
49 entry.kind !== "browser" || entry.snapshot.profileId !== INCOGNITO_BROWSER_PROFILE_ID;
50
51const isThreadRef = Schema.is(ScopedThreadRef);
52const isSnapshot = Schema.is(PreviewSessionSnapshot);
53const isAttachment = Schema.is(ChatFileAttachment);
54
55const isClosedViewEntry = (entry: unknown): entry is ClosedViewEntry => {
56 const view = entry as ClosedViewEntry | null;
57 if (!view || typeof view.id !== "string" || !isThreadRef(view.threadRef)) return false;
58 if (view.kind === "browser") return isSnapshot(view.snapshot);
59 if (view.kind !== "panel-tab") return false;
60 const surface = view.surface;
61 if (!surface || typeof surface.id !== "string") return false;
62 switch (surface.kind) {
63 case "diff":
64 case "files":
65 case "pull-requests":
66 return surface.id === surface.kind;
67 case "preview":
68 return surface.resourceId === null || typeof surface.resourceId === "string";
69 case "file":
70 return (
71 typeof surface.relativePath === "string" &&
72 (surface.revealLine === null || Number.isSafeInteger(surface.revealLine)) &&
73 Number.isSafeInteger(surface.revealRequestId) &&
74 (surface.attachment === undefined || isAttachment(surface.attachment))
75 );
76 case "device":
77 return (
78 (surface.title === undefined || typeof surface.title === "string") &&
79 (surface.target === undefined ||
80 (surface.target !== null &&
81 typeof surface.target.hostId === "string" &&
82 typeof surface.target.deviceId === "string" &&
83 typeof surface.target.name === "string" &&
84 (surface.target.platform === "ios" || surface.target.platform === "android")))
85 );
86 case "pull-request":
87 return (
88 typeof surface.projectId === "string" &&
89 typeof surface.repository === "string" &&
90 Number.isSafeInteger(surface.number) &&
91 surface.number > 0 &&
92 (surface.environmentId === undefined || typeof surface.environmentId === "string") &&
93 (surface.host === undefined || typeof surface.host === "string") &&
94 (surface.url === undefined || typeof surface.url === "string")
95 );
96 default:
97 return false;
98 }
99};
100
101export const useClosedViewStore = create<ClosedViewStoreState>()(
102 persist(
103 (set) => ({
104 entries: [],
105 remember: (view) => {
106 const id = randomUUID();
107 set((state) => ({
108 entries: [
109 { ...view, id },
110 ...state.entries.filter((entry) => !sameTarget(entry, view)),
111 ].slice(0, 20),
112 }));
113 return id;
114 },
115 defer: (id) =>
116 set((state) => {
117 const entry = state.entries.find((entry) => entry.id === id);
118 return entry
119 ? { entries: [...state.entries.filter((entry) => entry.id !== id), entry] }
120 : state;
121 }),
122 remove: (id) =>
123 set((state) => ({ entries: state.entries.filter((entry) => entry.id !== id) })),
124 }),
125 {
126 name: "t3code:closed-views:v2",
127 storage: createJSONStorage(() =>
128 resolveStorage(typeof window !== "undefined" ? window.localStorage : undefined),
129 ),
130 version: 2,
131 migrate: (persisted) => {
132 const entries = (persisted as Partial<Pick<ClosedViewStoreState, "entries">> | null)
133 ?.entries;
134 return {
135 entries: Array.isArray(entries)
136 ? entries.filter(isClosedViewEntry).filter(isPersistentView)
137 : [],
138 };
139 },
140 partialize: ({ entries }) => ({ entries: entries.filter(isPersistentView) }),
141 },
142 ),
143);