Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

feat(client): copy buttons, remembered model, confirm before key delete (#260)

danscMax26e8968010b8
diff --git a/client/src/components/copy-button.tsx b/client/src/components/copy-button.tsx
new file mode 100644
index 00000000..49be60fd
--- /dev/null
+++ b/client/src/components/copy-button.tsx
@@ -0,0 +1,32 @@
+import { useState } from 'react'
+import { Copy, Check } from 'lucide-react'
+import { cn } from '@/lib/utils'
+
+interface CopyButtonProps {
+ text: string
+ className?: string
+ label?: string
+}
+
+// Small icon button that copies `text` to the clipboard and briefly shows a
+// check. Shared by markdown code blocks and Playground replies.
+export function CopyButton({ text, className, label = 'Copy' }: CopyButtonProps) {
+ const [copied, setCopied] = useState(false)
+ return (
+ <button
+ type="button"
+ aria-label={copied ? 'Copied' : label}
+ onClick={() => {
+ void navigator.clipboard?.writeText(text)
+ setCopied(true)
+ setTimeout(() => setCopied(false), 1500)
+ }}
+ className={cn(
+ 'inline-flex items-center justify-center rounded-md border bg-background/80 text-muted-foreground transition-colors hover:text-foreground',
+ className,
+ )}
+ >
+ {copied ? <Check className="size-3.5" /> : <Copy className="size-3.5" />}
+ </button>
+ )
+}
diff --git a/client/src/components/markdown.tsx b/client/src/components/markdown.tsx
index 769380cc..700a6fb3 100644
--- a/client/src/components/markdown.tsx
+++ b/client/src/components/markdown.tsx
@@ -1,7 +1,31 @@
-import { memo } from 'react'
+import { memo, isValidElement, type ReactNode } from 'react'
import ReactMarkdown, { type Components } from 'react-markdown'
import remarkGfm from 'remark-gfm'
import { cn } from '@/lib/utils'
+import { CopyButton } from './copy-button'
+
+// react-markdown hands <pre> its rendered <code> element, not the raw source,
+// so pull the text back out of the React tree for the copy button.
+function nodeText(node: ReactNode): string {
+ if (node == null || typeof node === 'boolean') return ''
+ if (typeof node === 'string' || typeof node === 'number') return String(node)
+ if (Array.isArray(node)) return node.map(nodeText).join('')
+ if (isValidElement(node)) return nodeText((node.props as { children?: ReactNode }).children)
+ return ''
+}
+
+function Pre({ children }: { children?: ReactNode }) {
+ return (
+ <pre className="group relative my-2 overflow-x-auto rounded-lg border bg-background/60 p-3 first:mt-0 last:mb-0">
+ <CopyButton
+ text={nodeText(children).replace(/\n$/, '')}
+ label="Copy code"
+ className="absolute right-2 top-2 size-7 opacity-0 transition-opacity focus-visible:opacity-100 group-hover:opacity-100"
+ />
+ {children}
+ </pre>
+ )
+}
const components: Components = {
p: ({ children }) => (
@@ -90,11 +114,7 @@ const components: Components = {
</code>
)
},
- pre: ({ children }) => (
- <pre className="my-2 overflow-x-auto rounded-lg border bg-background/60 p-3 first:mt-0 last:mb-0">
- {children}
- </pre>
- ),
+ pre: ({ children }) => <Pre>{children}</Pre>,
}
interface MarkdownProps {
diff --git a/client/src/pages/KeysPage.tsx b/client/src/pages/KeysPage.tsx
index 671a30a4..485f034f 100644
--- a/client/src/pages/KeysPage.tsx
+++ b/client/src/pages/KeysPage.tsx
@@ -382,6 +382,7 @@ export default function KeysPage() {
const [label, setLabel] = useState('')
const [editingKeyId, setEditingKeyId] = useState<number | null>(null)
const [editingLabel, setEditingLabel] = useState('')
+ const [confirmDeleteId, setConfirmDeleteId] = useState<number | null>(null)
const editInputRef = useRef<HTMLInputElement>(null)
const { data: keys = [], isLoading } = useQuery<ApiKey[]>({
@@ -691,8 +692,22 @@ export default function KeysPage() {
<Button variant="ghost" size="xs" onClick={() => checkKey.mutate(k.id)} disabled={checkKey.isPending}>
Check
</Button>
- <Button variant="ghost" size="xs" className="text-muted-foreground hover:text-destructive" onClick={() => deleteKey.mutate(k.id)} disabled={deleteKey.isPending}>
- Remove
+ <Button
+ variant="ghost"
+ size="xs"
+ className={confirmDeleteId === k.id ? 'text-destructive' : 'text-muted-foreground hover:text-destructive'}
+ onClick={() => {
+ if (confirmDeleteId === k.id) {
+ deleteKey.mutate(k.id)
+ setConfirmDeleteId(null)
+ } else {
+ setConfirmDeleteId(k.id)
+ setTimeout(() => setConfirmDeleteId(c => (c === k.id ? null : c)), 3000)
+ }
+ }}
+ disabled={deleteKey.isPending}
+ >
+ {confirmDeleteId === k.id ? 'Confirm?' : 'Remove'}
</Button>
</div>
)
diff --git a/client/src/pages/PlaygroundPage.tsx b/client/src/pages/PlaygroundPage.tsx
index 77621e63..72a9f820 100644
--- a/client/src/pages/PlaygroundPage.tsx
+++ b/client/src/pages/PlaygroundPage.tsx
@@ -5,6 +5,7 @@ import { Button } from '@/components/ui/button'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
import { PageHeader } from '@/components/page-header'
import { Markdown } from '@/components/markdown'
+import { CopyButton } from '@/components/copy-button'
interface FallbackEntry {
modelDbId: number
@@ -32,7 +33,9 @@ export default function PlaygroundPage() {
const [messages, setMessages] = useState<ChatMessage[]>([])
const [input, setInput] = useState('')
const [loading, setLoading] = useState(false)
- const [selectedModel, setSelectedModel] = useState<string>('auto')
+ const [selectedModel, setSelectedModel] = useState<string>(
+ () => localStorage.getItem('playground.model') ?? 'auto',
+ )
const messagesEndRef = useRef<HTMLDivElement>(null)
const inputRef = useRef<HTMLTextAreaElement>(null)
@@ -144,7 +147,7 @@ export default function PlaygroundPage() {
description="Send a chat completion through the router and see which provider serves it."
actions={
<>
- <Select value={selectedModel} onValueChange={(v) => setSelectedModel(v ?? 'auto')}>
+ <Select value={selectedModel} onValueChange={(v) => { const m = v ?? 'auto'; setSelectedModel(m); localStorage.setItem('playground.model', m) }}>
<SelectTrigger className="w-[260px]">
<SelectValue />
</SelectTrigger>
@@ -192,7 +195,7 @@ export default function PlaygroundPage() {
{messages.map((msg, i) => (
<div key={i} className={`flex ${msg.role === 'user' ? 'justify-end' : 'justify-start'}`}>
<div
- className={`max-w-[78%] rounded-2xl px-4 py-2.5 text-sm leading-relaxed ${
+ className={`group relative max-w-[78%] rounded-2xl px-4 py-2.5 text-sm leading-relaxed ${
msg.role === 'user'
? 'bg-primary text-primary-foreground'
: 'bg-muted'
@@ -203,6 +206,13 @@ export default function PlaygroundPage() {
) : (
<div className="whitespace-pre-wrap">{msg.content}</div>
)}
+ {msg.role === 'assistant' && msg.content && (
+ <CopyButton
+ text={msg.content}
+ label="Copy reply"
+ className="absolute right-1.5 top-1.5 size-6 opacity-0 transition-opacity focus-visible:opacity-100 group-hover:opacity-100"
+ />
+ )}
{msg.meta && (
<div className="flex items-center gap-2 mt-2 flex-wrap text-[11px] opacity-70 tabular-nums">
{msg.meta.platform && <span>{msg.meta.platform}</span>}