diff options
author | Paul Frazee <pfrazee@gmail.com> | 2023-11-14 12:10:39 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-14 12:10:39 -0800 |
commit | 8e4a3ad5b6c5abac57559f5711261b9868eb0cd1 (patch) | |
tree | daef1dca0312ae135a05cbdad209154ffe80b7c3 /src/state/cache/profile-shadow.ts | |
parent | 00f8c8b06d2f789c38c5c3416ec195072bbfd450 (diff) | |
download | voidsky-8e4a3ad5b6c5abac57559f5711261b9868eb0cd1.tar.zst |
Add Shadow type (#1900)
Diffstat (limited to 'src/state/cache/profile-shadow.ts')
-rw-r--r-- | src/state/cache/profile-shadow.ts | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/src/state/cache/profile-shadow.ts b/src/state/cache/profile-shadow.ts index a1cf59954..59f79634d 100644 --- a/src/state/cache/profile-shadow.ts +++ b/src/state/cache/profile-shadow.ts @@ -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, } } |