prevent autosave from prompting on failure & granularize error messages

This commit is contained in:
dwelle 2021-04-04 15:13:56 +02:00
parent 6442a45bd4
commit 0b32757085
4 changed files with 24 additions and 7 deletions

View File

@ -924,7 +924,7 @@ class App extends React.Component<AppProps, AppState> {
?.classList.toggle("theme--dark", this.state.theme === "dark"); ?.classList.toggle("theme--dark", this.state.theme === "dark");
if (this.state.autosave && this.state.fileHandle && supported) { if (this.state.autosave && this.state.fileHandle && supported) {
this.saveLocalSceneDebounced( this.autosaveLocalSceneDebounced(
this.scene.getElementsIncludingDeleted(), this.scene.getElementsIncludingDeleted(),
this.state, this.state,
); );
@ -1062,18 +1062,31 @@ class App extends React.Component<AppProps, AppState> {
}); });
}, SCROLL_TIMEOUT); }, SCROLL_TIMEOUT);
private saveLocalSceneDebounced = debounce( private autosaveLocalSceneDebounced = debounce(
async (elements: readonly ExcalidrawElement[], state: AppState) => { async (elements: readonly ExcalidrawElement[], state: AppState) => {
if (this.state.autosave && this.state.fileHandle && supported) { if (this.state.autosave && this.state.fileHandle && supported) {
try { try {
await saveAsJSON(elements, state); await saveAsJSON(
elements,
state,
// only if fileHandle valid
true,
);
} catch (error) { } catch (error) {
// shouldn't (almost) ever happen, so let's log it
console.error(error);
this.setState({ this.setState({
autosave: false, autosave: false,
errorMessage: t("toast.autosaveFailed"), toastMessage:
error.name === "NotAllowedError"
? t("toast.autosaveFailed_notAllowed")
: error.name === "NotFoundError"
? t("toast.autosaveFailed_notFound")
: t("toast.autosaveFailed"),
}); });
// shouldn't happen, so let's log it
if (!["NotAllowedError", "NotFoundError"].includes(error.name)) {
console.error(error);
}
} }
} }
}, },

View File

@ -10,7 +10,7 @@
cursor: default; cursor: default;
left: 50%; left: 50%;
margin-left: -150px; margin-left: -150px;
padding: 4px 0; padding: 8px;
position: absolute; position: absolute;
text-align: center; text-align: center;
width: 300px; width: 300px;

View File

@ -27,6 +27,7 @@ export const serializeAsJSON = (
export const saveAsJSON = async ( export const saveAsJSON = async (
elements: readonly ExcalidrawElement[], elements: readonly ExcalidrawElement[],
appState: AppState, appState: AppState,
onlyIfFileHandleValid = false,
) => { ) => {
const serialized = serializeAsJSON(elements, appState); const serialized = serializeAsJSON(elements, appState);
const blob = new Blob([serialized], { const blob = new Blob([serialized], {
@ -41,6 +42,7 @@ export const saveAsJSON = async (
extensions: [".excalidraw"], extensions: [".excalidraw"],
}, },
appState.fileHandle, appState.fileHandle,
onlyIfFileHandleValid,
); );
return { fileHandle }; return { fileHandle };
}; };

View File

@ -251,6 +251,8 @@
"width": "Width" "width": "Width"
}, },
"toast": { "toast": {
"autosaveFailed_notAllowed": "Autosave was disabled.",
"autosaveFailed_notFound": "Autosave failed.\nIt seems the file no longer exists.",
"autosaveFailed": "Autosave failed.", "autosaveFailed": "Autosave failed.",
"copyStyles": "Copied styles.", "copyStyles": "Copied styles.",
"copyToClipboard": "Copied to clipboard.", "copyToClipboard": "Copied to clipboard.",