Skip to content

Commit bcfc04a

Browse files
committed
refactor: extract CopyButton as TextCopyButton
1 parent c385049 commit bcfc04a

2 files changed

Lines changed: 33 additions & 37 deletions

File tree

app/(model)/deploy-model/deploy-page.tsx

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
'use client'
22

33
import * as React from 'react'
4-
import { ReactNode } from 'react'
54
import { ModelIcon } from '@lobehub/icons'
6-
import { useClipboard } from '@mantine/hooks'
75
import { useQuery } from '@tanstack/react-query'
8-
import { AlertCircleIcon, CheckCircle2Icon, CheckIcon, CopyIcon } from 'lucide-react'
9-
import { toast } from 'sonner'
6+
import { AlertCircleIcon, CheckCircle2Icon, CheckIcon } from 'lucide-react'
107
import { match } from 'ts-pattern'
118

129
import { isCompleted, useProgress } from '@/lib/progress/utils'
13-
import { cn } from '@/lib/utils'
1410
import { AppCardSection } from '@/components/app/app-card'
1511
import { Callout } from '@/components/base/callout'
1612
import { Button } from '@/components/ui/button'
1713
import { Skeleton } from '@/components/ui/skeleton'
1814
import { Spinner } from '@/components/ui/spinner'
15+
import { TextCopyButton } from '@/app/(model)/text-copy-button'
1916
import { useModelStore } from '@/stores/model-store-provider'
2017
import { useTRPC } from '@/trpc/client'
2118

@@ -131,17 +128,9 @@ function HostStatusCard({ hostId }: { hostId: string }) {
131128
<dl className="contents">
132129
<dt className="text-muted-foreground">API 端点</dt>
133130
<dd>
134-
<CopyButton value={url} message="已复制 API 端点">
131+
<TextCopyButton value={url} message="已复制 API 端点">
135132
{url}
136-
</CopyButton>
137-
</dd>
138-
</dl>
139-
<dl className="contents">
140-
<dt className="text-muted-foreground">API 密钥</dt>
141-
<dd>
142-
<CopyButton value={deployment.apiKey} message="已复制 API 密钥">
143-
{deployment.apiKey}
144-
</CopyButton>
133+
</TextCopyButton>
145134
</dd>
146135
</dl>
147136
</div>
@@ -158,25 +147,3 @@ function DeploySuccessCallout() {
158147
</Callout>
159148
)
160149
}
161-
162-
function CopyButton({ value, message, children }: { value: string; message: string; children?: ReactNode }) {
163-
const { copy, copied, error } = useClipboard()
164-
165-
return (
166-
<button
167-
className={cn(
168-
'[&>svg]:text-primary hover:[&_svg]:text-primary/80 hover:text-foreground/90 inline-flex items-center gap-2 [&>svg]:size-3.5',
169-
error && '[&>svg]:text-destructive',
170-
copied && '[&>svg]:text-success',
171-
)}
172-
onClick={() => {
173-
copy(value)
174-
toast.success(message)
175-
}}
176-
>
177-
{children}
178-
{copied ? <CheckIcon /> : error ? <AlertCircleIcon /> : <CopyIcon />}
179-
<span className="sr-only">复制</span>
180-
</button>
181-
)
182-
}

app/(model)/text-copy-button.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as React from 'react'
2+
import { ReactNode } from 'react'
3+
import { useClipboard } from '@mantine/hooks'
4+
import { AlertCircleIcon, CheckIcon, CopyIcon } from 'lucide-react'
5+
import { toast } from 'sonner'
6+
7+
import { cn } from '@/lib/utils'
8+
9+
export function TextCopyButton({ value, message, children }: { value: string; message: string; children?: ReactNode }) {
10+
const { copy, copied, error } = useClipboard()
11+
12+
return (
13+
<button
14+
className={cn(
15+
'[&>svg]:text-primary hover:[&_svg]:text-primary/80 hover:text-foreground/90 inline-flex items-center gap-2 [&>svg]:size-3.5',
16+
error && '[&>svg]:text-destructive',
17+
copied && '[&>svg]:text-success',
18+
)}
19+
onClick={() => {
20+
copy(value)
21+
toast.success(message)
22+
}}
23+
>
24+
{children}
25+
{copied ? <CheckIcon /> : error ? <AlertCircleIcon /> : <CopyIcon />}
26+
<span className="sr-only">复制</span>
27+
</button>
28+
)
29+
}

0 commit comments

Comments
 (0)