From f14efa18b3f75a42bad87b48f62dad5d63fa029d Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Sun, 8 Mar 2026 20:34:12 +0530 Subject: [PATCH] refactor: enhance ApiKeyContent and MobileSidebar components with improved styling and event handling for better user experience --- .../components/ApiKeyContent.tsx | 109 ++++++++++++------ .../layout/ui/sidebar/MobileSidebar.tsx | 27 ++++- 2 files changed, 96 insertions(+), 40 deletions(-) diff --git a/surfsense_web/app/dashboard/[search_space_id]/user-settings/components/ApiKeyContent.tsx b/surfsense_web/app/dashboard/[search_space_id]/user-settings/components/ApiKeyContent.tsx index c45eca45c..a48b29bc7 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/user-settings/components/ApiKeyContent.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/user-settings/components/ApiKeyContent.tsx @@ -1,16 +1,30 @@ "use client"; -import { Check, Copy, Shield } from "lucide-react"; +import { Check, Copy, Info } from "lucide-react"; import { AnimatePresence, motion } from "motion/react"; +import { useCallback, useRef, useState } from "react"; import { useTranslations } from "next-intl"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"; import { useApiKey } from "@/hooks/use-api-key"; +import { copyToClipboard as copyToClipboardUtil } from "@/lib/utils"; export function ApiKeyContent() { const t = useTranslations("userSettings"); const { apiKey, isLoading, copied, copyToClipboard } = useApiKey(); + const [copiedUsage, setCopiedUsage] = useState(false); + const usageCopyTimeoutRef = useRef>(null); + + const copyUsageToClipboard = useCallback(async () => { + const text = `Authorization: Bearer ${apiKey || "YOUR_API_KEY"}`; + const success = await copyToClipboardUtil(text); + if (success) { + setCopiedUsage(true); + if (usageCopyTimeoutRef.current) clearTimeout(usageCopyTimeoutRef.current); + usageCopyTimeoutRef.current = setTimeout(() => setCopiedUsage(false), 2000); + } + }, [apiKey]); return ( @@ -22,49 +36,70 @@ export function ApiKeyContent() { transition={{ duration: 0.35, ease: [0.4, 0, 0.2, 1] }} className="space-y-6" > - - - {t("api_key_warning_title")} - {t("api_key_warning_description")} - + + + {t("api_key_warning_title")} + {t("api_key_warning_description")} + -
-

{t("your_api_key")}

- {isLoading ? ( -
- ) : apiKey ? ( -
-
+
+

{t("your_api_key")}

+ {isLoading ? ( +
+ ) : apiKey ? ( +
+
+

{apiKey} -

- - - - - - {copied ? t("copied") : t("copy")} - - +

- ) : ( -

{t("no_api_key")}

- )} -
+ + + + + + {copied ? t("copied") : t("copy")} + + +
+ ) : ( +

{t("no_api_key")}

+ )} +
-
-

{t("usage_title")}

-

{t("usage_description")}

-
+			
+

{t("usage_title")}

+

{t("usage_description")}

+
+
+
 						Authorization: Bearer {apiKey || "YOUR_API_KEY"}
 					
+ + + + + + {copiedUsage ? t("copied") : t("copy")} + + +
+
); diff --git a/surfsense_web/components/layout/ui/sidebar/MobileSidebar.tsx b/surfsense_web/components/layout/ui/sidebar/MobileSidebar.tsx index f5909ef85..18dafd5d8 100644 --- a/surfsense_web/components/layout/ui/sidebar/MobileSidebar.tsx +++ b/surfsense_web/components/layout/ui/sidebar/MobileSidebar.tsx @@ -166,9 +166,30 @@ export function MobileSidebar({ : undefined } user={user} - onSettings={onSettings} - onManageMembers={onManageMembers} - onUserSettings={onUserSettings} + onSettings={ + onSettings + ? () => { + onOpenChange(false); + onSettings(); + } + : undefined + } + onManageMembers={ + onManageMembers + ? () => { + onOpenChange(false); + onManageMembers(); + } + : undefined + } + onUserSettings={ + onUserSettings + ? () => { + onOpenChange(false); + onUserSettings(); + } + : undefined + } onLogout={onLogout} pageUsage={pageUsage} theme={theme}