Add Shadow type (#1900)
This commit is contained in:
parent
00f8c8b06d
commit
8e4a3ad5b6
10 changed files with 54 additions and 21 deletions
25
src/state/cache/profile-shadow.ts
vendored
25
src/state/cache/profile-shadow.ts
vendored
|
@ -1,6 +1,8 @@
|
|||
import {useEffect, useState, useCallback, useRef} from 'react'
|
||||
import EventEmitter from 'eventemitter3'
|
||||
import {AppBskyActorDefs} from '@atproto/api'
|
||||
import {Shadow} from './types'
|
||||
export type {Shadow} from './types'
|
||||
|
||||
const emitter = new EventEmitter()
|
||||
|
||||
|
@ -20,10 +22,10 @@ type ProfileView =
|
|||
| AppBskyActorDefs.ProfileViewBasic
|
||||
| AppBskyActorDefs.ProfileViewDetailed
|
||||
|
||||
export function useProfileShadow<T extends ProfileView>(
|
||||
profile: T,
|
||||
export function useProfileShadow(
|
||||
profile: ProfileView,
|
||||
ifAfterTS: number,
|
||||
): T {
|
||||
): Shadow<ProfileView> {
|
||||
const [state, setState] = useState<CacheEntry>({
|
||||
ts: Date.now(),
|
||||
value: fromProfile(profile),
|
||||
|
@ -54,7 +56,9 @@ export function useProfileShadow<T extends ProfileView>(
|
|||
firstRun.current = false
|
||||
}, [profile])
|
||||
|
||||
return state.ts > ifAfterTS ? mergeShadow(profile, state.value) : profile
|
||||
return state.ts > ifAfterTS
|
||||
? mergeShadow(profile, state.value)
|
||||
: {...profile, isShadowed: true}
|
||||
}
|
||||
|
||||
export function updateProfileShadow(
|
||||
|
@ -64,6 +68,12 @@ export function updateProfileShadow(
|
|||
emitter.emit(uri, value)
|
||||
}
|
||||
|
||||
export function isProfileShadowed<T extends ProfileView>(
|
||||
v: T | Shadow<T>,
|
||||
): v is Shadow<T> {
|
||||
return 'isShadowed' in v && !!v.isShadowed
|
||||
}
|
||||
|
||||
function fromProfile(profile: ProfileView): ProfileShadow {
|
||||
return {
|
||||
followingUri: profile.viewer?.following,
|
||||
|
@ -72,10 +82,10 @@ function fromProfile(profile: ProfileView): ProfileShadow {
|
|||
}
|
||||
}
|
||||
|
||||
function mergeShadow<T extends ProfileView>(
|
||||
profile: T,
|
||||
function mergeShadow(
|
||||
profile: ProfileView,
|
||||
shadow: ProfileShadow,
|
||||
): T {
|
||||
): Shadow<ProfileView> {
|
||||
return {
|
||||
...profile,
|
||||
viewer: {
|
||||
|
@ -84,5 +94,6 @@ function mergeShadow<T extends ProfileView>(
|
|||
muted: shadow.muted,
|
||||
blocking: shadow.blockingUri,
|
||||
},
|
||||
isShadowed: true,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue