mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-24 23:41:10 +02:00
- Updated all hooks to import and use AUTH_TOKEN_KEY constant from lib/constants - Updated all components to use AUTH_TOKEN_KEY instead of hardcoded string - Updated all atoms to use AUTH_TOKEN_KEY - Updated all app pages to use AUTH_TOKEN_KEY - Made CORS origin configurable via CORS_ORIGINS environment variable - Added CORS_ORIGINS to .env.example with documentation This improves maintainability by centralizing the auth token key string, preventing typos and making it easier to change the key name if needed.
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { useCallback, useEffect, useState } from "react";
|
|
import { toast } from "sonner";
|
|
import { AUTH_TOKEN_KEY } from "@/lib/constants";
|
|
|
|
interface UseApiKeyReturn {
|
|
apiKey: string | null;
|
|
isLoading: boolean;
|
|
copied: boolean;
|
|
copyToClipboard: () => Promise<void>;
|
|
}
|
|
|
|
export function useApiKey(): UseApiKeyReturn {
|
|
const [apiKey, setApiKey] = useState<string | null>(null);
|
|
const [copied, setCopied] = useState(false);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
// Load API key from localStorage
|
|
const loadApiKey = () => {
|
|
try {
|
|
const token = localStorage.getItem(AUTH_TOKEN_KEY);
|
|
setApiKey(token);
|
|
} catch (error) {
|
|
console.error("Error loading API key:", error);
|
|
toast.error("Failed to load API key");
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
// Add a small delay to simulate loading
|
|
const timer = setTimeout(loadApiKey, 500);
|
|
return () => clearTimeout(timer);
|
|
}, []);
|
|
|
|
const copyToClipboard = useCallback(async () => {
|
|
if (!apiKey) return;
|
|
|
|
try {
|
|
await navigator.clipboard.writeText(apiKey);
|
|
setCopied(true);
|
|
toast.success("API key copied to clipboard");
|
|
|
|
setTimeout(() => {
|
|
setCopied(false);
|
|
}, 2000);
|
|
} catch (err) {
|
|
console.error("Failed to copy:", err);
|
|
toast.error("Failed to copy API key");
|
|
}
|
|
}, [apiKey]);
|
|
|
|
return {
|
|
apiKey,
|
|
isLoading,
|
|
copied,
|
|
copyToClipboard,
|
|
};
|
|
}
|