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(website): store options TypeScript, Enable jsx and AST Viewer in browser's local storage #5769

Merged
merged 6 commits into from Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/website/src/components/hooks/useHashState.ts
Expand Up @@ -2,6 +2,7 @@ import { toJsonConfig } from '@site/src/components/config/utils';
import * as lz from 'lzstring.ts';
import { useCallback, useEffect, useState } from 'react';

import { hasOwnProperty } from '../lib/has-own-property';
import { shallowEqual } from '../lib/shallowEqual';
import type { ConfigModel } from '../types';

Expand Down Expand Up @@ -114,16 +115,69 @@ const writeStateToUrl = (newState: ConfigModel): string => {
return '';
};

const retrieveStateFromLocalStorage = (): Partial<ConfigModel> | undefined => {
try {
const configString = window.localStorage.getItem('config');
if (!configString) {
return undefined;
}

const config: unknown = JSON.parse(configString);
if (typeof config !== 'object' || !config) {
return undefined;
}

const state: Partial<ConfigModel> = {};
if (hasOwnProperty('ts', config)) {
const ts: unknown = config.ts;
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
if (typeof ts === 'string') {
state.ts = ts;
}
}
if (hasOwnProperty('jsx', config)) {
const jsx: unknown = config.jsx;
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
if (typeof jsx === 'boolean') {
state.jsx = jsx;
}
}
if (hasOwnProperty('showAST', config)) {
const showAST: unknown = config.showAST;
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
if (typeof showAST === 'boolean') {
state.showAST = showAST;
} else if (typeof showAST === 'string') {
state.showAST = readShowAST(showAST);
}
}

return state;
} catch (e) {
// eslint-disable-next-line no-console
console.warn(e);
}
return undefined;
};

const writeStateToLocalStorage = (newState: ConfigModel): void => {
const config: Partial<ConfigModel> = {
ts: newState.ts,
jsx: newState.jsx,
showAST: newState.showAST,
};
window.localStorage.setItem('config', JSON.stringify(config));
};

function useHashState(
initialState: ConfigModel,
): [ConfigModel, (cfg: Partial<ConfigModel>) => void] {
const [hash, setHash] = useState<string>(window.location.hash.slice(1));
const [state, setState] = useState<ConfigModel>(() => ({
...initialState,
...retrieveStateFromLocalStorage(),
...parseStateFromUrl(window.location.hash.slice(1)),
}));
const [tmpState, setTmpState] = useState<Partial<ConfigModel>>(() => ({
...initialState,
...retrieveStateFromLocalStorage(),
...parseStateFromUrl(window.location.hash.slice(1)),
}));

Expand All @@ -141,6 +195,7 @@ function useHashState(
useEffect(() => {
const newState = { ...state, ...tmpState };
if (!shallowEqual(newState, state)) {
writeStateToLocalStorage(newState);
const newHash = writeStateToUrl(newState);
setState(newState);
setHash(newHash);
Expand Down
6 changes: 6 additions & 0 deletions packages/website/src/components/lib/has-own-property.ts
@@ -0,0 +1,6 @@
export function hasOwnProperty<X extends object, Y extends PropertyKey>(
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Lol. What a great example of why TypeScript's in type narrowing sometimes falls short.

(not requesting changes, just griping about the world we live in)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, as I remember, current TS version can infer type from using in.

property: Y,
object: X,
): object is X & Record<Y, unknown> {
return property in object;
}