Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat(providers): add copy and reveal buttons to API key inputs

Akarshan Biswasec378cc3c34f
diff --git a/web-app/src/components/ui/secret-input.tsx b/web-app/src/components/ui/secret-input.tsx
new file mode 100644
index 000000000..2210c803c
--- /dev/null
+++ b/web-app/src/components/ui/secret-input.tsx
@@ -0,0 +1,64 @@
+import { Input } from '@/components/ui/input'
+import { cn } from '@/lib/utils'
+import {
+ IconCopy,
+ IconCopyCheck,
+ IconEye,
+ IconEyeOff,
+} from '@tabler/icons-react'
+import { useState } from 'react'
+
+type SecretInputProps = Omit<
+ React.ComponentProps<typeof Input>,
+ 'type'
+>
+
+export function SecretInput({ className, value, ...props }: SecretInputProps) {
+ const [revealed, setRevealed] = useState(false)
+ const [copied, setCopied] = useState(false)
+
+ const stringValue = typeof value === 'string' ? value : String(value ?? '')
+
+ const handleCopy = () => {
+ if (!stringValue) return
+ navigator.clipboard.writeText(stringValue)
+ setCopied(true)
+ setTimeout(() => setCopied(false), 2000)
+ }
+
+ return (
+ <div className="relative w-full">
+ <Input
+ {...props}
+ value={value}
+ type={revealed ? 'text' : 'password'}
+ className={cn('pr-16', className)}
+ />
+ <div className="absolute right-1.5 top-1/2 -translate-y-1/2 flex items-center gap-0.5">
+ <button
+ type="button"
+ tabIndex={-1}
+ aria-label={revealed ? 'Hide' : 'Reveal'}
+ className="p-1 rounded text-muted-foreground hover:bg-secondary/50"
+ onClick={() => setRevealed((v) => !v)}
+ >
+ {revealed ? <IconEyeOff size={16} /> : <IconEye size={16} />}
+ </button>
+ <button
+ type="button"
+ tabIndex={-1}
+ aria-label="Copy"
+ disabled={!stringValue}
+ className="p-1 rounded text-muted-foreground hover:bg-secondary/50 disabled:opacity-40 disabled:pointer-events-none"
+ onClick={handleCopy}
+ >
+ {copied ? (
+ <IconCopyCheck size={16} className="text-primary" />
+ ) : (
+ <IconCopy size={16} />
+ )}
+ </button>
+ </div>
+ </div>
+ )
+}
diff --git a/web-app/src/containers/ApiKeyInput.tsx b/web-app/src/containers/ApiKeyInput.tsx
index cfcf8ac63..83e8d6fe5 100644
--- a/web-app/src/containers/ApiKeyInput.tsx
+++ b/web-app/src/containers/ApiKeyInput.tsx
@@ -1,7 +1,7 @@
import { Input } from '@/components/ui/input'
import { useLocalApiServer } from '@/hooks/useLocalApiServer'
import { useState, useEffect, useCallback } from 'react'
-import { Eye, EyeOff } from 'lucide-react'
+import { Eye, EyeOff, Copy, CopyCheck } from 'lucide-react'
import { useTranslation } from '@/i18n/react-i18next-compat'
import { cn } from '@/lib/utils'
@@ -19,6 +19,7 @@ export function ApiKeyInput({
const { apiKey, setApiKey } = useLocalApiServer()
const [inputValue, setInputValue] = useState(apiKey.toString())
const [showPassword, setShowPassword] = useState(false)
+ const [copied, setCopied] = useState(false)
const [error, setError] = useState('')
const { t } = useTranslation()
@@ -61,6 +62,13 @@ export function ApiKeyInput({
}
}
+ const handleCopy = () => {
+ if (!inputValue) return
+ navigator.clipboard.writeText(inputValue)
+ setCopied(true)
+ setTimeout(() => setCopied(false), 2000)
+ }
+
const hasError = error && showError
return (
@@ -71,7 +79,7 @@ export function ApiKeyInput({
onChange={handleChange}
onBlur={handleBlur}
className={cn(
- 'w-full text-sm pr-10',
+ 'w-full text-sm pr-16',
hasError &&
'border border-destructive focus:border-destructive focus:ring-destructive',
isServerRunning && 'opacity-50 pointer-events-none'
@@ -86,6 +94,18 @@ export function ApiKeyInput({
>
{showPassword ? <EyeOff size={16} /> : <Eye size={16} />}
</button>
+ <button
+ onClick={handleCopy}
+ disabled={!inputValue}
+ className="p-1 rounded hover:bg-secondary/50 text-muted-foreground disabled:opacity-40 disabled:pointer-events-none"
+ type="button"
+ >
+ {copied ? (
+ <CopyCheck size={16} className="text-primary" />
+ ) : (
+ <Copy size={16} />
+ )}
+ </button>
</div>
{hasError && (
<p className="text-destructive text-xs mt-1 absolute -bottom-5 left-0">
diff --git a/web-app/src/routes/settings/providers/$providerName.tsx b/web-app/src/routes/settings/providers/$providerName.tsx
index bbc56ade6..2f207ec68 100644
--- a/web-app/src/routes/settings/providers/$providerName.tsx
+++ b/web-app/src/routes/settings/providers/$providerName.tsx
@@ -20,7 +20,7 @@ import { route } from '@/constants/routes'
import DeleteProvider from '@/containers/dialogs/DeleteProvider'
import { useServiceHub } from '@/hooks/useServiceHub'
import { Button } from '@/components/ui/button'
-import { Input } from '@/components/ui/input'
+import { SecretInput } from '@/components/ui/secret-input'
import { Switch } from '@/components/ui/switch'
import {
IconCircleCheck,
@@ -1001,8 +1001,7 @@ function ProviderDetail() {
</div>
{!showAdvancedApiKeys && (
<div className="flex flex-col gap-2">
- <Input
- type="password"
+ <SecretInput
className="font-mono"
placeholder={t('providers:apiKeys.primaryPlaceholder')}
value={primaryKeyDraft}
@@ -1092,8 +1091,7 @@ function ProviderDetail() {
</div>
<div className="min-w-0">
- <Input
- type="password"
+ <SecretInput
className="font-mono w-full"
placeholder={t('providers:apiKeys.keyPlaceholder')}
value={keyValue}
diff --git a/web-app/src/routes/settings/providers/__tests__/$providerName.test.tsx b/web-app/src/routes/settings/providers/__tests__/$providerName.test.tsx
index a422f3a74..293faccfa 100644
--- a/web-app/src/routes/settings/providers/__tests__/$providerName.test.tsx
+++ b/web-app/src/routes/settings/providers/__tests__/$providerName.test.tsx
@@ -288,6 +288,10 @@ vi.mock('@tabler/icons-react', () => ({
IconTrash: () => <span />,
IconCircle: () => <span />,
IconCircleCheck: () => <span />,
+ IconEye: () => <span />,
+ IconEyeOff: () => <span />,
+ IconCopy: () => <span />,
+ IconCopyCheck: () => <span />,
}))
vi.mock('@/lib/utils', () => ({