@esvndev/es-react-template-chat 0.0.89 → 0.0.90

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -72070,7 +72070,7 @@ const ChatLog = (props) => {
72070
72070
  const { typeChat, contactId, chatRoomId, active, messageByGroup, pinnedMessages, setScroll, checkScroll, pinMessageChatRoom, deleteMessageApi, recallMessageApi, get_message_by_group_cursor_api, getPinMessageChatRoomApi, deletePinMessage,
72071
72071
  //updateGroupTagApi,
72072
72072
  createReminderApi, senMessageApi, typingUsers } = useChat();
72073
- configureStore.useAppSelector((state) => state.hubNotification);
72073
+ const { connection } = configureStore.useAppSelector((state) => state.hubNotification);
72074
72074
  const [dynamicMenuItems, setDynamicMenuItems] = React.useState([]);
72075
72075
  React.useEffect(() => {
72076
72076
  const fetchFormConfig = async () => {
@@ -72111,38 +72111,68 @@ const ChatLog = (props) => {
72111
72111
  ], [dynamicMenuItems]);
72112
72112
  // Menu items cho Hành chính (chỉ administrative)
72113
72113
  React.useMemo(() => ADMINISTRATIVE_ITEMS, []);
72114
- React.useRef(false);
72115
- React.useRef(null);
72116
- React.useRef(null);
72114
+ const isTypingRef = React.useRef(false);
72115
+ const typingTimeoutRef = React.useRef(null);
72117
72116
  React.useRef(null);
72118
72117
  React.useRef(null);
72118
+ const inputContainer = React.useRef(null);
72119
72119
  const containerChatRef = React.useRef(null);
72120
72120
  const { connectHub, dataInfo, dataProfile, handleUser, handleUserSidebarRight, handleSidebar, handleSidebarRight, userSidebarLeft, dataHistory, chatGetType, approveUpdateStatus, handleModalAdministrative, setDataItem, handleModalAddUserGroup, handleModalGroup, typeOpenModalAdd, setTypeOpenModalAdd, unpinMessage, getPinnedMessages } = props;
72121
72121
  const groupChatUsers = dataHistory?.find((dt) => dt.id === active);
72122
72122
  //const dataInfoAvatar = dataInfo && dataInfo.avatar ? dataInfo.avatar : DEFAULT_AVATAR
72123
- React.useState([]);
72124
- React.useState(false);
72123
+ const [imgCopy, setImgCopy] = React.useState([]);
72124
+ const [isOpen, setIsOpen] = React.useState(false);
72125
72125
  const [openModalPreview, setOpenModalPreview] = React.useState(false);
72126
72126
  React.useState(false);
72127
72127
  React.useState(false);
72128
72128
  React.useState(false);
72129
72129
  React.useState('');
72130
72130
  React.useState(null);
72131
- React.useState(null);
72132
- React.useState(null);
72133
- React.useState({ x: 0, y: 0 });
72134
- React.useState(undefined);
72135
- React.useState(undefined);
72131
+ const [currentMessage, setCurrentMessage] = React.useState(null);
72132
+ const [replyMessage, setReplyMessage] = React.useState(null);
72133
+ const [position, setPosition] = React.useState({ x: 0, y: 0 });
72134
+ const [listContextShow, setListContextShow] = React.useState(undefined);
72135
+ const [listContextHide, setListContextHide] = React.useState(undefined);
72136
72136
  const [selectedMessage, setSelectedMessage] = React.useState(null);
72137
- React.useState(false);
72138
- React.useState(true);
72139
- React.useRef(0);
72137
+ const [isMultiLine, setIsMultiLine] = React.useState(false);
72138
+ const [isAtBottom, setIsAtBottom] = React.useState(true);
72139
+ const prevListLength = React.useRef(0);
72140
72140
  React.useState(0);
72141
72141
  React.useState(true);
72142
72142
  React.useState(new Set());
72143
- React.useState(null);
72143
+ const [clickedImageFile, setClickedImageFile] = React.useState(null);
72144
72144
  React.useState(false);
72145
72145
  React.useState(new Set());
72146
+ const BASE_CONTEXT_ITEMS = [
72147
+ "reply", "share", "pin", "bookmark",
72148
+ "selectMultiMessage", "detail", "other", "recall", "delete"
72149
+ ];
72150
+ const CONTEXT_CONFIG = {
72151
+ selection: {
72152
+ show: ["copyRangeText", ...BASE_CONTEXT_ITEMS, "save"],
72153
+ hide: ["copyText", "save", "copyImage"]
72154
+ },
72155
+ image: {
72156
+ show: ["copyImage", ...BASE_CONTEXT_ITEMS, "save"],
72157
+ hide: ["copyText", "copyRangeText"]
72158
+ },
72159
+ file: {
72160
+ show: [...BASE_CONTEXT_ITEMS, "save"],
72161
+ hide: ["copyText", "copyRangeText", "copyImage"]
72162
+ },
72163
+ text: {
72164
+ show: ["copyText", ...BASE_CONTEXT_ITEMS],
72165
+ hide: ["copyRangeText", "save", "copyImage"]
72166
+ },
72167
+ voucher: {
72168
+ show: ["copyText", ...BASE_CONTEXT_ITEMS],
72169
+ hide: ["copyRangeText", "save", "copyImage"]
72170
+ },
72171
+ default: {
72172
+ show: ["copyText", ...BASE_CONTEXT_ITEMS],
72173
+ hide: ["copyRangeText", "save", "copyImage"]
72174
+ }
72175
+ };
72146
72176
  React.useState(true);
72147
72177
  const scrollToBottom = () => {
72148
72178
  const messagesEndEle = document.getElementById('messagesEnd');
@@ -72178,188 +72208,133 @@ const ChatLog = (props) => {
72178
72208
  }, []);
72179
72209
  React.useState(false);
72180
72210
  reactRedux.useDispatch();
72181
- //từ đây
72182
- // const sendFile = async (event: any) => {
72183
- // if (event.target.files && event.target.files[0]) {
72184
- // const filesToUpload: File[] = Array.from(event.target.files)
72185
- // const errors: Array<{ file: File, error: any }> = []
72186
- // for (const file of filesToUpload) {
72187
- // const result = await uploadWithRetry(file)
72188
- // if (result.success && chatRoomId) {
72189
- // connectHub(chatRoomId, {
72190
- // sendTo: dataInfo.id,
72191
- // content: '',
72192
- // type: 'file',
72193
- // path: JSON.stringify(result.data?.infoFiles),
72194
- // typeChat
72195
- // })
72196
- // } else {
72197
- // errors.push({ file, error: result.error })
72198
- // }
72199
- // }
72200
- // if (errors.length > 0) {
72201
- // notificationError(`${errors.length} file upload thất bại`)
72202
- // }
72203
- // event.target.value = ''
72204
- // }
72205
- // }
72206
- // const sendImage = async (event: any) => {
72207
- // if (event.target.files && event.target.files[0]) {
72208
- // const filesToUpload: File[] = Array.from(event.target.files)
72209
- // const errors: Array<{ file: File, error: any }> = []
72210
- // const batchId = newGuid()
72211
- // for (const file of filesToUpload) {
72212
- // const result = await uploadWithRetry(file)
72213
- // const item = result.data?.infoFiles?.map((x: any) => ({ ...x, batchId, createdDate: new Date().toISOString() })) || []
72214
- // if (result.success && chatRoomId && result?.data?.status) {
72215
- // connectHub(chatRoomId, {
72216
- // sendTo: dataInfo.id,
72217
- // content: '',
72218
- // type: 'image',
72219
- // path: JSON.stringify(item),
72220
- // typeChat
72221
- // })
72222
- // } else {
72223
- // errors.push({ file, error: result.error })
72224
- // }
72225
- // }
72226
- // if (errors.length > 0) {
72227
- // notificationError(`${errors.length} ảnh upload thất bại`)
72228
- // }
72229
- // event.target.value = ''
72230
- // }
72231
- // }
72232
- // const handleRemoveImage = useCallback((idx: number) => {
72233
- // const newArr = [...imgCopy]
72234
- // newArr.splice(idx, 1)
72235
- // setImgCopy(newArr)
72236
- // }, [imgCopy])
72237
- // const onContextMenu = useCallback((e: any, chatMessage: any) => {
72238
- // const selection: any = window.document.getSelection()
72239
- // setIsOpen(true)
72240
- // setPosition({ x: e.clientX, y: e.clientY })
72241
- // if (e.target) {
72242
- // setCurrentMessage(chatMessage)
72243
- // }
72244
- // // Detect ảnh được click từ data attribute
72245
- // const fileIndex = e.target?.dataset?.fileIndex
72246
- // if (fileIndex !== undefined && chatMessage?.path) {
72247
- // setClickedImageFile(chatMessage.path[parseInt(fileIndex)])
72248
- // } else {
72249
- // setClickedImageFile(null)
72250
- // }
72251
- // e.preventDefault()
72252
- // const contextType = selection.toString() ? 'selection' : chatMessage?.type ? chatMessage.type : 'default'
72253
- // const config = CONTEXT_CONFIG[contextType]
72254
- // setListContextShow({ list: config.show, type: "SHOW" })
72255
- // setListContextHide({ list: config.hide, type: "HIDE" })
72256
- // }, [])
72257
- // const clearReply = () => {
72258
- // setReplyMessage(null)
72259
- // }
72260
- // const handleTypingStart = () => {
72261
- // if (!isTypingRef.current && connection && chatRoomId) {
72262
- // connection.invoke('StartTyping', chatRoomId).catch(console.error)
72263
- // isTypingRef.current = true
72264
- // }
72265
- // }
72266
- // const handleTypingStop = () => {
72267
- // if (typingTimeoutRef.current) { clearTimeout(typingTimeoutRef.current) }
72268
- // typingTimeoutRef.current = setTimeout(() => {
72269
- // if (isTypingRef.current && connection && chatRoomId) {
72270
- // connection.invoke('StopTyping', chatRoomId).catch(console.error)
72271
- // isTypingRef.current = false
72272
- // }
72273
- // }, 2000)
72274
- // }
72275
- // const handleChange = useCallback(
72276
- // (editorState: EditorState) => {
72277
- // if (
72278
- // inputContainer &&
72279
- // inputContainer.current &&
72280
- // inputContainer.current.clientHeight > 50
72281
- // ) {
72282
- // setIsMultiLine(true)
72283
- // } else {
72284
- // setIsMultiLine(false)
72285
- // }
72286
- // editorState.read(() => {
72287
- // handleTypingStart()
72288
- // handleTypingStop()
72289
- // })
72290
- // },
72291
- // [inputContainer, connection, chatRoomId]
72292
- // )
72293
- // const onEnter = (val: any) => {
72294
- // handleSendMsg(val)
72295
- // clearReply()
72296
- // setReplyMessage(null)
72297
- // }
72298
- // useEffect(() => {
72299
- // if (active) {
72300
- // setReplyMessage(null)
72301
- // setImgCopy([])
72302
- // setIsAtBottom(true)
72303
- // prevListLength.current = 0
72304
- // }
72305
- // }, [
72306
- // active,
72307
- // dataInfo
72308
- // //chatRoomId,
72309
- // //chatGetType
72310
- // ])
72311
- // // Chuyển phòng thì load lại message + pinned messages
72312
- // useEffect(() => {
72313
- // if (!chatRoomId && handleSidebar) { return }
72314
- // chatGetType(chatRoomId, undefined, "", "", undefined, undefined, 0, 0)
72315
- // if (chatRoomId) {
72316
- // getPinMessageChatRoomApi(chatRoomId)
72317
- // }
72318
- // }, [chatRoomId])
72319
- // const handleUnpinMessage = useCallback(async (message: any) => {
72320
- // const pinId = message?.pinId || message?.id
72321
- // if (!pinId) { return }
72322
- // await deletePinMessage(pinId)
72323
- // }, [deletePinMessage, chatRoomId])
72324
- // const handleViewPinnedMessage = useCallback((message: any) => {
72325
- // if (message?.id) {
72326
- // //setViewingPinnedMessage(message.id)
72327
- // // Tìm message element
72328
- // const element = document.getElementById(`msg-${message.id}`)
72329
- // const chatContainer = document.getElementById('scrollableDivChat')
72330
- // if (element && chatContainer) {
72331
- // // Lấy vị trí của element và container
72332
- // const elementRect = element.getBoundingClientRect()
72333
- // const containerRect = chatContainer.getBoundingClientRect()
72334
- // // Tính toán scroll position để đưa element vào giữa container
72335
- // const scrollTop = chatContainer.scrollTop
72336
- // const offset = elementRect.top - containerRect.top - (containerRect.height / 2) + (elementRect.height / 2)
72337
- // // Scroll trong container, không scroll toàn page
72338
- // chatContainer.scrollTo({
72339
- // top: scrollTop + offset,
72340
- // behavior: 'smooth'
72341
- // })
72342
- // // Highlight message
72343
- // const originalBg = element.style.backgroundColor
72344
- // const originalZIndex = element.style.zIndex
72345
- // element.style.backgroundColor = '#fff3cd'
72346
- // element.style.zIndex = '1000'
72347
- // element.style.position = 'relative'
72348
- // element.style.transition = 'background-color 0.3s'
72349
- // // Auto close sau 500ms
72350
- // setTimeout(() => {
72351
- // element.style.backgroundColor = originalBg || ''
72352
- // element.style.zIndex = originalZIndex || ''
72353
- // //setViewingPinnedMessage(null)
72354
- // }, 500)
72355
- // } else {
72356
- // console.log('Message not found in current view, may need to load more messages')
72357
- // setTimeout(() => {
72358
- // //setViewingPinnedMessage(null)
72359
- // }, 2000)
72360
- // }
72361
- // }
72362
- // }, [])
72211
+ React.useCallback((idx) => {
72212
+ const newArr = [...imgCopy];
72213
+ newArr.splice(idx, 1);
72214
+ setImgCopy(newArr);
72215
+ }, [imgCopy]);
72216
+ React.useCallback((e, chatMessage) => {
72217
+ const selection = window.document.getSelection();
72218
+ setIsOpen(true);
72219
+ setPosition({ x: e.clientX, y: e.clientY });
72220
+ if (e.target) {
72221
+ setCurrentMessage(chatMessage);
72222
+ }
72223
+ // Detect ảnh được click từ data attribute
72224
+ const fileIndex = e.target?.dataset?.fileIndex;
72225
+ if (fileIndex !== undefined && chatMessage?.path) {
72226
+ setClickedImageFile(chatMessage.path[parseInt(fileIndex)]);
72227
+ }
72228
+ else {
72229
+ setClickedImageFile(null);
72230
+ }
72231
+ e.preventDefault();
72232
+ const contextType = selection.toString() ? 'selection' : chatMessage?.type ? chatMessage.type : 'default';
72233
+ const config = CONTEXT_CONFIG[contextType];
72234
+ setListContextShow({ list: config.show, type: "SHOW" });
72235
+ setListContextHide({ list: config.hide, type: "HIDE" });
72236
+ }, []);
72237
+ const handleTypingStart = () => {
72238
+ if (!isTypingRef.current && connection && chatRoomId) {
72239
+ connection.invoke('StartTyping', chatRoomId).catch(console.error);
72240
+ isTypingRef.current = true;
72241
+ }
72242
+ };
72243
+ const handleTypingStop = () => {
72244
+ if (typingTimeoutRef.current) {
72245
+ clearTimeout(typingTimeoutRef.current);
72246
+ }
72247
+ typingTimeoutRef.current = setTimeout(() => {
72248
+ if (isTypingRef.current && connection && chatRoomId) {
72249
+ connection.invoke('StopTyping', chatRoomId).catch(console.error);
72250
+ isTypingRef.current = false;
72251
+ }
72252
+ }, 2000);
72253
+ };
72254
+ React.useCallback((editorState) => {
72255
+ if (inputContainer &&
72256
+ inputContainer.current &&
72257
+ inputContainer.current.clientHeight > 50) {
72258
+ setIsMultiLine(true);
72259
+ }
72260
+ else {
72261
+ setIsMultiLine(false);
72262
+ }
72263
+ editorState.read(() => {
72264
+ handleTypingStart();
72265
+ handleTypingStop();
72266
+ });
72267
+ }, [inputContainer, connection, chatRoomId]);
72268
+ React.useEffect(() => {
72269
+ if (active) {
72270
+ setReplyMessage(null);
72271
+ setImgCopy([]);
72272
+ setIsAtBottom(true);
72273
+ prevListLength.current = 0;
72274
+ }
72275
+ }, [
72276
+ active,
72277
+ dataInfo
72278
+ //chatRoomId,
72279
+ //chatGetType
72280
+ ]);
72281
+ // Chuyển phòng thì load lại message + pinned messages
72282
+ React.useEffect(() => {
72283
+ if (!chatRoomId && handleSidebar) {
72284
+ return;
72285
+ }
72286
+ chatGetType(chatRoomId, undefined, "", "", undefined, undefined, 0, 0);
72287
+ if (chatRoomId) {
72288
+ getPinMessageChatRoomApi(chatRoomId);
72289
+ }
72290
+ }, [chatRoomId]);
72291
+ React.useCallback(async (message) => {
72292
+ const pinId = message?.pinId || message?.id;
72293
+ if (!pinId) {
72294
+ return;
72295
+ }
72296
+ await deletePinMessage(pinId);
72297
+ }, [deletePinMessage, chatRoomId]);
72298
+ React.useCallback((message) => {
72299
+ if (message?.id) {
72300
+ //setViewingPinnedMessage(message.id)
72301
+ // Tìm message element
72302
+ const element = document.getElementById(`msg-${message.id}`);
72303
+ const chatContainer = document.getElementById('scrollableDivChat');
72304
+ if (element && chatContainer) {
72305
+ // Lấy vị trí của element và container
72306
+ const elementRect = element.getBoundingClientRect();
72307
+ const containerRect = chatContainer.getBoundingClientRect();
72308
+ // Tính toán scroll position để đưa element vào giữa container
72309
+ const scrollTop = chatContainer.scrollTop;
72310
+ const offset = elementRect.top - containerRect.top - (containerRect.height / 2) + (elementRect.height / 2);
72311
+ // Scroll trong container, không scroll toàn page
72312
+ chatContainer.scrollTo({
72313
+ top: scrollTop + offset,
72314
+ behavior: 'smooth'
72315
+ });
72316
+ // Highlight message
72317
+ const originalBg = element.style.backgroundColor;
72318
+ const originalZIndex = element.style.zIndex;
72319
+ element.style.backgroundColor = '#fff3cd';
72320
+ element.style.zIndex = '1000';
72321
+ element.style.position = 'relative';
72322
+ element.style.transition = 'background-color 0.3s';
72323
+ // Auto close sau 500ms
72324
+ setTimeout(() => {
72325
+ element.style.backgroundColor = originalBg || '';
72326
+ element.style.zIndex = originalZIndex || '';
72327
+ //setViewingPinnedMessage(null)
72328
+ }, 500);
72329
+ }
72330
+ else {
72331
+ console.log('Message not found in current view, may need to load more messages');
72332
+ setTimeout(() => {
72333
+ //setViewingPinnedMessage(null)
72334
+ }, 2000);
72335
+ }
72336
+ }
72337
+ }, []);
72363
72338
  // const onKeyDown = (event: any) => {
72364
72339
  // if (event.code === 'NumpadEnter') {
72365
72340
  // event.preventDefault()