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

198 lines
6.1 KiB
TypeScript
Raw Normal View History

import { AffineContext } from '@affine/core/components/context';
import { WindowsAppControls } from '@affine/core/components/pure/header/windows-app-controls';
import { AppContainer } from '@affine/core/desktop/components/app-container';
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';
2024-10-28 21:06:52 +03:00
import { ValidatorProvider } from '@affine/core/modules/cloud';
import {
configureDesktopApiModule,
DesktopApiService,
} from '@affine/core/modules/desktop-api';
import { GlobalDialogService } from '@affine/core/modules/dialogs';
import {
configureSpellCheckSettingModule,
EditorSettingService,
} from '@affine/core/modules/editor-setting';
import { configureFindInPageModule } from '@affine/core/modules/find-in-page';
import { I18nProvider } from '@affine/core/modules/i18n';
2024-07-15 06:21:08 +03:00
import { configureElectronStateStorageImpls } from '@affine/core/modules/storage';
import {
ClientSchemeProvider,
PopupWindowProvider,
} from '@affine/core/modules/url';
2024-09-11 10:55:37 +03:00
import { configureSqliteUserspaceStorageProvider } from '@affine/core/modules/userspace';
import {
configureDesktopWorkbenchModule,
WorkbenchService,
} 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, events } 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 {
DocsService,
2024-04-17 09:12:29 +03:00
Framework,
FrameworkRoot,
getCurrentStore,
GlobalContextService,
2024-04-17 09:12:29 +03:00
LifecycleService,
WorkspacesService,
2024-04-17 09:12:29 +03:00
} from '@toeverything/infra';
import { Suspense } from 'react';
import { RouterProvider } from 'react-router-dom';
2023-07-18 19:53:10 +03:00
import { DesktopThemeSync } from './theme-sync';
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);
configureFindInPageModule(framework);
configureDesktopApiModule(framework);
configureSpellCheckSettingModule(framework);
framework.impl(PopupWindowProvider, p => {
const apis = p.get(DesktopApiService).api;
return {
open: (url: string) => {
apis.handler.ui.openExternal(url).catch(e => {
console.error('Failed to open external URL', e);
});
},
};
});
framework.impl(ClientSchemeProvider, p => {
const appInfo = p.get(DesktopApiService).appInfo;
return {
getClientScheme() {
return appInfo?.scheme;
},
};
});
framework.impl(ValidatorProvider, p => {
const apis = p.get(DesktopApiService).api;
return {
async validate(_challenge, resource) {
const token = await apis.handler.ui.getChallengeResponse(resource);
if (!token) {
throw new Error('Challenge failed');
}
return token;
},
};
2024-10-28 21:06:52 +03:00
});
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();
window.addEventListener('unload', () => {
frameworkProvider
.get(DesktopApiService)
.api.handler.ui.pingAppLayoutReady(false)
.catch(console.error);
});
events?.applicationMenu.openAboutPageInSettingModal(() =>
frameworkProvider.get(GlobalDialogService).open('setting', {
activeTab: 'about',
})
);
events?.applicationMenu.onNewPageAction(() => {
const currentWorkspaceId = frameworkProvider
.get(GlobalContextService)
.globalContext.workspaceId.get();
const workspacesService = frameworkProvider.get(WorkspacesService);
const workspaceMetadata = currentWorkspaceId
? workspacesService.list.workspace$(currentWorkspaceId).value
: null;
const workspaceRef =
workspaceMetadata &&
workspacesService.open({ metadata: workspaceMetadata });
if (!workspaceRef) {
return;
}
const { workspace, dispose } = workspaceRef;
const editorSettingService = frameworkProvider.get(EditorSettingService);
const docsService = workspace.scope.get(DocsService);
const editorSetting = editorSettingService.editorSetting;
const docProps = {
note: editorSetting.get('affine:note'),
};
apis?.ui
.isActiveTab()
.then(isActive => {
if (!isActive) {
return;
}
const page = docsService.createDoc({ docProps });
workspace.scope.get(WorkbenchService).workbench.openDoc(page.id);
})
.catch(err => {
console.error(err);
});
dispose();
});
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()}>
<DesktopThemeSync />
<RouterProvider
fallbackElement={<AppContainer fallback />}
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
);
}