Skip to content

Commit

Permalink
refactor(shared/component): extract useCharacterAvatar from avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
runjuu committed Jan 17, 2023
1 parent 3af39be commit dabcdbb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
30 changes: 8 additions & 22 deletions shared/components/avatar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import React from "react";
import { useCharacter } from "@crossbell/indexer";
import { ipfsLinkToHttpLink } from "@crossbell/util-ipfs";
import { extractCharacterAvatar } from "@crossbell/util-metadata";
import { Avatar as Avatar_, AvatarProps } from "@mantine/core";
import { CharacterEntity } from "crossbell.js";
import { PropsWithChildren } from "react";

import CharacterHoverCard from "~/shared/components/character/character-hover-card";
import { getDefaultAvatar } from "~/shared/avatar";

import { useCharacterAvatar } from "./use-character-avatar";

export function Avatar({
characterId,
Expand All @@ -30,29 +28,17 @@ export function Avatar({
showHoverCard?: boolean;
} & AvatarProps
>) {
const { isLoading, data: character } = useCharacter(
characterId ?? initialCharacter?.characterId,
{
enabled: Boolean(characterId) && !src,
initialData: initialCharacter,
}
);

let src_ =
src ??
extractCharacterAvatar(character) ??
(isLoading
? getDefaultAvatar()
: extractCharacterAvatar(character) ??
getDefaultAvatar(character?.handle));

src_ = ipfsLinkToHttpLink(src_);
const { src: avatarSrc, character } = useCharacterAvatar({
character: initialCharacter,
characterId,
disabled: !!src,
});

return (
<CharacterHoverCard character={character} showHoverCard={showHoverCard}>
<Avatar_
className="bg-coolgray-100"
src={src_}
src={src ?? avatarSrc}
alt={alt}
radius="xl"
{...props}
Expand Down
40 changes: 40 additions & 0 deletions shared/components/avatar/use-character-avatar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useCharacter } from "@crossbell/indexer";
import { extractCharacterAvatar } from "@crossbell/util-metadata";
import { ipfsLinkToHttpLink } from "@crossbell/util-ipfs";
import { CharacterEntity } from "crossbell.js";

import { getDefaultAvatar } from "~/shared/avatar";
import React from "react";

export type UseCharacterAvatarParams = {
characterId?: number | null;
character?: CharacterEntity | null;
disabled?: boolean;
};

export function useCharacterAvatar({
character,
characterId,
disabled,
}: UseCharacterAvatarParams) {
const { isLoading, data } = useCharacter(
characterId ?? character?.characterId,
{
enabled: !!characterId && !disabled,
initialData: character,
}
);

return React.useMemo(
() => ({
src: ipfsLinkToHttpLink(
extractCharacterAvatar(data) ??
(isLoading
? getDefaultAvatar()
: extractCharacterAvatar(data) ?? getDefaultAvatar(data?.handle))
),
character: data,
}),
[data, isLoading]
);
}

0 comments on commit dabcdbb

Please sign in to comment.