Skip to content

Commit c971230

Browse files
authored
[C-5212] Open comment drawer from empty state (#10117)
1 parent 45da925 commit c971230

File tree

5 files changed

+64
-23
lines changed

5 files changed

+64
-23
lines changed

packages/mobile/src/components/comments/CommentDrawer.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ const BORDER_RADIUS = 40
199199
export type CommentDrawerData = {
200200
entityId: number
201201
navigation: NativeStackNavigationProp<ParamListBase>
202+
autoFocusInput?: boolean
202203
}
203204

204205
type CommentDrawerProps = {
@@ -207,7 +208,13 @@ type CommentDrawerProps = {
207208
} & CommentDrawerData
208209

209210
export const CommentDrawer = (props: CommentDrawerProps) => {
210-
const { entityId, navigation, bottomSheetModalRef, handleClose } = props
211+
const {
212+
entityId,
213+
navigation,
214+
bottomSheetModalRef,
215+
handleClose,
216+
autoFocusInput
217+
} = props
211218
const { color } = useTheme()
212219
const insets = useSafeAreaInsets()
213220
const commentListRef = useRef<BottomSheetFlatListMethods>(null)
@@ -245,6 +252,7 @@ export const CommentDrawer = (props: CommentDrawerProps) => {
245252
commentListRef={commentListRef}
246253
onAutocompleteChange={onAutoCompleteChange}
247254
setAutocompleteHandler={setAutocompleteHandler}
255+
autoFocus={autoFocusInput}
248256
/>
249257
</CommentSectionProvider>
250258
</BottomSheetFooter>

packages/mobile/src/components/comments/CommentDrawerContext.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ export const CommentDrawerProvider = (props: PropsWithChildren) => {
5454
{children}
5555
{drawerData ? (
5656
<CommentDrawer
57-
entityId={drawerData!.entityId}
58-
navigation={drawerData!.navigation}
57+
{...drawerData}
5958
bottomSheetModalRef={bottomSheetModalRef}
6059
handleClose={close}
6160
/>

packages/mobile/src/components/comments/CommentDrawerForm.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ type CommentDrawerFormProps = {
2323
commentListRef: RefObject<BottomSheetFlatListMethods>
2424
onAutocompleteChange?: (isActive: boolean, value: string) => void
2525
setAutocompleteHandler?: (handler: (user: UserMetadata) => void) => void
26+
autoFocus?: boolean
2627
}
2728

2829
export const CommentDrawerForm = (props: CommentDrawerFormProps) => {
29-
const { commentListRef, onAutocompleteChange, setAutocompleteHandler } = props
30+
const {
31+
commentListRef,
32+
onAutocompleteChange,
33+
setAutocompleteHandler,
34+
autoFocus
35+
} = props
3036
const insets = useSafeAreaInsets()
3137
const { entityId, replyingAndEditingState, setReplyingAndEditingState } =
3238
useCurrentCommentSection()
@@ -69,6 +75,7 @@ export const CommentDrawerForm = (props: CommentDrawerFormProps) => {
6975
onAutocompleteChange={onAutocompleteChange}
7076
setAutocompleteHandler={setAutocompleteHandler}
7177
TextInputComponent={BottomSheetTextInput as any}
78+
autoFocus={autoFocus}
7279
/>
7380
</Box>
7481
)

packages/mobile/src/components/comments/CommentForm.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,24 @@ const CommentFormHelperText = (props: CommentFormHelperTextProps) => {
6969
}
7070

7171
type CommentFormProps = {
72-
onSubmit: (commentMessage: string, mentions?: ID[]) => void
72+
onSubmit?: (commentMessage: string, mentions?: ID[]) => void
7373
initialValue?: string
7474
isLoading?: boolean
7575
onAutocompleteChange?: (isActive: boolean, value: string) => void
7676
setAutocompleteHandler?: (handler: (user: UserMetadata) => void) => void
7777
TextInputComponent?: typeof RNTextInput
78+
autoFocus?: boolean
7879
}
7980

8081
export const CommentForm = (props: CommentFormProps) => {
8182
const {
8283
isLoading,
8384
setAutocompleteHandler,
8485
onAutocompleteChange,
85-
onSubmit,
86+
onSubmit = () => {},
8687
initialValue,
87-
TextInputComponent
88+
TextInputComponent,
89+
autoFocus
8890
} = props
8991
const [messageId, setMessageId] = useState(0)
9092
const [initialMessage, setInitialMessage] = useState(initialValue)
@@ -128,6 +130,12 @@ export const CommentForm = (props: CommentFormProps) => {
128130
}
129131
}, [editingComment])
130132

133+
useEffect(() => {
134+
if (autoFocus) {
135+
ref.current?.focus()
136+
}
137+
}, [autoFocus])
138+
131139
const handleLayout = useCallback(() => {
132140
if (
133141
(replyingToComment || editingComment) &&

packages/mobile/src/components/comments/CommentSection.tsx

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import { useCallback } from 'react'
22

33
import {
44
CommentSectionProvider,
5-
useCurrentCommentSection,
6-
usePostComment
5+
useCurrentCommentSection
76
} from '@audius/common/context'
87
import { commentsMessages as messages } from '@audius/common/messages'
98
import type { ID } from '@audius/common/models'
10-
import { TouchableOpacity } from 'react-native'
9+
import { TouchableOpacity, TouchableWithoutFeedback, View } from 'react-native'
1110
import { useEffectOnce } from 'react-use'
1211

1312
import {
@@ -29,6 +28,7 @@ import { CommentForm } from './CommentForm'
2928
type CommentSectionHeaderProps = {
3029
openCommentDrawer: () => void
3130
}
31+
3232
const CommentSectionHeader = (props: CommentSectionHeaderProps) => {
3333
const { openCommentDrawer } = props
3434
const {
@@ -68,18 +68,25 @@ const CommentSectionHeader = (props: CommentSectionHeaderProps) => {
6868
)
6969
}
7070

71-
const CommentSectionContent = () => {
71+
type CommentSectionContentProps = {
72+
openCommentDrawer: (args?: { autoFocusInput?: boolean }) => void
73+
}
74+
75+
const CommentSectionContent = (props: CommentSectionContentProps) => {
76+
const { openCommentDrawer } = props
7277
const {
7378
commentSectionLoading: isLoading,
7479
commentIds,
7580
isEntityOwner
7681
} = useCurrentCommentSection()
7782

78-
const [postComment] = usePostComment()
83+
const handlePress = useCallback(() => {
84+
openCommentDrawer()
85+
}, [openCommentDrawer])
7986

80-
const handlePostComment = (message: string, mentions?: ID[]) => {
81-
postComment(message, undefined, undefined, mentions)
82-
}
87+
const handleFormPress = useCallback(() => {
88+
openCommentDrawer({ autoFocusInput: true })
89+
}, [openCommentDrawer])
8390

8491
// Loading state
8592
if (isLoading) {
@@ -101,12 +108,22 @@ const CommentSectionContent = () => {
101108
<Text variant='body'>
102109
{isEntityOwner ? messages.noCommentsOwner : messages.noComments}
103110
</Text>
104-
<CommentForm onSubmit={handlePostComment} />
111+
<TouchableWithoutFeedback onPress={handleFormPress}>
112+
<View>
113+
<View pointerEvents='none'>
114+
<CommentForm />
115+
</View>
116+
</View>
117+
</TouchableWithoutFeedback>
105118
</Flex>
106119
)
107120
}
108121

109-
return <CommentBlock commentId={commentIds[0]} hideActions />
122+
return (
123+
<TouchableOpacity onPress={handlePress}>
124+
<CommentBlock commentId={commentIds[0]} hideActions />
125+
</TouchableOpacity>
126+
)
110127
}
111128

112129
type CommentSectionProps = {
@@ -122,9 +139,13 @@ export const CommentSection = (props: CommentSectionProps) => {
122139
const navigation = useNavigation()
123140
const { open } = useCommentDrawer()
124141

125-
const openCommentDrawer = useCallback(() => {
126-
open({ entityId, navigation })
127-
}, [open, entityId, navigation])
142+
const openCommentDrawer = useCallback(
143+
(args: { autoFocusInput?: boolean } = {}) => {
144+
const { autoFocusInput } = args
145+
open({ entityId, navigation, autoFocusInput })
146+
},
147+
[open, entityId, navigation]
148+
)
128149

129150
useEffectOnce(() => {
130151
if (showComments) {
@@ -137,9 +158,7 @@ export const CommentSection = (props: CommentSectionProps) => {
137158
<Flex gap='s' direction='column' w='100%' alignItems='flex-start'>
138159
<CommentSectionHeader openCommentDrawer={openCommentDrawer} />
139160
<Paper w='100%' direction='column' gap='s' p='l'>
140-
<TouchableOpacity onPress={openCommentDrawer}>
141-
<CommentSectionContent />
142-
</TouchableOpacity>
161+
<CommentSectionContent openCommentDrawer={openCommentDrawer} />
143162
</Paper>
144163
</Flex>
145164
</CommentSectionProvider>

0 commit comments

Comments
 (0)