Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Generate sharable link for code-explorer #49

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to have a second persist here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, one is intended to store the data in local storage, while the other is meant to store the hashed data in the URL.

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