Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/users/GroupMembersDialog.tsx
T
2026-07-08 16:15:37 +02:00

172 lines
6.7 KiB
TypeScript

// R-P2b — groups bulk add/remove (interim UI per AD-28). David parked groups
// for RBAC V2, so this is a `~/ui` preview screen with an open design-nachzug:
// a real Dialog (M 720) with the group's members and the remaining tenant
// members as checkbox pick lists, wired to the idempotent bulk backend ops.
// Pure presentational: VM + action callbacks as props, no loader access.
import { useState } from "react";
import { useTranslation } from "react-i18next";
import type { GroupMemberRow, GroupsDetailVM } from "@hivegrid/view-models";
import { filterGroupMemberRows } from "@hivegrid/view-models";
import { Avatar } from "~/ui/components/Avatar";
import { Button } from "~/ui/components/Button";
import { Checkbox } from "~/ui/components/Checkbox";
import { Dialog } from "~/ui/components/Dialog";
import { EmptyState } from "~/ui/components/EmptyState";
import { SearchInput } from "~/ui/components/SearchInput";
export type GroupMembersActions = {
/** Posts the `bulk-add` intent (idempotent addUsersToGroup). */
onAdd: (userIds: string[]) => void;
/** Posts the `bulk-remove` intent (idempotent removeUsersFromGroup). */
onRemove: (userIds: string[]) => void;
};
export function GroupMembersDialog({
vm,
actions,
submitting,
onClose,
}: {
vm: GroupsDetailVM;
actions: GroupMembersActions;
submitting: boolean;
onClose: () => void;
}) {
const { t } = useTranslation();
const [query, setQuery] = useState("");
const [selectedMembers, setSelectedMembers] = useState<ReadonlySet<string>>(new Set());
const [selectedCandidates, setSelectedCandidates] = useState<ReadonlySet<string>>(new Set());
if (vm.status !== "ready" || !vm.data) return null;
const data = vm.data;
const members = filterGroupMemberRows(data.memberRows, query);
const candidates = filterGroupMemberRows(data.candidateRows, query);
function toggle(set: ReadonlySet<string>, userId: string, next: (value: Set<string>) => void) {
const updated = new Set(set);
if (updated.has(userId)) updated.delete(userId);
else updated.add(userId);
next(updated);
}
return (
<Dialog open onOpenChange={(open: boolean) => (open ? undefined : onClose())}>
<Dialog.Content className="w-[720px] max-w-[95vw] p-6">
<div className="flex w-full flex-col gap-6" data-ui-id="ui.settings.groups.bulk.dialog">
<div className="flex flex-col gap-0">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">
{t("settings.members.groups.bulk.title", { name: data.name })}
</span>
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t("settings.members.groups.bulk.description")}
</span>
</div>
<SearchInput
className="max-w-[320px]"
placeholder={t("settings.members.groups.bulk.searchPlaceholder")}
hideShortcut
value={query}
onChange={(event) => setQuery(event.target.value)}
/>
<div className="flex w-full gap-6 mobile:flex-col">
<PickList
titleKey="settings.members.groups.bulk.membersTitle"
rows={members}
emptyKey={data.emptyMembersKey}
selected={selectedMembers}
onToggle={(userId) => toggle(selectedMembers, userId, setSelectedMembers)}
dataUiId="ui.settings.groups.bulk.members"
/>
<PickList
titleKey="settings.members.groups.bulk.candidatesTitle"
rows={candidates}
emptyKey={data.emptyCandidatesKey}
selected={selectedCandidates}
onToggle={(userId) => toggle(selectedCandidates, userId, setSelectedCandidates)}
dataUiId="ui.settings.groups.bulk.candidates"
/>
</div>
<div className="flex w-full flex-wrap items-center justify-end gap-2">
<Button variant="tertiary" onClick={onClose}>
{t("shared.close")}
</Button>
{data.canManage ? (
<>
<Button
variant="destructive"
disabled={selectedMembers.size === 0}
loading={submitting}
onClick={() => {
actions.onRemove([...selectedMembers]);
setSelectedMembers(new Set());
}}
data-ui-id="ui.settings.groups.bulk.remove"
>
{t("settings.members.groups.bulk.removeSelected", { count: selectedMembers.size })}
</Button>
<Button
disabled={selectedCandidates.size === 0}
loading={submitting}
onClick={() => {
actions.onAdd([...selectedCandidates]);
setSelectedCandidates(new Set());
}}
data-ui-id="ui.settings.groups.bulk.add"
>
{t("settings.members.groups.bulk.addSelected", { count: selectedCandidates.size })}
</Button>
</>
) : null}
</div>
</div>
</Dialog.Content>
</Dialog>
);
}
function PickList({
titleKey,
rows,
emptyKey,
selected,
onToggle,
dataUiId,
}: {
titleKey: string;
rows: readonly GroupMemberRow[];
emptyKey: string;
selected: ReadonlySet<string>;
onToggle: (userId: string) => void;
dataUiId: string;
}) {
const { t } = useTranslation();
return (
<div className="flex w-full flex-col gap-4" data-ui-id={dataUiId}>
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{t(titleKey)}</span>
{rows.length === 0 ? (
<EmptyState variant="no-results" title={t(emptyKey)} />
) : (
<div className="flex max-h-[40vh] w-full flex-col gap-2 overflow-y-auto">
{rows.map((row) => (
<div key={row.userId} className="flex w-full items-center justify-between gap-3">
<div className="flex items-center gap-3">
<Avatar size="small">{row.initials}</Avatar>
<div className="flex flex-col items-start">
<span className="whitespace-nowrap text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{row.name}</span>
<span className="whitespace-nowrap text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">{row.email}</span>
</div>
</div>
<Checkbox checked={selected.has(row.userId)} onCheckedChange={() => onToggle(row.userId)} />
</div>
))}
</div>
)}
</div>
);
}