mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-20 02:10:15 +01:00
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
|
|
// client/src/hooks/Plugins/useCodeApiKeyForm.ts
|
||
|
|
import { useState, useCallback } from 'react';
|
||
|
|
import { useForm } from 'react-hook-form';
|
||
|
|
import type { ApiKeyFormData } from '~/common';
|
||
|
|
import useAuthCodeTool from '~/hooks/Plugins/useAuthCodeTool';
|
||
|
|
|
||
|
|
export default function useCodeApiKeyForm({
|
||
|
|
onSubmit,
|
||
|
|
onRevoke,
|
||
|
|
}: {
|
||
|
|
onSubmit?: () => void;
|
||
|
|
onRevoke?: () => void;
|
||
|
|
}) {
|
||
|
|
const methods = useForm<ApiKeyFormData>();
|
||
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
||
|
|
const { installTool, removeTool } = useAuthCodeTool({ isEntityTool: true });
|
||
|
|
const { reset } = methods;
|
||
|
|
|
||
|
|
const onSubmitHandler = useCallback(
|
||
|
|
(data: { apiKey: string }) => {
|
||
|
|
reset();
|
||
|
|
installTool(data.apiKey);
|
||
|
|
setIsDialogOpen(false);
|
||
|
|
onSubmit?.();
|
||
|
|
},
|
||
|
|
[onSubmit, reset, installTool],
|
||
|
|
);
|
||
|
|
|
||
|
|
const handleRevokeApiKey = useCallback(() => {
|
||
|
|
reset();
|
||
|
|
removeTool();
|
||
|
|
setIsDialogOpen(false);
|
||
|
|
onRevoke?.();
|
||
|
|
}, [reset, onRevoke, removeTool]);
|
||
|
|
|
||
|
|
return {
|
||
|
|
methods,
|
||
|
|
isDialogOpen,
|
||
|
|
setIsDialogOpen,
|
||
|
|
handleRevokeApiKey,
|
||
|
|
onSubmit: onSubmitHandler,
|
||
|
|
};
|
||
|
|
}
|