-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomponent.tsx
More file actions
289 lines (258 loc) · 9.77 KB
/
component.tsx
File metadata and controls
289 lines (258 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { BottomSheetModal } from '@gorhom/bottom-sheet';
import { useTranslation } from '@ronas-it/react-native-common-modules/i18n';
import { compact, debounce, delay } from 'lodash-es';
import { ForwardedRef, Fragment, ReactElement, useImperativeHandle, useMemo, useRef, useState } from 'react';
import { ShareChatModal } from '@open-webui-react-native/mobile/chat/features/share-chat-modal';
import {
FolderSearchItem,
useFolderSearchList,
} from '@open-webui-react-native/mobile/chat/utils/use-folder-search-list';
import {
UpsertFolderSheet,
UpsertFolderSheetMethods,
} from '@open-webui-react-native/mobile/folder/features/upsert-folder-sheet';
import { DownloadChatOptionsSheet } from '@open-webui-react-native/mobile/shared/features/download-chat-options-sheet';
import {
ActionButtonsModal,
ActionButtonsModalMethods,
ActionsBottomSheet,
ActionsBottomSheetProps,
ActionSheetItemProps,
FullScreenSearchModal,
FullScreenSearchModalMethods,
} from '@open-webui-react-native/mobile/shared/ui/ui-kit';
import { useInitialNavigation } from '@open-webui-react-native/mobile/shared/utils/navigation';
import {
Chat,
chatApi,
ChatListItem,
foldersApi,
MockFolderItemIds,
} from '@open-webui-react-native/shared/data-access/api';
import { withOfflineGuard } from '@open-webui-react-native/shared/features/network';
import { alertService } from '@open-webui-react-native/shared/utils/alert-service';
import { FeatureID, isFeatureEnabled } from '@open-webui-react-native/shared/utils/feature-flag';
import { ChatAction } from './enums';
export type ChatActionsMenuSheetMethods = {
present: (chat: ChatListItem, folderId?: string) => void;
};
export type ChatActionsMenuSheetRef = ForwardedRef<ChatActionsMenuSheetMethods>;
export interface ChatActionsMenuSheetProps extends Pick<ActionsBottomSheetProps, 'onClose'> {
goToChat: (id: string) => void;
isPinned?: boolean;
ref?: ChatActionsMenuSheetRef;
isInChat?: boolean;
}
export function ChatActionsMenuSheet({ goToChat, isPinned, ref, isInChat }: ChatActionsMenuSheetProps): ReactElement {
const translate = useTranslation('SHARED.CHAT_ACTIONS_MENU_SHEET');
const actionsSheetRef = useRef<BottomSheetModal>(null);
const renameModalRef = useRef<ActionButtonsModalMethods>(null);
const shareChatModalRef = useRef<BottomSheetModal>(null);
const downloadOptionsModalRef = useRef<BottomSheetModal>(null);
const fullScreenSearchModalRef = useRef<FullScreenSearchModalMethods>(null);
const upsertFolderSheetRef = useRef<UpsertFolderSheetMethods>(null);
const { resetToChatsListScreen } = useInitialNavigation();
const { mutateAsync: updateChat, isPending: isUpdating } = chatApi.useUpdate();
const { mutateAsync: updateChatFolder } = chatApi.useUpdateChatFolder();
const { mutateAsync: deleteChat, isPending: isDeleting } = chatApi.useDelete();
const { mutateAsync: pinChat, isPending: isPinning } = chatApi.usePinChat();
const { mutateAsync: cloneChat, isPending: isCloning } = chatApi.useCloneChat();
const { mutateAsync: archiveChat, isPending: isArchiving } = chatApi.useArchiveChat();
const { data: folders } = foldersApi.useGetFolders();
const [activeChat, setActiveChat] = useState<ChatListItem | null>(null);
const [folderId, setFolderId] = useState<string | undefined>(undefined);
const [isLoading, setIsLoading] = useState<boolean>(false);
const present = withOfflineGuard((chat: ChatListItem, folderId?: string): void => {
setActiveChat(chat);
setFolderId(folderId);
actionsSheetRef.current?.present();
});
const closeModals = (): void => {
renameModalRef.current?.close();
shareChatModalRef.current?.close();
downloadOptionsModalRef.current?.close();
};
useImperativeHandle(
ref,
() => ({
present,
}),
[present],
);
const chatId = activeChat?.id ?? '';
const chatTitle = activeChat?.title ?? '';
const { data: chatFullData } = chatApi.useGet(chatId, { enabled: !!chatId });
const openCreateFolderModal = (): void => {
upsertFolderSheetRef.current?.present();
};
const { emptyFolders } = useFolderSearchList({
noFolderText: translate('MOVE_CHAT_TO_FOLDER_MODAL.TEXT_NO_FOLDER'),
createFolderText: translate('MOVE_CHAT_TO_FOLDER_MODAL.TEXT_CREATE_NEW_FOLDER'),
onCreateFolderPress: openCreateFolderModal,
});
const handleAction = useMemo(
() =>
debounce(
async (action: ChatAction): Promise<void> => {
setIsLoading(true);
switch (action) {
case ChatAction.MOVE_TO_FOLDER:
await moveToFolderHandler();
break;
case ChatAction.PIN:
await pinChatHandler();
break;
case ChatAction.RENAME:
await openRenameModal();
break;
case ChatAction.CLONE:
await cloneChatHandler();
break;
case ChatAction.ARCHIVE:
await archiveChatHandler();
break;
case ChatAction.DELETE:
await onDeleteConfirm();
break;
case ChatAction.CLOSE:
closeModals();
break;
}
delay(() => setIsLoading(false), 500);
},
1000,
{ leading: true, trailing: false },
),
[activeChat, folderId],
);
const closeActionsModal = (): Promise<void> =>
// Need to delay to ensure the actions modal is closed before opening the new modal
new Promise((resolve) => {
actionsSheetRef?.current?.close();
setTimeout(() => resolve(), 600);
});
const openRenameModal = async (): Promise<void> => {
await closeActionsModal();
renameModalRef.current?.present(chatTitle);
};
const onRenameConfirm = async (title: string): Promise<void> => {
await updateChat({ id: chatId, chat: { title } as Chat });
renameModalRef.current?.close();
};
const openDeleteAlert = async (): Promise<void> => {
alertService.confirm({
title: translate('TEXT_DELETE_CHAT'),
message: translate('TEXT_THIS_WILL_DELETE', { name: chatTitle }),
confirmButtonText: translate('TEXT_DELETE'),
cancelButtonText: translate('TEXT_KEEP'),
confirmButtonStyle: 'destructive',
onConfirm: () => handleAction(ChatAction.DELETE),
});
};
const onDeleteConfirm = async (): Promise<void> => {
await deleteChat({ id: chatId, folderId });
actionsSheetRef.current?.close();
if (isInChat) {
resetToChatsListScreen();
}
};
const openShareChatModal = async (): Promise<void> => {
await closeActionsModal();
shareChatModalRef.current?.present();
};
const moveToFolderHandler = async (): Promise<void> => {
await closeActionsModal();
fullScreenSearchModalRef.current?.present();
};
const pinChatHandler = async (): Promise<void> => {
await pinChat({ id: chatId, isPinned: !!isPinned });
closeActionsModal();
};
const cloneChatHandler = async (): Promise<void> => {
const res = await cloneChat({
id: chatId,
title: translate('TEXT_CLONE_OF', { title: chatTitle }),
});
closeActionsModal();
if (res.id) {
goToChat(res.id);
}
};
const openDownloadOptionsModal = async (): Promise<void> => {
await closeActionsModal();
downloadOptionsModalRef.current?.present();
};
const archiveChatHandler = async (): Promise<void> => {
await archiveChat({ id: chatId, folderId });
closeActionsModal();
};
const onFolderSelectedHandler = async (id: string | null): Promise<void> => {
await updateChatFolder({
id: chatId,
folderId: id === MockFolderItemIds.NO_FOLDER_ID ? null : id,
oldFolderId: chatFullData?.folderId,
});
fullScreenSearchModalRef.current?.close();
};
const actions: Array<ActionSheetItemProps> = compact([
{
title: translate('TEXT_MOVE_TO_FOLDER'),
iconName: 'folderPlus',
onPress: () => handleAction(ChatAction.MOVE_TO_FOLDER),
},
{
title: isPinned ? translate('TEXT_UNPIN') : translate('TEXT_PIN'),
iconName: isPinned ? 'unpin' : 'pin',
isLoading: isPinning,
onPress: () => handleAction(ChatAction.PIN),
},
{ title: translate('TEXT_RENAME'), iconName: 'editPencil', onPress: () => handleAction(ChatAction.RENAME) },
{
title: translate('TEXT_CLONE'),
iconName: 'copy',
isLoading: isCloning,
onPress: () => handleAction(ChatAction.CLONE),
},
isFeatureEnabled(FeatureID.ARCHIVE_CHAT) && {
title: translate('TEXT_ARCHIVE'),
iconName: 'archive',
isLoading: isArchiving,
onPress: () => handleAction(ChatAction.ARCHIVE),
},
{ title: translate('TEXT_SHARE'), iconName: 'exportIcon', onPress: openShareChatModal },
{ title: translate('TEXT_DOWNLOAD'), iconName: 'download', onPress: openDownloadOptionsModal },
{
title: translate('TEXT_DELETE'),
iconName: 'trashCan',
isLoading: isDeleting,
onPress: openDeleteAlert,
isDanger: true,
},
]);
return (
<Fragment>
<ActionsBottomSheet
areActionsDisabled={isLoading}
actions={actions}
ref={actionsSheetRef} />
<ActionButtonsModal
ref={renameModalRef}
onConfirm={onRenameConfirm}
isConfirming={isUpdating}
title={translate('TEXT_RENAME_CHAT')}
withInput
/>
<ShareChatModal ref={shareChatModalRef} chatId={chatId} />
<DownloadChatOptionsSheet ref={downloadOptionsModalRef} chatId={chatId} />
<FullScreenSearchModal
data={folders || []}
unfilteredData={emptyFolders as Array<FolderSearchItem>}
selectedItemId={folderId}
ref={fullScreenSearchModalRef}
onSelectItem={onFolderSelectedHandler}
searchPlaceholder={translate('MOVE_CHAT_TO_FOLDER_MODAL.TEXT_SEARCH_FOLDER')}
modalComponent={<UpsertFolderSheet ref={upsertFolderSheetRef} />}
/>
</Fragment>
);
}