1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/avatar/avatar.tsx
Sebastian Malton 1031d754f6 convert all KubeApis to injectable with legacy global backups
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2022-05-04 08:47:19 -04:00

101 lines
2.5 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import styles from "./avatar.module.scss";
import type { ImgHTMLAttributes, MouseEventHandler } from "react";
import React from "react";
import randomColor from "randomcolor";
import GraphemeSplitter from "grapheme-splitter";
import type { SingleOrMany } from "../../utils";
import { cssNames, isDefined, iter } from "../../utils";
export interface AvatarProps {
title: string;
colorHash?: string;
size?: number;
src?: string;
background?: string;
variant?: "circle" | "rounded" | "square";
imgProps?: ImgHTMLAttributes<HTMLImageElement>;
disabled?: boolean;
children?: SingleOrMany<React.ReactNode>;
className?: string;
id?: string;
onClick?: MouseEventHandler<HTMLDivElement>;
}
function getNameParts(name: string): string[] {
const byWhitespace = name.split(/\s+/);
if (byWhitespace.length > 1) {
return byWhitespace;
}
const byDashes = name.split(/[-_]+/);
if (byDashes.length > 1) {
return byDashes;
}
return name.split(/@+/);
}
function getLabelFromTitle(title: string) {
if (!title) {
return "??";
}
const [rawFirst, rawSecond, rawThird] = getNameParts(title);
const splitter = new GraphemeSplitter();
const first = splitter.iterateGraphemes(rawFirst);
const second = rawSecond ? splitter.iterateGraphemes(rawSecond): first;
const third = rawThird ? splitter.iterateGraphemes(rawThird) : iter.newEmpty();
return [
...iter.take(first, 1),
...iter.take(second, 1),
...iter.take(third, 1),
].filter(isDefined).join("");
}
export function Avatar(props: AvatarProps) {
const { title, variant = "rounded", size = 32, colorHash, children, background, imgProps, src, className, disabled, id, onClick } = props;
const colorFromHash = randomColor({ seed: colorHash, luminosity: "dark" });
const renderContents = () => {
if (src) {
return (
<img
src={src}
{...imgProps}
alt={title}
/>
);
}
return children || getLabelFromTitle(title);
};
return (
<div
className={cssNames(styles.Avatar, {
[styles.circle]: variant == "circle",
[styles.rounded]: variant == "rounded",
[styles.disabled]: disabled,
}, className)}
style={{
width: `${size}px`,
height: `${size}px`,
background: background || (src ? "transparent" : colorFromHash),
}}
id={id}
onClick={onClick}
>
{renderContents()}
</div>
);
}