micro/packages/web/src/components/avatar.tsx

22 lines
726 B
TypeScript

/* eslint-disable react/no-danger */
import clsx from 'clsx';
import * as avatar from 'generate-avatar';
import type { FC } from 'react';
import { useMemo, useRef } from 'react';
export interface AvatarProps {
userId: string;
className?: string;
}
export const Avatar: FC<AvatarProps> = (props) => {
const classes = clsx('overflow-hidden rounded-full select-none', props.className);
const containerRef = useRef<HTMLDivElement>(null);
const svg = useMemo(() => {
const result = avatar.generateFromString(props.userId);
return result.replace(/(width|height)="(\d+)"/g, '$1="100%"');
}, [props.userId]);
return <div className={classes} dangerouslySetInnerHTML={{ __html: svg }} ref={containerRef} />;
};