Skip to content

Commit

Permalink
feat: Generate sharable link for code-explorer (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
amareshsm authored Oct 23, 2024
1 parent c1e0d56 commit d8bc2f7
Showing 1 changed file with 52 additions and 21 deletions.
73 changes: 52 additions & 21 deletions src/hooks/use-explorer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";
import {
devtools,
persist,
StateStorage,
createJSONStorage,
} from "zustand/middleware";
import type { Options } from "espree";
import {
defaultCode,
Expand Down Expand Up @@ -71,37 +76,63 @@ type ExplorerState = {
setPathIndex: (pathIndex: PathIndex) => void;
};

const hashStorage: StateStorage = {
getItem: (key): string => {
const searchParams = new URLSearchParams(location.hash.slice(1));
const storedValue = searchParams.get(key) ?? "";
return storedValue ? JSON.parse(atob(storedValue)) : "";
},
setItem: (key, newValue): void => {
const searchParams = new URLSearchParams(location.hash.slice(1));
const encodedValue = btoa(JSON.stringify(newValue));
searchParams.set(key, encodedValue);
location.hash = searchParams.toString();
},
removeItem: (key): void => {
const searchParams = new URLSearchParams(location.hash.slice(1));
searchParams.delete(key);
location.hash = searchParams.toString();
},
};

export const useExplorer = create<ExplorerState>()(
devtools(
persist(
set => ({
tool: "ast",
setTool: tool => set({ tool }),
persist(
set => ({
tool: "ast",
setTool: tool => set({ tool }),

code: defaultCode,
setCode: code => set({ code }),
code: defaultCode,
setCode: code => set({ code }),

language: "javascript",
setLanguage: language => set({ language }),
language: "javascript",
setLanguage: language => set({ language }),

jsOptions: defaultJsOptions,
setJsOptions: jsOptions => set({ jsOptions }),
jsOptions: defaultJsOptions,
setJsOptions: jsOptions => set({ jsOptions }),

jsonOptions: defaultJsonOptions,
setJsonOptions: jsonOptions => set({ jsonOptions }),
jsonOptions: defaultJsonOptions,
setJsonOptions: jsonOptions => set({ jsonOptions }),

markdownOptions: defaultMarkdownOptions,
setMarkdownOptions: markdownOptions => set({ markdownOptions }),
markdownOptions: defaultMarkdownOptions,
setMarkdownOptions: markdownOptions =>
set({ markdownOptions }),

wrap: true,
setWrap: wrap => set({ wrap }),
wrap: true,
setWrap: wrap => set({ wrap }),

viewModes: defaultViewModes,
setViewModes: viewModes => set({ viewModes }),
viewModes: defaultViewModes,
setViewModes: viewModes => set({ viewModes }),

pathIndex: defaultPathIndex,
setPathIndex: pathIndex => set({ pathIndex }),
}),
pathIndex: defaultPathIndex,
setPathIndex: pathIndex => set({ pathIndex }),
}),
{
name: "eslint-explorer",
storage: createJSONStorage(() => hashStorage),
},
),
{
name: "eslint-explorer",
},
Expand Down

0 comments on commit d8bc2f7

Please sign in to comment.