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

DRAFT: URQL POC #724

Draft
wants to merge 148 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
148 commits
Select commit Hold shift + click to select a range
fc98f4d
Superficial URQL POC
lorenjohnson Jun 6, 2024
a6a3261
Tidy
lorenjohnson Jun 6, 2024
7345c1f
Remove unused code
lorenjohnson Jun 22, 2024
e80857b
Remove
lorenjohnson Jun 22, 2024
05889f9
Add new hook for route params
lorenjohnson Jun 22, 2024
49771d4
Replace getRouteParam with useRouteParams
lorenjohnson Jun 22, 2024
aa9c84a
Remove getRouteParam
lorenjohnson Jun 22, 2024
e680d10
Merge branch 'remove-old-code' into initial-load-time
lorenjohnson Jun 22, 2024
0e8a7c6
Merge branch 'use-route-params' into initial-load-time
lorenjohnson Jun 22, 2024
a287719
Adds urql useQuery-like useHyloQuery for refactoring use
lorenjohnson Jun 22, 2024
7654b8f
Applies useHyloQuery to AuthRootNavigator
lorenjohnson Jun 22, 2024
90dc2a4
Applies useHyloQuery to PostDetails
lorenjohnson Jun 22, 2024
1298569
Update useHyloQuery
lorenjohnson Jun 22, 2024
709a60e
Update components
lorenjohnson Jun 22, 2024
5f51350
Add Reactotron for debugging
lorenjohnson Jun 22, 2024
1271c72
Updates
lorenjohnson Jun 22, 2024
94b77ad
Some evo graphql/store reconciliation
lorenjohnson Jun 22, 2024
7fc15a1
Switch to evo MeQuery for fetchCurrentUser
lorenjohnson Jun 22, 2024
01599a3
Remove unused constant
lorenjohnson Jun 22, 2024
06a2dcf
Merge branch 'transition-to-urql-setup' into transition-to-urql-setup…
lorenjohnson Jun 22, 2024
1199f45
evo parity
lorenjohnson Jun 22, 2024
1e13f60
Merge branch 'transition-to-urql-setup' into transition-to-urql-setup…
lorenjohnson Jun 22, 2024
2b97861
Merge branch 'dev' into transition-to-urql-setup-initial-load-time
lorenjohnson Jun 23, 2024
773972f
Better
lorenjohnson Jun 23, 2024
a5d8baf
Turn off Sentry error reporting when using Staging API
lorenjohnson Jun 23, 2024
faa0a2b
Merge branch 'dev' into turn-off-sentry-on-staging
lorenjohnson Jun 24, 2024
b748b36
Optionally enable Sentry error reporting in dev, add better meta data…
lorenjohnson Jun 24, 2024
539416a
Fix PostFooter test complaining about having no key for iterated render
lorenjohnson Jun 24, 2024
a1d3554
Merge branch 'dev' into transition-to-urql-setup-initial-load-time
lorenjohnson Jun 24, 2024
1ba22b1
Merge branch 'turn-off-sentry-on-staging' into transition-to-urql-set…
lorenjohnson Jun 24, 2024
7df27dc
Merge branch 'postfooter-test-fix' into transition-to-urql-setup-init…
lorenjohnson Jun 24, 2024
3881362
Simplify and document Sentry dev options
lorenjohnson Jun 24, 2024
46b4ae7
Merge branch 'turn-off-sentry-on-staging' into transition-to-urql-set…
lorenjohnson Jun 24, 2024
83583cd
Remove console.error
lorenjohnson Jun 24, 2024
dca4da8
groupFieldsFragment Parity with Hylo Web
lorenjohnson Jun 24, 2024
b2ea68f
Hylo Web parity groupTopicsQueryFragment
lorenjohnson Jun 24, 2024
d7e7d9a
Deep memoize useHyloQuery, nail down querying. Working
lorenjohnson Jun 24, 2024
92faebc
Update snapshots
lorenjohnson Jun 24, 2024
426fa42
Update PostDetails test, investigate and address most test warnings
lorenjohnson Jun 24, 2024
9cb10aa
Update CHANGELOG
lorenjohnson Jun 24, 2024
19c0a75
React Native Render HTML update, and tuning to improve render times
lorenjohnson Jun 24, 2024
492e12f
Make Sentry enabled boolean
lorenjohnson Jun 24, 2024
a7bfa8b
Merge branch 'turn-off-sentry-on-staging' into transition-to-urql-set…
lorenjohnson Jun 24, 2024
3ae0b90
Fixes "shadow" React Native warning
lorenjohnson Jun 24, 2024
b440047
Merge branch 'dev' into transition-to-urql-setup-initial-load-time
lorenjohnson Jun 24, 2024
c2552dd
Merge branch 'fix-shadow-warning' into transition-to-urql-setup-initi…
lorenjohnson Jun 24, 2024
ae966bf
Turn off i18n-next debug logging as default
lorenjohnson Jun 24, 2024
c187314
Merge branch 'turn-off-i18n-debugging' into transition-to-urql-setup-…
lorenjohnson Jun 24, 2024
ce8fc7f
Merge branch 'dev' into transition-to-urql-setup-initial-load-time
lorenjohnson Jun 24, 2024
661d745
Fix from merge
lorenjohnson Jun 24, 2024
53fc5ab
React Native Render HTML update, and tuning to improve render times
lorenjohnson Jun 24, 2024
9fde5f8
Linting
lorenjohnson Jun 24, 2024
34bea63
Merge branch 'memoize-native-html-render' into transition-to-urql-set…
lorenjohnson Jun 24, 2024
4414030
Fixes crashing bug with PostBodyProposals conditional rendering outpu…
lorenjohnson Jun 24, 2024
d31698f
Merge branch 'fix-proposals-body' into transition-to-urql-setup-initi…
lorenjohnson Jun 24, 2024
982f150
Update dev dependencies
lorenjohnson Jun 25, 2024
dfa214f
Remove unused script
lorenjohnson Jun 25, 2024
68cc3b3
Update CircleCI run environment to Node 20.15.0
lorenjohnson Jun 25, 2024
43e3dc3
CircleCI test run command update for Yarn > v1
lorenjohnson Jun 25, 2024
42d2e16
Merge branch 'update-dev-deps' into transition-to-urql-setup-initial-…
lorenjohnson Jun 25, 2024
7ef1d12
Harmonized depedencies with dev deps updates
lorenjohnson Jun 25, 2024
abdd121
Merge branch 'update-dev-deps' into transition-to-urql-setup-initial-…
lorenjohnson Jun 25, 2024
43428a8
Update broken test
lorenjohnson Jun 25, 2024
8161e6c
Merge branch 'update-dev-deps' into transition-to-urql-setup-initial-…
lorenjohnson Jun 25, 2024
b12e1ab
Merge branch 'dev' into transition-to-urql-setup-initial-load-time
lorenjohnson Jun 25, 2024
4c3f652
Merge branch 'dev' into transition-to-urql-setup-initial-load-time
lorenjohnson Jun 25, 2024
0982fe6
Merge branch 'dev' into transition-to-urql-setup-initial-load-time
lorenjohnson Jun 25, 2024
0662073
Linting
lorenjohnson Jun 26, 2024
38e88c1
Tidy
lorenjohnson Jun 26, 2024
5e691f5
Tidy
lorenjohnson Jun 26, 2024
6fba1c6
Update Podfile.lock
lorenjohnson Jun 26, 2024
e016b1a
Clean-up initial data loading through useHyloQuery
lorenjohnson Jun 26, 2024
262352e
Tidy-up and ready for review
lorenjohnson Jun 26, 2024
bbd07c3
Merge branch 'dev' into urql-poc
lorenjohnson Jun 26, 2024
0fcdca1
Fix merge issue
lorenjohnson Jun 26, 2024
2966a08
Merge branch 'transition-to-urql-setup-initial-load-time' into urql-p…
lorenjohnson Jun 26, 2024
6bb9b38
Fix issues afer merge
lorenjohnson Jun 26, 2024
dce265e
Merge branch 'dev' into transition-to-urql-setup-initial-load-time
lorenjohnson Jun 26, 2024
5e6be86
Merge branch 'transition-to-urql-setup-initial-load-time' into urql-poc
lorenjohnson Jun 26, 2024
76e56d5
Update useHyloQuery data to use URQL-standard result key wrapping
lorenjohnson Jun 26, 2024
2c21f4c
Merge branch 'transition-to-urql-setup-initial-load-time' into urql-poc
lorenjohnson Jun 26, 2024
e9a0031
Update modelExtractor to not abuse __typename #urql-shared
lorenjohnson Jun 28, 2024
7f6c012
Disabuse __typename in Search results #urql-shared
lorenjohnson Jun 28, 2024
a8459b1
Updatest
lorenjohnson Jul 1, 2024
7c6f245
Move client
lorenjohnson Jul 1, 2024
22cb795
Employ URQL + ReduxORM digesting useQueryAction
lorenjohnson Jul 1, 2024
2d80448
Apply useQueryAction to AuthRootNav
lorenjohnson Jul 1, 2024
d47c56b
Update NotificationsList
lorenjohnson Jul 1, 2024
844e998
Basic graphcaching config
lorenjohnson Jul 1, 2024
28b3b1d
Tidy-up
lorenjohnson Jul 2, 2024
0eeb0fe
Convert RootNav to use useQueryAction
lorenjohnson Jul 2, 2024
2f7123d
FeedList and NotificationsList calling usQueryAction
lorenjohnson Jul 2, 2024
c725fa4
Merge branch 'dev' into urql-poc
lorenjohnson Jul 2, 2024
dd1d553
Merge branch 'urql-poc' into urql-poc-run-ahead
lorenjohnson Jul 2, 2024
3728d8a
Merge fix
lorenjohnson Jul 2, 2024
4dec132
Merge branch 'dev' into urql-poc
lorenjohnson Jul 10, 2024
659dce4
Merge branch 'urql-poc' into urql-poc-run-ahead
lorenjohnson Jul 10, 2024
481f7d0
Merge branch 'dev' into urql-poc
lorenjohnson Jul 11, 2024
df7e903
Merge branch 'urql-poc' into urql-poc-run-ahead
lorenjohnson Jul 11, 2024
fe7a8bf
Move to yarn berry version
lorenjohnson Aug 3, 2024
b03eb8c
Merge branch 'move-to-yarn-berry' into urql-poc
lorenjohnson Aug 3, 2024
c530969
update yarn deps
lorenjohnson Aug 3, 2024
b680d0b
Merge branch 'dev' into urql-poc
lorenjohnson Aug 3, 2024
5e715ac
Corrects out-of-bounds cocoapods version in lockfile
lorenjohnson Aug 3, 2024
c7e9d2e
Merge branch 'correct-cocoapods-version' into urql-poc
lorenjohnson Aug 3, 2024
3218a08
Add URQL caching config for Roles/Responsibilities
lorenjohnson Aug 3, 2024
0daa05e
Pin to yarn 1.22.22 to make corepack happy
lorenjohnson Aug 3, 2024
110fc5a
Merge branch 'correct-cocoapods-version' into specify-yarn
lorenjohnson Aug 3, 2024
9190bfe
Fix Sentry script escaping breaking react-native-version script
lorenjohnson Aug 3, 2024
64896b3
5.7.1
lorenjohnson Aug 3, 2024
c0b123e
Merge branch 'yarn-pods-and-certs' into urql-poc
lorenjohnson Aug 3, 2024
79d45db
Merge branch 'dev' into urql-poc
lorenjohnson Aug 4, 2024
34adb7a
Move to 1.22 yarn lockfile first
lorenjohnson Aug 4, 2024
dac8665
Update pods
lorenjohnson Aug 4, 2024
d959329
Fix crashing bundler issue
lorenjohnson Aug 4, 2024
c21154c
Upgrade to Yarn 3.6.4
lorenjohnson Aug 4, 2024
4f01916
Merge branch 'yarn-3-6-4' into urql-poc
lorenjohnson Aug 4, 2024
e6b6d51
Update xcode project
lorenjohnson Aug 4, 2024
350c4df
Merge branch 'yarn-3-6-4' into urql-poc
lorenjohnson Aug 4, 2024
7299a31
Updgrade clipboard component, add mock (should have been there before…
lorenjohnson Aug 4, 2024
b614d29
Merge branch 'yarn-3-6-4' into urql-poc
lorenjohnson Aug 4, 2024
9d3ebc5
Linting
lorenjohnson Aug 4, 2024
e012824
Merge branch 'yarn-3-6-4' into urql-poc
lorenjohnson Aug 4, 2024
459cd5f
Merge branch 'dev' into urql-poc
lorenjohnson Aug 4, 2024
bfc5c82
Merge branch 'dev' into urql-poc
lorenjohnson Aug 4, 2024
f9c9126
Merge branch 'dev' into urql-poc
lorenjohnson Aug 4, 2024
fd26ef0
Merge branch 'dev' into urql-poc
lorenjohnson Aug 6, 2024
843d6f5
Update snapshot
lorenjohnson Aug 6, 2024
7a28f42
Make URQL work with recent RN upgrade and roles/responsiblities work
lorenjohnson Sep 2, 2024
a9d1f28
Rename the temporary useQueryAction to useUrqlQueryAction for clarity
lorenjohnson Sep 3, 2024
6a64e5a
Add queryset key for Responsibility
lorenjohnson Sep 3, 2024
8f619f0
Sync query from evo
lorenjohnson Sep 3, 2024
6effad3
Transition most getMe (Redux ORM selector) to useCurrentUser (URQL ca…
lorenjohnson Sep 5, 2024
4f269dd
Tidy
lorenjohnson Sep 6, 2024
b6d1500
WIP: URQL StreamList / PostRow
lorenjohnson Sep 6, 2024
184ab15
Fix from dev
lorenjohnson Sep 6, 2024
a4e9da1
Make fetchComments result stable
lorenjohnson Sep 6, 2024
4e5f0ea
Moved to URQL useQuery in StreamList, PostDetails, and Comments... Ma…
lorenjohnson Sep 9, 2024
0ea38d5
chore: cursorPagination for comments/childComments, move off Redux fe…
lorenjohnson Sep 30, 2024
8ace762
chore: Remove Notification model and update NotificationList, etc
lorenjohnson Oct 2, 2024
e907335
chore: clean-up URQL client configuration
lorenjohnson Oct 2, 2024
2469790
chore: Optimistic updates churn
lorenjohnson Oct 2, 2024
b7b37f7
chore: update URQL optimistic and updates
lorenjohnson Oct 2, 2024
8f115dc
Merge branch 'dev' into urql-poc
lorenjohnson Oct 2, 2024
4abb856
chore: add async URQL Client config for schema introspection
lorenjohnson Oct 2, 2024
9ab04c9
chore: tidy-up URQL setup
lorenjohnson Oct 2, 2024
5c19077
chore: remove unnecessary import
lorenjohnson Oct 3, 2024
877853e
chore: URQL refinements. Pagination broken :<
lorenjohnson Oct 3, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ npm-debug.log
!.yarn/sdks
!.yarn/versions
yarn-error.log
.yarn

# Google login
google-services.json
Expand Down
46 changes: 43 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { enableScreens } from 'react-native-screens'
import React, { useEffect, useState } from 'react'
// Required for react-native-root-toast
import { RootSiblingParent } from 'react-native-root-siblings'
import { Provider as UrqlProvider } from 'urql'
import { Provider } from 'react-redux'
import { AppRegistry, Platform, AppState, UIManager } from 'react-native'
import { AppRegistry, Platform, AppState, UIManager, LogBox } from 'react-native'
import Timer from 'react-native-background-timer'
import * as Sentry from '@sentry/react-native'
import { OneSignal } from 'react-native-onesignal'
import client from 'urql-shared/client'
import { sentryConfig } from 'config'
import store from 'store'
import { name as appName } from './app.json'
Expand All @@ -20,6 +22,7 @@ import './i18n'
import 'intl-pluralrules'
import { ActionSheetProvider } from '@expo/react-native-action-sheet'
import { baseStyle, tagsStyles, classesStyles } from 'components/HyloHTML/HyloHTML.styles'

// import FastImage from 'react-native-fast-image'

Sentry.init(sentryConfig)
Expand All @@ -45,6 +48,41 @@ if (Platform.OS === 'android') {

AppRegistry.registerComponent(appName, () => App)

if (__DEV__) {
const suppressedMessages = [
'Selector unknown returned a different result when called with the same parameters',
'Support for defaultProps will be removed from memo components',
'Sending `onAnimatedValueUpdate` with no listeners registered.'
]
console.log()
console.log('🗒️ NOTE: Logging and warnings suppressed for these known messages/issues:')
suppressedMessages.forEach(message => console.log(`⚠️ ${message}`))
console.log()

LogBox.ignoreLogs(suppressedMessages)

const connectConsoleTextFromArgs = (arrayOfStrings) =>
arrayOfStrings
.slice(1)
.reduce(
(baseString, currentString) => baseString.replace('%s', currentString),
arrayOfStrings[0]
)

const filterIgnoredMessages = (logger) => (...args) => {
const output = connectConsoleTextFromArgs(args)

if (output && !suppressedMessages.some((log) => output.includes(log))) {
logger(...args)
}
}

console.log = filterIgnoredMessages(console.log)
console.info = filterIgnoredMessages(console.info)
console.warn = filterIgnoredMessages(console.warn)
console.error = filterIgnoredMessages(console.error)
}

enableScreens()

// Useful to debug in dev to diagnose any potential image loading issues,
Expand Down Expand Up @@ -100,8 +138,10 @@ export default function App () {
systemFonts={[...defaultSystemFonts, 'Circular-Book']}
>
<Provider store={store}>
<VersionCheck />
<RootNavigator />
<UrqlProvider value={client}>
<VersionCheck />
<RootNavigator />
</UrqlProvider>
</Provider>
</TRenderEngineProvider>
</RootSiblingParent>
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
"@react-navigation/native": "^6.1.18",
"@react-navigation/stack": "^6.4.1",
"@sentry/react-native": "^5.24.1",
"@urql/devtools": "^2.0.3",
"@urql/exchange-graphcache": "^7.1.1",
"babel-plugin-import-graphql": "^2.8.1",
"cross-fetch": "^4.0.0",
"emoji-datasource-apple": "^15.1.2",
"emoji-mart": "^5.6.0",
"emoji-mart-native": "^0.6.5-beta",
Expand Down Expand Up @@ -100,7 +103,8 @@
"reselect": "^4.1.6",
"sails.io.js": "^1.2.1",
"socket.io-client": "2.3.1",
"tinycolor2": "^1.6.0"
"tinycolor2": "^1.6.0",
"urql": "^4.1.0"
},
"devDependencies": {
"@babel/core": "^7.24.7",
Expand Down
12 changes: 6 additions & 6 deletions src/components/Comment/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import Clipboard from '@react-native-clipboard/clipboard'
import { TextHelpers } from 'hylo-shared'
import { useDispatch, useSelector } from 'react-redux'
import useHyloActionSheet from 'hooks/useHyloActionSheet'
import useReactionActions from 'hooks/useReactionActions'
import useReactOnEntity from 'urql-shared/hooks/useReactOnEntity'
import useCurrentUser from 'urql-shared/hooks/useCurrentUser'
import deleteCommentAction from 'store/actions/deleteComment'
import getGroup from 'store/selectors/getGroup'
import getMe from 'store/selectors/getMe'
import { getPresentedPost } from 'store/selectors/getPost'
import Avatar from 'components/Avatar'
import EmojiRow from 'components/EmojiRow'
Expand Down Expand Up @@ -39,9 +39,9 @@ export default function Comment ({
const { t } = useTranslation()
const dispatch = useDispatch()
const { showHyloActionSheet } = useHyloActionSheet()
const { reactOnEntity, removeReactOnFromEntity } = useReactionActions()
const { reactOnEntity, deleteReactionFromEntity } = useReactOnEntity()
const [showEmojiPicker, setShowEmojiPicker] = useState(false)
const currentUser = useSelector(getMe)
const currentUser = useCurrentUser()
const group = useSelector(state => getGroup(state, { slug }))
const canModerate = useSelector(state => hasResponsibilityForGroup(state, { responsibility: RESP_MANAGE_CONTENT, groupId: group?.id }))
const isCreator = currentUser && (comment.creator.id === currentUser.id)
Expand All @@ -52,8 +52,8 @@ export default function Comment ({
const myEmojis = myReactions.map((reaction) => reaction.emojiFull)
const groupIds = post?.groups.map(g => g.id)

const handleReaction = (emojiFull) => reactOnEntity({ commentId: comment?.id, emojiFull, entityType: 'comment', groupIds, postId: post })
const handleRemoveReaction = (emojiFull) => removeReactOnFromEntity({ commentId: comment?.id, emojiFull, entityType: 'comment', postId: post })
const handleReaction = (emojiFull) => reactOnEntity('comment', comment?.id, emojiFull, postId)
const handleRemoveReaction = (emojiFull) => deleteReactionFromEntity('comment', comment?.id, emojiFull, postId)
const handleReply = onReply && (() => onReply(comment))
const handleRemove = (!isCreator && canModerate) && (
() => Alert.alert(
Expand Down
48 changes: 0 additions & 48 deletions src/components/Comments/Comments.connector.js

This file was deleted.

20 changes: 0 additions & 20 deletions src/components/Comments/Comments.connector.test.js

This file was deleted.

99 changes: 58 additions & 41 deletions src/components/Comments/Comments.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
/* eslint-disable camelcase */
import React, { useEffect, useState, useRef, useImperativeHandle, forwardRef, useCallback } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import React, { useState, useRef, useImperativeHandle, forwardRef, useCallback } from 'react'
import { Text, TouchableOpacity, View, SectionList } from 'react-native'
import { useQuery } from 'urql'
import { isIOS } from 'util/platform'
import commentsQuery from 'graphql/queries/commentsQuery'
import childCommentsQuery from 'graphql/queries/childCommentsQuery'
import Comment from 'components/Comment'
import Loading from 'components/Loading'
import styles from './Comments.styles'
import { isEmpty, omit } from 'lodash/fp'
import {
getHasMoreComments,
getComments,
getTotalComments
} from 'store/selectors/getComments'
import fetchCommentsAction from 'store/actions/fetchComments'
import { FETCH_COMMENTS } from 'store/constants'

function Comments ({
postId,
Expand All @@ -24,24 +18,43 @@ function Comments ({
panHandlers,
onSelect
}, ref) {
const dispatch = useDispatch()
const comments = useSelector(state => getComments(state, { postId })) || []
const pending = useSelector(state => state.pending[FETCH_COMMENTS])
const sections = comments.map(comment => ({
comment: omit(['subComments'], comment),
data: comment.subComments
}))
const [{ data, pending }] = useQuery({ query: commentsQuery, variables: { postId } })
const post = data?.post
const commentsQuerySet = post?.comments
const comments = commentsQuerySet?.items || []

const [highlightedComment, setHighlightedComment] = useState()
const commentsListRef = useRef()
const sections = comments?.map((comment, index) => {
return ({
comment,
data: comment.childComments?.items || []
})
})

const scrollToComment = useCallback((comment, viewPosition = 0.2) => {
const parentCommentId = comment.parentComment || comment.id
const subCommentId = comment.parentComment ? comment.id : null
const section = sections.find(s => parentCommentId === s.comment.id)
const sectionIndex = section.comment.sectionIndex
const itemIndex = section.data.find(subComment =>
subCommentId === subComment.id)?.itemIndex || section.data.length + 1
commentsListRef?.current.scrollToLocation({ sectionIndex, itemIndex, viewPosition })
const parentCommentId = comment.parentComment?.id || comment.id
const childCommentId = comment.parentComment ? comment.id : null
const parentCommentIndex = sections.findIndex(s => parentCommentId === s.comment.id)
const childCommentIndex = sections[parentCommentIndex].data.findIndex(childComment => childCommentId === childComment.id)
const hasChildComments = sections[parentCommentIndex].data.length > 0
const lastItemIndex = sections[parentCommentIndex].data.length - 1

// NOTE: The logic below is a bit convoluted due to inverted SectionList, but it works.
let itemIndex
if (childCommentId) {
itemIndex = childCommentIndex + 1
} else if (hasChildComments && !childCommentId) {
itemIndex = lastItemIndex + 1
} else {
itemIndex = 1
}

commentsListRef?.current.scrollToLocation({
sectionIndex: parentCommentIndex === -1 ? 0 : parentCommentIndex,
itemIndex,
viewPosition
})
}, [sections])

const selectComment = useCallback(comment => {
Expand All @@ -56,14 +69,11 @@ function Comments ({
clearHighlightedComment: () => setHighlightedComment(null)
}), [setHighlightedComment, scrollToComment])

useEffect(() => {
dispatch(fetchCommentsAction({ postId }))
}, [dispatch, postId])

// Comment rendering (parent)
const Header = () => (
<>
{providedHeader}
<ShowMore postId={postId} />
<ShowMore postOrComment={post} style={styles.childCommentsShowMore} />
{pending && (
<View style={styles.loadingContainer}>
<Loading style={styles.loading} />
Expand All @@ -72,10 +82,11 @@ function Comments ({
</>
)

// Comment rendering (parent)
const SectionFooter = ({ section: { comment } }) => {
return (
<>
<ShowMore commentId={comment.id} style={styles.subCommentsShowMore} forSubcomments />
<ShowMore postOrComment={comment} style={styles.childCommentsShowMore} />
<Comment
clearHighlighted={() => setHighlightedComment(null)}
comment={comment}
Expand All @@ -91,6 +102,7 @@ function Comments ({
)
}

// comment.childComments rendering
const Item = ({ item: comment }) => {
return (
<Comment
Expand All @@ -102,7 +114,7 @@ function Comments ({
setHighlighted={() => setHighlightedComment(comment)}
showMember={showMember}
slug={slug}
style={styles.subComment}
style={styles.childComment}
key={comment.id}
/>
)
Expand Down Expand Up @@ -131,21 +143,26 @@ function Comments ({

export default forwardRef(Comments)

export function ShowMore ({ postId, commentId, forSubcomments = false, style = {} }) {
const queryParams = commentId ? { commentId } : { postId }
const dispatch = useDispatch()
const fetchComments = () => dispatch(fetchCommentsAction(queryParams, { cursor }))
const comments = useSelector(state => getComments(state, queryParams)) || []
const cursor = !isEmpty(comments) && comments[comments.length - 1].id
const total = useSelector(state => getTotalComments(state, queryParams)) || 0
const hasMore = useSelector(state => getHasMoreComments(state, queryParams))
const extra = total - comments.length
export function ShowMore ({ postOrComment, style = {} }) {
const forSubcomments = !!postOrComment?.childComments
const commentQuerySet = forSubcomments
? postOrComment?.childComments
: postOrComment?.comments
const cursor = commentQuerySet?.items[commentQuerySet?.items.length - 1]?.id
const variables = forSubcomments
? { commentId: postOrComment?.id, cursor }
: { postId: postOrComment?.id, cursor }
const query = forSubcomments ? childCommentsQuery : commentsQuery
const [, fetchComments] = useQuery({ query, variables, pause: true })
const total = commentQuerySet?.total || 0
const hasMore = commentQuerySet?.hasMore
const extra = total - commentQuerySet?.items?.length || 0

if (!hasMore || extra < 1) return null

return (
<TouchableOpacity>
<Text style={[styles.showMore, style]} onPress={fetchComments}>
<Text style={[styles.showMore, style]} onPress={() => fetchComments()}>
View {extra} previous {forSubcomments ? 'replies' : `comment${extra > 1 ? 's' : ''}`}
</Text>
</TouchableOpacity>
Expand Down
Loading