Connect, collaborate, and grow with a community of TypeScript developers
hook editor up to local storage
Chris Bautistaf3900302b3e9
diff --git a/package.json b/package.jsonindex 3cdcec49..bb006f4d 100644--- a/package.json+++ b/package.json@@ -50,7 +50,8 @@"tailwindcss-animate": "^1.0.5","typescript": "5.1.3","uuid-by-string": "^4.0.0",- "zod": "^3.21.4"+ "zod": "^3.21.4",+ "zustand": "^4.3.8"},"devDependencies": {"@tailwindcss/typography": "^0.5.9",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 91644f13..629764de 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -118,6 +118,9 @@ dependencies:zod:specifier: ^3.21.4version: 3.21.4+ zustand:+ specifier: ^4.3.8+ version: 4.3.8([email protected])devDependencies:'@tailwindcss/typography':@@ -5319,3 +5322,19 @@ packages:resolution: {integrity: sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==}dev: false++ resolution: {integrity: sha512-4h28KCkHg5ii/wcFFJ5Fp+k1J3gJoasaIbppdgZFO4BPJnsNxL0mQXBSFgOgAdCdBj35aDTPvdAJReTMntFPGg==}+ engines: {node: '>=12.7.0'}+ peerDependencies:+ immer: '>=9.0'+ react: '>=16.8'+ peerDependenciesMeta:+ immer:+ optional: true+ react:+ optional: true+ dependencies:+ react: 18.2.0+ use-sync-external-store: 1.2.0([email protected])+ dev: falsediff --git a/src/components/challenge/editor/index.tsx b/src/components/challenge/editor/index.tsxindex 69ca85f0..f6a9b3cb 100644--- a/src/components/challenge/editor/index.tsx+++ b/src/components/challenge/editor/index.tsx@@ -2,6 +2,8 @@import Editor from '@monaco-editor/react';import { Settings } from 'lucide-react';import type * as monaco from 'monaco-editor/esm/vs/editor/editor.api';+import { useTheme } from 'next-themes';+import { useMemo } from 'react';import {Dialog,DialogContent,@@ -9,8 +11,8 @@ import {DialogTitle,DialogTrigger,} from '~/components/ui/dialog';-import useLocalStorage from '~/utils/useLocalStorage';-import { DEFAULT_SETTINGS, SettingsForm } from '../settings-form';+import { SettingsForm } from '../settings-form';+import { useEditorSettingsStore } from '../settings-store';import { createTwoslashInlayProvider } from './twoslash';import { VimStatusBar, loadVim } from './vimMode';@@ -28,6 +30,7 @@ const defaultOptions: monaco.editor.IStandaloneEditorConstructionOptions = {minimap: {enabled: false,},+ fontSize: 16,};type Monaco = typeof monaco;@@ -142,12 +145,22 @@ interface Props {prompt: string;}export const CodePanel = ({ prompt }: Props) => {- const [settings] = useLocalStorage('settings', JSON.stringify(DEFAULT_SETTINGS));- const parsedSettings = JSON.parse(settings);- const options = {- ...defaultOptions,- ...parsedSettings,- } as monaco.editor.IStandaloneEditorConstructionOptions;+ const { theme } = useTheme();++ const { settings } = useEditorSettingsStore();+ const editorTheme = theme === 'light' ? 'vs' : 'vs-dark';+ const editorOptions = useMemo(() => {+ const options = {+ ...defaultOptions,+ ...settings,+ fontSize: parseInt(settings.fontSize),+ tabSize: parseInt(settings.tabSize),+ };+ return {+ ...options,+ };+ }, [settings]);+return (<><div className="container flex items-end space-y-2 bg-muted py-4">@@ -168,8 +181,8 @@ export const CodePanel = ({ prompt }: Props) => {</div></div><Editor- theme="vs-dark"- options={options}+ theme={editorTheme}+ options={editorOptions}defaultLanguage="typescript"// eslint-disable-next-line @typescript-eslint/no-misused-promisesonMount={onMount(prompt)}diff --git a/src/components/challenge/editor/vimMode.tsx b/src/components/challenge/editor/vimMode.tsxindex c760a2f5..f52f411e 100644--- a/src/components/challenge/editor/vimMode.tsx+++ b/src/components/challenge/editor/vimMode.tsx@@ -6,18 +6,22 @@ const statusBarId = 'vim-status-bar';export const VimStatusBar = () => {return (- <div className='flex justify-between flex-row-reverse'>- <label className='flex gap-1'>- Vim Mode:- <input type="checkbox" defaultChecked={localStorage.getItem(vimEnabledKey) === "true"} onChange={function (ev) {- if (ev.currentTarget.checked) {- activateVimMode(globalThis.editor)- } else {- deactivateVimMode();- }- }} />+ <div className="flex flex-row-reverse justify-between">+ <label className="flex gap-1">+ Vim Mode:+ <input+ type="checkbox"+ defaultChecked={localStorage.getItem(vimEnabledKey) === 'true'}+ onChange={function (ev) {+ if (ev.currentTarget.checked) {+ activateVimMode(globalThis.editor);+ } else {+ deactivateVimMode();+ }+ }}+ /></label>- <div id={statusBarId} className="font-mono" />+ <div id={statusBarId} className="font-mono" /></div>);};@@ -59,7 +63,7 @@ export function deactivateVimMode() {globalThis.vimMode.dispose();- delete globalThis.vimMode;+ delete globalThis.vimMode;localStorage.setItem(vimEnabledKey, 'false');diff --git a/src/components/challenge/settings-form.tsx b/src/components/challenge/settings-form.tsxindex 466ce2fe..50a52f93 100644--- a/src/components/challenge/settings-form.tsx+++ b/src/components/challenge/settings-form.tsx@@ -1,6 +1,7 @@'use client';import { zodResolver } from '@hookform/resolvers/zod';+import * as DialogPrimitive from '@radix-ui/react-dialog';import { useForm } from 'react-hook-form';import * as z from 'zod';@@ -8,7 +9,6 @@ import { Button } from '~/components/ui/button';import {Form,FormControl,- FormDescription,FormField,FormItem,FormLabel,@@ -22,7 +22,7 @@ import {SelectValue,} from '~/components/ui/select';import { toast } from '~/components/ui/use-toast';-import useLocalStorage from '~/utils/useLocalStorage';+import { useEditorSettingsStore } from './settings-store';const formSchema = z.object({fontSize: z.string(),@@ -38,19 +38,14 @@ export const DEFAULT_SETTINGS = {tabSize: '2',};export function SettingsForm() {- const [settings, setSettings] = useLocalStorage('settings', JSON.stringify(DEFAULT_SETTINGS));- const parsedSettings = JSON.parse(settings) as FormSchema;+ const { settings, updateSettings } = useEditorSettingsStore();const form = useForm<FormSchema>({resolver: zodResolver(formSchema),- defaultValues: {- fontSize: parsedSettings.fontSize,- bindings: parsedSettings.bindings,- tabSize: parsedSettings.tabSize,- },+ defaultValues: settings,});function onSubmit(data: FormSchema) {- setSettings(JSON.stringify(data));+ updateSettings(data);toast({title: 'Settings updated!',description: (@@ -137,7 +132,9 @@ export function SettingsForm() {</FormItem>)}/>- <Button type="submit">Submit</Button>+ <DialogPrimitive.Close asChild>+ <Button type="submit">Submit</Button>+ </DialogPrimitive.Close></form></Form>);diff --git a/src/components/challenge/settings-store.ts b/src/components/challenge/settings-store.tsnew file mode 100644index 00000000..9a5188e0--- /dev/null+++ b/src/components/challenge/settings-store.ts@@ -0,0 +1,27 @@+import { create } from 'zustand';+import { persist } from 'zustand/middleware';++export const DEFAULT_SETTINGS = {+ fontSize: '12',+ bindings: 'standard',+ tabSize: '2',+};++type Settings = typeof DEFAULT_SETTINGS;++interface State {+ settings: Settings;+ updateSettings: (settings: Settings) => void;+}++export const useEditorSettingsStore = create<State>()(+ persist(+ (set, get) => ({+ settings: DEFAULT_SETTINGS,+ updateSettings: (settings) => set({ settings: { ...get().settings, ...settings } }),+ }),+ {+ name: 'editor-settings',+ },+ ),+);