import Button from "@/components/ui/button"; import { useKeys } from "@/pages/keys/hooks"; import { zodResolver } from "@hookform/resolvers/zod"; import { Plus } from "lucide-react"; import { useEffect } from "react"; import { Checkbox, Modal, Table } from "react-daisyui"; import { useFieldArray, useForm } from "react-hook-form"; import { AllowKeysSchema, allowKeysSchema } from "../schema"; import { useDisclosure } from "@/hooks/useDisclosure"; import { CheckboxField } from "@/components/ui/checkbox"; import { useAllowKey } from "../hooks"; import { toast } from "sonner"; import { handleError } from "@/lib/utils"; import { useQueryClient } from "@tanstack/react-query"; import { useBucketContext } from "../context"; type Props = { currentKeys?: string[]; }; const AllowKeyDialog = ({ currentKeys }: Props) => { const { bucket } = useBucketContext(); const { dialogRef, isOpen, onOpen, onClose } = useDisclosure(); const { data: keys } = useKeys(); const form = useForm({ resolver: zodResolver(allowKeysSchema), }); const { fields: keyFields } = useFieldArray({ control: form.control, name: "keys", }); const queryClient = useQueryClient(); const allowKey = useAllowKey(bucket.id, { onSuccess: () => { form.reset(); onClose(); toast.success("Key allowed!"); queryClient.invalidateQueries({ queryKey: ["bucket", bucket.id] }); }, onError: handleError, }); useEffect(() => { const _keys = keys ?.filter((key) => !currentKeys?.includes(key.id)) ?.map((key) => ({ checked: false, keyId: key.id, name: key.name, read: false, write: false, owner: false, })); form.setValue("keys", _keys || []); }, [keys, currentKeys]); const onToggleAll = ( e: React.ChangeEvent, field: keyof AllowKeysSchema["keys"][number] ) => { const curValues = form.getValues("keys"); const newValues = curValues.map((item) => ({ ...item, [field]: e.target.checked, })); form.setValue("keys", newValues); }; const onSubmit = form.handleSubmit((values) => { const data = values.keys .filter((key) => key.checked) .map((key) => ({ keyId: key.keyId, permissions: { read: key.read, write: key.write, owner: key.owner }, })); allowKey.mutate(data); }); return ( <> Allow Key

Enter the key you want to allow access to.

{!keyFields.length ? ( ) : null} {keyFields.map((field, index) => { const curKey = bucket.keys.find( (key) => key.accessKeyId === field.keyId ); return ( {curKey?.bucketLocalAliases?.join(", ") || "-"} ); })}
No keys found
); }; export default AllowKeyDialog;