perf: getEnvironment() -> env (#2636)
This commit is contained in:
@@ -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
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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]);
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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 () => {};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 />;
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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 />}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
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';
|
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])) {
|
||||||
|
|||||||
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;
|
export type Environment = Browser | Server | Desktop;
|
||||||
|
|
||||||
let environment: Environment | null = null;
|
export const env: Environment = (()=>{
|
||||||
|
let environment = null
|
||||||
export function getEnvironment() {
|
const isDebug = process.env.NODE_ENV === 'development';
|
||||||
if (environment) {
|
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
||||||
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);
|
|
||||||
environment = {
|
environment = {
|
||||||
...environment,
|
isDesktop: false,
|
||||||
isSafari: false,
|
isBrowser: false,
|
||||||
isFireFox: false,
|
isServer: true,
|
||||||
isChrome: true,
|
isDebug,
|
||||||
chromeVersion: uaHelper.getChromeVersion(),
|
} satisfies Server;
|
||||||
} satisfies ChromeBrowser;
|
} 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;
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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(
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user