File size: 2,506 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"use client"

import { useCallback, useEffect, useRef, useState } from "react"
import { useTranslations } from "next-intl"

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"

interface TranscriptPreviewProps {
  text: string
  clipTitle?: string | null
}

export function TranscriptPreview({ text, clipTitle }: TranscriptPreviewProps) {
  const t = useTranslations("task")
  const previewRef = useRef<HTMLParagraphElement>(null)
  const [isTruncated, setIsTruncated] = useState(false)

  const measureTruncation = useCallback(() => {
    const preview = previewRef.current
    if (!preview) return

    setIsTruncated(preview.scrollHeight > preview.clientHeight + 1)
  }, [])

  useEffect(() => {
    measureTruncation()

    const preview = previewRef.current
    if (!preview || typeof ResizeObserver === "undefined") return

    const observer = new ResizeObserver(measureTruncation)
    observer.observe(preview)

    return () => observer.disconnect()
  }, [measureTruncation, text])

  return (
    <div className="mb-4">
      <h4 className="mb-2 font-medium text-foreground">{t("transcript.heading")}</h4>
      <div className="rounded-lg bg-muted p-3">
        <p ref={previewRef} className="line-clamp-4 text-sm leading-6 text-foreground/80">
          {text}
        </p>

        {isTruncated && (
          <Dialog>
            <DialogTrigger asChild>
              <button
                type="button"
                className="mt-2 rounded-sm text-sm font-medium text-foreground underline decoration-muted-foreground underline-offset-4 transition-colors hover:decoration-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
              >
                {t("transcript.seeMore")}
              </button>
            </DialogTrigger>
            <DialogContent>
              <DialogHeader>
                <DialogTitle>{t("transcript.full")}</DialogTitle>
                <DialogDescription>
                  {clipTitle ? t("transcript.forTitle", { title: clipTitle }) : t("transcript.complete")}
                </DialogDescription>
              </DialogHeader>
              <div className="max-h-[60vh] overflow-y-auto rounded-lg bg-muted p-4 text-sm leading-6 text-foreground/80 whitespace-pre-wrap">
                {text}
              </div>
            </DialogContent>
          </Dialog>
        )}
      </div>
    </div>
  )
}