From 40d09357925e3c44ded3f8dd2519d7cf772440cc Mon Sep 17 00:00:00 2001 From: tzhangchi Date: Fri, 6 Jan 2023 01:13:02 +0800 Subject: [PATCH] feat: global avatar --- .../src/components/workspace-modal/index.tsx | 20 +---------------- .../WorkspaceSelector/WorkspaceSelector.tsx | 22 ++++++++++++++++--- packages/app/src/utils/colors.ts | 20 +++++++++++++++++ packages/app/src/utils/index.ts | 2 ++ 4 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 packages/app/src/utils/colors.ts diff --git a/packages/app/src/components/workspace-modal/index.tsx b/packages/app/src/components/workspace-modal/index.tsx index affc9492ec..caaec6d329 100644 --- a/packages/app/src/components/workspace-modal/index.tsx +++ b/packages/app/src/components/workspace-modal/index.tsx @@ -21,6 +21,7 @@ import { } from '@blocksuite/icons'; import { useConfirm } from '@/providers/confirm-provider'; import { toast } from '@/ui/toast'; +import { stringToColour } from '@/utils'; interface LoginModalProps { open: boolean; onClose: () => void; @@ -45,26 +46,7 @@ export const WorkspaceModal = ({ open, onClose }: LoginModalProps) => { const data = getUserInfo(); setUser(data); }; - const stringToColour = function (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; - }; return (
diff --git a/packages/app/src/components/workspace-slider-bar/WorkspaceSelector/WorkspaceSelector.tsx b/packages/app/src/components/workspace-slider-bar/WorkspaceSelector/WorkspaceSelector.tsx index 5ccc9a446b..9004ef72a9 100644 --- a/packages/app/src/components/workspace-slider-bar/WorkspaceSelector/WorkspaceSelector.tsx +++ b/packages/app/src/components/workspace-slider-bar/WorkspaceSelector/WorkspaceSelector.tsx @@ -1,9 +1,9 @@ import { Avatar, WorkspaceName, SelectorWrapper } from './styles'; import { useEffect, useState } from 'react'; -import { AffineIcon } from '../icons/icons'; +// import { AffineIcon } from '../icons/icons'; import { WorkspaceModal } from '@/components/workspace-modal'; import { getActiveWorkspace, getWorkspaces } from '@/hooks/mock-data/mock'; - +import { stringToColour } from '@/utils'; export const WorkspaceSelector = () => { const [workspaceListShow, setWorkspaceListShow] = useState(false); const [workspace, setWorkSpace] = useState(getActiveWorkspace()); @@ -34,7 +34,23 @@ export const WorkspaceSelector = () => { data-testid="workspace-avatar" src={workspace.avatar} > - +
+ {(workspace?.name ?? 'AFFiNE').substring(0, 1)} +
{workspace?.name ?? 'AFFiNE'} diff --git a/packages/app/src/utils/colors.ts b/packages/app/src/utils/colors.ts new file mode 100644 index 0000000000..431342fbf1 --- /dev/null +++ b/packages/app/src/utils/colors.ts @@ -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; +} diff --git a/packages/app/src/utils/index.ts b/packages/app/src/utils/index.ts index aeeaed611c..2d05a590cd 100644 --- a/packages/app/src/utils/index.ts +++ b/packages/app/src/utils/index.ts @@ -3,3 +3,5 @@ export { isDev } from './env'; export * from './useragent'; export * from './tools'; + +export * from './colors';