Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

getTutorialDataHandlers.ts

114 lines3.2 KB

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 jsonObj = JSON.parse(tutorialData);
31 } catch (e) {
32 console.error("Error parsing tutorial data from cookie:", e);
33 jsonObj = {};
34 }
35 }
36​
37 const data = tutorialDataSchema.parse(jsonObj);
38 return data;
40​
41const initialWindowSize = { width: 850, height: 500 };
42​
43const tutorialDataSchema = z
44 .object({
45 isClosed: z.boolean().default(false),
46 tutorialStep: z.string().default(steps[0].title),
47 windowSize: z
48 .object({
49 width: z.number().default(0),
50 height: z.number().default(0),
51 })
52 .default(initialWindowSize),
53 scrollPositions: z
54 .object(
55 steps.reduce(
56 (acc, step) => {
57 acc[step.title] = z.number().default(0);
58 return acc;
59 },
60 {} as Record<string, ZodDefault<ZodNumber>>,
61 ),
62 )
63 .catch({}),
64 })
65 .catch({
66 isClosed: false,
67 tutorialStep: steps[0].title,
68 scrollPositions: {},
69 windowSize: initialWindowSize,
70 });
71​
72export const getTutorialDataHandlers = createIsomorphicFn()
73 .server(async (): Promise<TutorialDataHandlers> => {
74 // get data from cookie
75 const tutorialDataString = await getTutorialDataFromCookie();
76 return {
77 updateTutorialData: async () => {
78 /**
79 Currently, there is no data in TutorialData that
80 needs to be updated on the server side.
81 */
82 throw new Error("Not implemented on server");
83 },
84 tutorialData: parseTutorialDataString(tutorialDataString),
85 };
86 })
87 .client(async (): Promise<TutorialDataHandlers> => {
88 // get data from localStorage, write to cookie via server fn
89​
90 const tutorialInLocalStorage = localStorage.getItem(
91 TUTORIAL_DATA_LOCAL_STORAGE_KEY,
92 );
93​
94 return {
95 tutorialData: parseTutorialDataString(tutorialInLocalStorage),
96 updateTutorialData: async (data: Partial<TutorialData>) => {
97 const existingDataString = localStorage.getItem(
98 TUTORIAL_DATA_LOCAL_STORAGE_KEY,
99 );
100 const existingData = parseTutorialDataString(existingDataString);
101 const updatedDataString = JSON.stringify({
102 ...existingData,
103 ...data,
104 });
105​
106 localStorage.setItem(
107 TUTORIAL_DATA_LOCAL_STORAGE_KEY,
108 updatedDataString,
109 );
110​
111 window.document.cookie = `${TUTORIAL_COOKIE_NAME}=${updatedDataString}; path=/;`;
112 },
113 };
114 });