-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTutorialWindow.tsx
More file actions
579 lines (523 loc) · 17.3 KB
/
TutorialWindow.tsx
File metadata and controls
579 lines (523 loc) · 17.3 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import { Tabs, TabsContent } from "@radix-ui/react-tabs";
import { useNavigate, useRouter, useSearch } from "@tanstack/react-router";
import { CheckIcon, DatabaseZapIcon, LinkIcon, XIcon } from "lucide-react";
import { AnimatePresence, motion } from "motion/react";
import {
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from "react";
import { toast } from "sonner";
import { steps as articles } from "@/data/tutorial";
import { useScrollShadow } from "@/hooks/use-scroll-shadow";
import { cn } from "@/lib/utils";
import {
getTutorialDataHandlers,
type TutorialData,
} from "@/utils/getTutorialDataHandlers";
import { ToggleFloatingWindowButton } from "../ToggleFloatingWindowButton";
import { Button } from "../ui/button";
import { ScrollArea } from "../ui/scroll-area";
import { ScrollShadow } from "../ui/scroll-shadow";
import { TutorialTableOfContents } from "./TutorialTableOfContents";
function CopyArticleLinkButton({ activeStep }: { activeStep: string | null }) {
const [copied, setCopied] = useState(false);
const copyLink = useCallback(() => {
if (!activeStep) return;
const url = new URL(window.location.href);
url.searchParams.set(
"article",
encodeURIComponent(activeStep.toLowerCase()),
);
navigator.clipboard
.writeText(url.toString())
.then(() => {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
})
.catch((_) => {
toast.error("Couldn't copy the url");
});
}, [activeStep]);
return (
<Button
variant="outline"
size="sm"
onClick={copyLink}
className="absolute top-2 right-4 z-20"
title="Copy link to this article"
>
<AnimatePresence mode="wait" initial={false}>
{copied ? (
<motion.div
key="check"
initial={{ scale: 0.5, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0.5, opacity: 0 }}
transition={{ duration: 0.15 }}
>
<CheckIcon className="h-4 w-4 text-green-600" />
</motion.div>
) : (
<motion.div
key="link"
initial={{ scale: 0.5, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0.5, opacity: 0 }}
transition={{ duration: 0.15 }}
>
<LinkIcon className="h-4 w-4" />
</motion.div>
)}
</AnimatePresence>
{copied ? "Copied!" : "Copy link"}
</Button>
);
}
function FloatingWindowHeader({ toggleWindow }: { toggleWindow: () => void }) {
return (
<div className="text-sm bg-linear-to-r bg-primary text-primary-foreground border-b border-border dark:border-b-primary px-2 py-1 flex items-center">
<div className="flex items-center gap-1 font-bold grow">
<DatabaseZapIcon className="h-4 w-4" /> TanStack DB Tutorial
</div>
<Button
variant="tutorial"
size="icon-xs"
className="bg-transparent border-black rounded-full border"
onClick={toggleWindow}
>
<XIcon className="" />
</Button>
</div>
);
}
function FloatingWindow({
tutorialData,
activeStep,
setActiveStep,
toggleWindow,
}: {
tutorialData: TutorialData;
activeStep: string | null;
setActiveStep: (step: string) => void;
toggleWindow: () => void;
}) {
const navigate = useNavigate();
const scrollRef = useRef<HTMLDivElement>(null);
const containerRef = useRef<HTMLDivElement>(null);
const { canScrollUp, canScrollDown } = useScrollShadow(scrollRef, [
activeStep,
]);
const [windowSize, setWindowSize] = useState(tutorialData.windowSize);
// Store initial limits as constants that never change
const INITIAL_MIN_WIDTH = 400;
const INITIAL_MAX_WIDTH = 850;
const INITIAL_MIN_HEIGHT = 300;
const INITIAL_MAX_HEIGHT = 2000;
const [
{ MIN_WIDTH, MAX_WIDTH, MIN_HEIGHT, MAX_HEIGHT },
setWindowSizeLimits,
] = useState({
MIN_WIDTH: INITIAL_MIN_WIDTH,
MAX_WIDTH: INITIAL_MAX_WIDTH,
MIN_HEIGHT: INITIAL_MIN_HEIGHT,
MAX_HEIGHT: INITIAL_MAX_HEIGHT,
});
const updateWindowSizeLimits = useCallback(() => {
if (typeof window !== "undefined") {
// Calculate new limits based on current window size
// MIN values: can't be larger than initial values, but can be smaller if window is small
// MAX values: can grow and shrink with window size
const newMinWidth = Math.min(window.innerWidth - 16, INITIAL_MIN_WIDTH);
const newMinHeight = Math.min(
window.innerHeight - 65,
INITIAL_MIN_HEIGHT,
);
const newLimits = {
MIN_WIDTH: newMinWidth,
// MAX_WIDTH should be at least MIN_WIDTH but never exceed what fits in viewport
MAX_WIDTH: Math.max(
newMinWidth,
Math.min(window.innerWidth * 0.9, INITIAL_MAX_WIDTH),
),
MIN_HEIGHT: newMinHeight,
// MAX_HEIGHT should be at least MIN_HEIGHT but never exceed what fits in viewport
MAX_HEIGHT: Math.max(
newMinHeight,
Math.min(window.innerHeight * 0.75, INITIAL_MAX_HEIGHT),
),
};
setWindowSizeLimits(newLimits);
// Clamp the current window size to fit within new limits
// Use a callback to get the current state value, not the saved tutorialData value
setWindowSize((currentSize) => ({
width: Math.max(
newLimits.MIN_WIDTH,
Math.min(newLimits.MAX_WIDTH, currentSize.width),
),
height: Math.max(
newLimits.MIN_HEIGHT,
Math.min(newLimits.MAX_HEIGHT, currentSize.height),
),
}));
}
}, []);
useEffect(() => {
// Make sure the window size is within limits
updateWindowSizeLimits();
if (typeof window !== "undefined") {
window.addEventListener("resize", updateWindowSizeLimits);
return () => {
window.removeEventListener("resize", updateWindowSizeLimits);
};
}
}, [
// Make sure the window size is within limits
updateWindowSizeLimits,
]);
// Resize state
const [isResizing, setIsResizing] = useState(false);
const [resizeDirection, setResizeDirection] = useState<
"top" | "right" | "corner" | null
>(null);
const resizeStart = useRef({ x: 0, y: 0, width: 0, height: 0 });
const saveTimeoutRef = useRef<number | undefined>(undefined);
// Debounced save to localStorage (only save after resize stops)
useEffect(() => {
if (isResizing) return; // Don't save while actively resizing
if (saveTimeoutRef.current) {
clearTimeout(saveTimeoutRef.current);
}
saveTimeoutRef.current = window.setTimeout(() => {
getTutorialDataHandlers().then(({ updateTutorialData }) => {
updateTutorialData({
windowSize,
});
});
}, 100);
return () => {
if (saveTimeoutRef.current) {
clearTimeout(saveTimeoutRef.current);
}
};
}, [windowSize, isResizing]);
const handleStepChange = useCallback(
async (stepTitle: string) => {
// clear all highlights
navigate({
to: ".",
search: ({ highlight: _, ...old }) => {
return { ...old };
},
});
const { tutorialData, updateTutorialData } =
await getTutorialDataHandlers();
tutorialData.tutorialStep = stepTitle;
updateTutorialData({
tutorialStep: stepTitle,
});
setActiveStep(stepTitle);
},
[
// clear all highlights
navigate,
setActiveStep,
],
);
// Restore scroll position on step change (useLayoutEffect to avoid flicker)
useLayoutEffect(() => {
if (activeStep) {
const saved = tutorialData.scrollPositions[activeStep];
if (saved && scrollRef.current) {
scrollRef.current.scrollTop = saved;
}
}
}, [activeStep, tutorialData.scrollPositions]);
// Save scroll position on scroll
const handleScroll = async () => {
const { updateTutorialData, tutorialData } =
await getTutorialDataHandlers();
// get current scroll positions
if (scrollRef.current && activeStep) {
updateTutorialData({
tutorialStep: activeStep,
scrollPositions: {
...tutorialData.scrollPositions,
[activeStep]: scrollRef.current.scrollTop,
},
});
}
};
// Resize handlers
const handleResizeStart = (
e: React.MouseEvent,
direction: "top" | "right" | "corner",
) => {
e.preventDefault();
e.stopPropagation();
setIsResizing(true);
setResizeDirection(direction);
resizeStart.current = {
x: e.clientX,
y: e.clientY,
width: windowSize.width,
height: windowSize.height,
};
};
useEffect(() => {
if (!isResizing || !containerRef.current) return;
const container = containerRef.current;
const handleMouseMove = (e: MouseEvent) => {
e.preventDefault();
const deltaX = e.clientX - resizeStart.current.x;
const deltaY = e.clientY - resizeStart.current.y;
let newWidth = resizeStart.current.width;
let newHeight = resizeStart.current.height;
if (resizeDirection === "right" || resizeDirection === "corner") {
newWidth = Math.max(
MIN_WIDTH,
Math.min(MAX_WIDTH, resizeStart.current.width + deltaX),
);
}
if (resizeDirection === "top" || resizeDirection === "corner") {
// For top resize, we subtract deltaY because dragging up should increase height
newHeight = Math.max(
MIN_HEIGHT,
Math.min(MAX_HEIGHT, resizeStart.current.height - deltaY),
);
}
// Direct DOM manipulation - no React re-render
container.style.width = `${newWidth}px`;
container.style.height = `${newHeight}px`;
};
const handleMouseUp = () => {
// Sync final size to React state
if (containerRef.current) {
const finalWidth = containerRef.current.offsetWidth;
const finalHeight = containerRef.current.offsetHeight;
setWindowSize({ width: finalWidth, height: finalHeight });
}
setIsResizing(false);
setResizeDirection(null);
};
document.addEventListener("mousemove", handleMouseMove, { passive: false });
document.addEventListener("mouseup", handleMouseUp);
return () => {
document.removeEventListener("mousemove", handleMouseMove);
document.removeEventListener("mouseup", handleMouseUp);
};
}, [
isResizing,
resizeDirection,
MAX_HEIGHT,
MAX_WIDTH,
MIN_HEIGHT,
MIN_WIDTH,
]);
return (
<div
ref={containerRef}
className={cn(
"mb-2 drop-shadow-md dark:drop-shadow-lg bg-card rounded-lg border border-border dark:border-primary/50 relative",
!isResizing && "transition-all",
isResizing && "select-none",
)}
style={{
width: `${windowSize.width}px`,
height: `${windowSize.height}px`,
willChange: isResizing ? "width, height" : "auto",
contain: isResizing ? "layout style paint" : "none",
}}
>
{/* Top resize handle - larger hit area with invisible padding */}
<div
className="absolute top-0 left-0 right-0 cursor-ns-resize z-50 group h-2 -mt-1"
onMouseDown={(e) => handleResizeStart(e, "top")}
>
<div className="absolute top-1/2 left-0 right-0 h-px -translate-y-1/2 group-hover:bg-orange-500/30 transition-colors" />
</div>
{/* Right resize handle - larger hit area with invisible padding */}
<div
className="absolute top-0 right-0 bottom-0 cursor-ew-resize z-50 group -mr-1 w-2"
onMouseDown={(e) => handleResizeStart(e, "right")}
>
<div className="absolute top-0 left-1/2 bottom-0 w-px -translate-x-1/2 group-hover:bg-orange-500/30 transition-colors" />
</div>
{/* Corner resize handle - larger hit area */}
<div
className="absolute top-0 right-0 cursor-nesw-resize z-50 group w-4 h-4 -mt-1 -mr-1"
onMouseDown={(e) => handleResizeStart(e, "corner")}
>
<div className="absolute top-1/2 left-1/2 w-1 h-1 -translate-x-1/2 -translate-y-1/2 group-hover:bg-orange-500/50 transition-colors rounded-full" />
</div>
<div
className={cn(
"opacity-100 translate-y-0 h-full flex flex-col overflow-hidden rounded-lg",
)}
>
<FloatingWindowHeader toggleWindow={toggleWindow} />
<Tabs
orientation="vertical"
value={activeStep || undefined}
onValueChange={handleStepChange}
className={cn(
"w-full flex-1 flex flex-row p-2 overflow-hidden",
isResizing && "pointer-events-none",
)}
>
<ScrollArea type="hover" className="h-full py-2">
<TutorialTableOfContents
activeStep={activeStep}
onStepChange={handleStepChange}
/>
</ScrollArea>
<div
className={cn(
"relative w-full h-full ml-4 min-w-0 overflow-hidden",
isResizing && "pointer-events-none",
)}
>
<CopyArticleLinkButton activeStep={activeStep} />
<ScrollShadow
position="top"
visible={canScrollUp}
fromColor="from-card"
/>
<div
ref={scrollRef}
key={activeStep}
onScroll={handleScroll}
className={cn(
"h-full overflow-y-auto overflow-x-hidden",
isResizing ? "contain-strict" : "contain-none",
)}
>
{articles.map((step) => (
<TabsContent
key={step.title}
value={step.title}
className="w-full overflow-x-hidden pb-3"
>
<div className="fade-in animate-in prose dark:prose-invert prose-md prose-neutral prose-base rounded-lg">
{<step.file />}
{step.nextStepName && (
<div className="mt-4">
<button
type="button"
onClick={() =>
step.nextStepName &&
handleStepChange(step.nextStepName)
}
className="text-primary underline hover:brightness-75 transition-colors cursor-pointer"
>
Next: {step.nextStepName}
</button>
</div>
)}
</div>
</TabsContent>
))}
</div>
<ScrollShadow
position="bottom"
visible={canScrollDown}
fromColor="from-card"
/>
</div>
</Tabs>
</div>
</div>
);
}
export function TutorialWindow({
tutorialData,
}: {
tutorialData: TutorialData;
}) {
const [isClosed, setIsClosed] = useState(tutorialData.isClosed);
const { article: activeArticleFromSearch } = useSearch({ strict: false });
const [activeStep, setActiveStep] = useState(
tutorialData.tutorialStep || articles[0].title,
);
useEffect(() => {
if (
activeArticleFromSearch &&
typeof activeArticleFromSearch === "string"
) {
const articleInSearch = decodeURIComponent(
activeArticleFromSearch.toLowerCase(),
);
if (articles.find((a) => a.title === articleInSearch)) {
setActiveStep(articleInSearch);
}
}
}, [activeArticleFromSearch]);
const router = useRouter();
const toggleWindow = useCallback(async () => {
setIsClosed((o) => !o);
const { updateTutorialData } = await getTutorialDataHandlers();
/*
We need to invalidate the route so that any components using
the `tutorialData` from the loader get the updated data
(important for the `tutorialData.windowSize`).
*/
router.invalidate({
filter: (route) => route.id === "/_tutorial",
});
updateTutorialData({
isClosed: !isClosed,
});
}, [isClosed, router]);
return (
<div className="w-0 h-0">
<div
className={
"absolute bottom-0 left-0 p-2 z-50 overflow-hidden max-w-full"
}
>
<AnimatePresence initial={false}>
{!isClosed && (
<motion.div
key="floating-window"
initial={{
opacity: 0,
translateY: 10,
filter: "blur(4px)", // add blur to initial
}}
animate={{ opacity: 1, translateY: 0, filter: "blur(0px)" }}
exit={{ opacity: 0, translateY: 10, filter: "blur(4px)" }} // add blur to exit
transition={{
translateY: {
type: "spring",
stiffness: 600,
},
opacity: {
duration: 0.2,
ease: "easeInOut",
},
filter: {
duration: 0.2,
ease: "easeInOut",
},
}}
>
<FloatingWindow
tutorialData={tutorialData}
activeStep={activeStep}
setActiveStep={setActiveStep}
toggleWindow={toggleWindow}
/>
</motion.div>
)}
</AnimatePresence>
<ToggleFloatingWindowButton
isClosed={isClosed}
activeStep={activeStep}
toggleWindow={toggleWindow}
/>
</div>
</div>
);
}