The best way to control Claude Code, Codex and more from any computer or phone

main
ts 104 lines 3.81 KB
Raw
1 import * as Schema from "effect/Schema";
2
3 import {
4 EnvironmentId,
5 ISSUE_LIST_MAX_EXCLUDED_PROJECTS,
6 IssueInvolvement,
7 IssueListState,
8 ProjectId,
9 } from "@t3tools/contracts";
10
11 /**
12 * What the issues page remembers between visits; the selection in the URL is not part of it.
13 * `who` and `status` rather than `involvement` and `state`: the router merges every route's
14 * search into one type for `navigate`, and sharing the pull-request page's key names would widen
15 * its own literal unions with this page's values.
16 */
17 export interface IssueListPreferences {
18 readonly who: IssueInvolvement;
19 readonly status: IssueListState;
20 /** Projects left out of the list, so a project added later shows. */
21 readonly hideProjects?: ReadonlyArray<readonly [EnvironmentId, ProjectId]> | undefined;
22 readonly host?: string | undefined;
23 readonly q?: string | undefined;
24 /** `label`, `assignee` and `order` for the same reason: the pull-request page has `labels` and `sort`. */
25 readonly label?: ReadonlyArray<string> | undefined;
26 readonly assignee?: string | undefined;
27 readonly order?: "created" | undefined;
28 }
29
30 const DEFAULT_ISSUE_LIST_PREFERENCES = {
31 who: "all",
32 status: "open",
33 } as const satisfies IssueListPreferences;
34
35 const STORAGE_KEY = "t3.issues.preferences";
36
37 const BoundedPreference = Schema.String.check(Schema.isMaxLength(200));
38 export const IssueListPreferencesSchema = Schema.Struct({
39 who: IssueInvolvement,
40 status: IssueListState,
41 hideProjects: Schema.optional(
42 Schema.Array(Schema.Tuple([EnvironmentId, ProjectId])).check(
43 Schema.isMaxLength(ISSUE_LIST_MAX_EXCLUDED_PROJECTS),
44 ),
45 ),
46 host: Schema.optional(BoundedPreference),
47 q: Schema.optional(BoundedPreference),
48 label: Schema.optional(Schema.Array(BoundedPreference).check(Schema.isMaxLength(20))),
49 assignee: Schema.optional(BoundedPreference),
50 order: Schema.optional(Schema.Literal("created")),
51 });
52 const decodePreferences = Schema.decodeUnknownOption(IssueListPreferencesSchema);
53
54 type PreferenceStorage = Pick<Storage, "getItem" | "setItem">;
55
56 function storage(): PreferenceStorage | undefined {
57 return typeof window === "undefined" ? undefined : window.localStorage;
58 }
59
60 /** Only list controls are remembered. The selected issue remains a URL and right-panel concern. */
61 export function issueListPreferences(search: {
62 readonly who: IssueInvolvement;
63 readonly status: IssueListState;
64 readonly hideProjects?: ReadonlyArray<readonly [EnvironmentId, ProjectId]> | undefined;
65 readonly host?: string | undefined;
66 readonly q?: string | undefined;
67 readonly label?: ReadonlyArray<string> | undefined;
68 readonly assignee?: string | undefined;
69 readonly order?: "created" | undefined;
70 }): IssueListPreferences {
71 return {
72 who: search.who,
73 status: search.status,
74 ...(search.hideProjects?.length
75 ? { hideProjects: search.hideProjects.slice(0, ISSUE_LIST_MAX_EXCLUDED_PROJECTS) }
76 : {}),
77 ...(search.host ? { host: search.host } : {}),
78 ...(search.q ? { q: search.q } : {}),
79 ...(search.label?.length ? { label: search.label } : {}),
80 ...(search.assignee ? { assignee: search.assignee } : {}),
81 ...(search.order ? { order: search.order } : {}),
82 };
83 }
84
85 export function readIssueListPreferences(): IssueListPreferences {
86 try {
87 const raw = storage()?.getItem(STORAGE_KEY);
88 if (!raw) return DEFAULT_ISSUE_LIST_PREFERENCES;
89 const decoded = decodePreferences(JSON.parse(raw));
90 return decoded._tag === "Some"
91 ? issueListPreferences(decoded.value)
92 : DEFAULT_ISSUE_LIST_PREFERENCES;
93 } catch {
94 return DEFAULT_ISSUE_LIST_PREFERENCES;
95 }
96 }
97
98 export function writeIssueListPreferences(preferences: IssueListPreferences): void {
99 try {
100 storage()?.setItem(STORAGE_KEY, JSON.stringify(issueListPreferences(preferences)));
101 } catch {
102 // Storage can be full or forbidden; the page still works with defaults.
103 }
104 }