Skip to content

Commit

Permalink
Fix infinite loading
Browse files Browse the repository at this point in the history
  • Loading branch information
MattMcAdams committed Feb 21, 2024
1 parent eb0043f commit 78c7203
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 31 deletions.
69 changes: 60 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"js-easing-functions": "^1.0.3",
"next": "14.1.0",
"react": "^18",
"react-dom": "^18"
"react-dom": "^18",
"remotestoragejs": "^2.0.0-beta.6"
},
"devDependencies": {
"@types/color": "^3.0.6",
Expand Down
2 changes: 0 additions & 2 deletions src/app/library/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import LibraryConfigInput from "../../components/inputs/LibraryConfiguration";
const Library = () => {
const Session = useSessionContext();

// const check = arr.some((e) => e.name === obj.name);

return (
<main className="space-y-16 lg:p-16 md:p-8 p-4">
{Session.providerLoaded ? (
Expand Down
56 changes: 37 additions & 19 deletions src/data/session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ const Provider: React.FC<Props> = ({ children }) => {
================================================================= */

const providerLoaded = true;
const [configLoaded, setConfigLoaded] = useState<boolean>(defaults.configLoaded);
const [configLoaded, setConfigLoaded] = useState<boolean>(
defaults.configLoaded
);
const [config, setConfig] = useState<config>({ ...defaults.config });
const [advColorInfo, setAdvColorInfo] = useState<boolean>(defaults.advColorInfo);
const [advColorInfo, setAdvColorInfo] = useState<boolean>(
defaults.advColorInfo
);
const [library, setLibrary] = useState<config[]>([]);
const [libraryLoaded, setLibraryLoaded] = useState<boolean>(false);

Expand All @@ -53,7 +57,7 @@ const Provider: React.FC<Props> = ({ children }) => {
value = value.substring(0, 8);
}
setConfig({ ...config, keyColor: value });
};
}

function updateConfig(
key: "light" | "dark",
Expand All @@ -70,7 +74,7 @@ const Provider: React.FC<Props> = ({ children }) => {
}
newConfig[key][property] = sanitizedValue;
setConfig(newConfig);
};
}

function updateConfigEasing(
key: "light" | "dark",
Expand All @@ -84,7 +88,7 @@ const Provider: React.FC<Props> = ({ children }) => {
}
newConfig[key][`${property}Ease`] = sanitizedValue;
setConfig(newConfig);
};
}

/* !SECTION Update configuration functions */
/* =================================================================
Expand Down Expand Up @@ -160,7 +164,7 @@ const Provider: React.FC<Props> = ({ children }) => {
newConfig.light.saturationEase = CONFIG.lightSaturationEasing;
}
setConfig(newConfig);
};
}

const loadConfig = useCallback((configString: string) => {
const CONFIG = JSON.parse(configString || "{}");
Expand All @@ -183,7 +187,7 @@ const Provider: React.FC<Props> = ({ children }) => {
angleEase: defaults.config.light.angleEase,
saturation: defaults.config.light.saturation,
saturationEase: defaults.config.light.saturationEase,
}
},
};
if (
CONFIG.darkCount ||
Expand All @@ -199,8 +203,8 @@ const Provider: React.FC<Props> = ({ children }) => {
CONFIG.darkSaturation ||
CONFIG.darkSaturationEasing ||
CONFIG.lightSaturation ||
CONFIG.lightSaturationEasing)
{
CONFIG.lightSaturationEasing
) {
loadLegacyConfig(configString);
} else {
if (CONFIG.keyColor !== undefined) {
Expand Down Expand Up @@ -266,6 +270,8 @@ const Provider: React.FC<Props> = ({ children }) => {
const CONFIG = localStorage.getItem("colorToolConfig");
if (!configLoaded && CONFIG) {
loadConfig(CONFIG);
} else if (!configLoaded && !CONFIG) {
setConfigLoaded(true);
}
}, [configLoaded, loadConfig]);

Expand All @@ -274,11 +280,17 @@ const Provider: React.FC<Props> = ({ children }) => {
/* SECTION Save config to local storage
================================================================= */

const saveConfigToLocalStorage = useCallback((config: config) => {
if (configLoaded) {
localStorage.setItem("colorToolConfig", JSON.stringify(config, undefined, 4));
}
}, [configLoaded]);
const saveConfigToLocalStorage = useCallback(
(config: config) => {
if (configLoaded) {
localStorage.setItem(
"colorToolConfig",
JSON.stringify(config, undefined, 4)
);
}
},
[configLoaded]
);

useEffect(() => {
saveConfigToLocalStorage(config);
Expand Down Expand Up @@ -307,11 +319,17 @@ const Provider: React.FC<Props> = ({ children }) => {
/* SECTION Save Library to local storage
================================================================= */

const saveLibraryToLocalStorage = useCallback((library: config[]) => {
if (libraryLoaded) {
localStorage.setItem("colorToolLibrary", JSON.stringify(library, undefined, 4));
}
}, [libraryLoaded]);
const saveLibraryToLocalStorage = useCallback(
(library: config[]) => {
if (libraryLoaded) {
localStorage.setItem(
"colorToolLibrary",
JSON.stringify(library, undefined, 4)
);
}
},
[libraryLoaded]
);

useEffect(() => {
saveLibraryToLocalStorage(library);
Expand Down

0 comments on commit 78c7203

Please sign in to comment.