Skip to content

Commit

Permalink
Do less work (#1953)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Nov 17, 2023
1 parent 9c8a1b8 commit c858b58
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 29 deletions.
10 changes: 6 additions & 4 deletions src/state/cache/post-shadow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useEffect, useState, useCallback, useRef} from 'react'
import {useEffect, useState, useMemo, useCallback, useRef} from 'react'
import EventEmitter from 'eventemitter3'
import {AppBskyFeedDefs} from '@atproto/api'
import {Shadow} from './types'
Expand Down Expand Up @@ -55,9 +55,11 @@ export function usePostShadow(
firstRun.current = false
}, [post])

return state.ts > ifAfterTS
? mergeShadow(post, state.value)
: {...post, isShadowed: true}
return useMemo(() => {
return state.ts > ifAfterTS
? mergeShadow(post, state.value)
: {...post, isShadowed: true}
}, [post, state, ifAfterTS])
}

export function updatePostShadow(uri: string, value: Partial<PostShadow>) {
Expand Down
10 changes: 6 additions & 4 deletions src/state/cache/profile-shadow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useEffect, useState, useCallback, useRef} from 'react'
import {useEffect, useState, useMemo, useCallback, useRef} from 'react'
import EventEmitter from 'eventemitter3'
import {AppBskyActorDefs} from '@atproto/api'
import {Shadow} from './types'
Expand Down Expand Up @@ -56,9 +56,11 @@ export function useProfileShadow(
firstRun.current = false
}, [profile])

return state.ts > ifAfterTS
? mergeShadow(profile, state.value)
: {...profile, isShadowed: true}
return useMemo(() => {
return state.ts > ifAfterTS
? mergeShadow(profile, state.value)
: {...profile, isShadowed: true}
}, [profile, state, ifAfterTS])
}

export function updateProfileShadow(
Expand Down
20 changes: 5 additions & 15 deletions src/state/queries/preferences/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {useEffect, useState} from 'react'
import {useMemo} from 'react'
import {useQuery, useMutation, useQueryClient} from '@tanstack/react-query'
import {
LabelPreference,
BskyFeedViewPreference,
ModerationOpts,
} from '@atproto/api'
import isEqual from 'lodash.isequal'
import {LabelPreference, BskyFeedViewPreference} from '@atproto/api'

import {track} from '#/lib/analytics/analytics'
import {getAge} from '#/lib/strings/time'
Expand Down Expand Up @@ -91,21 +86,16 @@ export function usePreferencesQuery() {

export function useModerationOpts() {
const {currentAccount} = useSession()
const [opts, setOpts] = useState<ModerationOpts | undefined>()
const prefs = usePreferencesQuery()
useEffect(() => {
const opts = useMemo(() => {
if (!prefs.data) {
return
}
// only update this hook when the moderation options change
const newOpts = getModerationOpts({
return getModerationOpts({
userDid: currentAccount?.did || '',
preferences: prefs.data,
})
if (!isEqual(opts, newOpts)) {
setOpts(newOpts)
}
}, [prefs.data, currentAccount, opts, setOpts])
}, [currentAccount?.did, prefs.data])
return opts
}

Expand Down
9 changes: 5 additions & 4 deletions src/view/com/post-thread/PostThreadItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useMemo} from 'react'
import React, {memo, useMemo} from 'react'
import {StyleSheet, View} from 'react-native'
import {
AtUri,
Expand Down Expand Up @@ -118,7 +118,7 @@ function PostThreadItemDeleted() {
)
}

function PostThreadItemLoaded({
let PostThreadItemLoaded = ({
post,
record,
richText,
Expand All @@ -144,12 +144,12 @@ function PostThreadItemLoaded({
showParentReplyLine?: boolean
hasPrecedingItem: boolean
onPostReply: () => void
}) {
}): React.ReactNode => {
const pal = usePalette('default')
const langPrefs = useLanguagePrefs()
const {openComposer} = useComposerControls()
const [limitLines, setLimitLines] = React.useState(
countLines(richText?.text) >= MAX_POST_LINES,
() => countLines(richText?.text) >= MAX_POST_LINES,
)
const styles = useStyles()
const hasEngagement = post.likeCount || post.repostCount
Expand Down Expand Up @@ -565,6 +565,7 @@ function PostThreadItemLoaded({
)
}
}
PostThreadItemLoaded = memo(PostThreadItemLoaded)

function PostOuterWrapper({
post,
Expand Down
2 changes: 1 addition & 1 deletion src/view/com/post/Post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function PostInner({
const pal = usePalette('default')
const {openComposer} = useComposerControls()
const [limitLines, setLimitLines] = useState(
countLines(richText?.text) >= MAX_POST_LINES,
() => countLines(richText?.text) >= MAX_POST_LINES,
)
const itemUrip = new AtUri(post.uri)
const itemHref = makeProfileLink(post.author, 'post', itemUrip.rkey)
Expand Down
2 changes: 1 addition & 1 deletion src/view/com/posts/FeedItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ let FeedItemInner = ({
const pal = usePalette('default')
const {track} = useAnalytics()
const [limitLines, setLimitLines] = useState(
countLines(richText.text) >= MAX_POST_LINES,
() => countLines(richText.text) >= MAX_POST_LINES,
)

const href = useMemo(() => {
Expand Down

0 comments on commit c858b58

Please sign in to comment.