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

threadSelectionStore.ts

122 lines4.1 KB

1/**
2 * Zustand store for sidebar thread multi-selection state.
3 *
4 * Supports Cmd/Ctrl+Click (toggle individual), Shift+Click (range select),
5 * and bulk actions on the selected set.
6 */
7import { create } from "zustand";
8​
9export interface ThreadSelectionState {
10 /** Currently selected scoped thread keys. */
11 selectedThreadKeys: ReadonlySet<string>;
12 /** The scoped thread key that anchors shift-click range selection. */
13 anchorThreadKey: string | null;
15​
16interface ThreadSelectionStore extends ThreadSelectionState {
17 /** Toggle a single scoped thread key in the selection (Cmd/Ctrl+Click). */
18 toggleThread: (threadKey: string) => void;
19 /**
20 * Select a range of threads (Shift+Click).
21 * Requires the ordered list of scoped thread keys within the same project
22 * so the store can compute which threads fall between anchor and target.
23 */
24 rangeSelectTo: (threadKey: string, orderedThreadKeys: readonly string[]) => void;
25 /** Clear all selection state. */
26 clearSelection: () => void;
27 /** Remove specific scoped thread keys from the selection (e.g. after deletion). */
28 removeFromSelection: (threadKeys: readonly string[]) => void;
29 /** Set the anchor thread without adding it to the selection (e.g. on plain-click navigate). */
30 setAnchor: (threadKey: string) => void;
31 /** Check if any threads are selected. */
32 hasSelection: () => boolean;
34​
35const EMPTY_SET = new Set<string>();
36​
37export const useThreadSelectionStore = create<ThreadSelectionStore>((set, get) => ({
38 selectedThreadKeys: EMPTY_SET,
39 anchorThreadKey: null,
40​
41 toggleThread: (threadKey) => {
42 set((state) => {
43 const next = new Set(state.selectedThreadKeys);
44 if (next.has(threadKey)) {
45 next.delete(threadKey);
46 } else {
47 next.add(threadKey);
48 }
49 return {
50 selectedThreadKeys: next,
51 anchorThreadKey: next.has(threadKey) ? threadKey : state.anchorThreadKey,
52 };
53 });
54 },
55​
56 rangeSelectTo: (threadKey, orderedThreadKeys) => {
57 set((state) => {
58 const anchor = state.anchorThreadKey;
59 if (anchor === null) {
60 // No anchor yet — treat as a single toggle
61 const next = new Set(state.selectedThreadKeys);
62 next.add(threadKey);
63 return { selectedThreadKeys: next, anchorThreadKey: threadKey };
64 }
65​
66 const anchorIndex = orderedThreadKeys.indexOf(anchor);
67 const targetIndex = orderedThreadKeys.indexOf(threadKey);
68 if (anchorIndex === -1 || targetIndex === -1) {
69 // Anchor or target not in this list (different project?) — fallback to toggle
70 const next = new Set(state.selectedThreadKeys);
71 next.add(threadKey);
72 return { selectedThreadKeys: next, anchorThreadKey: threadKey };
73 }
74​
75 const start = Math.min(anchorIndex, targetIndex);
76 const end = Math.max(anchorIndex, targetIndex);
77 const next = new Set(state.selectedThreadKeys);
78 for (let i = start; i <= end; i++) {
79 const key = orderedThreadKeys[i];
80 if (key !== undefined) {
81 next.add(key);
82 }
83 }
84 // Keep anchor stable so subsequent shift-clicks extend from the same point
85 return { selectedThreadKeys: next, anchorThreadKey: anchor };
86 });
87 },
88​
89 clearSelection: () => {
90 const state = get();
91 if (state.selectedThreadKeys.size === 0 && state.anchorThreadKey === null) return;
92 set({ selectedThreadKeys: EMPTY_SET, anchorThreadKey: null });
93 },
94​
95 setAnchor: (threadKey) => {
96 if (get().anchorThreadKey === threadKey) return;
97 set({ anchorThreadKey: threadKey });
98 },
99​
100 removeFromSelection: (threadKeys) => {
101 set((state) => {
102 const toRemove = new Set(threadKeys);
103 let changed = false;
104 const next = new Set<string>();
105 for (const key of state.selectedThreadKeys) {
106 if (toRemove.has(key)) {
107 changed = true;
108 } else {
109 next.add(key);
110 }
111 }
112 if (!changed) return state;
113 const newAnchor =
114 state.anchorThreadKey !== null && toRemove.has(state.anchorThreadKey)
115 ? null
116 : state.anchorThreadKey;
117 return { selectedThreadKeys: next, anchorThreadKey: newAnchor };
118 });
119 },
120​
121 hasSelection: () => get().selectedThreadKeys.size > 0,
122}));