Expand AnyProfileView to include chat profile view, update post shadow to reflect this

This commit is contained in:
Eric Bailey
2025-01-06 16:01:07 -06:00
parent d1906e3159
commit 4cb5cfa8d4
2 changed files with 12 additions and 7 deletions
+6 -5
View File
@@ -1,9 +1,9 @@
import {useEffect, useMemo, useState} from 'react'
import {AppBskyActorDefs} from '@atproto/api'
import {QueryClient} from '@tanstack/react-query'
import EventEmitter from 'eventemitter3'
import {batchedUpdates} from '#/lib/batchedUpdates'
import * as atp from '#/types/atproto'
import {findAllProfilesInQueryData as findAllProfilesInActorSearchQueryData} from '../queries/actor-search'
import {findAllProfilesInQueryData as findAllProfilesInKnownFollowersQueryData} from '../queries/known-followers'
import {findAllProfilesInQueryData as findAllProfilesInListMembersQueryData} from '../queries/list-members'
@@ -20,6 +20,7 @@ import {findAllProfilesInQueryData as findAllProfilesInProfileFollowersQueryData
import {findAllProfilesInQueryData as findAllProfilesInProfileFollowsQueryData} from '../queries/profile-follows'
import {findAllProfilesInQueryData as findAllProfilesInSuggestedFollowsQueryData} from '../queries/suggested-follows'
import {castAsShadow, Shadow} from './types'
export type {Shadow} from './types'
export interface ProfileShadow {
@@ -29,13 +30,13 @@ export interface ProfileShadow {
}
const shadows: WeakMap<
AppBskyActorDefs.ProfileView,
atp.profile.AnyProfileView,
Partial<ProfileShadow>
> = new WeakMap()
const emitter = new EventEmitter()
export function useProfileShadow<
TProfileView extends Omit<AppBskyActorDefs.ProfileView, '$type'>,
TProfileView extends atp.profile.AnyProfileView,
>(profile: TProfileView): Shadow<TProfileView> {
const [shadow, setShadow] = useState(() => shadows.get(profile))
const [prevPost, setPrevPost] = useState(profile)
@@ -77,7 +78,7 @@ export function updateProfileShadow(
})
}
function mergeShadow<TProfileView extends AppBskyActorDefs.ProfileView>(
function mergeShadow<TProfileView extends atp.profile.AnyProfileView>(
profile: TProfileView,
shadow: Partial<ProfileShadow>,
): Shadow<TProfileView> {
@@ -99,7 +100,7 @@ function mergeShadow<TProfileView extends AppBskyActorDefs.ProfileView>(
function* findProfilesInCache(
queryClient: QueryClient,
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
): Generator<atp.profile.AnyProfileView, void> {
yield* findAllProfilesInListMembersQueryData(queryClient, did)
yield* findAllProfilesInMyBlockedAccountsQueryData(queryClient, did)
yield* findAllProfilesInMyMutedAccountsQueryData(queryClient, did)
+6 -2
View File
@@ -1,4 +1,4 @@
import {AppBskyActorDefs} from '@atproto/api'
import {AppBskyActorDefs, ChatBskyActorDefs} from '@atproto/api'
export {
/**
@@ -25,6 +25,7 @@ export type AnyProfileView =
| AppBskyActorDefs.ProfileViewBasic
| AppBskyActorDefs.ProfileView
| AppBskyActorDefs.ProfileViewDetailed
| ChatBskyActorDefs.ProfileViewBasic
/**
* Maps any profile view type to `ProfileViewBasic`.
@@ -41,7 +42,10 @@ export function anyToBasic(
associated: view.associated,
viewer: view.viewer,
labels: view.labels,
createdAt: view.createdAt,
// @ts-expect-error `createdAt` doesn't exist on chat view
createdAt: ChatBskyActorDefs.isProfileViewBasic(view)
? undefined
: view.createdAt,
}
}