Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadSelectionStore.ts

112 lines3.7 KB

1import { create } from "zustand";
2​
3export interface ThreadSelectionState {
4 selectedThreadKeys: ReadonlySet<string>;
5 anchorThreadKey: string | null;
6}
7​
8interface ThreadSelectionStore extends ThreadSelectionState {
9 /** The open thread looks selected, so a new selection starts from it. */
10 toggleThread: (threadKey: string, openThreadKey?: string | null) => void;
11 /** Requires ordered scoped thread keys from the same project. */
12 rangeSelectTo: (
13 threadKey: string,
14 orderedThreadKeys: readonly string[],
15 openThreadKey?: string | null,
16 ) => void;
17 clearSelection: () => void;
18 removeFromSelection: (threadKeys: readonly string[]) => void;
19 hasSelection: () => boolean;
21​
22const EMPTY_SET = new Set<string>();
23​
24export function getThreadKeysToDeselectAfterDelete(
25 selectedThreadKeys: readonly string[],
26 deletedThreadKeys: ReadonlySet<string>,
27 hasThread: (threadKey: string) => boolean,
28): string[] {
29 return selectedThreadKeys.filter((key) => deletedThreadKeys.has(key) || !hasThread(key));
31​
32export const useThreadSelectionStore = create<ThreadSelectionStore>((set, get) => ({
33 selectedThreadKeys: EMPTY_SET,
34 anchorThreadKey: null,
35​
36 toggleThread: (threadKey, openThreadKey = null) => {
37 set((state) => {
38 const next = new Set(state.selectedThreadKeys);
39 if (next.size === 0 && openThreadKey !== null && openThreadKey !== threadKey) {
40 next.add(openThreadKey);
41 next.add(threadKey);
42 } else if (next.has(threadKey)) {
43 next.delete(threadKey);
44 } else {
45 next.add(threadKey);
46 }
47 return {
48 selectedThreadKeys: next,
49 anchorThreadKey: next.has(threadKey) ? threadKey : state.anchorThreadKey,
50 };
51 });
52 },
53​
54 rangeSelectTo: (threadKey, orderedThreadKeys, openThreadKey = null) => {
55 set((state) => {
56 const anchor = state.selectedThreadKeys.size === 0 ? openThreadKey : state.anchorThreadKey;
57 if (anchor === null) {
58 const next = new Set(state.selectedThreadKeys);
59 next.add(threadKey);
60 return { selectedThreadKeys: next, anchorThreadKey: threadKey };
61 }
62​
63 const anchorIndex = orderedThreadKeys.indexOf(anchor);
64 const targetIndex = orderedThreadKeys.indexOf(threadKey);
65 if (anchorIndex === -1 || targetIndex === -1) {
66 const next = new Set(state.selectedThreadKeys);
67 next.add(threadKey);
68 return { selectedThreadKeys: next, anchorThreadKey: threadKey };
69 }
70​
71 const start = Math.min(anchorIndex, targetIndex);
72 const end = Math.max(anchorIndex, targetIndex);
73 const next = new Set(state.selectedThreadKeys);
74 for (let i = start; i <= end; i++) {
75 const key = orderedThreadKeys[i];
76 if (key !== undefined) {
77 next.add(key);
78 }
79 }
80 return { selectedThreadKeys: next, anchorThreadKey: anchor };
81 });
82 },
83​
84 clearSelection: () => {
85 const state = get();
86 if (state.selectedThreadKeys.size === 0 && state.anchorThreadKey === null) return;
87 set({ selectedThreadKeys: EMPTY_SET, anchorThreadKey: null });
88 },
89​
90 removeFromSelection: (threadKeys) => {
91 set((state) => {
92 const toRemove = new Set(threadKeys);
93 let changed = false;
94 const next = new Set<string>();
95 for (const key of state.selectedThreadKeys) {
96 if (toRemove.has(key)) {
97 changed = true;
98 } else {
99 next.add(key);
100 }
101 }
102 if (!changed) return state;
103 const newAnchor =
104 state.anchorThreadKey !== null && toRemove.has(state.anchorThreadKey)
105 ? null
106 : state.anchorThreadKey;
107 return { selectedThreadKeys: next, anchorThreadKey: newAnchor };
108 });
109 },
110​
111 hasSelection: () => get().selectedThreadKeys.size > 0,
112}));