Open History to see the last commit
1import { createIsomorphicFn } from "@tanstack/react-start";
2import { type ZodDefault, type ZodNumber, z } from "zod";
3import { steps } from "@/data/tutorial";
4import { getTutorialDataFromCookie } from "@/server/functions/getTutorialDataFromCookie";
5
6export type TutorialData = {
7 isClosed: boolean;
8 tutorialStep: string | null;
9 scrollPositions: Record<string, number>;
10 windowSize: {
11 width: number;
12 height: number;
13 };
14};
15
16type TutorialDataHandlers = {
17 tutorialData: TutorialData;
18 updateTutorialData: (data: Partial<TutorialData>) => Promise<void>;
19};
20
21const TUTORIAL_DATA_LOCAL_STORAGE_KEY = "tutorialData";
22export const TUTORIAL_COOKIE_NAME = "tutorialCookie";
23
24function parseTutorialDataString(
25 tutorialData: string | null | undefined,
26): TutorialData {
27 let jsonObj = {};
28 if (tutorialData) {
29 try {
30 // TODO: use a zod schema to validate this data
31 jsonObj = JSON.parse(tutorialData);
32 } catch (e) {
33 console.error("Error parsing tutorial data from cookie:", e);
34 jsonObj = {};
35 }
36 }
37
38 const data = tutorialDataSchema.parse(jsonObj);
39 return data;
40}
41
42const initialWindowSize = { width: 850, height: 500 };
43
44const tutorialDataSchema = z
45 .object({
46 isClosed: z.boolean().default(false),
47 tutorialStep: z.string().default(steps[0].title),
48 windowSize: z
49 .object({
50 width: z.number().default(0),
51 height: z.number().default(0),
52 })
53 .default(initialWindowSize),
54 scrollPositions: z
55 .object(
56 steps.reduce(
57 (acc, step) => {
58 acc[step.title] = z.number().default(0);
59 return acc;
60 },
61 {} as Record<string, ZodDefault<ZodNumber>>,
62 ),
63 )
64 .catch({}),
65 })
66 .catch({
67 isClosed: false,
68 tutorialStep: steps[0].title,
69 scrollPositions: {},
70 windowSize: initialWindowSize,
71 });
72
73export const getTutorialDataHandlers = createIsomorphicFn()
74 .server(async (): Promise<TutorialDataHandlers> => {
75 // get data from cookie
76 const tutorialDataString = await getTutorialDataFromCookie();
77 return {
78 updateTutorialData: async () => {
79 /**
80 Currently, there is no data in TutorialData that
81 needs to be updated on the server side.
82 */
83 throw new Error("Not implemented on server");
84 },
85 tutorialData: parseTutorialDataString(tutorialDataString),
86 };
87 })
88 .client(async (): Promise<TutorialDataHandlers> => {
89 // get data from localStorage, write to cookie via server fn
90
91 const tutorialInLocalStorage = localStorage.getItem(
92 TUTORIAL_DATA_LOCAL_STORAGE_KEY,
93 );
94
95 return {
96 tutorialData: parseTutorialDataString(tutorialInLocalStorage),
97 updateTutorialData: async (data: Partial<TutorialData>) => {
98 const existingDataString = localStorage.getItem(
99 TUTORIAL_DATA_LOCAL_STORAGE_KEY,
100 );
101 const existingData = parseTutorialDataString(existingDataString);
102 const updatedDataString = JSON.stringify({
103 ...existingData,
104 ...data,
105 });
106
107 localStorage.setItem(
108 TUTORIAL_DATA_LOCAL_STORAGE_KEY,
109 updatedDataString,
110 );
111
112 window.document.cookie = `${TUTORIAL_COOKIE_NAME}=${updatedDataString}; path=/;`;
113 },
114 };
115 });