about summary refs log tree commit diff
path: root/src/state/cache/profile-shadow.ts
diff options
context:
space:
mode:
authorSamuel Newman <mozzius@protonmail.com>2025-02-03 14:37:24 -0800
committerGitHub <noreply@github.com>2025-02-03 22:37:24 +0000
commit32b28d666229ac24cf7b1ac328d1566fb089e1a1 (patch)
tree2e721117c9a859ca1cae52e1c15642d5e6db4d5b /src/state/cache/profile-shadow.ts
parentfa8607b861e0719d76778aa14af0745313640e33 (diff)
downloadvoidsky-32b28d666229ac24cf7b1ac328d1566fb089e1a1.tar.zst
Fix convo header loading state (#7603)
* get initial convo state from cache

* undo useConvoQuery changes

* fix shadowing situation with new hook
Diffstat (limited to 'src/state/cache/profile-shadow.ts')
-rw-r--r--src/state/cache/profile-shadow.ts38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/state/cache/profile-shadow.ts b/src/state/cache/profile-shadow.ts
index afd3f1935..4d823ec8e 100644
--- a/src/state/cache/profile-shadow.ts
+++ b/src/state/cache/profile-shadow.ts
@@ -63,6 +63,44 @@ export function useProfileShadow<
   }, [profile, shadow])
 }
 
+/**
+ * Same as useProfileShadow, but allows for the profile to be undefined.
+ * This is useful for when the profile is not guaranteed to be loaded yet.
+ */
+export function useMaybeProfileShadow<
+  TProfileView extends AppBskyActorDefs.ProfileView,
+>(profile?: TProfileView): Shadow<TProfileView> | undefined {
+  const [shadow, setShadow] = useState(() =>
+    profile ? shadows.get(profile) : undefined,
+  )
+  const [prevPost, setPrevPost] = useState(profile)
+  if (profile !== prevPost) {
+    setPrevPost(profile)
+    setShadow(profile ? shadows.get(profile) : undefined)
+  }
+
+  useEffect(() => {
+    if (!profile) return
+    function onUpdate() {
+      if (!profile) return
+      setShadow(shadows.get(profile))
+    }
+    emitter.addListener(profile.did, onUpdate)
+    return () => {
+      emitter.removeListener(profile.did, onUpdate)
+    }
+  }, [profile])
+
+  return useMemo(() => {
+    if (!profile) return undefined
+    if (shadow) {
+      return mergeShadow(profile, shadow)
+    } else {
+      return castAsShadow(profile)
+    }
+  }, [profile, shadow])
+}
+
 export function updateProfileShadow(
   queryClient: QueryClient,
   did: string,