Spaces:
Running
Running
Upload APIKeyManager.tsx
Browse files
app/components/chat/APIKeyManager.tsx
CHANGED
@@ -32,7 +32,7 @@ export function getApiKeysFromCookies() {
|
|
32 |
// eslint-disable-next-line @typescript-eslint/naming-convention
|
33 |
export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey, setApiKey }) => {
|
34 |
const [isEditing, setIsEditing] = useState(false);
|
35 |
-
const [tempKey, setTempKey] = useState(apiKey);
|
36 |
const [isPromptCachingEnabled, setIsPromptCachingEnabled] = useState(() => {
|
37 |
// Read initial state from localStorage, defaulting to true
|
38 |
const savedState = localStorage.getItem('PROMPT_CACHING_ENABLED');
|
@@ -44,9 +44,32 @@ export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey,
|
|
44 |
localStorage.setItem('PROMPT_CACHING_ENABLED', JSON.stringify(isPromptCachingEnabled));
|
45 |
}, [isPromptCachingEnabled]);
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
const handleSave = () => {
|
48 |
setApiKey(tempKey);
|
49 |
setIsEditing(false);
|
|
|
50 |
};
|
51 |
|
52 |
return (
|
|
|
32 |
// eslint-disable-next-line @typescript-eslint/naming-convention
|
33 |
export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey, setApiKey }) => {
|
34 |
const [isEditing, setIsEditing] = useState(false);
|
35 |
+
const [tempKey, setTempKey] = useState(apiKey || cachedApiKeysOps().get());
|
36 |
const [isPromptCachingEnabled, setIsPromptCachingEnabled] = useState(() => {
|
37 |
// Read initial state from localStorage, defaulting to true
|
38 |
const savedState = localStorage.getItem('PROMPT_CACHING_ENABLED');
|
|
|
44 |
localStorage.setItem('PROMPT_CACHING_ENABLED', JSON.stringify(isPromptCachingEnabled));
|
45 |
}, [isPromptCachingEnabled]);
|
46 |
|
47 |
+
function cachedApiKeysOps() {
|
48 |
+
const STORAGE_KEY = 'PROVIDER_API_KEYS';
|
49 |
+
|
50 |
+
const providerName = provider?.name;
|
51 |
+
const getStoredData = () => JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}');
|
52 |
+
|
53 |
+
const get = () => {
|
54 |
+
const savedData = getStoredData();
|
55 |
+
return providerName ? savedData[providerName] || '' : savedData;
|
56 |
+
};
|
57 |
+
|
58 |
+
const save = (value: string) => {
|
59 |
+
const savedData = getStoredData();
|
60 |
+
savedData[providerName] = value;
|
61 |
+
localStorage.setItem(STORAGE_KEY, JSON.stringify(savedData));
|
62 |
+
|
63 |
+
return value;
|
64 |
+
};
|
65 |
+
|
66 |
+
return { get, save };
|
67 |
+
}
|
68 |
+
|
69 |
const handleSave = () => {
|
70 |
setApiKey(tempKey);
|
71 |
setIsEditing(false);
|
72 |
+
cachedApiKeysOps().save(tempKey);
|
73 |
};
|
74 |
|
75 |
return (
|