mirror of
https://github.com/toeverything/AFFiNE.git
synced 2025-01-05 15:43:11 +03:00
feat: refactor the usage of toast (#1699)
This commit is contained in:
parent
66dec34209
commit
449ffbc73f
@ -1,11 +1,4 @@
|
|||||||
import {
|
import { Button, IconButton, Menu, MenuItem, Wrapper } from '@affine/component';
|
||||||
Button,
|
|
||||||
IconButton,
|
|
||||||
Menu,
|
|
||||||
MenuItem,
|
|
||||||
toast,
|
|
||||||
Wrapper,
|
|
||||||
} from '@affine/component';
|
|
||||||
import { PermissionType } from '@affine/datacenter';
|
import { PermissionType } from '@affine/datacenter';
|
||||||
import { useTranslation } from '@affine/i18n';
|
import { useTranslation } from '@affine/i18n';
|
||||||
import { WorkspaceFlavour } from '@affine/workspace/type';
|
import { WorkspaceFlavour } from '@affine/workspace/type';
|
||||||
@ -19,6 +12,7 @@ import { useCallback, useState } from 'react';
|
|||||||
|
|
||||||
import { useMembers } from '../../../../../hooks/affine/use-members';
|
import { useMembers } from '../../../../../hooks/affine/use-members';
|
||||||
import type { AffineWorkspace, LocalWorkspace } from '../../../../../shared';
|
import type { AffineWorkspace, LocalWorkspace } from '../../../../../shared';
|
||||||
|
import { toast } from '../../../../../utils';
|
||||||
import { Unreachable } from '../../../affine-error-eoundary';
|
import { Unreachable } from '../../../affine-error-eoundary';
|
||||||
import { TransformWorkspaceToAffineModal } from '../../../transform-workspace-to-affine-modal';
|
import { TransformWorkspaceToAffineModal } from '../../../transform-workspace-to-affine-modal';
|
||||||
import type { PanelProps } from '../../index';
|
import type { PanelProps } from '../../index';
|
||||||
|
@ -3,7 +3,6 @@ import {
|
|||||||
Content,
|
Content,
|
||||||
FlexWrapper,
|
FlexWrapper,
|
||||||
Input,
|
Input,
|
||||||
toast,
|
|
||||||
Wrapper,
|
Wrapper,
|
||||||
} from '@affine/component';
|
} from '@affine/component';
|
||||||
import { useTranslation } from '@affine/i18n';
|
import { useTranslation } from '@affine/i18n';
|
||||||
@ -18,6 +17,7 @@ import type {
|
|||||||
AffineWorkspace,
|
AffineWorkspace,
|
||||||
LocalWorkspace,
|
LocalWorkspace,
|
||||||
} from '../../../../../shared';
|
} from '../../../../../shared';
|
||||||
|
import { toast } from '../../../../../utils';
|
||||||
import { Unreachable } from '../../../affine-error-eoundary';
|
import { Unreachable } from '../../../affine-error-eoundary';
|
||||||
import { EnableAffineCloudModal } from '../../../enable-affine-cloud-modal';
|
import { EnableAffineCloudModal } from '../../../enable-affine-cloud-modal';
|
||||||
import type { WorkspaceSettingDetailProps } from '../../index';
|
import type { WorkspaceSettingDetailProps } from '../../index';
|
||||||
|
@ -6,7 +6,6 @@ import {
|
|||||||
MenuItem,
|
MenuItem,
|
||||||
Tooltip,
|
Tooltip,
|
||||||
} from '@affine/component';
|
} from '@affine/component';
|
||||||
import { toast } from '@affine/component';
|
|
||||||
import { useTranslation } from '@affine/i18n';
|
import { useTranslation } from '@affine/i18n';
|
||||||
import {
|
import {
|
||||||
DeletePermanentlyIcon,
|
DeletePermanentlyIcon,
|
||||||
@ -21,6 +20,8 @@ import type { PageMeta } from '@blocksuite/store';
|
|||||||
import type React from 'react';
|
import type React from 'react';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
import { toast } from '../../../../utils';
|
||||||
|
|
||||||
export type OperationCellProps = {
|
export type OperationCellProps = {
|
||||||
pageMeta: PageMeta;
|
pageMeta: PageMeta;
|
||||||
onOpenPageInNewTab: (pageId: string) => void;
|
onOpenPageInNewTab: (pageId: string) => void;
|
||||||
|
@ -5,7 +5,7 @@ import {
|
|||||||
TableHead,
|
TableHead,
|
||||||
TableRow,
|
TableRow,
|
||||||
} from '@affine/component';
|
} from '@affine/component';
|
||||||
import { Content, IconButton, toast, Tooltip } from '@affine/component';
|
import { Content, IconButton, Tooltip } from '@affine/component';
|
||||||
import { useTranslation } from '@affine/i18n';
|
import { useTranslation } from '@affine/i18n';
|
||||||
import {
|
import {
|
||||||
EdgelessIcon,
|
EdgelessIcon,
|
||||||
@ -29,6 +29,7 @@ import {
|
|||||||
usePageMetaHelper,
|
usePageMetaHelper,
|
||||||
} from '../../../../hooks/use-page-meta';
|
} from '../../../../hooks/use-page-meta';
|
||||||
import type { BlockSuiteWorkspace } from '../../../../shared';
|
import type { BlockSuiteWorkspace } from '../../../../shared';
|
||||||
|
import { toast } from '../../../../utils';
|
||||||
import DateCell from './DateCell';
|
import DateCell from './DateCell';
|
||||||
import Empty from './Empty';
|
import Empty from './Empty';
|
||||||
import { OperationCell, TrashOperationCell } from './OperationCell';
|
import { OperationCell, TrashOperationCell } from './OperationCell';
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import { toast } from '@affine/component';
|
|
||||||
import { assertExists } from '@blocksuite/store';
|
import { assertExists } from '@blocksuite/store';
|
||||||
import { useAtomValue, useSetAtom } from 'jotai';
|
import { useAtomValue, useSetAtom } from 'jotai';
|
||||||
import type { CSSProperties } from 'react';
|
import type { CSSProperties } from 'react';
|
||||||
@ -6,6 +5,7 @@ import type { CSSProperties } from 'react';
|
|||||||
import { workspacePreferredModeAtom } from '../../../../atoms';
|
import { workspacePreferredModeAtom } from '../../../../atoms';
|
||||||
import { usePageMeta } from '../../../../hooks/use-page-meta';
|
import { usePageMeta } from '../../../../hooks/use-page-meta';
|
||||||
import type { BlockSuiteWorkspace } from '../../../../shared';
|
import type { BlockSuiteWorkspace } from '../../../../shared';
|
||||||
|
import { toast } from '../../../../utils';
|
||||||
import { StyledEditorModeSwitch } from './style';
|
import { StyledEditorModeSwitch } from './style';
|
||||||
import { EdgelessSwitchItem, PageSwitchItem } from './switch-items';
|
import { EdgelessSwitchItem, PageSwitchItem } from './switch-items';
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
// fixme(himself65): refactor this file
|
// fixme(himself65): refactor this file
|
||||||
import { Confirm, FlexWrapper, Menu, MenuItem } from '@affine/component';
|
import { Confirm, FlexWrapper, Menu, MenuItem } from '@affine/component';
|
||||||
import { IconButton } from '@affine/component';
|
import { IconButton } from '@affine/component';
|
||||||
import { toast } from '@affine/component';
|
|
||||||
import { useTranslation } from '@affine/i18n';
|
import { useTranslation } from '@affine/i18n';
|
||||||
import {
|
import {
|
||||||
DeleteTemporarilyIcon,
|
DeleteTemporarilyIcon,
|
||||||
@ -25,6 +24,7 @@ import {
|
|||||||
usePageMeta,
|
usePageMeta,
|
||||||
usePageMetaHelper,
|
usePageMetaHelper,
|
||||||
} from '../../../../hooks/use-page-meta';
|
} from '../../../../hooks/use-page-meta';
|
||||||
|
import { toast } from '../../../../utils';
|
||||||
|
|
||||||
export const EditorOptionMenu = () => {
|
export const EditorOptionMenu = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import { toast } from '@affine/component';
|
|
||||||
import { MessageCode } from '@affine/datacenter';
|
import { MessageCode } from '@affine/datacenter';
|
||||||
import { messages } from '@affine/datacenter';
|
import { messages } from '@affine/datacenter';
|
||||||
import type React from 'react';
|
import type React from 'react';
|
||||||
@ -6,6 +5,7 @@ import { memo, useEffect, useState } from 'react';
|
|||||||
|
|
||||||
import { useAffineLogOut } from '../../../hooks/affine/use-affine-log-out';
|
import { useAffineLogOut } from '../../../hooks/affine/use-affine-log-out';
|
||||||
import { apis } from '../../../shared/apis';
|
import { apis } from '../../../shared/apis';
|
||||||
|
import { toast } from '../../../utils';
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface DocumentEventMap {
|
interface DocumentEventMap {
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import { toast } from '@affine/component';
|
|
||||||
import {
|
import {
|
||||||
createAffineAuth,
|
createAffineAuth,
|
||||||
setLoginStorage,
|
setLoginStorage,
|
||||||
@ -8,6 +7,7 @@ import { useRouter } from 'next/router';
|
|||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
|
|
||||||
import { apis } from '../../shared/apis';
|
import { apis } from '../../shared/apis';
|
||||||
|
import { toast } from '../../utils';
|
||||||
|
|
||||||
export const affineAuth = createAffineAuth();
|
export const affineAuth = createAffineAuth();
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Button, toast } from '@affine/component';
|
import { Button } from '@affine/component';
|
||||||
import { DebugLogger } from '@affine/debug';
|
import { DebugLogger } from '@affine/debug';
|
||||||
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
|
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
|
||||||
import { nanoid } from '@blocksuite/store';
|
import { nanoid } from '@blocksuite/store';
|
||||||
@ -10,6 +10,7 @@ import { createBroadCastChannelProvider } from '../../blocksuite/providers';
|
|||||||
import PageList from '../../components/blocksuite/block-suite-page-list/page-list';
|
import PageList from '../../components/blocksuite/block-suite-page-list/page-list';
|
||||||
import { StyledPage, StyledWrapper } from '../../layouts/styles';
|
import { StyledPage, StyledWrapper } from '../../layouts/styles';
|
||||||
import type { BroadCastChannelProvider } from '../../shared';
|
import type { BroadCastChannelProvider } from '../../shared';
|
||||||
|
import { toast } from '../../utils';
|
||||||
|
|
||||||
const logger = new DebugLogger('broadcast');
|
const logger = new DebugLogger('broadcast');
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Button, toast } from '@affine/component';
|
import { Button } from '@affine/component';
|
||||||
import { currentAffineUserAtom } from '@affine/workspace/affine/atom';
|
import { currentAffineUserAtom } from '@affine/workspace/affine/atom';
|
||||||
import {
|
import {
|
||||||
clearLoginStorage,
|
clearLoginStorage,
|
||||||
@ -14,6 +14,8 @@ import type { NextPage } from 'next';
|
|||||||
import dynamic from 'next/dynamic';
|
import dynamic from 'next/dynamic';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
|
import { toast } from '../../utils';
|
||||||
|
|
||||||
const Viewer = dynamic(
|
const Viewer = dynamic(
|
||||||
() => import('@rich-data/viewer').then(m => ({ default: m.JsonViewer })),
|
() => import('@rich-data/viewer').then(m => ({ default: m.JsonViewer })),
|
||||||
{ ssr: false }
|
{ ssr: false }
|
||||||
|
@ -1,20 +1,5 @@
|
|||||||
export function stringToColour(str: string) {
|
export * from './blocksuite';
|
||||||
str = str || 'affine';
|
export * from './create-emotion-cache';
|
||||||
let colour = '#';
|
export * from './is-valid-ip-address';
|
||||||
let hash = 0;
|
export * from './string2color';
|
||||||
// str to hash
|
export * from './toast';
|
||||||
for (
|
|
||||||
let i = 0;
|
|
||||||
i < str.length;
|
|
||||||
hash = str.charCodeAt(i++) + ((hash << 5) - hash)
|
|
||||||
);
|
|
||||||
|
|
||||||
// int/hash to hex
|
|
||||||
for (
|
|
||||||
let i = 0;
|
|
||||||
i < 3;
|
|
||||||
colour += ('00' + ((hash >> (i++ * 8)) & 0xff).toString(16)).slice(-2)
|
|
||||||
);
|
|
||||||
|
|
||||||
return colour;
|
|
||||||
}
|
|
||||||
|
20
apps/web/src/utils/string2color.ts
Normal file
20
apps/web/src/utils/string2color.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
export function stringToColour(str: string) {
|
||||||
|
str = str || 'affine';
|
||||||
|
let colour = '#';
|
||||||
|
let hash = 0;
|
||||||
|
// str to hash
|
||||||
|
for (
|
||||||
|
let i = 0;
|
||||||
|
i < str.length;
|
||||||
|
hash = str.charCodeAt(i++) + ((hash << 5) - hash)
|
||||||
|
);
|
||||||
|
|
||||||
|
// int/hash to hex
|
||||||
|
for (
|
||||||
|
let i = 0;
|
||||||
|
i < 3;
|
||||||
|
colour += ('00' + ((hash >> (i++ * 8)) & 0xff).toString(16)).slice(-2)
|
||||||
|
);
|
||||||
|
|
||||||
|
return colour;
|
||||||
|
}
|
12
apps/web/src/utils/toast.ts
Normal file
12
apps/web/src/utils/toast.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import type { ToastOptions } from '@affine/component';
|
||||||
|
import { toast as basicToast } from '@affine/component';
|
||||||
|
|
||||||
|
export const toast = (message: string, options?: ToastOptions) => {
|
||||||
|
const mainContainer = document.querySelector(
|
||||||
|
'.main-container'
|
||||||
|
) as HTMLElement;
|
||||||
|
return basicToast(message, {
|
||||||
|
portal: mainContainer || document.body,
|
||||||
|
...options,
|
||||||
|
});
|
||||||
|
};
|
@ -24,7 +24,8 @@ const htmlToElement = <T extends ChildNode>(html: string | TemplateResult) => {
|
|||||||
return template.content.firstChild as T;
|
return template.content.firstChild as T;
|
||||||
};
|
};
|
||||||
|
|
||||||
const createToastContainer = (portalQuery?: string) => {
|
const createToastContainer = (portal?: HTMLElement) => {
|
||||||
|
portal = portal || document.body;
|
||||||
const styles = css`
|
const styles = css`
|
||||||
position: absolute;
|
position: absolute;
|
||||||
z-index: 9999;
|
z-index: 9999;
|
||||||
@ -39,14 +40,13 @@ const createToastContainer = (portalQuery?: string) => {
|
|||||||
`;
|
`;
|
||||||
const template = html`<div style="${styles}"></div>`;
|
const template = html`<div style="${styles}"></div>`;
|
||||||
const element = htmlToElement<HTMLDivElement>(template);
|
const element = htmlToElement<HTMLDivElement>(template);
|
||||||
const portal = portalQuery && document.querySelector(portalQuery);
|
portal.appendChild(element);
|
||||||
(portal || document.body).appendChild(element);
|
|
||||||
return element;
|
return element;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ToastOptions = {
|
export type ToastOptions = {
|
||||||
duration: number;
|
duration?: number;
|
||||||
portalQuery?: string;
|
portal?: HTMLElement;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,12 +57,12 @@ export type ToastOptions = {
|
|||||||
*/
|
*/
|
||||||
export const toast = (
|
export const toast = (
|
||||||
message: string,
|
message: string,
|
||||||
{ duration, portalQuery = '.main-container' }: ToastOptions = {
|
{ duration, portal }: ToastOptions = {
|
||||||
duration: 2500,
|
duration: 2500,
|
||||||
}
|
}
|
||||||
) => {
|
) => {
|
||||||
if (!ToastContainer) {
|
if (!ToastContainer) {
|
||||||
ToastContainer = createToastContainer(portalQuery);
|
ToastContainer = createToastContainer(portal);
|
||||||
}
|
}
|
||||||
|
|
||||||
const styles = css`
|
const styles = css`
|
||||||
|
Loading…
Reference in New Issue
Block a user