quivr/frontend/app/user/components/ApiKeyConfig/hooks/useApiKeyConfig.ts
Mamadou DICKO 3529222b95
Brain management 4 (#762)
* feat: add <ApiKeyConfig/>

* feat(SDK): add update brain

* feat: add removeUndefined helper

* feat: remove unnecessary autofocus flag

* add brain settings tab

* ui: add tab delimitor

* feat: improve ux
2023-07-25 23:12:46 +02:00

43 lines
1.0 KiB
TypeScript

import { useState } from "react";
import { useAuthApi } from "@/lib/api/auth/useAuthApi";
import { useEventTracking } from "@/services/analytics/useEventTracking";
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useApiKeyConfig = () => {
const [apiKey, setApiKey] = useState("");
const { track } = useEventTracking();
const { createApiKey } = useAuthApi();
const handleCreateClick = async () => {
try {
void track("CREATE_API_KEY");
const createdApiKey = await createApiKey();
setApiKey(createdApiKey);
} catch (error) {
console.error("Error creating API key: ", error);
}
};
const copyToClipboard = async (text: string) => {
try {
void track("COPY_API_KEY");
await navigator.clipboard.writeText(text);
} catch (err) {
console.error("Failed to copy:", err);
}
};
const handleCopyClick = () => {
if (apiKey !== "") {
void copyToClipboard(apiKey);
}
};
return {
handleCreateClick,
apiKey,
handleCopyClick,
};
};