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

Discussion: adds local storage clearing #155

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/core/app/components/utils/useSeedHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function undoReducer(state, action) {
const getNewSeed = () => seedrandom(null, { pass: (_, seed) => ({ seed }) }).seed;

export function useSeedHistory(functionName) {
const key = `--${functionName}-seed`;
const key = `mechanic_seed_${functionName}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that the mechanic_ prefix is used in multiple places now I think this should be a constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

const [state, dispatch] = useReducer(
undoReducer,
initialize(key, {
Expand Down
14 changes: 13 additions & 1 deletion packages/core/app/components/utils/useValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ function useLocalStorageState(key, initialState, clean) {
return [value, setValue, remove];
}

function clearLocalStorage() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this also take an optional argument so it can be used to delete a very specific local storage entry?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if (typeof localStorage === "undefined") {
return null;
}
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key.startsWith("mechanic_")) {
localStorage.removeItem(key);
}
}
}

const cleanValues = (object, reference) => {
for (let property in object) {
if (!(property in reference) && property !== "preset") {
Expand Down Expand Up @@ -195,4 +207,4 @@ const useValues = (functionName, functionInputs, presets) => {
return [values, setValues];
};

export { useValues };
export { useValues, clearLocalStorage };