/* * Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors. * SPDX-License-Identifier: GPL-2.0-or-later */ import { useRef } from "react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { toast } from "react-hot-toast"; import { TrashIcon } from "@heroicons/react/24/outline"; import { KeyField } from "@components/fields/text"; import { DeleteModal } from "@components/modals"; import { APIKeyAddForm } from "@forms/settings/APIKeyAddForm"; import Toast from "@components/notifications/Toast"; import { APIClient } from "@api/APIClient"; import { useToggle } from "@hooks/hooks"; import { classNames } from "@utils"; import { EmptySimple } from "@components/emptystates"; export const apiKeys = { all: ["api_keys"] as const, lists: () => [...apiKeys.all, "list"] as const, details: () => [...apiKeys.all, "detail"] as const, // detail: (id: number) => [...apiKeys.details(), id] as const detail: (id: string) => [...apiKeys.details(), id] as const }; function APISettings() { const [addFormIsOpen, toggleAddForm] = useToggle(false); const { data } = useQuery({ queryKey: apiKeys.lists(), queryFn: APIClient.apikeys.getAll, retry: false, refetchOnWindowFocus: false, onError: (err) => console.log(err) }); return (

API keys

Manage API keys.

{data && data.length > 0 ? (
  1. Name
    Key
  2. {data && data.map((k, idx) => )}
) : ( )}
); } interface ApiKeyItemProps { apikey: APIKey; } function APIListItem({ apikey }: ApiKeyItemProps) { const cancelModalButtonRef = useRef(null); const [deleteModalIsOpen, toggleDeleteModal] = useToggle(false); const queryClient = useQueryClient(); const deleteMutation = useMutation({ mutationFn: (key: string) => APIClient.apikeys.delete(key), onSuccess: () => { queryClient.invalidateQueries({ queryKey: apiKeys.lists() }); queryClient.invalidateQueries({ queryKey: apiKeys.detail(apikey.key) }); toast.custom((t) => ( )); } }); return (
  • { deleteMutation.mutate(apikey.key); toggleDeleteModal(); }} title={`Remove API key: ${apikey.name}`} text="Are you sure you want to remove this API key? This action cannot be undone." />
    {apikey.name}
  • ); } export default APISettings;