refactor: improve types around dataState and libraryData (#3427)

This commit is contained in:
David Luzar 2021-04-10 19:17:49 +02:00 committed by GitHub
parent c19c8ecd27
commit a7cbe68ae8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 84 additions and 55 deletions

View file

@ -245,10 +245,10 @@ const importFromBackend = async (
export const loadScene = async (
id: string | null,
privateKey: string | null,
// Supply initialData even if importing from backend to ensure we restore
// Supply local state even if importing from backend to ensure we restore
// localStorage user settings which we do not persist on server.
// Non-optional so we don't forget to pass it even if `undefined`.
initialData: ImportedDataState | undefined | null,
localDataState: ImportedDataState | undefined | null,
) => {
let data;
if (id != null) {
@ -256,10 +256,10 @@ export const loadScene = async (
// extra care not to leak it
data = restore(
await importFromBackend(id, privateKey),
initialData?.appState,
localDataState?.appState,
);
} else {
data = restore(initialData || null, null);
data = restore(localDataState || null, null);
}
return {

View file

@ -19,7 +19,7 @@ import {
VERSION_TIMEOUT,
} from "../constants";
import { loadFromBlob } from "../data/blob";
import { DataState, ImportedDataState } from "../data/types";
import { ImportedDataState } from "../data/types";
import {
ExcalidrawElement,
NonDeletedExcalidrawElement,
@ -50,6 +50,7 @@ import {
saveToLocalStorage,
} from "./data/localStorage";
import CustomStats from "./CustomStats";
import { RestoredDataState } from "../data/restore";
const languageDetector = new LanguageDetector();
languageDetector.init({
@ -81,13 +82,11 @@ const initializeScene = async (opts: {
);
const externalUrlMatch = window.location.hash.match(/^#url=(.*)$/);
const initialData = importFromLocalStorage();
const localDataState = importFromLocalStorage();
let scene: DataState & { scrollToContent?: boolean } = await loadScene(
null,
null,
initialData,
);
let scene: RestoredDataState & {
scrollToContent?: boolean;
} = await loadScene(null, null, localDataState);
let roomLinkData = getCollaborationLinkData(window.location.href);
const isExternalScene = !!(id || jsonBackendMatch || roomLinkData);
@ -102,12 +101,12 @@ const initializeScene = async (opts: {
) {
// Backwards compatibility with legacy url format
if (id) {
scene = await loadScene(id, null, initialData);
scene = await loadScene(id, null, localDataState);
} else if (jsonBackendMatch) {
scene = await loadScene(
jsonBackendMatch[1],
jsonBackendMatch[2],
initialData,
localDataState,
);
}
scene.scrollToContent = true;