AFFiNE/packages/frontend/apps/electron/renderer/app.tsx

113 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-07-18 19:53:10 +03:00
import { AffineContext } from '@affine/component/context';
import { GlobalLoading } from '@affine/component/global-loading';
2024-05-27 11:05:20 +03:00
import { AppFallback } from '@affine/core/components/affine/app-container';
import { WindowsAppControls } from '@affine/core/components/pure/header/windows-app-controls';
import { Telemetry } from '@affine/core/components/telemetry';
import { router } from '@affine/core/desktop/router';
2024-07-15 06:21:08 +03:00
import { configureCommonModules } from '@affine/core/modules';
import { configureAppTabsHeaderModule } from '@affine/core/modules/app-tabs-header';
import { I18nProvider } from '@affine/core/modules/i18n';
2024-07-15 06:21:08 +03:00
import { configureElectronStateStorageImpls } from '@affine/core/modules/storage';
import { CustomThemeModifier } from '@affine/core/modules/theme-editor';
import {
ClientSchemaProvider,
PopupWindowProvider,
} from '@affine/core/modules/url';
2024-09-11 10:55:37 +03:00
import { configureSqliteUserspaceStorageProvider } from '@affine/core/modules/userspace';
import { configureDesktopWorkbenchModule } from '@affine/core/modules/workbench';
2024-04-17 09:12:29 +03:00
import {
configureBrowserWorkspaceFlavours,
configureSqliteWorkspaceEngineStorageProvider,
} from '@affine/core/modules/workspace-engine';
import createEmotionCache from '@affine/core/utils/create-emotion-cache';
import { apis, appInfo } from '@affine/electron-api';
2023-07-18 19:53:10 +03:00
import { CacheProvider } from '@emotion/react';
2024-04-17 09:12:29 +03:00
import {
Framework,
FrameworkRoot,
getCurrentStore,
LifecycleService,
} from '@toeverything/infra';
import { Suspense } from 'react';
import { RouterProvider } from 'react-router-dom';
2023-07-18 19:53:10 +03:00
const desktopWhiteList = [
'/open-app/signin-redirect',
'/open-app/url',
'/upgrade-success',
'/ai-upgrade-success',
'/share',
'/oauth',
'/magic-link',
];
2024-04-17 09:12:29 +03:00
if (
!BUILD_CONFIG.isElectron &&
BUILD_CONFIG.debug &&
desktopWhiteList.every(path => !location.pathname.startsWith(path))
2024-04-17 09:12:29 +03:00
) {
document.body.innerHTML = `<h1 style="color:red;font-size:5rem;text-align:center;">Don't run electron entry in browser.</h1>`;
throw new Error('Wrong distribution');
}
2023-07-18 19:53:10 +03:00
const cache = createEmotionCache();
const future = {
v7_startTransition: true,
} as const;
2024-04-17 09:12:29 +03:00
const framework = new Framework();
configureCommonModules(framework);
2024-07-15 06:21:08 +03:00
configureElectronStateStorageImpls(framework);
2024-04-17 09:12:29 +03:00
configureBrowserWorkspaceFlavours(framework);
configureSqliteWorkspaceEngineStorageProvider(framework);
2024-09-11 10:55:37 +03:00
configureSqliteUserspaceStorageProvider(framework);
configureDesktopWorkbenchModule(framework);
configureAppTabsHeaderModule(framework);
framework.impl(PopupWindowProvider, {
open: (url: string) => {
apis?.ui.openExternal(url).catch(e => {
console.error('Failed to open external URL', e);
});
},
});
framework.impl(ClientSchemaProvider, {
getClientSchema() {
return appInfo?.schema;
},
});
2024-04-17 09:12:29 +03:00
const frameworkProvider = framework.provider();
// setup application lifecycle events, and emit application start event
window.addEventListener('focus', () => {
frameworkProvider.get(LifecycleService).applicationFocus();
});
frameworkProvider.get(LifecycleService).applicationStart();
export function App() {
2023-07-18 19:53:10 +03:00
return (
<Suspense>
2024-04-17 09:12:29 +03:00
<FrameworkRoot framework={frameworkProvider}>
<CacheProvider value={cache}>
<I18nProvider>
<AffineContext store={getCurrentStore()}>
<Telemetry />
<CustomThemeModifier />
<GlobalLoading />
<RouterProvider
fallbackElement={<AppFallback />}
router={router}
future={future}
/>
{environment.isWindows && (
<div style={{ position: 'fixed', right: 0, top: 0, zIndex: 5 }}>
<WindowsAppControls />
</div>
)}
</AffineContext>
</I18nProvider>
</CacheProvider>
2024-04-17 09:12:29 +03:00
</FrameworkRoot>
</Suspense>
2023-07-18 19:53:10 +03:00
);
}