mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
perf: getEnvironment() -> env (#2636)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { getEnvironment } from '@affine/env/config';
|
||||
import { env } from '@affine/env/config';
|
||||
import { atomWithObservable, atomWithStorage } from 'jotai/utils';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
@@ -21,9 +21,8 @@ function rpcToObservable<
|
||||
) {
|
||||
return new Observable<T>(subscriber => {
|
||||
subscriber.next(initialValue);
|
||||
const environment = getEnvironment();
|
||||
onSubscribe?.();
|
||||
if (typeof window === 'undefined' || !environment.isDesktop || !event) {
|
||||
if (typeof window === 'undefined' || !env.isDesktop || !event) {
|
||||
subscriber.complete();
|
||||
return () => {};
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getEnvironment } from '@affine/env';
|
||||
import { env } from '@affine/env';
|
||||
import { Skeleton } from '@mui/material';
|
||||
import { assignInlineVars } from '@vanilla-extract/dynamic';
|
||||
import { useAtom, useAtomValue } from 'jotai';
|
||||
@@ -79,8 +79,7 @@ export function AppSidebar(props: AppSidebarProps): ReactElement {
|
||||
// disable animation to avoid UI flash
|
||||
const enableAnimation = useEnableAnimation();
|
||||
|
||||
const environment = getEnvironment();
|
||||
const isMacosDesktop = environment.isDesktop && environment.isMacOs;
|
||||
const isMacosDesktop = env.isDesktop && env.isMacOs;
|
||||
if (initialRender) {
|
||||
// avoid the UI flash
|
||||
return <div />;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getEnvironment } from '@affine/env/config';
|
||||
import { env } from '@affine/env/config';
|
||||
import { useAFFiNEI18N } from '@affine/i18n/hooks';
|
||||
import { SearchIcon } from '@blocksuite/icons';
|
||||
import clsx from 'clsx';
|
||||
@@ -13,8 +13,7 @@ interface QuickSearchInputProps extends React.HTMLAttributes<HTMLDivElement> {
|
||||
// Although it is called an input, it is actually a button.
|
||||
export function QuickSearchInput({ onClick, ...props }: QuickSearchInputProps) {
|
||||
const t = useAFFiNEI18N();
|
||||
const environment = getEnvironment();
|
||||
const isMac = environment.isBrowser && environment.isMacOs;
|
||||
const isMac = env.isBrowser && env.isMacOs;
|
||||
|
||||
return (
|
||||
<div
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getEnvironment } from '@affine/env/config';
|
||||
import { env } from '@affine/env/config';
|
||||
import { ArrowLeftSmallIcon, ArrowRightSmallIcon } from '@blocksuite/icons';
|
||||
import { useAtomValue } from 'jotai';
|
||||
|
||||
@@ -18,12 +18,11 @@ export type SidebarHeaderProps = {
|
||||
|
||||
export const SidebarHeader = (props: SidebarHeaderProps) => {
|
||||
const open = useAtomValue(appSidebarOpenAtom);
|
||||
const environment = getEnvironment();
|
||||
return (
|
||||
<div className={navHeaderStyle} data-open={open}>
|
||||
{environment.isDesktop && (
|
||||
{env.isDesktop && (
|
||||
<>
|
||||
{environment.isMacOs && <div style={{ flex: 1 }} />}
|
||||
{env.isMacOs && <div style={{ flex: 1 }} />}
|
||||
<IconButton
|
||||
size="middle"
|
||||
data-testid="app-sidebar-arrow-button-back"
|
||||
@@ -51,7 +50,7 @@ export const SidebarHeader = (props: SidebarHeaderProps) => {
|
||||
<ArrowRightSmallIcon />
|
||||
</IconButton>
|
||||
|
||||
{!environment.isMacOs && <div style={{ flex: 1 }} />}
|
||||
{!env.isMacOs && <div style={{ flex: 1 }} />}
|
||||
</>
|
||||
)}
|
||||
{open && <SidebarSwitch />}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { getEnvironment } from '@affine/env';
|
||||
import { env } from '@affine/env';
|
||||
import debug from 'debug';
|
||||
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
||||
|
||||
const env = getEnvironment();
|
||||
|
||||
const SESSION_KEY = 'affine:debug';
|
||||
const development = env.isDebug;
|
||||
|
||||
4
packages/env/src/api.ts
vendored
4
packages/env/src/api.ts
vendored
@@ -1,8 +1,8 @@
|
||||
import { config, getEnvironment } from './config';
|
||||
import { config, env } from './config';
|
||||
import { isValidIPAddress } from './is-valid-ip-address';
|
||||
|
||||
let prefixUrl = '/';
|
||||
if (typeof window === 'undefined' || getEnvironment().isDesktop) {
|
||||
if (typeof window === 'undefined' || env.isDesktop) {
|
||||
// SSR or Desktop
|
||||
const serverAPI = config.serverAPI;
|
||||
if (isValidIPAddress(serverAPI.split(':')[0])) {
|
||||
|
||||
93
packages/env/src/config.ts
vendored
93
packages/env/src/config.ts
vendored
@@ -106,54 +106,51 @@ interface Desktop extends ChromeBrowser {
|
||||
|
||||
export type Environment = Browser | Server | Desktop;
|
||||
|
||||
let environment: Environment | null = null;
|
||||
|
||||
export function getEnvironment() {
|
||||
if (environment) {
|
||||
return environment;
|
||||
}
|
||||
const isDebug = process.env.NODE_ENV === 'development';
|
||||
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
||||
environment = {
|
||||
isDesktop: false,
|
||||
isBrowser: false,
|
||||
isServer: true,
|
||||
isDebug,
|
||||
} satisfies Server;
|
||||
} else {
|
||||
const uaHelper = new UaHelper(navigator);
|
||||
|
||||
environment = {
|
||||
origin: window.location.origin,
|
||||
isDesktop: !!window.appInfo?.electron,
|
||||
isBrowser: true,
|
||||
isServer: false,
|
||||
isDebug,
|
||||
isLinux: uaHelper.isLinux,
|
||||
isMacOs: uaHelper.isMacOs,
|
||||
isSafari: uaHelper.isSafari,
|
||||
isWindows: uaHelper.isWindows,
|
||||
isFireFox: uaHelper.isFireFox,
|
||||
isMobile: uaHelper.isMobile,
|
||||
isChrome: uaHelper.isChrome,
|
||||
isIOS: uaHelper.isIOS,
|
||||
} as Browser;
|
||||
// Chrome on iOS is still Safari
|
||||
if (environment.isChrome && !environment.isIOS) {
|
||||
assertEquals(environment.isSafari, false);
|
||||
assertEquals(environment.isFireFox, false);
|
||||
export const env: Environment = (()=>{
|
||||
let environment = null
|
||||
const isDebug = process.env.NODE_ENV === 'development';
|
||||
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
||||
environment = {
|
||||
...environment,
|
||||
isSafari: false,
|
||||
isFireFox: false,
|
||||
isChrome: true,
|
||||
chromeVersion: uaHelper.getChromeVersion(),
|
||||
} satisfies ChromeBrowser;
|
||||
isDesktop: false,
|
||||
isBrowser: false,
|
||||
isServer: true,
|
||||
isDebug,
|
||||
} satisfies Server;
|
||||
} else {
|
||||
const uaHelper = new UaHelper(navigator);
|
||||
|
||||
environment = {
|
||||
origin: window.location.origin,
|
||||
isDesktop: !!window.appInfo?.electron,
|
||||
isBrowser: true,
|
||||
isServer: false,
|
||||
isDebug,
|
||||
isLinux: uaHelper.isLinux,
|
||||
isMacOs: uaHelper.isMacOs,
|
||||
isSafari: uaHelper.isSafari,
|
||||
isWindows: uaHelper.isWindows,
|
||||
isFireFox: uaHelper.isFireFox,
|
||||
isMobile: uaHelper.isMobile,
|
||||
isChrome: uaHelper.isChrome,
|
||||
isIOS: uaHelper.isIOS,
|
||||
} as Browser;
|
||||
// Chrome on iOS is still Safari
|
||||
if (environment.isChrome && !environment.isIOS) {
|
||||
assertEquals(environment.isSafari, false);
|
||||
assertEquals(environment.isFireFox, false);
|
||||
environment = {
|
||||
...environment,
|
||||
isSafari: false,
|
||||
isFireFox: false,
|
||||
isChrome: true,
|
||||
chromeVersion: uaHelper.getChromeVersion(),
|
||||
} satisfies ChromeBrowser;
|
||||
}
|
||||
}
|
||||
}
|
||||
globalThis.environment = environment;
|
||||
return environment;
|
||||
}
|
||||
globalThis.environment = environment;
|
||||
return environment;
|
||||
})();
|
||||
|
||||
|
||||
function printBuildInfo() {
|
||||
console.group('Build info');
|
||||
@@ -185,8 +182,8 @@ export function setupGlobal() {
|
||||
if (globalThis.$AFFINE_SETUP) {
|
||||
return;
|
||||
}
|
||||
globalThis.environment = getEnvironment();
|
||||
if (getEnvironment().isBrowser) {
|
||||
globalThis.environment = env;
|
||||
if (env.isBrowser) {
|
||||
printBuildInfo();
|
||||
globalThis.editorVersion = config.editorVersion;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { getEnvironment } from '@affine/env';
|
||||
import { env } from '@affine/env';
|
||||
import type { EditorContainer } from '@blocksuite/editor';
|
||||
import { atom } from 'jotai';
|
||||
|
||||
export const lottieAtom = atom(import('lottie-web').then(m => m.default));
|
||||
|
||||
export const editorContainerModuleAtom = atom<Promise<typeof EditorContainer>>(
|
||||
getEnvironment().isServer
|
||||
env.isServer
|
||||
? async () =>
|
||||
import('@blocksuite/editor').then(module => module.EditorContainer)
|
||||
: (import('@blocksuite/editor').then(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { DebugLogger } from '@affine/debug';
|
||||
import { getEnvironment } from '@affine/env';
|
||||
import { env } from '@affine/env';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
import { Slot } from '@blocksuite/store';
|
||||
import { initializeApp } from 'firebase/app';
|
||||
@@ -165,7 +165,6 @@ export function createAffineAuth(prefix = '/') {
|
||||
method: SignMethod
|
||||
): Promise<LoginResponse | null> => {
|
||||
const auth = getAuth();
|
||||
const environment = getEnvironment();
|
||||
if (!auth) {
|
||||
throw new Error('Failed to initialize firebase');
|
||||
}
|
||||
@@ -189,7 +188,7 @@ export function createAffineAuth(prefix = '/') {
|
||||
}
|
||||
try {
|
||||
let idToken: string | undefined;
|
||||
if (environment.isDesktop) {
|
||||
if (env.isDesktop) {
|
||||
idToken = await signInWithElectron(auth);
|
||||
} else {
|
||||
const response = await signInWithPopup(auth, provider);
|
||||
|
||||
Reference in New Issue
Block a user