mirror of
https://github.com/henrygd/beszel.git
synced 2025-12-17 18:56:17 +01:00
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
import { Trans } from "@lingui/react/macro";
|
|
import { useEffect, useMemo, useRef } from "react"
|
|
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "./ui/dialog"
|
|
import { Textarea } from "./ui/textarea"
|
|
import { $copyContent } from "@/lib/stores"
|
|
|
|
export default function CopyToClipboard({ content }: { content: string }) {
|
|
return (
|
|
<Dialog defaultOpen={true}>
|
|
<DialogContent className="w-[90%] rounded-lg md:pt-4" style={{ maxWidth: 530 }}>
|
|
<DialogHeader>
|
|
<DialogTitle>
|
|
<Trans>Copy text</Trans>
|
|
</DialogTitle>
|
|
<DialogDescription className="hidden xs:block">
|
|
<Trans>Automatic copy requires a secure context.</Trans>
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<CopyTextarea content={content} />
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|
|
|
|
function CopyTextarea({ content }: { content: string }) {
|
|
const textareaRef = useRef<HTMLTextAreaElement>(null)
|
|
|
|
const rows = useMemo(() => {
|
|
return content.split("\n").length
|
|
}, [content])
|
|
|
|
useEffect(() => {
|
|
if (textareaRef.current) {
|
|
textareaRef.current.select()
|
|
}
|
|
}, [textareaRef])
|
|
|
|
useEffect(() => {
|
|
return () => $copyContent.set("")
|
|
}, [])
|
|
|
|
return (
|
|
<Textarea
|
|
className="font-mono overflow-hidden whitespace-pre"
|
|
rows={rows}
|
|
value={content}
|
|
readOnly
|
|
ref={textareaRef}
|
|
/>
|
|
)
|
|
}
|