177 lines
7.0 KiB
TypeScript
177 lines
7.0 KiB
TypeScript
import { useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { Description as DialogDescription, Title as DialogTitle } from "@radix-ui/react-dialog";
|
|
import { FeatherArchive, FeatherShield, FeatherTrash2 } from "@subframe/core";
|
|
import type { ChatAuditAdministrationVM } from "@hivegrid/view-models";
|
|
|
|
import { Alert } from "~/ui/components/Alert";
|
|
import { Avatar } from "~/ui/components/Avatar";
|
|
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { ButtonGroup } from "~/ui/components/ButtonGroup";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { Dialog } from "~/ui/components/Dialog";
|
|
|
|
export type ChatAuditAdministrationActions = Readonly<{
|
|
onArchive: () => void | Promise<void>;
|
|
onChangeMemberRole: (userId: string, role: "admin" | "member") => void | Promise<void>;
|
|
onRemoveMember: (userId: string) => void | Promise<void>;
|
|
}>;
|
|
|
|
type Confirmation = Readonly<{ type: "archive" }> | Readonly<{ type: "remove"; userId: string; memberName: string }>;
|
|
|
|
export function ChatAuditAdministrationPanel({
|
|
vm,
|
|
actions,
|
|
busy = false,
|
|
actionErrorKey = null,
|
|
}: {
|
|
readonly vm: ChatAuditAdministrationVM;
|
|
readonly actions: ChatAuditAdministrationActions;
|
|
readonly busy?: boolean;
|
|
readonly actionErrorKey?: string | null;
|
|
}): React.JSX.Element | null {
|
|
const { t } = useTranslation();
|
|
const [confirmation, setConfirmation] = useState<Confirmation | null>(null);
|
|
if (vm.status !== "ready") return null;
|
|
const data = vm.data;
|
|
|
|
return (
|
|
<div className="flex w-full flex-col gap-4 px-6 pt-6" data-ui-id="ui.chat.audit.administration">
|
|
{actionErrorKey ? (
|
|
<Alert variant="error" notDismissible title={t("chat.audit.errors.title")} description={t(actionErrorKey)} />
|
|
) : null}
|
|
{!data.canAdminister ? (
|
|
<Alert
|
|
variant="information"
|
|
notDismissible
|
|
title={t("chat.audit.administration.readOnlyTitle")}
|
|
description={t("chat.audit.administration.readOnlyDescription")}
|
|
/>
|
|
) : (
|
|
<Card size="large">
|
|
<ButtonGroup align="space-between">
|
|
<ButtonGroup>
|
|
<FeatherShield aria-hidden="true" />
|
|
<ButtonGroup align="stack">
|
|
<strong>{t("chat.audit.administration.title")}</strong>
|
|
<span>{t("chat.audit.administration.description")}</span>
|
|
</ButtonGroup>
|
|
</ButtonGroup>
|
|
{data.canArchive ? (
|
|
<Button
|
|
variant="destructive"
|
|
icon={<FeatherArchive />}
|
|
disabled={busy}
|
|
onClick={() => setConfirmation({ type: "archive" })}
|
|
data-ui-id="ui.chat.audit.archiveChannel"
|
|
>
|
|
{t("chat.audit.actions.archive")}
|
|
</Button>
|
|
) : (
|
|
<BadgeFeedback farbe="subtle-neutral">{t("chat.audit.archived")}</BadgeFeedback>
|
|
)}
|
|
</ButtonGroup>
|
|
|
|
<div className="flex w-full flex-col gap-2">
|
|
<strong>{t("chat.audit.members.title")}</strong>
|
|
{data.members.map((member) => (
|
|
<Card key={member.userId} size="small">
|
|
<ButtonGroup align="space-between">
|
|
<ButtonGroup>
|
|
<Avatar image={member.avatarUrl ?? undefined} size="small">
|
|
{initials(member.displayName)}
|
|
</Avatar>
|
|
<strong>{member.displayName}</strong>
|
|
<BadgeFeedback farbe={member.roleInChat === "admin" ? "subtle-brand-primary" : "subtle-neutral"} size="small">
|
|
{t(member.roleLabelKey)}
|
|
</BadgeFeedback>
|
|
</ButtonGroup>
|
|
<ButtonGroup>
|
|
{member.canPromote ? (
|
|
<Button
|
|
size="small"
|
|
variant="tertiary"
|
|
disabled={busy}
|
|
onClick={() => void actions.onChangeMemberRole(member.userId, "admin")}
|
|
>
|
|
{t("chat.audit.actions.promote")}
|
|
</Button>
|
|
) : null}
|
|
{member.canDemote ? (
|
|
<Button
|
|
size="small"
|
|
variant="tertiary"
|
|
disabled={busy}
|
|
onClick={() => void actions.onChangeMemberRole(member.userId, "member")}
|
|
>
|
|
{t("chat.audit.actions.demote")}
|
|
</Button>
|
|
) : null}
|
|
{member.canRemove ? (
|
|
<Button
|
|
size="small"
|
|
variant="destructive"
|
|
icon={<FeatherTrash2 />}
|
|
disabled={busy}
|
|
onClick={() => setConfirmation({ type: "remove", userId: member.userId, memberName: member.displayName })}
|
|
aria-label={t("chat.audit.actions.removeMemberLabel", { member: member.displayName })}
|
|
/>
|
|
) : null}
|
|
</ButtonGroup>
|
|
</ButtonGroup>
|
|
</Card>
|
|
))}
|
|
</div>
|
|
</Card>
|
|
)}
|
|
|
|
<Dialog open={confirmation !== null} onOpenChange={(open: boolean) => (open ? undefined : setConfirmation(null))}>
|
|
<Dialog.Content
|
|
className="w-[420px] max-w-[90vw] p-4"
|
|
aria-label={t(confirmation?.type === "remove" ? "chat.audit.confirm.remove.title" : "chat.audit.confirm.archive.title")}
|
|
>
|
|
<ButtonGroup align="stack">
|
|
<DialogTitle>
|
|
{t(confirmation?.type === "remove" ? "chat.audit.confirm.remove.title" : "chat.audit.confirm.archive.title")}
|
|
</DialogTitle>
|
|
<DialogDescription>
|
|
{confirmation?.type === "remove"
|
|
? t("chat.audit.confirm.remove.body", { member: confirmation.memberName })
|
|
: t("chat.audit.confirm.archive.body")}
|
|
</DialogDescription>
|
|
</ButtonGroup>
|
|
<ButtonGroup align="end">
|
|
<Button variant="tertiary" disabled={busy} onClick={() => setConfirmation(null)}>
|
|
{t("shared.cancel")}
|
|
</Button>
|
|
<Button
|
|
variant="destructive"
|
|
loading={busy}
|
|
onClick={() => {
|
|
if (confirmation?.type === "archive") void actions.onArchive();
|
|
if (confirmation?.type === "remove") void actions.onRemoveMember(confirmation.userId);
|
|
setConfirmation(null);
|
|
}}
|
|
>
|
|
{t("shared.confirm")}
|
|
</Button>
|
|
</ButtonGroup>
|
|
</Dialog.Content>
|
|
</Dialog>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function initials(label: string): string {
|
|
return (
|
|
label
|
|
.trim()
|
|
.split(/\s+/)
|
|
.filter(Boolean)
|
|
.slice(0, 2)
|
|
.map((part) => part[0]?.toLocaleUpperCase() ?? "")
|
|
.join("") || "?"
|
|
);
|
|
}
|