Add Shadow type (#1900)
This commit is contained in:
parent
00f8c8b06d
commit
8e4a3ad5b6
10 changed files with 54 additions and 21 deletions
17
src/state/cache/post-shadow.ts
vendored
17
src/state/cache/post-shadow.ts
vendored
|
@ -1,6 +1,8 @@
|
|||
import {useEffect, useState, useCallback, useRef} from 'react'
|
||||
import EventEmitter from 'eventemitter3'
|
||||
import {AppBskyFeedDefs} from '@atproto/api'
|
||||
import {Shadow} from './types'
|
||||
export type {Shadow} from './types'
|
||||
|
||||
const emitter = new EventEmitter()
|
||||
|
||||
|
@ -22,7 +24,7 @@ interface CacheEntry {
|
|||
export function usePostShadow(
|
||||
post: AppBskyFeedDefs.PostView,
|
||||
ifAfterTS: number,
|
||||
): AppBskyFeedDefs.PostView | typeof POST_TOMBSTONE {
|
||||
): Shadow<AppBskyFeedDefs.PostView> | typeof POST_TOMBSTONE {
|
||||
const [state, setState] = useState<CacheEntry>({
|
||||
ts: Date.now(),
|
||||
value: fromPost(post),
|
||||
|
@ -53,13 +55,21 @@ export function usePostShadow(
|
|||
firstRun.current = false
|
||||
}, [post])
|
||||
|
||||
return state.ts > ifAfterTS ? mergeShadow(post, state.value) : post
|
||||
return state.ts > ifAfterTS
|
||||
? mergeShadow(post, state.value)
|
||||
: {...post, isShadowed: true}
|
||||
}
|
||||
|
||||
export function updatePostShadow(uri: string, value: Partial<PostShadow>) {
|
||||
emitter.emit(uri, value)
|
||||
}
|
||||
|
||||
export function isPostShadowed(
|
||||
v: AppBskyFeedDefs.PostView | Shadow<AppBskyFeedDefs.PostView>,
|
||||
): v is Shadow<AppBskyFeedDefs.PostView> {
|
||||
return 'isShadowed' in v && !!v.isShadowed
|
||||
}
|
||||
|
||||
function fromPost(post: AppBskyFeedDefs.PostView): PostShadow {
|
||||
return {
|
||||
likeUri: post.viewer?.like,
|
||||
|
@ -73,7 +83,7 @@ function fromPost(post: AppBskyFeedDefs.PostView): PostShadow {
|
|||
function mergeShadow(
|
||||
post: AppBskyFeedDefs.PostView,
|
||||
shadow: PostShadow,
|
||||
): AppBskyFeedDefs.PostView | typeof POST_TOMBSTONE {
|
||||
): Shadow<AppBskyFeedDefs.PostView> | typeof POST_TOMBSTONE {
|
||||
if (shadow.isDeleted) {
|
||||
return POST_TOMBSTONE
|
||||
}
|
||||
|
@ -86,5 +96,6 @@ function mergeShadow(
|
|||
like: shadow.likeUri,
|
||||
repost: shadow.repostUri,
|
||||
},
|
||||
isShadowed: true,
|
||||
}
|
||||
}
|
||||
|
|
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,
|
||||
}
|
||||
}
|
||||
|
|
1
src/state/cache/types.ts
vendored
Normal file
1
src/state/cache/types.ts
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
export type Shadow<T> = T & {isShadowed: true}
|
Loading…
Add table
Add a link
Reference in a new issue