-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(console): console log levels are not persistent across restarts (#…
…6885) Using local storage to save some of the console view settings across app restarts Fixes #2806 ## Checklist - [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [ ] Description explains motivation and solution - [ ] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
- Loading branch information
Showing
10 changed files
with
261 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
apps/wing-console/console/ui/src/features/localstorage-context/localstorage-context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { PropsWithChildren } from "react"; | ||
import { createContext, useMemo } from "react"; | ||
|
||
const hashCode = (value: string) => { | ||
let hash = 0; | ||
if (value.length === 0) { | ||
return hash.toString(); | ||
} | ||
for (let index = 0; index < value.length; index++) { | ||
const chr = value.codePointAt(index); | ||
hash = (hash << 5) - hash + chr!; | ||
hash = Math.trunc(hash); | ||
} | ||
return hash.toString(); | ||
}; | ||
|
||
export const AppLocalStorageContext = createContext<{ | ||
storageKey: string | undefined; | ||
}>({ | ||
storageKey: undefined, | ||
}); | ||
|
||
export interface AppLocalStorageProviderProps extends PropsWithChildren { | ||
storageKey: string | undefined; | ||
} | ||
|
||
export const AppLocalStorageProvider = ( | ||
props: AppLocalStorageProviderProps, | ||
) => { | ||
const key = useMemo(() => { | ||
if (props.storageKey) { | ||
const hash = hashCode(props.storageKey); | ||
return hash; | ||
} | ||
}, [props.storageKey]); | ||
return ( | ||
<AppLocalStorageContext.Provider | ||
value={{ | ||
storageKey: key, | ||
}} | ||
> | ||
{props.children} | ||
</AppLocalStorageContext.Provider> | ||
); | ||
}; |
77 changes: 77 additions & 0 deletions
77
apps/wing-console/console/ui/src/features/localstorage-context/use-localstorage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Imported and modified from https://github.com/streamich/react-use/blob/master/src/useLocalStorage.ts | ||
import type { Dispatch, SetStateAction } from "react"; | ||
import { | ||
useCallback, | ||
useState, | ||
useRef, | ||
useLayoutEffect, | ||
useEffect, | ||
useContext, | ||
} from "react"; | ||
|
||
import { AppLocalStorageContext } from "./localstorage-context.js"; | ||
|
||
export const useLocalStorage = <T>( | ||
key: string, | ||
initialValue: T, | ||
): [T, Dispatch<SetStateAction<T>>, boolean, () => void] => { | ||
const [valueExists] = useState(() => { | ||
try { | ||
const localStorageValue = localStorage.getItem(key); | ||
return localStorageValue !== null; | ||
} catch { | ||
return false; | ||
} | ||
}); | ||
|
||
const initializer = useRef((key: string) => { | ||
try { | ||
const localStorageValue = localStorage.getItem(key); | ||
return localStorageValue === null | ||
? initialValue | ||
: JSON.parse(localStorageValue); | ||
} catch { | ||
// If user is in private mode or has storage restriction | ||
// localStorage can throw. JSON.parse and JSON.stringify | ||
// can throw, too. | ||
return initialValue; | ||
} | ||
}); | ||
|
||
const [state, setState] = useState<T>(() => initializer.current(key)); | ||
|
||
useLayoutEffect(() => setState(initializer.current(key)), [key]); | ||
|
||
useEffect(() => { | ||
try { | ||
localStorage.setItem(key, JSON.stringify(state)); | ||
} catch { | ||
// If user is in private mode or has storage restriction | ||
// localStorage can throw. Also JSON.stringify can throw. | ||
} | ||
}, [state, key]); | ||
|
||
const remove = useCallback(() => { | ||
try { | ||
localStorage.removeItem(key); | ||
setState(initialValue); | ||
} catch { | ||
// If user is in private mode or has storage restriction | ||
// localStorage can throw. | ||
} | ||
}, [initialValue, key]); | ||
|
||
return [state, setState, valueExists, remove]; | ||
}; | ||
|
||
export const useAppLocalStorage = <T>( | ||
key: string, | ||
initialValue: T, | ||
): [T, Dispatch<SetStateAction<T>>, boolean, () => void] => { | ||
const { storageKey } = useContext(AppLocalStorageContext); | ||
if (!storageKey) { | ||
throw new Error("AppLocalStorageContext.storageKey is not provided"); | ||
} | ||
|
||
return useLocalStorage(`${storageKey}.${key}`, initialValue); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.