Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
[web] Offer to clear localstorage if loading data is too long (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxijonson committed Jul 2, 2023
1 parent ac40f31 commit d5348cc
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 53 deletions.
14 changes: 10 additions & 4 deletions packages/web/src/contexts/providers/PersistenceProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,17 @@ const PersistenceProvider = ({ children }: PersistenceProviderProps) => {
};

const load = async () => {
await Promise.all([loadConversations(), loadCallableFunctions()]);
};
load().then(() => {
try {
await Promise.all([
loadConversations(),
loadCallableFunctions(),
]);
} catch (e) {
console.error(e);
}
setHasInit(true);
});
};
load();
}, [
addCallableFunction,
addConversation,
Expand Down
94 changes: 46 additions & 48 deletions packages/web/src/hooks/useStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,57 +78,55 @@ const useStorage = <T,>(
return value;
})()
),
deserialize: (v) =>
(() => {
const value = JSON.parse(v);
if (!schema) return value;
deserialize: (v) => {
const value = JSON.parse(v);
if (!schema) return value;

const result = schema.safeParse(value);
if (result.success) {
return result.data;
}
if (warns.has(key)) return value;
const result = schema.safeParse(value);
if (result.success) {
return result.data;
}
if (warns.has(key)) return value;

const repaired = repairValueFromZodError(
value,
defaultValue,
result.error
);
const repaired = repairValueFromZodError(
value,
defaultValue,
result.error
);

try {
return schema.parse(repaired);
} catch {
warns.add(key);
console.error(result.error.format());
notifications.show({
title: `Invalid ${key} Local Storage value`,
message: (
<Stack spacing="xs">
<Text size="xs">
The value for {key} in the browser's local
storage is invalid or outdated. The app may
not function properly. It is recommended to
clear the value. See the console for more
details.
</Text>
<Button
variant="subtle"
color="red"
onClick={() => {
removeValue();
window.location.reload();
}}
>
Delete storage and reload
</Button>
</Stack>
),
color: "orange",
autoClose: false,
});
}
return value;
})(),
try {
return schema.parse(repaired);
} catch {
warns.add(key);
console.error(result.error.format());
notifications.show({
title: `Invalid ${key} Local Storage value`,
message: (
<Stack spacing="xs">
<Text size="xs">
The value for {key} in the browser's local
storage is invalid or outdated. The app may not
function properly. It is recommended to clear
the value. See the console for more details.
</Text>
<Button
variant="subtle"
color="red"
onClick={() => {
removeValue();
window.location.reload();
}}
>
Delete storage and reload
</Button>
</Stack>
),
color: "orange",
autoClose: false,
});
}
return value;
},
});

return {
Expand Down
34 changes: 33 additions & 1 deletion packages/web/src/pages/ConversationPage.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
import { Divider, Stack, Text } from "@mantine/core";
import { Button, Divider, Group, Stack, Text } from "@mantine/core";
import AddConversation from "../components/AddConversation";
import useConversationManager from "../hooks/useConversationManager";
import Messages from "../components/Messages";
import Prompt from "../components/Prompt";
import usePersistence from "../hooks/usePersistence";
import ContentLoader from "../components/ContentLoader";
import ConversationPageShell from "../components/ConversationPageShell";
import { useTimeout } from "@mantine/hooks";
import React from "react";

const ConversationPage = () => {
const { activeConversation } = useConversationManager();
const { hasInit: hasPersistenceInit } = usePersistence();
const [longLoading, setLongLoading] = React.useState(false);
const { start, clear } = useTimeout(() => setLongLoading(true), 5000);

React.useEffect(() => {
start();
return clear;
}, [start, clear]);

if (!hasPersistenceInit) {
return (
<ContentLoader size="xl">
<Text size="xl">Loading Saved Data</Text>
{longLoading && (
<Stack>
<Text size="sm" color="gray">
This is taking a while. Try refreshing the page or
deleting your saved data.
</Text>
<Group position="center">
<Button onClick={() => window.location.reload()}>
Refresh
</Button>
<Button
variant="outline"
color="red"
onClick={() => {
localStorage.clear();
window.location.reload();
}}
>
Delete Saved Data
</Button>
</Group>
</Stack>
)}
</ContentLoader>
);
}
Expand Down

0 comments on commit d5348cc

Please sign in to comment.