SurfSense/surfsense_web/hooks/use-api-key.ts
2026-04-04 01:28:45 +05:30

66 lines
1.7 KiB
TypeScript

import { useCallback, useEffect, useRef, useState } from "react";
import { toast } from "sonner";
import { getBearerToken } from "@/lib/auth-utils";
import { copyToClipboard as copyToClipboardUtil } from "@/lib/utils";
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);
const copyTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
useEffect(() => {
return () => {
if (copyTimerRef.current) clearTimeout(copyTimerRef.current);
};
}, []);
useEffect(() => {
// Load API key from localStorage
const loadApiKey = () => {
try {
const token = getBearerToken();
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;
const success = await copyToClipboardUtil(apiKey);
if (success) {
setCopied(true);
toast.success("API key copied to clipboard");
if (copyTimerRef.current) clearTimeout(copyTimerRef.current);
copyTimerRef.current = setTimeout(() => {
setCopied(false);
}, 2000);
} else {
toast.error("Failed to copy API key");
}
}, [apiKey]);
return {
apiKey,
isLoading,
copied,
copyToClipboard,
};
}