Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix rendering of tenant avatars (#77075)

Sergii Vozniuk892974e74ebc
diff --git a/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantsListing/TenantsListing.tsx b/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantsListing/TenantsListing.tsx
index 787a1ed1022..347b6bfd716 100644
--- a/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantsListing/TenantsListing.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantsListing/TenantsListing.tsx
@@ -100,7 +100,7 @@ export const TenantsListing = ({
gap="md"
>
<UserAvatar
- user={{ first_name: tenant.name }}
+ user={{ name: tenant.name }}
bg={tenantIdToColor(tenant.id)}
/>
<Box component="span" fw={700} c="core-brand">
diff --git a/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantsListing/TenantsListing.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantsListing/TenantsListing.unit.spec.tsx
index 3bfb7375882..ef37796b8e0 100644
--- a/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantsListing/TenantsListing.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/tenants/components/TenantsListing/TenantsListing.unit.spec.tsx
@@ -47,6 +47,13 @@ describe("TenantsListing", () => {
expect(screen.getByText("Acme Corp")).toBeInTheDocument();
});
+ it("renders the tenant's first initial in the avatar, not a question mark", () => {
+ setup([createMockTenant({ id: 11, name: "Acme Corp", slug: "acme-corp" })]);
+
+ expect(screen.getByText("A")).toBeInTheDocument();
+ expect(screen.queryByText("?")).not.toBeInTheDocument();
+ });
+
it("filters non-Latin tenants by search text", async () => {
setup([
createMockTenant({ id: 10, name: "здравей", slug: "zdravey" }),
diff --git a/frontend/src/metabase/common/components/UserAvatar/UserAvatar.tsx b/frontend/src/metabase/common/components/UserAvatar/UserAvatar.tsx
index 7eeb7bb708b..e06e8651021 100644
--- a/frontend/src/metabase/common/components/UserAvatar/UserAvatar.tsx
+++ b/frontend/src/metabase/common/components/UserAvatar/UserAvatar.tsx
@@ -1,8 +1,8 @@
import {
type PartialGroup,
+ type PartialTenant,
type PartialUser,
- initial,
- userInitials,
+ prepareInitials,
} from "metabase/common/utils/user";
import type { AvatarProps } from "./UserAvatar.styled";
@@ -12,14 +12,17 @@ interface UserAvatarProps extends AvatarProps {
user: PartialUser;
}
-interface GroupProps {
+interface GroupProps extends AvatarProps {
user: PartialGroup;
}
-export function UserAvatar({ user, ...props }: UserAvatarProps | GroupProps) {
- return <StyledAvatar {...props}>{userInitials(user) || "?"}</StyledAvatar>;
+interface TenantProps extends AvatarProps {
+ user: PartialTenant;
}
-export function Avatar({ children, ...props }: { children: string }) {
- return <StyledAvatar {...props}>{initial(children) ?? "?"}</StyledAvatar>;
+export function UserAvatar({
+ user,
+ ...props
+}: UserAvatarProps | GroupProps | TenantProps) {
+ return <StyledAvatar {...props}>{prepareInitials(user) || "?"}</StyledAvatar>;
}
diff --git a/frontend/src/metabase/common/components/UserAvatar/index.ts b/frontend/src/metabase/common/components/UserAvatar/index.ts
index f60cf30199c..181000414b6 100644
--- a/frontend/src/metabase/common/components/UserAvatar/index.ts
+++ b/frontend/src/metabase/common/components/UserAvatar/index.ts
@@ -1 +1 @@
-export { UserAvatar, Avatar } from "./UserAvatar";
+export { UserAvatar } from "./UserAvatar";
diff --git a/frontend/src/metabase/common/utils/user.ts b/frontend/src/metabase/common/utils/user.ts
index f810d6b64d9..3b1be99c463 100644
--- a/frontend/src/metabase/common/utils/user.ts
+++ b/frontend/src/metabase/common/utils/user.ts
@@ -1,38 +1,28 @@
import { isEmail } from "metabase/utils/email";
-import type { BaseUser, Group } from "metabase-types/api";
+import type { BaseUser, Group, Tenant } from "metabase-types/api";
+// Requires at least an `email` or `common_name` (mirroring `isUser` below), so
+// name-only objects like a tenant's `{ name }` can't be passed as first_name alone.
export type PartialUser = Partial<
Pick<BaseUser, "first_name" | "last_name" | "email" | "common_name">
->;
+> &
+ (Pick<BaseUser, "email"> | Pick<BaseUser, "common_name">);
export type PartialGroup = Pick<Group, "name">;
-const isUser = (user: PartialUser | PartialGroup): user is PartialUser => {
- return "common_name" in user || "email" in user;
-};
-
-export function userInitials(user: PartialGroup | PartialUser) {
- const initials = nameInitials(user);
-
- if (initials) {
- return initials;
- } else if (isUser(user)) {
- return emailInitials(user);
- }
+export type PartialTenant = Pick<Tenant, "name">;
- return null;
-}
+export type Named = PartialUser | PartialGroup | PartialTenant;
-function nameInitials(user: PartialUser | PartialGroup) {
- if (isUser(user)) {
- return initial(user.first_name) + initial(user.last_name);
+export function prepareInitials(namedParty: Named): string | null {
+ if (isUser(namedParty)) {
+ return (
+ initial(namedParty.first_name) + initial(namedParty.last_name) ||
+ emailInitials(namedParty)
+ );
} else {
- // render group
- return initial(user.name);
+ return initial(namedParty.name) || null;
}
}
-export function initial(name?: string | null) {
- return name ? name.charAt(0).toUpperCase() : "";
-}
function emailInitials(user: PartialUser) {
const email = [user.email, user.common_name].find((maybeEmail) =>
@@ -45,3 +35,11 @@ function emailInitials(user: PartialUser) {
return null;
}
+
+const isUser = (user: Named): user is PartialUser => {
+ return "common_name" in user || "email" in user;
+};
+
+function initial(name?: string | null) {
+ return name ? name.charAt(0).toUpperCase() : "";
+}
diff --git a/frontend/src/metabase/nav/components/AppSwitcher/AppSwitcher.tsx b/frontend/src/metabase/nav/components/AppSwitcher/AppSwitcher.tsx
index 8011d6d6fce..a565478eaa0 100644
--- a/frontend/src/metabase/nav/components/AppSwitcher/AppSwitcher.tsx
+++ b/frontend/src/metabase/nav/components/AppSwitcher/AppSwitcher.tsx
@@ -7,7 +7,7 @@ import { ExternalLink } from "metabase/common/components/ExternalLink";
import { ForwardRefLink } from "metabase/common/components/Link";
import { trackDataStudioOpened } from "metabase/common/data-studio/analytics";
import { canAccessDataStudio as canAccessDataStudioSelector } from "metabase/common/data-studio/selectors";
-import { userInitials } from "metabase/common/utils/user";
+import { prepareInitials } from "metabase/common/utils/user";
import { useDispatch, useSelector } from "metabase/redux";
import { openDiagnostics } from "metabase/redux/app";
import { logout } from "metabase/redux/auth";
@@ -177,7 +177,7 @@ export const AppSwitcher = ({ className }: { className?: string }) => {
bd="1px solid var(--mb-color-border-neutral)"
data-testid="app-switcher-target"
>
- {user ? userInitials(user) : "?"}
+ {user ? prepareInitials(user) : "?"}
</Avatar>
)}
</Menu.Target>
@@ -191,7 +191,7 @@ export const AppSwitcher = ({ className }: { className?: string }) => {
>
<Group wrap="nowrap">
<Avatar color="core-brand" radius="lg" size={32}>
- {user ? userInitials(user) : "?"}
+ {user ? prepareInitials(user) : "?"}
</Avatar>
<Stack gap="xs">
<Text lh="xs">{user?.first_name}</Text>