Skip to content
Myr

copy.tsx

This file

Path

apps/web/src/features/code/copy.tsx

42 lines · 981 bytes · TypeScript

Last changed by

Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72

History of this file

Take it

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 1–42why, one step back
1import { Effect } from "effect";
2import { Check, Copy } from "lucide-react";3import { useState } from "react";45import { Button } from "@/components/ui/button";67const copyFor = (text: string, shown: (copied: boolean) => void) =>8 Effect.tryPromise(() => navigator.clipboard.writeText(text)).pipe(9 Effect.andThen(10 Effect.sync(() => {11 shown(true);12 })13 ),14 Effect.andThen(Effect.sleep("2 seconds")),15 Effect.andThen(16 Effect.sync(() => {17 shown(false);18 })19 ),20 Effect.ignore21 );2223export const CopyButton = ({24 label,25 text,26}: {27 readonly label: string;28 readonly text: string;29}) => {30 const [copied, setCopied] = useState(false);3132 const copy = () => {33 Effect.runFork(copyFor(text, setCopied));34 };3536 return (37 <Button onClick={copy} size="sm" type="button" variant="ghost">38 {copied ? <Check aria-hidden="true" /> : <Copy aria-hidden="true" />}39 {copied ? "Copied" : label}40 </Button>41 );42};