Skip to content

Commit c1b5d4e

Browse files
committed
feat: improve clipboard error handling with user-friendly message
1 parent 737b5e5 commit c1b5d4e

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

web/src/app/_features/CopyToClipboardButton/CopyToClipboardButton.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,42 @@ import { useCallback, useState } from 'react'
77
import { Tooltip } from './Tooltip'
88
import { useSuccessTooltip } from './useSuccessTooltip'
99

10-
import { Button } from '@/app/_components'
10+
import { Alert, Button } from '@/app/_components'
1111

1212
interface Props {
1313
text: string
1414
}
1515

1616
export const CopyToClipboardButton: FC<Props> = ({ text }) => {
1717
const [isLoading, setIsLoading] = useState(false)
18+
const [error, setError] = useState<string | null>(null)
1819
const [showTooltip, setShowTooltip] = useSuccessTooltip()
1920

2021
const handleClick = useCallback(() => {
22+
setError(null)
2123
setIsLoading(true)
2224
navigator.clipboard
2325
.writeText(text)
24-
.then(() => setShowTooltip(true))
25-
.catch((_e) => {
26-
/* エラー処理 */
26+
.then(() => {
27+
setShowTooltip(true)
28+
setIsLoading(false)
29+
})
30+
.catch((e) => {
31+
console.error('Failed to copy to clipboard', e)
32+
setError('Failed to copy to clipboard. Please check your browser permissions.')
33+
setIsLoading(false)
2734
})
28-
setIsLoading(false)
2935
}, [text, setShowTooltip])
3036

3137
return (
32-
<span className="relative">
33-
<Tooltip show={showTooltip} />
34-
<Button isLoading={isLoading} onClick={handleClick} variant="secondary">
35-
Copy to Clipboard
36-
</Button>
37-
</span>
38+
<div className="space-y-2">
39+
{error && <Alert>{error}</Alert>}
40+
<span className="relative">
41+
<Tooltip show={showTooltip} />
42+
<Button isLoading={isLoading} onClick={handleClick} variant="secondary">
43+
Copy to Clipboard
44+
</Button>
45+
</span>
46+
</div>
3847
)
3948
}

0 commit comments

Comments
 (0)