-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.ts
299 lines (254 loc) · 6.7 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { JSXInternal } from "preact/src/jsx";
import { effect, Signal, signal } from "@preact/signals";
import { invoke } from "@tauri-apps/api/tauri";
import { listen } from "@tauri-apps/api/event";
const Scru128IdBrand = Symbol("Scru128Id");
export type Scru128Id = string & { readonly brand: typeof Scru128IdBrand };
const SSRIBrand = Symbol("SSRI");
export type SSRI = string & { readonly brand: typeof SSRIBrand };
export interface Content {
mime_type: string;
content_type: string;
terse: string;
tiktokens: number;
words: number;
chars: number;
preview: string;
}
export interface Cacheable {
id: Scru128Id;
hash: SSRI;
ephemeral: boolean;
}
export function getContent(item: Cacheable): Signal<Content | null> {
if (item.ephemeral) {
return ContentCache.byId(item.id);
}
ContentCache.clearId(item.id);
return ContentCache.byHash(item.hash);
}
export const ContentCache = (() => {
const hashCache: Map<SSRI, Signal<Content | null>> = new Map();
const idCache: Map<Scru128Id, Signal<Content | null>> = new Map();
function byHash(hash: SSRI): Signal<Content | null> {
let ret = hashCache.get(hash);
if (!ret) {
ret = new Signal(null);
hashCache.set(hash, ret);
(async () => {
ret.value = await invoke("store_get_content", {
hash: hash,
});
})();
}
return ret;
}
function byId(id: Scru128Id): Signal<Content | null> {
let ret = idCache.get(id);
if (!ret) {
ret = new Signal(null);
idCache.set(id, ret);
}
return ret;
}
function clearId(id: Scru128Id) {
idCache.delete(id);
}
async function initListener() {
const d1 = await listen(
"streaming",
(event: { payload: [Scru128Id, Content] }) => {
const [id, content] = event.payload;
console.log("streaming", id, content);
const cache = byId(id);
cache.value = content;
},
);
const d2 = await listen(
"content",
(event: { payload: SSRI }) => {
const hash = event.payload;
console.log("content", hash);
let ret = hashCache.get(hash);
if (ret) {
(async () => {
ret.value = await invoke("store_get_content", { hash: hash });
})();
}
},
);
if (import.meta.hot) {
import.meta.hot.dispose(() => {
if (d1) d1();
if (d2) d2();
});
}
}
initListener();
return {
byHash,
byId,
clearId,
};
})();
export interface Item {
id: Scru128Id;
stack_id?: Scru128Id;
name: string;
last_touched: Scru128Id;
touched: Scru128Id[];
hash: SSRI;
ephemeral: boolean;
locked: boolean;
ordered: boolean;
cross_stream: boolean;
}
export interface Layer {
items: Item[];
selected: Item;
is_focus: boolean;
}
export interface Nav {
root?: Layer;
sub?: Layer;
undo?: Item;
}
const createFilter = () => {
const curr = signal("");
const content_type = signal("All");
return {
curr,
content_type,
dirty: () => curr.value != "" || content_type.value != "All",
clear: () => {
curr.value = "";
content_type.value = "All";
},
};
};
export class Stack {
filter: {
curr: Signal<string>;
content_type: Signal<string>;
dirty: () => boolean;
clear: () => void;
};
nav: Signal<Nav>;
constructor(nav: Nav) {
this.filter = createFilter();
this.nav = signal(nav);
this.initListener();
effect(() => {
this.set_filter(this.filter.curr.value, this.filter.content_type.value);
});
}
async initListener() {
const d1 = await listen("refresh-items", () => {
this.refresh();
});
if (import.meta.hot) {
import.meta.hot.dispose(() => {
if (d1) d1();
});
}
}
// toggles the current stack's lock status
toggleLock() {
const curr = this.nav.value.root?.selected;
if (!curr) return;
const command = curr.locked ? "store_stack_unlock" : "store_stack_lock";
invoke(command, { sourceId: curr.id });
}
// is the current stack locked?
isLocked(): boolean {
return !!this.nav.value.root?.selected.locked;
}
// returns the item which is currently focused
selected(): Item | undefined {
const nav = this.nav.value;
if (nav.sub && nav.sub.is_focus) return nav.sub.selected;
return nav.root?.selected;
}
// returns the currently selected stack item: which may not be the current
// focus
selected_stack(): Item | undefined {
return this.nav.value.root?.selected;
}
// returns the currently selected leaf item: which may not be the current
// focus
selected_item(): Item | undefined {
return this.nav.value.sub?.selected;
}
async getRoot(): Promise<Item[]> {
return await invoke<Item[]>("store_get_root", {});
}
async undo() {
await invoke<Item[]>("store_undo", {});
}
async touch() {
const item = this.selected();
if (!item) return;
await invoke<Item[]>("store_touch", {
sourceId: item.id,
});
}
async refresh() {
this.nav.value = await invoke<Nav>("store_nav_refresh", {});
}
async reset() {
this.nav.value = await invoke<Nav>("store_nav_reset", {});
this.filter.clear();
}
async triggerCopy() {
const item = this.selected_item();
if (!item) return;
await invoke("store_copy_to_clipboard", {
sourceId: item.id,
});
await invoke("spotlight_hide");
}
async select(id: string) {
this.nav.value = await invoke<Nav>("store_nav_select", { focusedId: id });
}
async set_filter(filter: string, contentType: string) {
this.nav.value = await invoke<Nav>("store_nav_set_filter", {
filter: filter,
contentType: contentType,
});
}
async selectUp() {
this.nav.value = await invoke<Nav>("store_nav_select_up", {});
}
async selectDown() {
this.nav.value = await invoke<Nav>("store_nav_select_down", {});
}
async selectDownStack() {
this.nav.value = await invoke<Nav>("store_nav_select_down_stack", {});
}
async selectUpStack() {
this.nav.value = await invoke<Nav>("store_nav_select_up_stack", {});
}
async selectRight() {
this.nav.value = await invoke<Nav>("store_nav_select_right", {});
}
async selectLeft() {
this.nav.value = await invoke<Nav>("store_nav_select_left", {});
}
async moveUp() {
const item = this.selected();
if (!item) return;
await invoke("store_move_up", { sourceId: item.id });
}
async moveDown() {
const item = this.selected();
if (!item) return;
await invoke("store_move_down", { sourceId: item.id });
}
}
export interface Action {
name: string;
keys?: (string | JSXInternal.Element)[];
trigger?: (stack: Stack) => void;
canApply?: (stack: Stack) => boolean;
matchKeyEvent?: (event: KeyboardEvent) => boolean;
}