/** * Github Contributors Avatar List */ import React from 'react'; import useSWR from 'swr'; export interface AvatarListItem { username?: string; url?: string; } export interface ButtonProps { className?: string; style?: React.CSSProperties; fileName: string; owner: string; repo: string; filter?: (item: AvatarListItem) => boolean; emptyRender?: (fileName: string, owner: string, repo: string, branch: string) => React.ReactNode; renderItem?: (item?: AvatarListItem, loading?: boolean) => React.ReactNode; cache?: boolean; branch?: string; } // 获取头像列表 const getAvatarList = async ({ fileName, repo, owner, branch, }: { owner: string; repo: string; fileName: string; branch: string; }): Promise => { const url = `https://proapi.azurewebsites.net/doc/getAvatarList?filename=${fileName}&owner=${owner}&repo=${repo}&branch=${branch}`; const data = await fetch(url, { mode: 'cors' }) .then((res) => res.json()) .catch((e) => console.log(e)); if (!data) { return []; } return data; }; const AvatarList: React.FC = ({ className, renderItem, repo, owner, style, fileName, filter = () => true, emptyRender, branch = 'master', }: ButtonProps) => { const { data, isLoading } = useSWR( `/doc/getAvatarList?filename=${fileName}&owner=${owner}&repo=${repo}&branch=${branch}`, () => { return getAvatarList({ owner, repo, fileName, branch }); }, ); if (isLoading) { return (
{(renderItem && renderItem({}, true)) || loading}
); } const displayList = data?.filter(filter) || []; return ( <> ); }; export default AvatarList;