From e8a53dcea84afc3b313037dbaf059c68121eb9ab Mon Sep 17 00:00:00 2001
From: dan <dan.abramov@gmail.com>
Date: Thu, 24 Oct 2024 20:41:50 +0100
Subject: Refactor post meta to return PostView (#5645)

---
 src/view/com/util/post-embeds/QuoteEmbed.tsx | 58 ++++++++++------------------
 1 file changed, 20 insertions(+), 38 deletions(-)

(limited to 'src/view/com/util/post-embeds/QuoteEmbed.tsx')

diff --git a/src/view/com/util/post-embeds/QuoteEmbed.tsx b/src/view/com/util/post-embeds/QuoteEmbed.tsx
index c44ec3b84..49cfb2150 100644
--- a/src/view/com/util/post-embeds/QuoteEmbed.tsx
+++ b/src/view/com/util/post-embeds/QuoteEmbed.tsx
@@ -33,7 +33,6 @@ import {useModerationOpts} from '#/state/preferences/moderation-opts'
 import {precacheProfile} from '#/state/queries/profile'
 import {useResolveLinkQuery} from '#/state/queries/resolve-link'
 import {useSession} from '#/state/session'
-import {ComposerOptsQuote} from '#/state/shell/composer'
 import {atoms as a, useTheme} from '#/alf'
 import {RichText} from '#/components/RichText'
 import {ContentHider} from '../../../../components/moderation/ContentHider'
@@ -68,7 +67,6 @@ export function MaybeQuoteEmbed({
     return (
       <QuoteEmbedModerated
         viewRecord={embed.record}
-        postRecord={embed.record.value}
         onOpen={onOpen}
         style={style}
         allowNestedQuotes={allowNestedQuotes}
@@ -118,39 +116,31 @@ export function MaybeQuoteEmbed({
 
 function QuoteEmbedModerated({
   viewRecord,
-  postRecord,
   onOpen,
   style,
   allowNestedQuotes,
   viewContext,
 }: {
   viewRecord: AppBskyEmbedRecord.ViewRecord
-  postRecord: AppBskyFeedPost.Record
   onOpen?: () => void
   style?: StyleProp<ViewStyle>
   allowNestedQuotes?: boolean
   viewContext?: QuoteEmbedViewContext
 }) {
   const moderationOpts = useModerationOpts()
+  const postView = React.useMemo(
+    () => viewRecordToPostView(viewRecord),
+    [viewRecord],
+  )
   const moderation = React.useMemo(() => {
     return moderationOpts
-      ? moderatePost_wrapped(viewRecordToPostView(viewRecord), moderationOpts)
+      ? moderatePost_wrapped(postView, moderationOpts)
       : undefined
-  }, [viewRecord, moderationOpts])
-
-  const quote = {
-    author: viewRecord.author,
-    cid: viewRecord.cid,
-    uri: viewRecord.uri,
-    indexedAt: viewRecord.indexedAt,
-    text: postRecord.text,
-    facets: postRecord.facets,
-    embeds: viewRecord.embeds,
-  }
+  }, [postView, moderationOpts])
 
   return (
     <QuoteEmbed
-      quote={quote}
+      quote={postView}
       moderation={moderation}
       onOpen={onOpen}
       style={style}
@@ -167,7 +157,7 @@ export function QuoteEmbed({
   style,
   allowNestedQuotes,
 }: {
-  quote: ComposerOptsQuote
+  quote: AppBskyFeedDefs.PostView
   moderation?: ModerationDecision
   onOpen?: () => void
   style?: StyleProp<ViewStyle>
@@ -181,16 +171,18 @@ export function QuoteEmbed({
   const itemHref = makeProfileLink(quote.author, 'post', itemUrip.rkey)
   const itemTitle = `Post by ${quote.author.handle}`
 
-  const richText = React.useMemo(
-    () =>
-      quote.text.trim()
-        ? new RichTextAPI({text: quote.text, facets: quote.facets})
-        : undefined,
-    [quote.text, quote.facets],
-  )
+  const richText = React.useMemo(() => {
+    const text = AppBskyFeedPost.isRecord(quote.record) ? quote.record.text : ''
+    const facets = AppBskyFeedPost.isRecord(quote.record)
+      ? quote.record.facets
+      : undefined
+    return text.trim()
+      ? new RichTextAPI({text: text, facets: facets})
+      : undefined
+  }, [quote.record])
 
   const embed = React.useMemo(() => {
-    const e = quote.embeds?.[0]
+    const e = quote.embed
 
     if (allowNestedQuotes) {
       return e
@@ -210,7 +202,7 @@ export function QuoteEmbed({
         return e.media
       }
     }
-  }, [quote.embeds, allowNestedQuotes])
+  }, [quote.embed, allowNestedQuotes])
 
   const onBeforePress = React.useCallback(() => {
     precacheProfile(queryClient, quote.author)
@@ -292,17 +284,7 @@ export function LazyQuoteEmbed({uri}: {uri: string}) {
   if (!data || data.type !== 'record' || data.kind !== 'post') {
     return null
   }
-  return (
-    <QuoteEmbed
-      quote={{
-        cid: data.record.cid,
-        uri: data.record.uri,
-        author: data.meta.author,
-        indexedAt: data.meta.indexedAt,
-        text: data.meta.text,
-      }}
-    />
-  )
+  return <QuoteEmbed quote={data.meta} />
 }
 
 function viewRecordToPostView(
-- 
cgit 1.4.1

