@airplane/views
Version:
A React library for building Airplane views. Views components are optimized in style and functionality to produce internal apps that are easy to build and maintain.
62 lines (61 loc) • 2.08 kB
JavaScript
import { jsx } from "react/jsx-runtime";
import { Avatar as Avatar$1 } from "@mantine/core";
import { useQuery } from "@tanstack/react-query";
import { USERS_GET } from "../../client/endpoints.js";
import { Fetcher } from "../../client/fetcher.js";
import { ComponentErrorBoundary } from "../errorBoundary/ComponentErrorBoundary.js";
const getInitials = (displayName) => {
if (!displayName) {
return void 0;
}
const nameParts = displayName.split(" ");
if (nameParts.length === 1) {
if (nameParts[0].length === 0) {
return void 0;
} else {
return nameParts[0][0].toUpperCase();
}
}
return nameParts[0][0].toUpperCase() + nameParts[nameParts.length - 1][0].toUpperCase();
};
const AvatarComponent = ({
color = "primary",
size = "md",
radius = 9999,
email,
userID,
src,
children,
...props
}) => {
var _a, _b, _c;
const emailOrUserID = email || userID;
const {
data
} = useQuery([USERS_GET, emailOrUserID], async () => {
const fetcher = new Fetcher();
return await fetcher.get(USERS_GET, email ? {
email
} : {
userID
});
}, {
enabled: !!emailOrUserID
});
const imageSrc = src || ((_a = data == null ? void 0 : data.user) == null ? void 0 : _a.avatarURL) || void 0;
let avatarChildren = children;
if (imageSrc === void 0 && children === void 0 && emailOrUserID !== void 0) {
const nameInitials = getInitials((_b = data == null ? void 0 : data.user) == null ? void 0 : _b.name);
const emailInitials = (_c = email == null ? void 0 : email[0]) == null ? void 0 : _c.toUpperCase();
avatarChildren = nameInitials || emailInitials;
}
return /* @__PURE__ */ jsx(Avatar$1, { color, radius, size, variant: "light", src: imageSrc, ...props, children: avatarChildren });
};
const Avatar = (props) => /* @__PURE__ */ jsx(ComponentErrorBoundary, { componentName: Avatar.displayName, children: /* @__PURE__ */ jsx(AvatarComponent, { ...props }) });
Avatar.displayName = "Avatar";
export {
Avatar,
AvatarComponent,
getInitials
};
//# sourceMappingURL=Avatar.js.map