Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Shadow type #1900

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/state/cache/post-shadow.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand All @@ -86,5 +96,6 @@ function mergeShadow(
like: shadow.likeUri,
repost: shadow.repostUri,
},
isShadowed: true,
}
}
25 changes: 18 additions & 7 deletions src/state/cache/profile-shadow.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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),
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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: {
Expand All @@ -84,5 +94,6 @@ function mergeShadow<T extends ProfileView>(
muted: shadow.muted,
blocking: shadow.blockingUri,
},
isShadowed: true,
}
}
1 change: 1 addition & 0 deletions src/state/cache/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Shadow<T> = T & {isShadowed: true}
11 changes: 9 additions & 2 deletions src/view/com/auth/onboarding/RecommendedFollowsItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Animated, {FadeInRight} from 'react-native-reanimated'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {useAnalytics} from 'lib/analytics/analytics'
import {Trans} from '@lingui/macro'
import {useProfileShadow} from '#/state/cache/profile-shadow'
import {Shadow, useProfileShadow} from '#/state/cache/profile-shadow'
import {
useProfileFollowMutation,
useProfileUnfollowMutation,
Expand Down Expand Up @@ -66,7 +66,14 @@ export function ProfileCard({
profile,
onFollowStateChange,
moderation,
}: Omit<Props, 'dataUpdatedAt'>) {
}: {
profile: Shadow<SuggestedActor>
moderation: ProfileModeration
onFollowStateChange: (props: {
did: string
following: boolean
}) => Promise<void>
}) {
const {track} = useAnalytics()
const pal = usePalette('default')
const [addingMoreSuggestions, setAddingMoreSuggestions] =
Expand Down
4 changes: 2 additions & 2 deletions src/view/com/post-thread/PostThreadItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {MAX_POST_LINES} from 'lib/constants'
import {Trans} from '@lingui/macro'
import {useLanguagePrefs} from '#/state/preferences'
import {usePostShadow, POST_TOMBSTONE} from '#/state/cache/post-shadow'
import {useComposerControls} from '#/state/shell/composer'
import {useModerationOpts} from '#/state/queries/preferences'
import {Shadow, usePostShadow, POST_TOMBSTONE} from '#/state/cache/post-shadow'

export function PostThreadItem({
post,
Expand Down Expand Up @@ -132,7 +132,7 @@ function PostThreadItemLoaded({
hasPrecedingItem,
onPostReply,
}: {
post: AppBskyFeedDefs.PostView
post: Shadow<AppBskyFeedDefs.PostView>
record: AppBskyFeedPost.Record
richText: RichTextAPI
moderation: PostModeration
Expand Down
4 changes: 2 additions & 2 deletions src/view/com/post/Post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {makeProfileLink} from 'lib/routes/links'
import {MAX_POST_LINES} from 'lib/constants'
import {countLines} from 'lib/strings/helpers'
import {useModerationOpts} from '#/state/queries/preferences'
import {usePostShadow, POST_TOMBSTONE} from '#/state/cache/post-shadow'
import {useComposerControls} from '#/state/shell/composer'
import {Shadow, usePostShadow, POST_TOMBSTONE} from '#/state/cache/post-shadow'

export function Post({
post,
Expand Down Expand Up @@ -89,7 +89,7 @@ function PostInner({
showReplyLine,
style,
}: {
post: AppBskyFeedDefs.PostView
post: Shadow<AppBskyFeedDefs.PostView>
record: AppBskyFeedPost.Record
richText: RichTextAPI
moderation: PostModeration
Expand Down
4 changes: 2 additions & 2 deletions src/view/com/posts/FeedItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import {makeProfileLink} from 'lib/routes/links'
import {isEmbedByEmbedder} from 'lib/embeds'
import {MAX_POST_LINES} from 'lib/constants'
import {countLines} from 'lib/strings/helpers'
import {usePostShadow, POST_TOMBSTONE} from '#/state/cache/post-shadow'
import {useComposerControls} from '#/state/shell/composer'
import {Shadow, usePostShadow, POST_TOMBSTONE} from '#/state/cache/post-shadow'

export function FeedItem({
post,
Expand Down Expand Up @@ -93,7 +93,7 @@ function FeedItemInner({
isThreadLastChild,
isThreadParent,
}: {
post: AppBskyFeedDefs.PostView
post: Shadow<AppBskyFeedDefs.PostView>
record: AppBskyFeedPost.Record
reason: AppBskyFeedDefs.ReasonRepost | ReasonFeedSource | undefined
richText: RichTextAPI
Expand Down
3 changes: 2 additions & 1 deletion src/view/com/profile/ProfileHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ import {shareUrl} from 'lib/sharing'
import {s, colors} from 'lib/styles'
import {logger} from '#/logger'
import {useSession} from '#/state/session'
import {Shadow} from '#/state/cache/types'

interface Props {
profile: AppBskyActorDefs.ProfileViewDetailed
profile: Shadow<AppBskyActorDefs.ProfileViewDetailed>
moderation: ProfileModeration
hideBackButton?: boolean
isProfilePreview?: boolean
Expand Down
3 changes: 2 additions & 1 deletion src/view/com/util/forms/PostDropdownBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {usePostDeleteMutation} from '#/state/queries/post'
import {useMutedThreads, useToggleThreadMute} from '#/state/muted-threads'
import {useLanguagePrefs} from '#/state/preferences'
import {logger} from '#/logger'
import {Shadow} from '#/state/cache/types'

export function PostDropdownBtn({
testID,
Expand All @@ -28,7 +29,7 @@ export function PostDropdownBtn({
style,
}: {
testID: string
post: AppBskyFeedDefs.PostView
post: Shadow<AppBskyFeedDefs.PostView>
record: AppBskyFeedPost.Record
style?: StyleProp<ViewStyle>
}) {
Expand Down
3 changes: 2 additions & 1 deletion src/view/com/util/post-ctrls/PostCtrls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
usePostUnrepostMutation,
} from '#/state/queries/post'
import {useComposerControls} from '#/state/shell/composer'
import {Shadow} from '#/state/cache/types'

export function PostCtrls({
big,
Expand All @@ -33,7 +34,7 @@ export function PostCtrls({
onPressReply,
}: {
big?: boolean
post: AppBskyFeedDefs.PostView
post: Shadow<AppBskyFeedDefs.PostView>
record: AppBskyFeedPost.Record
style?: StyleProp<ViewStyle>
onPressReply: () => void
Expand Down
Loading