mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 17:30:16 +01:00
fix: Ensure Message Send Requires Key 🔑 (#1281)
* fix: only allow message send when key is provided when required - create useRequiresKey hook - pass same disabled prop to Textarea, AttachFile, and SendButton - EndpointItem: add localization, stopPropagation, and remove commented code - separate some hooks to new Input dir - completely remove textareaHeight recoil state as is not needed - update imports for moved hooks - pass disabled prop to useTextarea * feat: add localization to textarea placeholders
This commit is contained in:
parent
f6118879e5
commit
00b6af8c74
14 changed files with 54 additions and 50 deletions
60
client/src/hooks/Input/useUserKey.ts
Normal file
60
client/src/hooks/Input/useUserKey.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { useMemo, useCallback } from 'react';
|
||||
import {
|
||||
useUpdateUserKeysMutation,
|
||||
useUserKeyQuery,
|
||||
useGetEndpointsQuery,
|
||||
} from 'librechat-data-provider';
|
||||
|
||||
const useUserKey = (endpoint: string) => {
|
||||
const { data: endpointsConfig } = useGetEndpointsQuery();
|
||||
const config = endpointsConfig?.[endpoint];
|
||||
|
||||
const { azure } = config ?? {};
|
||||
let keyEndpoint = endpoint;
|
||||
|
||||
if (azure) {
|
||||
keyEndpoint = 'azureOpenAI';
|
||||
} else if (keyEndpoint === 'gptPlugins') {
|
||||
keyEndpoint = 'openAI';
|
||||
}
|
||||
|
||||
const updateKey = useUpdateUserKeysMutation();
|
||||
const checkUserKey = useUserKeyQuery(keyEndpoint);
|
||||
const getExpiry = useCallback(() => {
|
||||
if (checkUserKey.data) {
|
||||
return checkUserKey.data.expiresAt;
|
||||
}
|
||||
}, [checkUserKey.data]);
|
||||
|
||||
const checkExpiry = useCallback(() => {
|
||||
const expiresAt = getExpiry();
|
||||
if (!expiresAt) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const expiresAtDate = new Date(expiresAt);
|
||||
if (expiresAtDate < new Date()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}, [getExpiry]);
|
||||
|
||||
const saveUserKey = useCallback(
|
||||
(value: string, expiresAt: number) => {
|
||||
const dateStr = new Date(expiresAt).toISOString();
|
||||
updateKey.mutate({
|
||||
name: keyEndpoint,
|
||||
value,
|
||||
expiresAt: dateStr,
|
||||
});
|
||||
},
|
||||
[updateKey, keyEndpoint],
|
||||
);
|
||||
|
||||
return useMemo(
|
||||
() => ({ getExpiry, checkExpiry, saveUserKey }),
|
||||
[getExpiry, checkExpiry, saveUserKey],
|
||||
);
|
||||
};
|
||||
|
||||
export default useUserKey;
|
||||
Loading…
Add table
Add a link
Reference in a new issue