-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
541 additions
and
400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/frontend/component/src/components/workspace-avatar/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
useLiveData, | ||
useService, | ||
type WorkspaceMetadata, | ||
WorkspacesService, | ||
} from '@toeverything/infra'; | ||
import { useEffect, useLayoutEffect, useState } from 'react'; | ||
|
||
import { Avatar, type AvatarProps } from '../../ui/avatar'; | ||
|
||
const cache = new Map<string, { imageBitmap: ImageBitmap; key: string }>(); | ||
|
||
/** | ||
* workspace avatar component with automatic cache, and avoid flashing | ||
*/ | ||
export const WorkspaceAvatar = ({ | ||
meta, | ||
...otherProps | ||
}: { meta: WorkspaceMetadata } & AvatarProps) => { | ||
const workspacesService = useService(WorkspacesService); | ||
|
||
const profile = workspacesService.getProfile(meta); | ||
|
||
useEffect(() => { | ||
profile.revalidate(); | ||
}, [meta, profile]); | ||
|
||
const avatarKey = useLiveData(profile.profile$.map(v => v?.avatar)); | ||
|
||
const [downloadedAvatar, setDownloadedAvatar] = useState< | ||
{ imageBitmap: ImageBitmap; key: string } | undefined | ||
>(cache.get(meta.id)); | ||
|
||
useLayoutEffect(() => { | ||
if (!avatarKey || !meta) { | ||
setDownloadedAvatar(undefined); | ||
return; | ||
} | ||
|
||
let canceled = false; | ||
workspacesService | ||
.getWorkspaceBlob(meta, avatarKey) | ||
.then(async blob => { | ||
if (blob && !canceled) { | ||
const image = document.createElement('img'); | ||
const objectUrl = URL.createObjectURL(blob); | ||
image.src = objectUrl; | ||
await image.decode(); | ||
// limit the size of the image data to reduce memory usage | ||
const hRatio = 128 / image.naturalWidth; | ||
const vRatio = 128 / image.naturalHeight; | ||
const ratio = Math.min(hRatio, vRatio); | ||
const imageBitmap = await createImageBitmap(image, { | ||
resizeWidth: image.naturalWidth * ratio, | ||
resizeHeight: image.naturalHeight * ratio, | ||
}); | ||
URL.revokeObjectURL(objectUrl); | ||
setDownloadedAvatar(prev => { | ||
if (prev?.key === avatarKey) { | ||
return prev; | ||
} | ||
return { imageBitmap, key: avatarKey }; | ||
}); | ||
cache.set(meta.id, { | ||
imageBitmap, | ||
key: avatarKey, | ||
}); | ||
} | ||
}) | ||
.catch(err => { | ||
console.error('get workspace blob error: ' + err); | ||
}); | ||
|
||
return () => { | ||
canceled = true; | ||
}; | ||
}, [meta, workspacesService, avatarKey]); | ||
|
||
return <Avatar image={downloadedAvatar?.imageBitmap} {...otherProps} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.