mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-12 23:56:36 +08:00
perf: getEnvironment() -> env (#2636)
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import { DownloadTips } from '@affine/component/affine-banner';
|
||||
import { getEnvironment } from '@affine/env';
|
||||
import { env } from '@affine/env';
|
||||
import { useAtom } from 'jotai';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import { guideDownloadClientTipAtom } from '../../../atoms/guide';
|
||||
|
||||
export const DownloadClientTip = () => {
|
||||
const env = getEnvironment();
|
||||
const [showDownloadClientTips, setShowDownloadClientTips] = useAtom(
|
||||
guideDownloadClientTipAtom
|
||||
);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getEnvironment } from '@affine/env';
|
||||
import { env } from '@affine/env';
|
||||
import { Trans } from '@affine/i18n';
|
||||
import { useAFFiNEI18N } from '@affine/i18n/hooks';
|
||||
import type React from 'react';
|
||||
@@ -7,7 +7,6 @@ import { useEffect, useState } from 'react';
|
||||
const minimumChromeVersion = 102;
|
||||
|
||||
export const shouldShowWarning = () => {
|
||||
const env = getEnvironment();
|
||||
if (env.isDesktop) {
|
||||
// even though desktop have compatibility issues, we don't want to show the warning
|
||||
return false;
|
||||
@@ -28,7 +27,6 @@ export const OSWarningMessage: React.FC = () => {
|
||||
const [notChrome, setNotChrome] = useState(false);
|
||||
const [notGoodVersion, setNotGoodVersion] = useState(false);
|
||||
useEffect(() => {
|
||||
const env = getEnvironment();
|
||||
setNotChrome(env.isBrowser && !env.isChrome);
|
||||
setNotGoodVersion(
|
||||
env.isBrowser && env.isChrome && env.chromeVersion < minimumChromeVersion
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { MuiFade, Tooltip } from '@affine/component';
|
||||
import { config, getEnvironment } from '@affine/env';
|
||||
import { config, env } from '@affine/env';
|
||||
import { useAFFiNEI18N } from '@affine/i18n/hooks';
|
||||
import { CloseIcon, NewIcon, UserGuideIcon } from '@blocksuite/icons';
|
||||
import { useAtom } from 'jotai';
|
||||
@@ -15,7 +15,6 @@ import {
|
||||
StyledIsland,
|
||||
StyledTriggerWrapper,
|
||||
} from './style';
|
||||
const env = getEnvironment();
|
||||
const ContactModal = lazy(() =>
|
||||
import('@affine/component/contact-modal').then(({ ContactModal }) => ({
|
||||
default: ContactModal,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Modal, ModalWrapper } from '@affine/component';
|
||||
import { getEnvironment } from '@affine/env';
|
||||
import { env } from '@affine/env';
|
||||
import { useAFFiNEI18N } from '@affine/i18n/hooks';
|
||||
import { Command } from 'cmdk';
|
||||
import type { NextRouter } from 'next/router';
|
||||
@@ -27,7 +27,6 @@ import {
|
||||
} from './style';
|
||||
|
||||
const isMac = () => {
|
||||
const env = getEnvironment();
|
||||
return env.isBrowser && env.isMacOs;
|
||||
};
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
MuiClickAwayListener,
|
||||
MuiSlide,
|
||||
} from '@affine/component';
|
||||
import { getEnvironment } from '@affine/env';
|
||||
import { env } from '@affine/env';
|
||||
import { useAFFiNEI18N } from '@affine/i18n/hooks';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
@@ -27,7 +27,6 @@ type ModalProps = {
|
||||
};
|
||||
|
||||
const checkIsMac = () => {
|
||||
const env = getEnvironment();
|
||||
return env.isBrowser && env.isMacOs;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getEnvironment } from '@affine/env';
|
||||
import { env } from '@affine/env';
|
||||
import {
|
||||
rootCurrentWorkspaceIdAtom,
|
||||
rootWorkspacesMetadataAtom,
|
||||
@@ -56,7 +56,6 @@ export function CurrentWorkspaceModals() {
|
||||
openOnboardingModalAtom
|
||||
);
|
||||
|
||||
const env = getEnvironment();
|
||||
const onCloseOnboardingModal = useCallback(() => {
|
||||
setOpenOnboardingModal(false);
|
||||
}, [setOpenOnboardingModal]);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { getEnvironment } from '@affine/env';
|
||||
import { env } from '@affine/env';
|
||||
import createCache from '@emotion/cache';
|
||||
|
||||
const isBrowser = getEnvironment().isBrowser;
|
||||
const isBrowser = env.isBrowser;
|
||||
|
||||
export default function createEmotionCache() {
|
||||
let insertionPoint;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Vendored
+2
-2
@@ -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])) {
|
||||
|
||||
Vendored
+45
-48
@@ -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