2020-08-26 16:15:54 +05:30
|
|
|
import React, { useState, useLayoutEffect, useEffect } from "react";
|
2020-01-05 15:10:42 +01:00
|
|
|
import ReactDOM from "react-dom";
|
2020-03-31 09:37:51 +01:00
|
|
|
import * as Sentry from "@sentry/browser";
|
2020-04-07 15:54:49 +02:00
|
|
|
import * as SentryIntegrations from "@sentry/integrations";
|
2020-04-13 16:08:39 +02:00
|
|
|
|
2020-05-13 19:19:49 +02:00
|
|
|
import { EVENT } from "./constants";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { TopErrorBoundary } from "./components/TopErrorBoundary";
|
2020-08-20 16:45:20 +02:00
|
|
|
import Excalidraw from "./excalidraw-embed/index";
|
2020-04-13 16:08:39 +02:00
|
|
|
import { register as registerServiceWorker } from "./serviceWorker";
|
|
|
|
|
2020-08-26 16:15:54 +05:30
|
|
|
import { debounce } from "./utils";
|
|
|
|
import {
|
|
|
|
importFromLocalStorage,
|
|
|
|
importUsernameFromLocalStorage,
|
|
|
|
saveUsernameToLocalStorage,
|
|
|
|
saveToLocalStorage,
|
|
|
|
} from "./data/localStorage";
|
|
|
|
|
|
|
|
import { SAVE_TO_LOCAL_STORAGE_TIMEOUT } from "./time_constants";
|
2020-09-22 21:51:49 +02:00
|
|
|
import { ImportedDataState } from "./data/types";
|
2020-08-26 16:15:54 +05:30
|
|
|
import { LoadingMessage } from "./components/LoadingMessage";
|
|
|
|
import { ExcalidrawElement } from "./element/types";
|
|
|
|
import { AppState } from "./types";
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-04-16 14:23:39 +02:00
|
|
|
// On Apple mobile devices add the proprietary app icon and splashscreen markup.
|
|
|
|
// No one should have to do this manually, and eventually this annoyance will
|
|
|
|
// go away once https://bugs.webkit.org/show_bug.cgi?id=183937 is fixed.
|
2020-04-16 17:27:23 +02:00
|
|
|
if (
|
|
|
|
/\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent) &&
|
|
|
|
!matchMedia("(display-mode: standalone)").matches
|
|
|
|
) {
|
2020-05-27 00:21:03 +05:30
|
|
|
import(/* webpackChunkName: "pwacompat" */ "pwacompat");
|
2020-04-16 14:23:39 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 13:37:43 +02:00
|
|
|
const SentryEnvHostnameMap: { [key: string]: string } = {
|
2020-03-31 09:37:51 +01:00
|
|
|
"excalidraw.com": "production",
|
2020-06-30 22:03:13 +01:00
|
|
|
"vercel.app": "staging",
|
2020-03-31 09:37:51 +01:00
|
|
|
};
|
|
|
|
|
2020-06-30 22:03:13 +01:00
|
|
|
const REACT_APP_DISABLE_SENTRY =
|
|
|
|
process.env.REACT_APP_DISABLE_SENTRY === "true";
|
|
|
|
const REACT_APP_GIT_SHA = process.env.REACT_APP_GIT_SHA as string;
|
|
|
|
|
2020-06-18 12:18:57 +01:00
|
|
|
// Disable Sentry locally or inside the Docker to avoid noise/respect privacy
|
|
|
|
const onlineEnv =
|
2020-06-30 22:03:13 +01:00
|
|
|
!REACT_APP_DISABLE_SENTRY &&
|
2020-06-18 12:18:57 +01:00
|
|
|
Object.keys(SentryEnvHostnameMap).find(
|
|
|
|
(item) => window.location.hostname.indexOf(item) >= 0,
|
|
|
|
);
|
2020-03-31 09:37:51 +01:00
|
|
|
|
|
|
|
Sentry.init({
|
|
|
|
dsn: onlineEnv
|
|
|
|
? "https://7bfc596a5bf945eda6b660d3015a5460@sentry.io/5179260"
|
|
|
|
: undefined,
|
2020-04-11 13:37:43 +02:00
|
|
|
environment: onlineEnv ? SentryEnvHostnameMap[onlineEnv] : undefined,
|
2020-06-30 22:03:13 +01:00
|
|
|
release: REACT_APP_GIT_SHA,
|
2020-04-13 15:34:39 +01:00
|
|
|
ignoreErrors: [
|
|
|
|
"undefined is not an object (evaluating 'window.__pad.performLoop')", // Only happens on Safari, but spams our servers. Doesn't break anything
|
|
|
|
],
|
2020-04-07 15:54:49 +02:00
|
|
|
integrations: [
|
|
|
|
new SentryIntegrations.CaptureConsole({
|
|
|
|
levels: ["error"],
|
|
|
|
}),
|
|
|
|
],
|
2020-07-02 12:02:16 +02:00
|
|
|
beforeSend(event) {
|
|
|
|
if (event.request?.url) {
|
|
|
|
event.request.url = event.request.url.replace(/#.*$/, "");
|
|
|
|
}
|
|
|
|
return event;
|
|
|
|
},
|
2020-03-31 09:37:51 +01:00
|
|
|
});
|
|
|
|
|
2020-06-30 22:03:13 +01:00
|
|
|
window.__EXCALIDRAW_SHA__ = REACT_APP_GIT_SHA;
|
|
|
|
|
2020-08-26 16:15:54 +05:30
|
|
|
const saveDebounced = debounce(
|
|
|
|
(elements: readonly ExcalidrawElement[], state: AppState) => {
|
|
|
|
saveToLocalStorage(elements, state);
|
|
|
|
},
|
|
|
|
SAVE_TO_LOCAL_STORAGE_TIMEOUT,
|
|
|
|
);
|
|
|
|
|
|
|
|
const onUsernameChange = (username: string) => {
|
|
|
|
saveUsernameToLocalStorage(username);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onBlur = () => {
|
|
|
|
saveDebounced.flush();
|
|
|
|
};
|
|
|
|
|
2020-07-07 20:40:39 +05:30
|
|
|
function ExcalidrawApp() {
|
2020-08-26 16:15:54 +05:30
|
|
|
// dimensions
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2020-07-07 20:40:39 +05:30
|
|
|
const [dimensions, setDimensions] = useState({
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight,
|
|
|
|
});
|
|
|
|
useLayoutEffect(() => {
|
2020-08-26 16:15:54 +05:30
|
|
|
const onResize = () => {
|
|
|
|
setDimensions({
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-07-07 20:40:39 +05:30
|
|
|
window.addEventListener("resize", onResize);
|
|
|
|
|
|
|
|
return () => window.removeEventListener("resize", onResize);
|
|
|
|
}, []);
|
|
|
|
|
2020-08-26 16:15:54 +05:30
|
|
|
// initial state
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const [initialState, setInitialState] = useState<{
|
2020-09-22 21:51:49 +02:00
|
|
|
data: ImportedDataState;
|
2020-08-26 16:15:54 +05:30
|
|
|
user: {
|
|
|
|
name: string | null;
|
|
|
|
};
|
|
|
|
} | null>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setInitialState({
|
|
|
|
data: importFromLocalStorage(),
|
|
|
|
user: {
|
|
|
|
name: importUsernameFromLocalStorage(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// blur/unload
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener(EVENT.UNLOAD, onBlur, false);
|
|
|
|
window.addEventListener(EVENT.BLUR, onBlur, false);
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener(EVENT.UNLOAD, onBlur, false);
|
|
|
|
window.removeEventListener(EVENT.BLUR, onBlur, false);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if (!initialState) {
|
|
|
|
return <LoadingMessage />;
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:40:39 +05:30
|
|
|
return (
|
|
|
|
<TopErrorBoundary>
|
2020-08-26 16:15:54 +05:30
|
|
|
<Excalidraw
|
|
|
|
width={dimensions.width}
|
|
|
|
height={dimensions.height}
|
|
|
|
onChange={saveDebounced}
|
|
|
|
initialData={initialState.data}
|
|
|
|
user={initialState.user}
|
|
|
|
onUsernameChange={onUsernameChange}
|
|
|
|
/>
|
2020-07-07 20:40:39 +05:30
|
|
|
</TopErrorBoundary>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:10:42 +01:00
|
|
|
const rootElement = document.getElementById("root");
|
2020-01-21 15:50:25 +01:00
|
|
|
|
2020-07-07 20:40:39 +05:30
|
|
|
ReactDOM.render(<ExcalidrawApp />, rootElement);
|
2020-04-13 16:08:39 +02:00
|
|
|
|
2020-05-13 19:19:49 +02:00
|
|
|
registerServiceWorker({
|
|
|
|
onUpdate: (registration) => {
|
|
|
|
const waitingServiceWorker = registration.waiting;
|
|
|
|
if (waitingServiceWorker) {
|
|
|
|
waitingServiceWorker.addEventListener(
|
|
|
|
EVENT.STATE_CHANGE,
|
|
|
|
(event: Event) => {
|
|
|
|
const target = event.target as ServiceWorker;
|
|
|
|
const state = target.state as ServiceWorkerState;
|
|
|
|
if (state === "activated") {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
waitingServiceWorker.postMessage({ type: "SKIP_WAITING" });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|