import { useState } from "react"; import { Button, Group, Loader, Select, Stack } from "@mantine/core"; import { clientApi } from "@homarr/api/client"; import type { GroupPermissionKey } from "@homarr/definitions"; import { useForm } from "@homarr/form"; import { createModal } from "@homarr/modals"; import { useI18n } from "@homarr/translation/client"; interface InnerProps { withPermissions?: boolean; presentGroupIds: string[]; onSelect: (props: { id: string; name: string; permissions?: GroupPermissionKey[] }) => void | Promise; confirmLabel?: string; } interface GroupSelectFormType { groupId: string; } export const GroupSelectModal = createModal(({ actions, innerProps }) => { const t = useI18n(); const { data: groups, isPending } = clientApi.group.selectable.useQuery({ withPermissions: innerProps.withPermissions, }); const [loading, setLoading] = useState(false); const form = useForm(); const handleSubmitAsync = async (values: GroupSelectFormType) => { const currentGroup = groups?.find((group) => group.id === values.groupId); if (!currentGroup) return; setLoading(true); await innerProps.onSelect({ id: currentGroup.id, name: currentGroup.name, permissions: "permissions" in currentGroup ? (currentGroup.permissions as GroupPermissionKey[]) : undefined, }); setLoading(false); actions.closeModal(); }; const confirmLabel = innerProps.confirmLabel ?? t("common.action.add"); return (
void handleSubmitAsync(values))}>