Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aac16fd

Browse files
committedFeb 7, 2025··
BA-2234 Optimistic updates
1 parent 8de60b1 commit aac16fd

File tree

6 files changed

+69
-29
lines changed

6 files changed

+69
-29
lines changed
 

‎packages/components/__generated__/useMessagesListSubscription.graphql.ts

+27-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/components/modules/messages/MessagesList/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const MessagesList: FC<MessagesListProps> = ({
7777
}
7878
}, [room?.id, room?.unreadMessages?.count, currentProfile])
7979

80-
useMessagesListSubscription(room?.id)
80+
useMessagesListSubscription(room.id, currentProfile!.id)
8181

8282
const renderLoadingState = () => {
8383
if (!isLoadingNext) return <Box sx={{ height: 50 }} />

‎packages/components/modules/messages/SendMessage/index.tsx

+34-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { forwardRef } from 'react'
44

55
import { useCurrentProfile } from '@baseapp-frontend/authentication'
6-
import { setFormRelayErrors } from '@baseapp-frontend/utils'
6+
import { setFormRelayErrors, useNotification } from '@baseapp-frontend/utils'
77

88
import { zodResolver } from '@hookform/resolvers/zod'
99
import { useForm } from 'react-hook-form'
@@ -15,6 +15,7 @@ import {
1515
SOCIAL_UPSERT_FORM_VALIDATION_SCHEMA,
1616
} from '../../__shared__/constants'
1717
import { SocialUpsertForm } from '../../__shared__/types'
18+
import { MESSAGE_TYPE } from '../constants'
1819
import { useSendMessageMutation } from '../graphql/mutations/SendMessage'
1920
import { SendMessageProps } from './types'
2021

@@ -76,6 +77,7 @@ let nextClientMutationId = 0
7677
const SendMessage = forwardRef<HTMLInputElement, SendMessageProps>(
7778
({ roomId, SocialInput = DefaultSocialInput, SocialInputProps = {} }, ref) => {
7879
const { currentProfile } = useCurrentProfile()
80+
const { sendToast } = useNotification()
7981

8082
const form = useForm<SocialUpsertForm>({
8183
defaultValues: DEFAULT_SOCIAL_UPSERT_FORM_VALUES,
@@ -90,32 +92,57 @@ const SendMessage = forwardRef<HTMLInputElement, SendMessageProps>(
9092
const clientMutationId = nextClientMutationId.toString()
9193

9294
const connectionID = ConnectionHandler.getConnectionID(roomId, 'chatRoom_allMessages')
95+
const content = data.body
9396

9497
commitMutation({
9598
variables: {
9699
input: {
97-
content: data.body,
100+
content,
98101
profileId: currentProfile?.id,
99102
roomId,
100103
clientMutationId,
101104
},
102105
connections: [connectionID],
103106
},
107+
optimisticResponse: {
108+
chatRoomSendMessage: {
109+
message: {
110+
node: {
111+
id: `client:new_message:${Date.now()}`,
112+
content,
113+
created: new Date(Date.now()).toISOString(),
114+
extraData: null,
115+
messageType: MESSAGE_TYPE.user,
116+
inReplyTo: null,
117+
isRead: true,
118+
profile: {
119+
id: currentProfile.id,
120+
},
121+
verb: 'SENT_MESSAGE',
122+
},
123+
},
124+
},
125+
},
104126
onCompleted: (response, errors) => {
105127
if (errors) {
106128
// TODO: handle errors
107129
console.error(errors)
130+
sendToast('Your last message could not be sent. Please try again.', { type: 'error' })
108131
}
109132
const mutationErrors = response?.chatRoomSendMessage?.errors
110-
setFormRelayErrors(form, mutationErrors)
111-
112-
if (!mutationErrors?.length) {
113-
form.reset()
133+
if (mutationErrors) {
134+
console.log(mutationErrors)
135+
setFormRelayErrors(form, mutationErrors)
136+
sendToast('Your last message could not be sent. Please try again.', { type: 'error' })
114137
}
115138
},
116139
// TODO: handle errors
117-
onError: console.error,
140+
onError: (errors) => {
141+
console.error(errors)
142+
sendToast('Your last message could not be sent. Please try again.', { type: 'error' })
143+
},
118144
})
145+
form.reset()
119146
}
120147

121148
return (

‎packages/components/modules/messages/graphql/mutations/SendMessage.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { Disposable, UseMutationConfig, graphql, useMutation } from 'react-relay
55
import { SendMessageMutation } from '../../../../__generated__/SendMessageMutation.graphql'
66

77
export const SendMessageMutationQuery = graphql`
8-
mutation SendMessageMutation($input: ChatRoomSendMessageInput!, $connections: [ID!]!) {
8+
mutation SendMessageMutation($input: ChatRoomSendMessageInput!, $connections: [ID!]!)
9+
@raw_response_type {
910
chatRoomSendMessage(input: $input) {
1011
message @prependEdge(connections: $connections) {
1112
node {

‎packages/components/modules/messages/graphql/subscriptions/useMessagesListSubscription.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { useMemo } from 'react'
33
import { ConnectionHandler, graphql, useSubscription } from 'react-relay'
44

55
export const newMessageSubscription = graphql`
6-
subscription useMessagesListSubscription($roomId: ID!, $connections: [ID!]!) {
7-
chatRoomOnNewMessage(roomId: $roomId) {
6+
subscription useMessagesListSubscription($roomId: ID!, $profileId: ID!, $connections: [ID!]!) {
7+
chatRoomOnNewMessage(roomId: $roomId, profileId: $profileId) {
88
message @prependEdge(connections: $connections) {
99
node {
1010
...MessageItemFragment
@@ -17,13 +17,14 @@ export const newMessageSubscription = graphql`
1717
}
1818
`
1919

20-
const useMessagesListSubscription = (roomId: string) => {
20+
const useMessagesListSubscription = (roomId: string, profileId: string) => {
2121
const config = useMemo(() => {
2222
const connectionID = ConnectionHandler.getConnectionID(roomId, 'chatRoom_allMessages')
2323
return {
2424
subscription: newMessageSubscription,
2525
variables: {
2626
roomId,
27+
profileId,
2728
connections: [connectionID],
2829
},
2930
onError: console.error,

‎packages/components/schema.graphql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ type RoleUpdatePayload {
16481648
}
16491649

16501650
type Subscription {
1651-
chatRoomOnNewMessage(roomId: ID!): ChatRoomOnNewMessage
1651+
chatRoomOnNewMessage(profileId: ID!, roomId: ID!): ChatRoomOnNewMessage
16521652
chatRoomOnRoomUpdate(profileId: ID!): ChatRoomOnRoomUpdate
16531653
chatRoomOnMessagesCountUpdate(profileId: ID!): ChatRoomOnMessagesCountUpdate
16541654
onNotificationChange: OnNotificationChange

0 commit comments

Comments
 (0)
Please sign in to comment.