-
Notifications
You must be signed in to change notification settings - Fork 546
Expand file tree
/
Copy pathdevtool.tsx
More file actions
352 lines (325 loc) · 9.47 KB
/
devtool.tsx
File metadata and controls
352 lines (325 loc) · 9.47 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import { useCallback, useEffect, useState } from "react";
import { useStores } from "tinybase/ui-react";
import { commands as appleCalendarCommands } from "@hypr/plugin-apple-calendar";
import { commands as windowsCommands } from "@hypr/plugin-windows";
import { cn } from "@hypr/utils";
import {
type Store as MainStore,
STORE_ID as STORE_ID_PERSISTED,
} from "../../../store/tinybase/store/main";
import { useTabs } from "../../../store/zustand/tabs";
import { type SeedDefinition, seeds } from "../../devtool/seed/index";
import { useTrialBeginModal } from "../../devtool/trial-begin-modal";
import { useTrialExpiredModal } from "../../devtool/trial-expired-modal";
import { getLatestVersion } from "../body/changelog";
declare global {
interface Window {
__dev?: {
seed: (id?: string) => void;
seeds: Array<{ id: string; label: string }>;
};
}
}
export function DevtoolView() {
const stores = useStores();
const persistedStore = stores[STORE_ID_PERSISTED] as unknown as
| MainStore
| undefined;
const [fixtureKey, setFixtureKey] = useState(0);
useEffect(() => {
if (typeof window === "undefined") {
return;
}
if (!persistedStore) {
return;
}
const api = {
seed: (id?: string) => {
const target = id ? seeds.find((item) => item.id === id) : seeds[0];
if (target) {
target.run(persistedStore);
}
},
seeds: seeds.map(({ id, label }) => ({ id, label })),
};
window.__dev = api;
return () => {
if (window.__dev === api) {
delete window.__dev;
}
};
}, [persistedStore]);
const handleSeed = useCallback(
async (seed: SeedDefinition) => {
if (!persistedStore) {
return;
}
seed.run(persistedStore);
try {
await appleCalendarCommands.resetFixture();
setFixtureKey((k) => k + 1);
} catch {
// fixture feature not enabled
}
},
[persistedStore],
);
if (!persistedStore) {
return null;
}
return (
<div className="h-full flex flex-col overflow-hidden">
<div className="flex-1 overflow-y-auto px-1 py-2 flex flex-col gap-2">
<NavigationCard />
<SeedCard onSeed={handleSeed} />
<CalendarMockCard key={fixtureKey} />
<ModalsCard />
</div>
</div>
);
}
function DevtoolCard({
title,
children,
maxHeight,
}: {
title: string;
children: React.ReactNode;
maxHeight?: string;
}) {
return (
<div
className={cn([
"rounded-lg border border-neutral-200 bg-white",
"shadow-sm",
"overflow-hidden",
"shrink-0",
])}
>
<div className="px-2 py-1.5 border-b border-neutral-100 bg-neutral-50">
<h2 className="text-xs font-semibold text-neutral-600 uppercase tracking-wide">
{title}
</h2>
</div>
<div
className="p-2 overflow-y-auto"
style={maxHeight ? { maxHeight } : undefined}
>
{children}
</div>
</div>
);
}
interface FixtureInfo {
current_step: number;
max_steps: number;
step_name: string;
}
function CalendarMockCard() {
const [fixtureInfo, setFixtureInfo] = useState<FixtureInfo | null>(null);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
const loadFixtureInfo = async () => {
try {
const info = await appleCalendarCommands.getFixtureInfo();
setFixtureInfo(info);
} catch {
// fixture feature not enabled
}
};
loadFixtureInfo();
}, []);
const handleAdvance = useCallback(async () => {
setIsLoading(true);
try {
const info = await appleCalendarCommands.advanceFixture();
setFixtureInfo(info);
} catch {
// fixture feature not enabled
} finally {
setIsLoading(false);
}
}, []);
if (fixtureInfo === null) {
return null;
}
const isAtEnd = fixtureInfo.current_step >= fixtureInfo.max_steps - 1;
return (
<DevtoolCard title="Calendar Mock">
<div className="flex flex-col gap-2">
<div className="flex items-center justify-between px-1">
<span className="text-xs text-neutral-500">
Step {fixtureInfo.current_step + 1} of {fixtureInfo.max_steps}
</span>
<span className="text-xs font-medium text-neutral-700">
{fixtureInfo.step_name}
</span>
</div>
<button
type="button"
onClick={handleAdvance}
disabled={isLoading || isAtEnd}
className={cn([
"w-full px-2 py-1.5 rounded-md",
"text-xs font-medium",
"border transition-colors",
isAtEnd
? ["border-neutral-100 text-neutral-300", "cursor-default"]
: [
"border-blue-200 bg-blue-50 text-blue-700",
"hover:bg-blue-100 hover:border-blue-300",
"cursor-pointer",
],
isLoading && "opacity-50 cursor-wait",
])}
>
Advance
</button>
</div>
</DevtoolCard>
);
}
function SeedCard({ onSeed }: { onSeed: (seed: SeedDefinition) => void }) {
return (
<DevtoolCard title="Seeds" maxHeight="200px">
<div className="flex flex-col gap-1.5">
{seeds.map((seed) => (
<button
key={seed.id}
type="button"
onClick={() => onSeed(seed)}
className={cn([
"w-full px-2 py-1.5 rounded-md",
"text-xs font-medium text-left",
"border border-neutral-200 text-neutral-700",
"cursor-pointer transition-colors",
"hover:bg-neutral-50 hover:border-neutral-300",
])}
>
{seed.label}
</button>
))}
</div>
</DevtoolCard>
);
}
function NavigationCard() {
const openNew = useTabs((s) => s.openNew);
const handleShowMain = useCallback(() => {
void windowsCommands.windowShow({ type: "main" });
}, []);
const handleShowOnboarding = useCallback(() => {
void windowsCommands.windowShow({ type: "onboarding" }).then(() => {
void windowsCommands.windowEmitNavigate(
{ type: "onboarding" },
{ path: "/app/onboarding", search: {} },
);
});
}, []);
const handleShowControl = useCallback(() => {
void windowsCommands.windowShow({ type: "control" });
}, []);
const handleShowChangelog = useCallback(() => {
const latestVersion = getLatestVersion();
if (latestVersion) {
openNew({
type: "changelog",
state: { current: latestVersion, previous: null },
});
}
}, [openNew]);
return (
<DevtoolCard title="Navigation">
<div className="flex flex-col gap-1.5">
<button
type="button"
onClick={handleShowOnboarding}
className={cn([
"w-full px-2.5 py-1.5 rounded-md",
"text-xs font-medium text-left",
"border border-neutral-200 text-neutral-700",
"cursor-pointer transition-colors",
"hover:bg-neutral-50 hover:border-neutral-300",
])}
>
Onboarding
</button>
<button
type="button"
onClick={handleShowMain}
className={cn([
"w-full px-2.5 py-1.5 rounded-md",
"text-xs font-medium text-left",
"border border-neutral-200 text-neutral-700",
"cursor-pointer transition-colors",
"hover:bg-neutral-50 hover:border-neutral-300",
])}
>
Main
</button>
<button
type="button"
onClick={handleShowControl}
className={cn([
"w-full px-2.5 py-1.5 rounded-md",
"text-xs font-medium text-left",
"border border-neutral-200 text-neutral-700",
"cursor-pointer transition-colors",
"hover:bg-neutral-50 hover:border-neutral-300",
])}
>
Control
</button>
<button
type="button"
onClick={handleShowChangelog}
className={cn([
"w-full px-2.5 py-1.5 rounded-md",
"text-xs font-medium text-left",
"border border-neutral-200 text-neutral-700",
"cursor-pointer transition-colors",
"hover:bg-neutral-50 hover:border-neutral-300",
])}
>
Changelog
</button>
</div>
</DevtoolCard>
);
}
function ModalsCard() {
const { open: openTrialBeginModal } = useTrialBeginModal();
const { open: openTrialExpiredModal } = useTrialExpiredModal();
return (
<DevtoolCard title="Modals">
<div className="flex flex-col gap-1.5">
<button
type="button"
onClick={openTrialBeginModal}
className={cn([
"w-full px-2.5 py-1.5 rounded-md",
"text-xs font-medium text-left",
"border border-neutral-200 text-neutral-700",
"cursor-pointer transition-colors",
"hover:bg-neutral-50 hover:border-neutral-300",
])}
>
Trial Begin
</button>
<button
type="button"
onClick={openTrialExpiredModal}
className={cn([
"w-full px-2.5 py-1.5 rounded-md",
"text-xs font-medium text-left",
"border border-neutral-200 text-neutral-700",
"cursor-pointer transition-colors",
"hover:bg-neutral-50 hover:border-neutral-300",
])}
>
Trial Expired
</button>
</div>
</DevtoolCard>
);
}