11'use client' ;
22
33import { useEffect , useState } from 'react' ;
4+ import { ChevronDown } from 'lucide-react' ;
5+ import { useApiKey } from './api-key-context' ;
46
57interface ApiKeyLinkProps {
68 text ?: string ;
79}
810
911export function ApiKeyLink ( { text } : ApiKeyLinkProps ) {
1012 const [ isZh , setIsZh ] = useState ( false ) ;
13+ const [ isOpen , setIsOpen ] = useState ( false ) ;
14+ const { apiKeys, selectedApiKey, setSelectedApiKey, isLoading, error } = useApiKey ( ) ;
1115
1216 useEffect ( ( ) => {
1317 if ( typeof window !== 'undefined' ) {
@@ -18,20 +22,83 @@ export function ApiKeyLink({ text }: ApiKeyLinkProps) {
1822 const handleClick = ( ) => {
1923 if ( typeof window !== 'undefined' ) {
2024 const origin = window . location . origin . replace ( / \/ d o c s \/ ? $ / , '' ) ;
21- window . location . href = `${ origin } /api-keys ` ;
25+ window . location . href = `${ origin } /apps ` ;
2226 }
2327 } ;
2428
25- const displayText = text || ( isZh ? '点此查看 API Key' : 'Click to view API Key' ) ;
26- const title = isZh ? '点击前往 API Keys 页面' : 'Click to go to API Keys page' ;
29+ // If loading, error, or no API keys, fallback to original behavior
30+ if ( isLoading || error || apiKeys . length === 0 ) {
31+ const displayText = text || ( isZh ? '点此查看 API Key' : 'Click to view API Key' ) ;
32+ const title = isZh ? '点击前往 API Keys 页面' : 'Click to go to API Keys page' ;
33+
34+ return (
35+ < button
36+ onClick = { handleClick }
37+ className = "fd-inline-code cursor-pointer underline decoration-dotted underline-offset-2 hover:text-fd-primary transition-colors"
38+ title = { title }
39+ >
40+ { displayText }
41+ </ button >
42+ ) ;
43+ }
44+
45+ // Find the selected key info
46+ const selectedKeyInfo = apiKeys . find ( k => k . key === selectedApiKey ) ;
47+ const displayName = selectedKeyInfo ?. comment || ( isZh ? '选择 API Key' : 'Select API Key' ) ;
48+
49+ // Mask the API key for display
50+ const maskedKey = selectedApiKey
51+ ? `${ selectedApiKey . slice ( 0 , 8 ) } ...${ selectedApiKey . slice ( - 4 ) } `
52+ : '' ;
2753
2854 return (
29- < button
30- onClick = { handleClick }
31- className = "fd-inline-code cursor-pointer underline decoration-dotted underline-offset-2 hover:text-fd-primary transition-colors"
32- title = { title }
33- >
34- { displayText }
35- </ button >
55+ < span className = "relative inline-block" >
56+ < button
57+ onClick = { ( ) => setIsOpen ( ! isOpen ) }
58+ className = "fd-inline-code cursor-pointer inline-flex items-center gap-1 hover:text-fd-primary transition-colors"
59+ title = { isZh ? '点击选择 API Key' : 'Click to select API Key' }
60+ >
61+ < span > { maskedKey || displayName } </ span >
62+ < ChevronDown className = "size-3" />
63+ </ button >
64+
65+ { isOpen && (
66+ < >
67+ { /* Backdrop to close dropdown */ }
68+ < div
69+ className = "fixed inset-0 z-40"
70+ onClick = { ( ) => setIsOpen ( false ) }
71+ />
72+ { /* Dropdown menu */ }
73+ < div className = "absolute left-0 top-full mt-1 z-50 min-w-[200px] rounded-md border bg-fd-popover p-1 shadow-md" >
74+ { apiKeys . map ( ( apiKey ) => (
75+ < button
76+ key = { apiKey . key }
77+ onClick = { ( ) => {
78+ setSelectedApiKey ( apiKey . key ) ;
79+ setIsOpen ( false ) ;
80+ } }
81+ className = { `w-full text-left px-3 py-2 text-sm rounded-sm hover:bg-fd-accent hover:text-fd-accent-foreground transition-colors ${
82+ selectedApiKey === apiKey . key ? 'bg-fd-accent/50' : ''
83+ } `}
84+ >
85+ < div className = "font-medium" > { apiKey . comment || ( isZh ? '未命名' : 'Unnamed' ) } </ div >
86+ < div className = "text-xs text-fd-muted-foreground font-mono" >
87+ { apiKey . key . slice ( 0 , 8 ) } ...{ apiKey . key . slice ( - 4 ) }
88+ </ div >
89+ </ button >
90+ ) ) }
91+ < div className = "border-t mt-1 pt-1" >
92+ < button
93+ onClick = { handleClick }
94+ className = "w-full text-left px-3 py-2 text-sm rounded-sm hover:bg-fd-accent hover:text-fd-accent-foreground transition-colors text-fd-muted-foreground"
95+ >
96+ { isZh ? '管理 API Keys →' : 'Manage API Keys →' }
97+ </ button >
98+ </ div >
99+ </ div >
100+ </ >
101+ ) }
102+ </ span >
36103 ) ;
37104}
0 commit comments