sitcod3 commited on
Commit
e66d96b
·
verified ·
1 Parent(s): af073d0

Upload src/context/ApiKeyContext.tsx with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/context/ApiKeyContext.tsx +93 -0
src/context/ApiKeyContext.tsx ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { createContext, useContext, useState, useEffect, ReactNode } from "react";
2
+
3
+ interface ApiKey {
4
+ id: string;
5
+ name: string;
6
+ key: string;
7
+ createdAt: string;
8
+ }
9
+
10
+ interface ApiKeyContextType {
11
+ apiKeys: ApiKey[];
12
+ activeKeyId: string | null;
13
+ activeKey: string | null;
14
+ addApiKey: (name: string, key: string) => void;
15
+ removeApiKey: (id: string) => void;
16
+ renameApiKey: (id: string, newName: string) => void;
17
+ setActiveKeyId: (id: string) => void;
18
+ }
19
+
20
+ const ApiKeyContext = createContext<ApiKeyContextType | undefined>(undefined);
21
+
22
+ export function ApiKeyProvider({ children }: { children: ReactNode }) {
23
+ const [apiKeys, setApiKeys] = useState<ApiKey[]>(() => {
24
+ const saved = localStorage.getItem("smartlead_api_keys");
25
+ return saved ? JSON.parse(saved) : [];
26
+ });
27
+
28
+ const [activeKeyId, setActiveKeyIdState] = useState<string | null>(() => {
29
+ return localStorage.getItem("smartlead_active_key_id");
30
+ });
31
+
32
+ useEffect(() => {
33
+ localStorage.setItem("smartlead_api_keys", JSON.stringify(apiKeys));
34
+ }, [apiKeys]);
35
+
36
+ useEffect(() => {
37
+ if (activeKeyId) {
38
+ localStorage.setItem("smartlead_active_key_id", activeKeyId);
39
+ } else {
40
+ localStorage.removeItem("smartlead_active_key_id");
41
+ }
42
+ }, [activeKeyId]);
43
+
44
+ const addApiKey = (name: string, key: string) => {
45
+ const newKey: ApiKey = {
46
+ id: Math.random().toString(36).substring(7),
47
+ name,
48
+ key,
49
+ createdAt: new Date().toISOString()
50
+ };
51
+ setApiKeys((prev) => [...prev, newKey]);
52
+ if (!activeKeyId) {
53
+ setActiveKeyIdState(newKey.id);
54
+ }
55
+ };
56
+
57
+ const removeApiKey = (id: string) => {
58
+ setApiKeys((prev) => prev.filter((k) => k.id !== id));
59
+ if (activeKeyId === id) {
60
+ setActiveKeyIdState(null);
61
+ }
62
+ };
63
+
64
+ const renameApiKey = (id: string, newName: string) => {
65
+ setApiKeys((prev) => prev.map((k) => k.id === id ? { ...k, name: newName } : k));
66
+ };
67
+
68
+ const activeKey = apiKeys.find((k) => k.id === activeKeyId)?.key || null;
69
+
70
+ return (
71
+ <ApiKeyContext.Provider
72
+ value={{
73
+ apiKeys,
74
+ activeKeyId,
75
+ activeKey,
76
+ addApiKey,
77
+ removeApiKey,
78
+ renameApiKey,
79
+ setActiveKeyId: setActiveKeyIdState,
80
+ }}
81
+ >
82
+ {children}
83
+ </ApiKeyContext.Provider>
84
+ );
85
+ }
86
+
87
+ export function useApiKey() {
88
+ const context = useContext(ApiKeyContext);
89
+ if (context === undefined) {
90
+ throw new Error("useApiKey must be used within an ApiKeyProvider");
91
+ }
92
+ return context;
93
+ }