2024-03-22 19:43:26 +03:00
|
|
|
import './polyfill/dispose';
|
2024-03-28 13:24:25 +03:00
|
|
|
import '@affine/core/bootstrap/preload';
|
2024-08-02 05:02:03 +03:00
|
|
|
import './global.css';
|
2024-03-19 10:48:56 +03:00
|
|
|
|
2024-03-28 13:24:25 +03:00
|
|
|
import { appConfigProxy } from '@affine/core/hooks/use-app-config-storage';
|
2024-03-19 10:48:56 +03:00
|
|
|
import { performanceLogger } from '@affine/core/shared';
|
2024-07-29 14:05:22 +03:00
|
|
|
import { apis, appInfo, events } from '@affine/electron-api';
|
2024-03-28 13:24:25 +03:00
|
|
|
import {
|
2024-05-21 07:30:22 +03:00
|
|
|
init,
|
2024-03-28 13:24:25 +03:00
|
|
|
reactRouterV6BrowserTracingIntegration,
|
2024-05-21 07:30:22 +03:00
|
|
|
setTags,
|
2024-03-28 13:24:25 +03:00
|
|
|
} from '@sentry/react';
|
|
|
|
import { debounce } from 'lodash-es';
|
|
|
|
import { StrictMode, useEffect } from 'react';
|
2024-03-19 10:48:56 +03:00
|
|
|
import { createRoot } from 'react-dom/client';
|
2024-03-28 13:24:25 +03:00
|
|
|
import {
|
|
|
|
createRoutesFromChildren,
|
|
|
|
matchRoutes,
|
|
|
|
useLocation,
|
|
|
|
useNavigationType,
|
|
|
|
} from 'react-router-dom';
|
2024-03-19 10:48:56 +03:00
|
|
|
|
|
|
|
import { App } from './app';
|
|
|
|
|
|
|
|
const performanceMainLogger = performanceLogger.namespace('main');
|
|
|
|
function main() {
|
|
|
|
performanceMainLogger.info('start');
|
|
|
|
|
2024-04-01 10:04:39 +03:00
|
|
|
// load persistent config for electron
|
2024-06-21 10:54:14 +03:00
|
|
|
// TODO(@Peng): should be sync, but it's not necessary for now
|
2024-04-01 10:04:39 +03:00
|
|
|
appConfigProxy
|
|
|
|
.getSync()
|
|
|
|
.catch(() => console.error('failed to load app config'));
|
|
|
|
|
2024-03-19 10:48:56 +03:00
|
|
|
// skip bootstrap setup for desktop onboarding
|
2024-08-12 07:12:51 +03:00
|
|
|
if (
|
2024-08-19 17:24:34 +03:00
|
|
|
appInfo?.windowName === 'onboarding' ||
|
|
|
|
appInfo?.windowName === 'theme-editor'
|
2024-08-12 07:12:51 +03:00
|
|
|
) {
|
2024-03-19 10:48:56 +03:00
|
|
|
performanceMainLogger.info('skip setup');
|
|
|
|
} else {
|
|
|
|
performanceMainLogger.info('setup start');
|
2024-03-28 13:24:25 +03:00
|
|
|
if (window.SENTRY_RELEASE || environment.isDebug) {
|
|
|
|
// https://docs.sentry.io/platforms/javascript/guides/electron/
|
2024-05-21 07:30:22 +03:00
|
|
|
init({
|
|
|
|
dsn: process.env.SENTRY_DSN,
|
|
|
|
environment: process.env.BUILD_TYPE ?? 'development',
|
|
|
|
integrations: [
|
|
|
|
reactRouterV6BrowserTracingIntegration({
|
|
|
|
useEffect,
|
|
|
|
useLocation,
|
|
|
|
useNavigationType,
|
|
|
|
createRoutesFromChildren,
|
|
|
|
matchRoutes,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2024-03-28 13:24:25 +03:00
|
|
|
setTags({
|
|
|
|
appVersion: runtimeConfig.appVersion,
|
|
|
|
editorVersion: runtimeConfig.editorVersion,
|
|
|
|
});
|
2024-08-17 07:32:49 +03:00
|
|
|
|
|
|
|
apis?.ui.handleNetworkChange(navigator.onLine);
|
2024-03-28 13:24:25 +03:00
|
|
|
window.addEventListener('offline', () => {
|
|
|
|
apis?.ui.handleNetworkChange(false);
|
|
|
|
});
|
|
|
|
window.addEventListener('online', () => {
|
|
|
|
apis?.ui.handleNetworkChange(true);
|
|
|
|
});
|
|
|
|
}
|
2024-04-01 10:04:39 +03:00
|
|
|
|
2024-03-28 13:24:25 +03:00
|
|
|
const handleMaximized = (maximized: boolean | undefined) => {
|
|
|
|
document.documentElement.dataset.maximized = String(maximized);
|
|
|
|
};
|
|
|
|
const handleFullscreen = (fullscreen: boolean | undefined) => {
|
|
|
|
document.documentElement.dataset.fullscreen = String(fullscreen);
|
|
|
|
};
|
|
|
|
apis?.ui.isMaximized().then(handleMaximized).catch(console.error);
|
|
|
|
apis?.ui.isFullScreen().then(handleFullscreen).catch(console.error);
|
|
|
|
events?.ui.onMaximized(handleMaximized);
|
|
|
|
events?.ui.onFullScreen(handleFullscreen);
|
|
|
|
|
2024-07-29 14:05:22 +03:00
|
|
|
const tabId = appInfo?.viewId;
|
|
|
|
const handleActiveTabChange = (active: boolean) => {
|
|
|
|
document.documentElement.dataset.active = String(active);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (tabId) {
|
|
|
|
apis?.ui
|
|
|
|
.isActiveTab()
|
|
|
|
.then(active => {
|
|
|
|
handleActiveTabChange(active);
|
|
|
|
events?.ui.onActiveTabChanged(id => {
|
|
|
|
handleActiveTabChange(id === tabId);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(console.error);
|
|
|
|
}
|
|
|
|
|
2024-03-28 13:24:25 +03:00
|
|
|
const handleResize = debounce(() => {
|
|
|
|
apis?.ui.handleWindowResize().catch(console.error);
|
|
|
|
}, 50);
|
|
|
|
window.addEventListener('resize', handleResize);
|
2024-03-19 10:48:56 +03:00
|
|
|
performanceMainLogger.info('setup done');
|
2024-08-02 05:02:03 +03:00
|
|
|
window.addEventListener('dragstart', () => {
|
|
|
|
document.documentElement.dataset.dragging = 'true';
|
|
|
|
});
|
|
|
|
window.addEventListener('dragend', () => {
|
|
|
|
document.documentElement.dataset.dragging = 'false';
|
|
|
|
});
|
2024-03-19 10:48:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mountApp();
|
|
|
|
}
|
|
|
|
|
|
|
|
function mountApp() {
|
|
|
|
performanceMainLogger.info('import app');
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
const root = document.getElementById('app')!;
|
|
|
|
performanceMainLogger.info('render app');
|
|
|
|
createRoot(root).render(
|
|
|
|
<StrictMode>
|
|
|
|
<App />
|
|
|
|
</StrictMode>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
main();
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Failed to bootstrap app', err);
|
|
|
|
}
|