1
1
import { api , type RouterOutputs } from '@/src/lib/trpc' ;
2
2
import { useGlobalStore } from '@/src/providers/global-store-provider' ;
3
3
import { type TypeId } from '@u22n/utils/typeid' ;
4
+ import { type InfiniteData } from '@tanstack/react-query' ;
5
+ import { useCallback } from 'react' ;
4
6
5
7
export function formatParticipantData (
6
8
participant : RouterOutputs [ 'convos' ] [ 'getOrgMemberConvos' ] [ 'data' ] [ number ] [ 'participants' ] [ number ]
@@ -56,9 +58,9 @@ export function formatParticipantData(
56
58
57
59
export function useAddSingleConvo$Cache ( ) {
58
60
const orgShortCode = useGlobalStore ( ( state ) => state . currentOrg . shortCode ) ;
59
- const convoListApi = api . useUtils ( ) . convos . getOrgMemberConvos ;
60
- const getOrgMemberSpecificConvoApi =
61
- api . useUtils ( ) . convos . getOrgMemberSpecificConvo ;
61
+ const utils = api . useUtils ( ) ;
62
+ const convoListApi = utils . convos . getOrgMemberConvos ;
63
+ const getOrgMemberSpecificConvoApi = utils . convos . getOrgMemberSpecificConvo ;
62
64
63
65
return async ( convoId : TypeId < 'convos' > ) => {
64
66
const convo = await getOrgMemberSpecificConvoApi . fetch ( {
@@ -78,10 +80,11 @@ export function useAddSingleConvo$Cache() {
78
80
export function useDeleteConvo$Cache ( ) {
79
81
const orgShortCode = useGlobalStore ( ( state ) => state . currentOrg . shortCode ) ;
80
82
const convoListApi = api . useUtils ( ) . convos . getOrgMemberConvos ;
81
-
82
- return async ( convoId : TypeId < 'convos' > ) => {
83
- await convoListApi . cancel ( { orgShortCode } ) ;
84
- convoListApi . setInfiniteData ( { orgShortCode } , ( updater ) => {
83
+ const deleteFn = useCallback (
84
+ (
85
+ convoId : TypeId < 'convos' > ,
86
+ updater ?: InfiniteData < RouterOutputs [ 'convos' ] [ 'getOrgMemberConvos' ] >
87
+ ) => {
85
88
if ( ! updater ) return ;
86
89
const clonedUpdater = structuredClone ( updater ) ;
87
90
for ( const page of clonedUpdater . pages ) {
@@ -93,52 +96,58 @@ export function useDeleteConvo$Cache() {
93
96
break ;
94
97
}
95
98
return clonedUpdater ;
96
- } ) ;
99
+ } ,
100
+ [ ]
101
+ ) ;
102
+
103
+ return async ( convoId : TypeId < 'convos' > ) => {
104
+ await convoListApi . cancel ( { orgShortCode } ) ;
105
+ await convoListApi . cancel ( { orgShortCode, includeHidden : true } ) ;
106
+
107
+ convoListApi . setInfiniteData ( { orgShortCode } , ( updater ) =>
108
+ deleteFn ( convoId , updater )
109
+ ) ;
110
+ convoListApi . setInfiniteData (
111
+ { orgShortCode, includeHidden : true } ,
112
+ ( updater ) => deleteFn ( convoId , updater )
113
+ ) ;
97
114
} ;
98
115
}
99
116
117
+ // TODO: Simplify this function later, its too complex
100
118
export function useToggleConvoHidden$Cache ( ) {
101
119
const orgShortCode = useGlobalStore ( ( state ) => state . currentOrg . shortCode ) ;
102
- const convoApi = api . useUtils ( ) . convos . getConvo ;
103
- const convoListApi = api . useUtils ( ) . convos . getOrgMemberConvos ;
104
- const specificConvoApi = api . useUtils ( ) . convos . getOrgMemberSpecificConvo ;
120
+ const utils = api . useUtils ( ) ;
121
+ const convoApi = utils . convos . getConvo ;
122
+ const convoListApi = utils . convos . getOrgMemberConvos ;
123
+ const specificConvoApi = utils . convos . getOrgMemberSpecificConvo ;
105
124
106
- return async ( convoId : TypeId < 'convos' > , hide = false ) => {
107
- const convoToAdd = ! hide
108
- ? await specificConvoApi . fetch ( {
109
- convoPublicId : convoId ,
110
- orgShortCode
111
- } )
112
- : null ;
113
-
114
- await convoApi . cancel ( { convoPublicId : convoId , orgShortCode } ) ;
115
- convoApi . setData ( { convoPublicId : convoId , orgShortCode } , ( updater ) => {
125
+ // This function is a bit complex, but basically what it does is updates the provided updater by either removing or adding a convo based on the parameters
126
+ const convoListUpdaterFn = useCallback (
127
+ (
128
+ hideFromList : boolean ,
129
+ convoToAdd : RouterOutputs [ 'convos' ] [ 'getOrgMemberSpecificConvo' ] | null ,
130
+ convoToRemove : TypeId < 'convos' > | null ,
131
+ updater ?: InfiniteData < RouterOutputs [ 'convos' ] [ 'getOrgMemberConvos' ] >
132
+ ) => {
116
133
if ( ! updater ) return ;
117
134
const clonedUpdater = structuredClone ( updater ) ;
118
- const participantIndex = clonedUpdater . data . participants . findIndex (
119
- ( participant ) => participant . publicId === updater . ownParticipantPublicId
120
- ) ;
121
- if ( participantIndex === - 1 ) return ;
122
- clonedUpdater . data . participants [ participantIndex ] ! . hidden = hide ;
123
- return clonedUpdater ;
124
- } ) ;
125
135
126
- await convoListApi . cancel ( { orgShortCode } ) ;
127
- convoListApi . setInfiniteData ( { orgShortCode } , ( updater ) => {
128
- if ( ! updater ) return ;
129
- const clonedUpdater = structuredClone ( updater ) ;
130
-
131
- if ( hide ) {
136
+ if ( hideFromList ) {
132
137
for ( const page of clonedUpdater . pages ) {
133
138
const convoIndex = page . data . findIndex (
134
- ( convo ) => convo . publicId === convoId
139
+ ( convo ) => convo . publicId === convoToRemove
135
140
) ;
136
141
if ( convoIndex === - 1 ) continue ;
137
142
page . data . splice ( convoIndex , 1 ) ;
138
143
break ;
139
144
}
140
145
} else {
141
- const clonedConvo = structuredClone ( convoToAdd ) ! ; // We know it's not null as we are not hiding
146
+ if ( ! convoToAdd )
147
+ throw new Error (
148
+ 'Trying to unhide from convo list without providing the convo to add'
149
+ ) ;
150
+ const clonedConvo = structuredClone ( convoToAdd ) ;
142
151
let convoAlreadyAdded = false ;
143
152
for ( const page of clonedUpdater . pages ) {
144
153
const insertIndex = page . data . findIndex (
@@ -159,14 +168,81 @@ export function useToggleConvoHidden$Cache() {
159
168
}
160
169
}
161
170
return clonedUpdater ;
171
+ } ,
172
+ [ ]
173
+ ) ;
174
+
175
+ return async ( convoId : TypeId < 'convos' > , hide = false ) => {
176
+ await convoApi . cancel ( { convoPublicId : convoId , orgShortCode } ) ;
177
+ convoApi . setData ( { convoPublicId : convoId , orgShortCode } , ( updater ) => {
178
+ if ( ! updater ) return ;
179
+ const clonedUpdater = structuredClone ( updater ) ;
180
+ const participantIndex = clonedUpdater . data . participants . findIndex (
181
+ ( participant ) => participant . publicId === updater . ownParticipantPublicId
182
+ ) ;
183
+ if ( participantIndex === - 1 ) return ;
184
+ clonedUpdater . data . participants [ participantIndex ] ! . hidden = hide ;
185
+ return clonedUpdater ;
162
186
} ) ;
187
+
188
+ const convoToAdd = await specificConvoApi . fetch ( {
189
+ convoPublicId : convoId ,
190
+ orgShortCode
191
+ } ) ;
192
+
193
+ // Update both hidden and non-hidden convo lists
194
+ await convoListApi . cancel ( { orgShortCode, includeHidden : true } ) ;
195
+ await convoListApi . cancel ( { orgShortCode } ) ;
196
+
197
+ // if we are hiding a convo, we need to remove it from the non-hidden list and add to hidden list
198
+ if ( hide ) {
199
+ convoListApi . setInfiniteData ( { orgShortCode } , ( updater ) =>
200
+ convoListUpdaterFn (
201
+ /* hide from non-hidden */ true ,
202
+ null ,
203
+ convoId ,
204
+ updater
205
+ )
206
+ ) ;
207
+ convoListApi . setInfiniteData (
208
+ { orgShortCode, includeHidden : true } ,
209
+ ( updater ) =>
210
+ convoListUpdaterFn (
211
+ /* add from hidden */ false ,
212
+ convoToAdd ,
213
+ null ,
214
+ updater
215
+ )
216
+ ) ;
217
+ } else {
218
+ // if we are un-hiding a convo, we need to remove it from the hidden list and add to non-hidden list
219
+ convoListApi . setInfiniteData ( { orgShortCode } , ( updater ) =>
220
+ convoListUpdaterFn (
221
+ /* add to non-hidden */ false ,
222
+ convoToAdd ,
223
+ null ,
224
+ updater
225
+ )
226
+ ) ;
227
+ convoListApi . setInfiniteData (
228
+ { orgShortCode, includeHidden : true } ,
229
+ ( updater ) =>
230
+ convoListUpdaterFn (
231
+ /* hide from hidden */ true ,
232
+ null ,
233
+ convoId ,
234
+ updater
235
+ )
236
+ ) ;
237
+ }
163
238
} ;
164
239
}
165
240
166
241
export function useUpdateConvoMessageList$Cache ( ) {
167
242
const orgShortCode = useGlobalStore ( ( state ) => state . currentOrg . shortCode ) ;
168
- const convoEntiresApi = api . useUtils ( ) . convos . entries . getConvoEntries ;
169
- const singleConvoEntryApi = api . useUtils ( ) . convos . entries . getConvoSingleEntry ;
243
+ const utils = api . useUtils ( ) ;
244
+ const convoEntiresApi = utils . convos . entries . getConvoEntries ;
245
+ const singleConvoEntryApi = utils . convos . entries . getConvoSingleEntry ;
170
246
171
247
// TODO: make the reply mutation return the new convo entry, to save one API call
172
248
return async (
0 commit comments