perf: getEnvironment() -> env (#2636)

This commit is contained in:
Simon He
2023-06-01 11:23:38 +08:00
committed by GitHub
parent e1314730be
commit fc9462eee9
16 changed files with 70 additions and 86 deletions

View File

@@ -1,12 +1,11 @@
import { DownloadTips } from '@affine/component/affine-banner'; import { DownloadTips } from '@affine/component/affine-banner';
import { getEnvironment } from '@affine/env'; import { env } from '@affine/env';
import { useAtom } from 'jotai'; import { useAtom } from 'jotai';
import { useCallback } from 'react'; import { useCallback } from 'react';
import { guideDownloadClientTipAtom } from '../../../atoms/guide'; import { guideDownloadClientTipAtom } from '../../../atoms/guide';
export const DownloadClientTip = () => { export const DownloadClientTip = () => {
const env = getEnvironment();
const [showDownloadClientTips, setShowDownloadClientTips] = useAtom( const [showDownloadClientTips, setShowDownloadClientTips] = useAtom(
guideDownloadClientTipAtom guideDownloadClientTipAtom
); );

View File

@@ -1,4 +1,4 @@
import { getEnvironment } from '@affine/env'; import { env } from '@affine/env';
import { Trans } from '@affine/i18n'; import { Trans } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks'; import { useAFFiNEI18N } from '@affine/i18n/hooks';
import type React from 'react'; import type React from 'react';
@@ -7,7 +7,6 @@ import { useEffect, useState } from 'react';
const minimumChromeVersion = 102; const minimumChromeVersion = 102;
export const shouldShowWarning = () => { export const shouldShowWarning = () => {
const env = getEnvironment();
if (env.isDesktop) { if (env.isDesktop) {
// even though desktop have compatibility issues, we don't want to show the warning // even though desktop have compatibility issues, we don't want to show the warning
return false; return false;
@@ -28,7 +27,6 @@ export const OSWarningMessage: React.FC = () => {
const [notChrome, setNotChrome] = useState(false); const [notChrome, setNotChrome] = useState(false);
const [notGoodVersion, setNotGoodVersion] = useState(false); const [notGoodVersion, setNotGoodVersion] = useState(false);
useEffect(() => { useEffect(() => {
const env = getEnvironment();
setNotChrome(env.isBrowser && !env.isChrome); setNotChrome(env.isBrowser && !env.isChrome);
setNotGoodVersion( setNotGoodVersion(
env.isBrowser && env.isChrome && env.chromeVersion < minimumChromeVersion env.isBrowser && env.isChrome && env.chromeVersion < minimumChromeVersion

View File

@@ -1,5 +1,5 @@
import { MuiFade, Tooltip } from '@affine/component'; 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 { useAFFiNEI18N } from '@affine/i18n/hooks';
import { CloseIcon, NewIcon, UserGuideIcon } from '@blocksuite/icons'; import { CloseIcon, NewIcon, UserGuideIcon } from '@blocksuite/icons';
import { useAtom } from 'jotai'; import { useAtom } from 'jotai';
@@ -15,7 +15,6 @@ import {
StyledIsland, StyledIsland,
StyledTriggerWrapper, StyledTriggerWrapper,
} from './style'; } from './style';
const env = getEnvironment();
const ContactModal = lazy(() => const ContactModal = lazy(() =>
import('@affine/component/contact-modal').then(({ ContactModal }) => ({ import('@affine/component/contact-modal').then(({ ContactModal }) => ({
default: ContactModal, default: ContactModal,

View File

@@ -1,5 +1,5 @@
import { Modal, ModalWrapper } from '@affine/component'; import { Modal, ModalWrapper } from '@affine/component';
import { getEnvironment } from '@affine/env'; import { env } from '@affine/env';
import { useAFFiNEI18N } from '@affine/i18n/hooks'; import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { Command } from 'cmdk'; import { Command } from 'cmdk';
import type { NextRouter } from 'next/router'; import type { NextRouter } from 'next/router';
@@ -27,7 +27,6 @@ import {
} from './style'; } from './style';
const isMac = () => { const isMac = () => {
const env = getEnvironment();
return env.isBrowser && env.isMacOs; return env.isBrowser && env.isMacOs;
}; };

View File

@@ -3,7 +3,7 @@ import {
MuiClickAwayListener, MuiClickAwayListener,
MuiSlide, MuiSlide,
} from '@affine/component'; } from '@affine/component';
import { getEnvironment } from '@affine/env'; import { env } from '@affine/env';
import { useAFFiNEI18N } from '@affine/i18n/hooks'; import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
@@ -27,7 +27,6 @@ type ModalProps = {
}; };
const checkIsMac = () => { const checkIsMac = () => {
const env = getEnvironment();
return env.isBrowser && env.isMacOs; return env.isBrowser && env.isMacOs;
}; };

View File

@@ -1,4 +1,4 @@
import { getEnvironment } from '@affine/env'; import { env } from '@affine/env';
import { import {
rootCurrentWorkspaceIdAtom, rootCurrentWorkspaceIdAtom,
rootWorkspacesMetadataAtom, rootWorkspacesMetadataAtom,
@@ -56,7 +56,6 @@ export function CurrentWorkspaceModals() {
openOnboardingModalAtom openOnboardingModalAtom
); );
const env = getEnvironment();
const onCloseOnboardingModal = useCallback(() => { const onCloseOnboardingModal = useCallback(() => {
setOpenOnboardingModal(false); setOpenOnboardingModal(false);
}, [setOpenOnboardingModal]); }, [setOpenOnboardingModal]);

View File

@@ -1,7 +1,7 @@
import { getEnvironment } from '@affine/env'; import { env } from '@affine/env';
import createCache from '@emotion/cache'; import createCache from '@emotion/cache';
const isBrowser = getEnvironment().isBrowser; const isBrowser = env.isBrowser;
export default function createEmotionCache() { export default function createEmotionCache() {
let insertionPoint; let insertionPoint;

View File

@@ -1,4 +1,4 @@
import { getEnvironment } from '@affine/env/config'; import { env } from '@affine/env/config';
import { atomWithObservable, atomWithStorage } from 'jotai/utils'; import { atomWithObservable, atomWithStorage } from 'jotai/utils';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
@@ -21,9 +21,8 @@ function rpcToObservable<
) { ) {
return new Observable<T>(subscriber => { return new Observable<T>(subscriber => {
subscriber.next(initialValue); subscriber.next(initialValue);
const environment = getEnvironment();
onSubscribe?.(); onSubscribe?.();
if (typeof window === 'undefined' || !environment.isDesktop || !event) { if (typeof window === 'undefined' || !env.isDesktop || !event) {
subscriber.complete(); subscriber.complete();
return () => {}; return () => {};
} }

View File

@@ -1,4 +1,4 @@
import { getEnvironment } from '@affine/env'; import { env } from '@affine/env';
import { Skeleton } from '@mui/material'; import { Skeleton } from '@mui/material';
import { assignInlineVars } from '@vanilla-extract/dynamic'; import { assignInlineVars } from '@vanilla-extract/dynamic';
import { useAtom, useAtomValue } from 'jotai'; import { useAtom, useAtomValue } from 'jotai';
@@ -79,8 +79,7 @@ export function AppSidebar(props: AppSidebarProps): ReactElement {
// disable animation to avoid UI flash // disable animation to avoid UI flash
const enableAnimation = useEnableAnimation(); const enableAnimation = useEnableAnimation();
const environment = getEnvironment(); const isMacosDesktop = env.isDesktop && env.isMacOs;
const isMacosDesktop = environment.isDesktop && environment.isMacOs;
if (initialRender) { if (initialRender) {
// avoid the UI flash // avoid the UI flash
return <div />; return <div />;

View File

@@ -1,4 +1,4 @@
import { getEnvironment } from '@affine/env/config'; import { env } from '@affine/env/config';
import { useAFFiNEI18N } from '@affine/i18n/hooks'; import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { SearchIcon } from '@blocksuite/icons'; import { SearchIcon } from '@blocksuite/icons';
import clsx from 'clsx'; 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. // Although it is called an input, it is actually a button.
export function QuickSearchInput({ onClick, ...props }: QuickSearchInputProps) { export function QuickSearchInput({ onClick, ...props }: QuickSearchInputProps) {
const t = useAFFiNEI18N(); const t = useAFFiNEI18N();
const environment = getEnvironment(); const isMac = env.isBrowser && env.isMacOs;
const isMac = environment.isBrowser && environment.isMacOs;
return ( return (
<div <div

View File

@@ -1,4 +1,4 @@
import { getEnvironment } from '@affine/env/config'; import { env } from '@affine/env/config';
import { ArrowLeftSmallIcon, ArrowRightSmallIcon } from '@blocksuite/icons'; import { ArrowLeftSmallIcon, ArrowRightSmallIcon } from '@blocksuite/icons';
import { useAtomValue } from 'jotai'; import { useAtomValue } from 'jotai';
@@ -18,12 +18,11 @@ export type SidebarHeaderProps = {
export const SidebarHeader = (props: SidebarHeaderProps) => { export const SidebarHeader = (props: SidebarHeaderProps) => {
const open = useAtomValue(appSidebarOpenAtom); const open = useAtomValue(appSidebarOpenAtom);
const environment = getEnvironment();
return ( return (
<div className={navHeaderStyle} data-open={open}> <div className={navHeaderStyle} data-open={open}>
{environment.isDesktop && ( {env.isDesktop && (
<> <>
{environment.isMacOs && <div style={{ flex: 1 }} />} {env.isMacOs && <div style={{ flex: 1 }} />}
<IconButton <IconButton
size="middle" size="middle"
data-testid="app-sidebar-arrow-button-back" data-testid="app-sidebar-arrow-button-back"
@@ -51,7 +50,7 @@ export const SidebarHeader = (props: SidebarHeaderProps) => {
<ArrowRightSmallIcon /> <ArrowRightSmallIcon />
</IconButton> </IconButton>
{!environment.isMacOs && <div style={{ flex: 1 }} />} {!env.isMacOs && <div style={{ flex: 1 }} />}
</> </>
)} )}
{open && <SidebarSwitch />} {open && <SidebarSwitch />}

View File

@@ -1,8 +1,7 @@
import { getEnvironment } from '@affine/env'; import { env } from '@affine/env';
import debug from 'debug'; import debug from 'debug';
type LogLevel = 'debug' | 'info' | 'warn' | 'error'; type LogLevel = 'debug' | 'info' | 'warn' | 'error';
const env = getEnvironment();
const SESSION_KEY = 'affine:debug'; const SESSION_KEY = 'affine:debug';
const development = env.isDebug; const development = env.isDebug;

View File

@@ -1,8 +1,8 @@
import { config, getEnvironment } from './config'; import { config, env } from './config';
import { isValidIPAddress } from './is-valid-ip-address'; import { isValidIPAddress } from './is-valid-ip-address';
let prefixUrl = '/'; let prefixUrl = '/';
if (typeof window === 'undefined' || getEnvironment().isDesktop) { if (typeof window === 'undefined' || env.isDesktop) {
// SSR or Desktop // SSR or Desktop
const serverAPI = config.serverAPI; const serverAPI = config.serverAPI;
if (isValidIPAddress(serverAPI.split(':')[0])) { if (isValidIPAddress(serverAPI.split(':')[0])) {

View File

@@ -106,12 +106,8 @@ interface Desktop extends ChromeBrowser {
export type Environment = Browser | Server | Desktop; export type Environment = Browser | Server | Desktop;
let environment: Environment | null = null; export const env: Environment = (()=>{
let environment = null
export function getEnvironment() {
if (environment) {
return environment;
}
const isDebug = process.env.NODE_ENV === 'development'; const isDebug = process.env.NODE_ENV === 'development';
if (typeof window === 'undefined' || typeof navigator === 'undefined') { if (typeof window === 'undefined' || typeof navigator === 'undefined') {
environment = { environment = {
@@ -153,7 +149,8 @@ export function getEnvironment() {
} }
globalThis.environment = environment; globalThis.environment = environment;
return environment; return environment;
} })();
function printBuildInfo() { function printBuildInfo() {
console.group('Build info'); console.group('Build info');
@@ -185,8 +182,8 @@ export function setupGlobal() {
if (globalThis.$AFFINE_SETUP) { if (globalThis.$AFFINE_SETUP) {
return; return;
} }
globalThis.environment = getEnvironment(); globalThis.environment = env;
if (getEnvironment().isBrowser) { if (env.isBrowser) {
printBuildInfo(); printBuildInfo();
globalThis.editorVersion = config.editorVersion; globalThis.editorVersion = config.editorVersion;
} }

View File

@@ -1,11 +1,11 @@
import { getEnvironment } from '@affine/env'; import { env } from '@affine/env';
import type { EditorContainer } from '@blocksuite/editor'; import type { EditorContainer } from '@blocksuite/editor';
import { atom } from 'jotai'; import { atom } from 'jotai';
export const lottieAtom = atom(import('lottie-web').then(m => m.default)); export const lottieAtom = atom(import('lottie-web').then(m => m.default));
export const editorContainerModuleAtom = atom<Promise<typeof EditorContainer>>( export const editorContainerModuleAtom = atom<Promise<typeof EditorContainer>>(
getEnvironment().isServer env.isServer
? async () => ? async () =>
import('@blocksuite/editor').then(module => module.EditorContainer) import('@blocksuite/editor').then(module => module.EditorContainer)
: (import('@blocksuite/editor').then( : (import('@blocksuite/editor').then(

View File

@@ -1,5 +1,5 @@
import { DebugLogger } from '@affine/debug'; import { DebugLogger } from '@affine/debug';
import { getEnvironment } from '@affine/env'; import { env } from '@affine/env';
import { assertExists } from '@blocksuite/global/utils'; import { assertExists } from '@blocksuite/global/utils';
import { Slot } from '@blocksuite/store'; import { Slot } from '@blocksuite/store';
import { initializeApp } from 'firebase/app'; import { initializeApp } from 'firebase/app';
@@ -165,7 +165,6 @@ export function createAffineAuth(prefix = '/') {
method: SignMethod method: SignMethod
): Promise<LoginResponse | null> => { ): Promise<LoginResponse | null> => {
const auth = getAuth(); const auth = getAuth();
const environment = getEnvironment();
if (!auth) { if (!auth) {
throw new Error('Failed to initialize firebase'); throw new Error('Failed to initialize firebase');
} }
@@ -189,7 +188,7 @@ export function createAffineAuth(prefix = '/') {
} }
try { try {
let idToken: string | undefined; let idToken: string | undefined;
if (environment.isDesktop) { if (env.isDesktop) {
idToken = await signInWithElectron(auth); idToken = await signInWithElectron(auth);
} else { } else {
const response = await signInWithPopup(auth, provider); const response = await signInWithPopup(auth, provider);