Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/components/account/avatar.tsx
Views: 687
/*1Show an avatar for a given user account.2*/34import { Avatar as AntdAvatar, Popover } from "antd";5import { CSSProperties, ReactNode } from "react";6import { avatar_fontcolor } from "@cocalc/frontend/account/avatar/font-color";7import A from "components/misc/A";8import useProfile from "lib/hooks/profile";9import useCustomize from "lib/use-customize";1011interface Props {12account_id: string;13size?: number;14style?: CSSProperties;15showName?: boolean;16extra?: ReactNode; // extra component that gets rendered below avatar when hoving, e.g., could be a "remove" action...17zIndex?: number;18}1920export default function Avatar({21account_id,22size,23style,24showName,25extra,26zIndex,27}: Props) {28const { account } = useCustomize();29if (size == null) {30// Default size=40 to match the cocalc logo.31size = 40;32}33const profile = useProfile({ account_id });3435if (!profile) {36// not loaded yet37return <DisplayAvatar style={style} size={size} />;38}3940return (41<Popover42mouseLeaveDelay={0.3}43zIndex={zIndex}44title={45<div style={{ textAlign: "center", fontSize: "13pt" }}>46{profile.first_name} {profile.last_name}{" "}47{profile.name ? `(@${profile.name})` : ""}48{account_id == account?.account_id && (49<A50href="/config/account/name"51style={{52fontSize: "11px",53marginLeft: "10px",54float: "right",55}}56>57edit58</A>59)}60</div>61}62content={63<div style={{ textAlign: "center" }}>64{profile.image ? (65<DisplayAvatar66style={style}67size={size * 4}68image={profile.image}69/>70) : (71<DisplayAvatar72style={style}73size={size * 4}74color={profile.color}75letter={profile.first_name?.[0]}76/>77)}78{account_id == account?.account_id && (79<div style={{ marginTop: "10px" }}>80<A81href="/config/account/avatar"82style={{83fontSize: "11px",84}}85>86edit87</A>88</div>89)}90{extra}91</div>92}93>94<span style={{ cursor: "pointer" }}>95{profile.image ? (96<DisplayAvatar style={style} size={size} image={profile.image} />97) : (98<DisplayAvatar99style={style}100size={size}101color={profile.color}102letter={profile.first_name?.[0]}103/>104)}105{showName && (106<>107<br />108{profile.first_name} {profile.last_name}109</>110)}111</span>112</Popover>113);114}115116interface DisplayProps extends Partial<Props> {117image?: string;118color?: string;119letter?: string;120}121122export function DisplayAvatar({123style,124size,125image,126color,127letter,128}: DisplayProps) {129if (!image && !color && !letter) {130return (131<AntdAvatar132style={{133verticalAlign: "middle",134...style,135}}136size={size}137></AntdAvatar>138);139}140141if (image) {142return (143<AntdAvatar144style={{145verticalAlign: "middle",146...style,147}}148size={size}149src={<img src={image} />}150/>151);152}153154let fontSize: string | undefined = undefined;155if (size != null) {156if (size >= 32) {157fontSize = `${0.75 * size}px`;158} else if (size >= 24) {159fontSize = "16pt";160}161}162163if (!color) {164// color needs to be something, or below the avatar165// will be completely invisible on a black background, e.g.,166// in the title bar.167color = "#888";168}169return (170<AntdAvatar171style={{172backgroundColor: color,173verticalAlign: "middle",174fontSize,175color: avatar_fontcolor(color),176...style,177}}178size={size}179>180{letter ? letter : "?"}181</AntdAvatar>182);183}184185186