File size: 2,069 Bytes
5285b72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75a318d
5285b72
 
 
b2358b2
5285b72
 
 
 
 
4ddbdae
 
5285b72
 
 
 
 
 
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
import Markdown from "marked-react"
import { useEffect, useState } from "react"
import { useClient } from "urql"
import { ScrollArea } from "~/components/ui/scroll-area"
import { Separator } from "~/components/ui/separator"
import { graphql } from "~/generated/gql"
import type { AnswersQuery, Subscription } from "~/generated/graphql"

const GenerateSummarySubscription = graphql(`
  subscription generateSummary(
    $query: String!
    $docIds: [String!]!
  ) {
    generateSummary(
      query: $query
      docIds: $docIds
    ) {
      summary
    }
  }
`)

type AnswerSummaryProps = {
  query: string
  items: NonNullable<NonNullable<AnswersQuery["answers"]>["answers"]>
}

export function AnswerSummary({ query, items }: AnswerSummaryProps) {
  const client = useClient()
  const [summary, setSummary] = useState<string | undefined>()

  useEffect(() => {
    setSummary(undefined)
    const { unsubscribe } = client
      .subscription<Subscription>(GenerateSummarySubscription, {
        query,
        docIds: items.map((x) => x.docId).slice(0, 10),
      })
      .subscribe((result) => {
        setSummary(
          (prev) =>
            (prev ?? "") + (result.data?.generateSummary?.summary ?? ""),
        )
      })
    return () => {
      unsubscribe()
    }
  }, [query, items, client])

  return (
    <div className="flex h-full flex-col">
      <div className="flex items-center px-4 py-2">
        <h1 className="text-xl font-bold">TL;DR</h1>
      </div>
      <Separator />
      <ScrollArea className="h-screen">
        {summary ? (
          <div className="flex flex-1 flex-col">
            <div className="flex-1 p-4">
              <article className="prose prose-sm max-w-none overscroll-auto mb-[550px] sm:mb-0">
                <Markdown>{summary}</Markdown>
              </article>
            </div>
          </div>
        ) : (
          <div className="pt-8 md:pt-40 text-center text-muted-foreground text-sm">
            ζ€œη΄’η΅ζžœγŒγΎγ γ‚γ‚ŠγΎγ›γ‚“
          </div>
        )}
      </ScrollArea>
    </div>
  )
}