| 204 | }; |
| 205 | |
| 206 | export const CommitCopyButton = ({ |
| 207 | hash, |
| 208 | onCopy, |
| 209 | onError, |
| 210 | timeout = 2000, |
| 211 | children, |
| 212 | className, |
| 213 | ...props |
| 214 | }: CommitCopyButtonProps) => { |
| 215 | const [isCopied, setIsCopied] = useState(false); |
| 216 | const timeoutRef = useRef<number>(0); |
| 217 | |
| 218 | const copyToClipboard = useCallback(async () => { |
| 219 | if (typeof window === "undefined" || !navigator?.clipboard?.writeText) { |
| 220 | onError?.(new Error("Clipboard API not available")); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | try { |
| 225 | if (!isCopied) { |
| 226 | await navigator.clipboard.writeText(hash); |
| 227 | setIsCopied(true); |
| 228 | onCopy?.(); |
| 229 | timeoutRef.current = window.setTimeout( |
| 230 | () => setIsCopied(false), |
| 231 | timeout |
| 232 | ); |
| 233 | } |
| 234 | } catch (error) { |
| 235 | onError?.(error as Error); |
| 236 | } |
| 237 | }, [hash, onCopy, onError, timeout, isCopied]); |
| 238 | |
| 239 | useEffect( |
| 240 | () => () => { |
| 241 | window.clearTimeout(timeoutRef.current); |
| 242 | }, |
| 243 | [] |
| 244 | ); |
| 245 | |
| 246 | const Icon = isCopied ? CheckIcon : CopyIcon; |
| 247 | |
| 248 | return ( |
| 249 | <Button |
| 250 | className={cn("size-7 shrink-0", className)} |
| 251 | onClick={copyToClipboard} |
| 252 | size="icon" |
| 253 | variant="ghost" |
| 254 | {...props} |
| 255 | > |
| 256 | {children ?? <Icon size={14} />} |
| 257 | </Button> |
| 258 | ); |
| 259 | }; |
| 260 | |
| 261 | export type CommitContentProps = ComponentProps<typeof CollapsibleContent>; |
| 262 | |