Files

312 lines
13 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,
FeatherArrowLeft,
FeatherLock,
FeatherLogOut,
FeatherSettings,
FeatherShield,
FeatherTrash2,
FeatherUnlock,
FeatherUserPlus,
} from "@subframe/core";
import type { ChatChannelSettingsMemberRole, ChatChannelSettingsVM } 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";
import { EmptyState } from "~/ui/components/EmptyState";
import { LoadingIndicatorSkeleton } from "~/ui/components/LoadingIndicatorSkeleton";
import { Select } from "~/ui/components/Select";
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
import { ChatSwitch } from "./ChatSwitch";
export type ChatChannelSettingsActions = Readonly<{
onBack: () => void;
onRetry: () => void | Promise<void>;
onAddMember: (userId: string) => void | Promise<void>;
onChangeRole: (userId: string, role: ChatChannelSettingsMemberRole) => void | Promise<void>;
onRemoveMember: (userId: string) => void | Promise<void>;
onPrivacyChange: (privacy: "public" | "private") => void | Promise<void>;
onLeave: () => void | Promise<void>;
onArchive: () => void | Promise<void>;
}>;
export type ChatChannelSettingsPageProps = Readonly<{
vm: ChatChannelSettingsVM;
actions: ChatChannelSettingsActions;
busy?: boolean;
actionErrorKey?: string | null;
}>;
export function ChatChannelSettingsPage({ vm, actions, busy = false, actionErrorKey = null }: ChatChannelSettingsPageProps): React.JSX.Element {
const { t } = useTranslation();
const [candidateUserId, setCandidateUserId] = useState("");
const [confirmAction, setConfirmAction] = useState<"archive" | "leave" | null>(null);
const readyData = vm.status === "ready" ? vm.data : null;
const headerActions = (
<Button variant="tertiary" icon={<FeatherArrowLeft />} onClick={actions.onBack} disabled={busy}>
{t("chat.channelSettings.actions.back")}
</Button>
);
if (vm.status === "idle" || vm.status === "loading") {
return (
<SettingsShell actions={headerActions}>
<LoadingIndicatorSkeleton
variant="card"
role="status"
aria-label={t("chat.channelSettings.members.loading")}
data-ui-id="ui.chat.channelSettings.loading"
/>
</SettingsShell>
);
}
if (vm.status === "error") {
return (
<SettingsShell actions={headerActions}>
<EmptyState
outlined
variant="error"
title={t("shared.error")}
body={t(vm.error.messageKey)}
action={
<Button variant="tertiary" onClick={() => void actions.onRetry()}>
{t("shared.retry")}
</Button>
}
data-ui-id="ui.chat.channelSettings.error"
/>
</SettingsShell>
);
}
if (vm.status === "empty" || !readyData) {
return (
<SettingsShell actions={headerActions}>
<EmptyState outlined title={t("chat.channelSettings.empty.title")} body={t("chat.channelSettings.empty.body")} />
</SettingsShell>
);
}
const canAddMember = readyData.permissions.canManageGroupMembers || readyData.permissions.canManageManualGuests;
return (
<SettingsShell actions={headerActions}>
{actionErrorKey ? (
<Alert
variant="error"
notDismissible
title={t("chat.channelSettings.errors.actionTitle")}
description={t(actionErrorKey)}
data-ui-id="ui.chat.channelSettings.actionError"
/>
) : null}
<Card size="large" data-ui-id="ui.chat.channelSettings.summary">
<ButtonGroup align="space-between">
<ButtonGroup align="stack">
<span className="text-heading-2 font-heading-2 text-text-neutral-primary">{readyData.channel.title}</span>
{readyData.channel.description ? (
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">{readyData.channel.description}</span>
) : null}
</ButtonGroup>
<BadgeFeedback farbe={readyData.channel.isArchived ? "subtle-neutral" : "subtle-brand-primary"}>
{t(`chat.channel.kind.${readyData.channel.kind}`)}
</BadgeFeedback>
</ButtonGroup>
</Card>
{readyData.infoKey ? <Alert variant="information" notDismissible title={t(readyData.infoKey)} /> : null}
{readyData.channel.kind === "group" ? (
<Card size="large" data-ui-id="ui.chat.channelSettings.privacy">
<ButtonGroup align="space-between">
<ButtonGroup>
{readyData.channel.privacy === "private" ? <FeatherLock aria-hidden="true" /> : <FeatherUnlock aria-hidden="true" />}
<ButtonGroup align="stack">
<strong>{t("chat.channelSettings.privacy.title")}</strong>
<span>{t(`chat.channelSettings.privacy.${readyData.channel.privacy}Help`)}</span>
</ButtonGroup>
</ButtonGroup>
<ChatSwitch
checked={readyData.channel.privacy === "private"}
disabled={busy || !readyData.permissions.canChangeGroupPrivacy}
onCheckedChange={(checked) => void actions.onPrivacyChange(checked ? "private" : "public")}
aria-label={t("chat.channelSettings.privacy.private")}
data-ui-id="ui.chat.channelSettings.privacyToggle"
/>
</ButtonGroup>
</Card>
) : null}
<Card size="large" data-ui-id="ui.chat.channelSettings.members">
<ButtonGroup align="space-between">
<ButtonGroup align="stack">
<span className="text-heading-2 font-heading-2 text-text-neutral-primary">{t("chat.channelSettings.members.title")}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{t("chat.channelSettings.members.count", { count: readyData.members.length })}
</span>
</ButtonGroup>
<FeatherShield aria-hidden="true" />
</ButtonGroup>
{canAddMember && readyData.candidateUsers.length > 0 ? (
<ButtonGroup align="start" data-ui-id="ui.chat.channelSettings.addMember">
<Select
className="min-w-64"
value={candidateUserId}
onValueChange={setCandidateUserId}
placeholder={t("chat.channelSettings.candidates.placeholder")}
triggerAriaLabel={t("chat.channelSettings.candidates.placeholder")}
>
{readyData.candidateUsers.map((candidate) => (
<Select.Item key={candidate.userId} value={candidate.userId}>
{candidate.displayName}
</Select.Item>
))}
</Select>
<Button
variant="secondary"
icon={<FeatherUserPlus />}
disabled={!candidateUserId || busy}
onClick={() => {
if (!candidateUserId) return;
void actions.onAddMember(candidateUserId);
}}
data-ui-id="ui.chat.channelSettings.addMemberSubmit"
>
{t("chat.channelSettings.actions.addMember")}
</Button>
</ButtonGroup>
) : null}
{readyData.members.length === 0 ? (
<EmptyState outlined title={t(readyData.keys.membersEmptyKey)} />
) : (
readyData.members.map((member) => (
<Card key={member.userId} size="small" variant={member.isCurrentUser ? "information" : "default"}>
<ButtonGroup align="space-between">
<ButtonGroup>
<Avatar image={member.avatar.imageUrl ?? undefined} size="small">
{member.avatar.fallbackLabel}
</Avatar>
<ButtonGroup align="stack">
<strong>{member.displayName}</strong>
<span>{member.email}</span>
</ButtonGroup>
</ButtonGroup>
<ButtonGroup>
<BadgeFeedback farbe={member.role === "admin" ? "subtle-brand-primary" : "subtle-neutral"} size="small">
{t(member.roleLabelKey)}
</BadgeFeedback>
<BadgeFeedback farbe="subtle-neutral" size="small">
{t(member.sourceLabelKey)}
</BadgeFeedback>
{member.actions.canPromoteToAdmin ? (
<Button size="small" variant="tertiary" disabled={busy} onClick={() => void actions.onChangeRole(member.userId, "admin")}>
{t("chat.channelSettings.actions.promote")}
</Button>
) : null}
{member.actions.canDemoteFromAdmin ? (
<Button size="small" variant="tertiary" disabled={busy} onClick={() => void actions.onChangeRole(member.userId, "member")}>
{t("chat.channelSettings.actions.demote")}
</Button>
) : null}
{member.actions.canRemoveFromGroup || member.actions.canRemoveManualGuest ? (
<Button
size="small"
variant="destructive"
icon={<FeatherTrash2 />}
disabled={busy}
onClick={() => void actions.onRemoveMember(member.userId)}
aria-label={t("chat.channelSettings.actions.removeMember", { member: member.displayName })}
/>
) : null}
</ButtonGroup>
</ButtonGroup>
</Card>
))
)}
</Card>
{readyData.leave.canLeave || readyData.leave.blockedReasonKey || readyData.permissions.canArchive ? (
<Card size="large" data-ui-id="ui.chat.channelSettings.channelActions">
<ButtonGroup align="stack">
<span className="text-heading-2 font-heading-2 text-text-neutral-primary">{t("chat.channelSettings.channelActions.title")}</span>
{readyData.leave.blockedReasonKey ? <Alert variant="warning" notDismissible title={t(readyData.leave.blockedReasonKey)} /> : null}
<ButtonGroup>
{readyData.leave.canLeave ? (
<Button variant="secondary" icon={<FeatherLogOut />} disabled={busy} onClick={() => setConfirmAction("leave")}>
{t("chat.channelSettings.actions.leave")}
</Button>
) : null}
{readyData.permissions.canArchive ? (
<Button variant="destructive" icon={<FeatherArchive />} disabled={busy} onClick={() => setConfirmAction("archive")}>
{t("chat.channelSettings.actions.archive")}
</Button>
) : null}
</ButtonGroup>
</ButtonGroup>
</Card>
) : null}
<Dialog open={confirmAction !== null} onOpenChange={(open: boolean) => (open ? undefined : setConfirmAction(null))}>
<Dialog.Content
className="w-[420px] max-w-[90vw] p-4"
aria-label={t(`chat.channelSettings.confirm.${confirmAction ?? "leave"}.title`)}
data-ui-id="ui.chat.channelSettings.confirm"
>
<ButtonGroup align="stack">
<DialogTitle className="text-heading-0 font-heading-0 text-text-neutral-primary">
{t(`chat.channelSettings.confirm.${confirmAction ?? "leave"}.title`)}
</DialogTitle>
<DialogDescription className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t(`chat.channelSettings.confirm.${confirmAction ?? "leave"}.body`)}
</DialogDescription>
</ButtonGroup>
<ButtonGroup align="end">
<Button variant="tertiary" disabled={busy} onClick={() => setConfirmAction(null)}>
{t("shared.cancel")}
</Button>
<Button
variant="destructive"
loading={busy}
onClick={() => {
if (confirmAction === "archive") void actions.onArchive();
if (confirmAction === "leave") void actions.onLeave();
}}
>
{t(`chat.channelSettings.confirm.${confirmAction ?? "leave"}.confirm`)}
</Button>
</ButtonGroup>
</Dialog.Content>
</Dialog>
</SettingsShell>
);
}
function SettingsShell({ children, actions }: { readonly children: React.ReactNode; readonly actions?: React.ReactNode }): React.JSX.Element {
const { t } = useTranslation();
return (
<PageShell uiId="ui.chat.channelSettings" className="mobile:[&_button]:min-h-11 mobile:[&_button]:min-w-11">
<PageHeader
icon={<FeatherSettings className={PAGE_HEADER_ICON_CLASS} />}
title={t("chat.channelSettings.title")}
subtitle={t("chat.channelSettings.subtitle")}
actions={actions}
uiId="ui.chat.channelSettings.header"
/>
<PageBody>{children}</PageBody>
</PageShell>
);
}