Skip to content

Commit 653b446

Browse files
committed
Add localstorage persist logic for config on home page
1 parent f0cdb19 commit 653b446

File tree

5 files changed

+77
-27
lines changed

5 files changed

+77
-27
lines changed

components/editor/SnippngCodeArea.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef, useState } from "react";
1+
import { useEffect, useRef, useState } from "react";
22

33
import { DEFAULT_BASE_SETUP } from "@/lib/constants";
44
import {
@@ -7,6 +7,7 @@ import {
77
getEditorWrapperBg,
88
getLanguage,
99
getTheme,
10+
LocalStorage,
1011
} from "@/utils";
1112

1213
import { langs, loadLanguage } from "@uiw/codemirror-extensions-langs";
@@ -107,6 +108,13 @@ const SnippngCodeArea = () => {
107108
}
108109
};
109110

111+
useEffect(() => {
112+
// if there is a uid means we are on edit page where we want to avoid persisting the editor config
113+
if (uid) return;
114+
// persist the editor config changes only when user is creating new snippet
115+
LocalStorage.set("config", { ...editorConfig, uid: undefined });
116+
}, [editorConfig, uid]);
117+
110118
return (
111119
<>
112120
<section

components/editor/SnippngControlHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ const SnippngControlHeader: React.FC<{
213213
leaveFrom="transform opacity-100 scale-100"
214214
leaveTo="transform opacity-0 scale-95"
215215
>
216-
<Menu.Items className="absolute right-0 max-h-[500px] z-30 mt-2 w-72 origin-top-right divide-y-[1px] dark:divide-zinc-400 divide-zinc-300 dark:bg-black bg-white overflow-auto text-sm rounded-sm outline outline-[1px] dark:outline-zinc-400 outline-zinc-300 dark:text-white text-zinc-900">
216+
<Menu.Items className="absolute right-0 max-h-[500px] z-30 mt-2 w-96 origin-top-right divide-y-[1px] dark:divide-zinc-400 divide-zinc-300 dark:bg-black bg-white overflow-auto text-sm rounded-sm outline outline-[1px] dark:outline-zinc-400 outline-zinc-300 dark:text-white text-zinc-900">
217217
<Menu.Button
218218
className="w-full text-left p-2 inline-flex items-center"
219219
onClick={() => {

lib/constants.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { SnippngEditorConfigInterface } from "@/types";
2+
import { LocalStorage } from "@/utils";
3+
4+
export const isBrowser = typeof window !== "undefined";
25

36
export const THEMES = [
47
{
@@ -514,31 +517,40 @@ export const PEXELS_QUERY_STRINGS = [
514517
"technology",
515518
];
516519

517-
export const defaultEditorConfig: SnippngEditorConfigInterface = {
518-
code: DEFAULT_CODE_SNIPPET,
519-
snippetsName: "",
520-
editorFontSize: 16,
521-
editorWindowControlsType: "mac-left",
522-
fileName: "@utils/debounce.ts",
523-
hasDropShadow: true,
524-
lineHeight: 19,
525-
paddingHorizontal: 70,
526-
paddingVertical: 70,
527-
rounded: true,
528-
selectedLang:
529-
LANGUAGES.find((language) => language.id === "typescript") || LANGUAGES[0],
530-
selectedTheme:
531-
THEMES.find((theme) => theme.id === "tokyoNightStorm") || THEMES[0],
532-
showFileName: true,
533-
showLineNumbers: true,
534-
wrapperBg: "#eee811",
535-
gradients: ["#ba68c8", "#ffa7c4", "#e57373"],
536-
gradientAngle: 140,
537-
editorWidth: 0,
538-
bgImageVisiblePatch: null,
539-
bgBlur: 0,
520+
const getConfig = (): SnippngEditorConfigInterface => {
521+
let persistedConfig = LocalStorage.get(
522+
"config"
523+
) as SnippngEditorConfigInterface;
524+
if (persistedConfig) return persistedConfig;
525+
return {
526+
code: DEFAULT_CODE_SNIPPET,
527+
snippetsName: "",
528+
editorFontSize: 16,
529+
editorWindowControlsType: "mac-left",
530+
fileName: "@utils/debounce.ts",
531+
hasDropShadow: true,
532+
lineHeight: 19,
533+
paddingHorizontal: 70,
534+
paddingVertical: 70,
535+
rounded: true,
536+
selectedLang:
537+
LANGUAGES.find((language) => language.id === "typescript") ||
538+
LANGUAGES[0],
539+
selectedTheme:
540+
THEMES.find((theme) => theme.id === "tokyoNightStorm") || THEMES[0],
541+
showFileName: true,
542+
showLineNumbers: true,
543+
wrapperBg: "#eee811",
544+
gradients: ["#ba68c8", "#ffa7c4", "#e57373"],
545+
gradientAngle: 140,
546+
editorWidth: 0,
547+
bgImageVisiblePatch: null,
548+
bgBlur: 0,
549+
};
540550
};
541551

552+
export const defaultEditorConfig: SnippngEditorConfigInterface = getConfig();
553+
542554
export const DEFAULT_BASE_SETUP = {
543555
foldGutter: false,
544556
highlightActiveLine: false,

pages/snippet/[uid].tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ const SavedSnippet = () => {
4747
if (!router.isReady) return;
4848
fetchCodeSnippet();
4949
return () => {
50-
setEditorConfig({ ...defaultEditorConfig });
50+
// set uid to undefined because we are leaving the snippet details page, the only page where uid is required
51+
setEditorConfig({ ...defaultEditorConfig, uid: undefined });
5152
};
5253
}, [router.isReady]);
5354

utils/index.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DEFAULT_RANGES, DEFAULT_WIDTHS } from "@/lib/constants";
1+
import { DEFAULT_RANGES, DEFAULT_WIDTHS, isBrowser } from "@/lib/constants";
22
import {
33
exportedTypeSuite,
44
SnippngEditorConfigInterface,
@@ -137,3 +137,32 @@ export const validateSnippngConfig = (config: SnippngEditorConfigInterface) => {
137137
export const copyJSONText = async <T extends object>(data: T) => {
138138
return navigator.clipboard?.writeText(JSON.stringify(data));
139139
};
140+
141+
export class LocalStorage {
142+
static get(key: string) {
143+
if (!isBrowser) return;
144+
let value = localStorage.getItem(key);
145+
if (value) {
146+
try {
147+
return JSON.parse(value);
148+
} catch (err) {
149+
return null;
150+
}
151+
}
152+
return null;
153+
}
154+
static set(key: string, value: any) {
155+
if (!isBrowser) return;
156+
localStorage.setItem(key, JSON.stringify(value));
157+
}
158+
159+
static remove(key: string) {
160+
if (!isBrowser) return;
161+
localStorage.removeItem(key);
162+
}
163+
164+
static clear() {
165+
if (!isBrowser) return;
166+
localStorage.clear();
167+
}
168+
}

0 commit comments

Comments
 (0)