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

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.mjs CHANGED
@@ -17,9 +17,9 @@ import { isNullOrUndefined as isNullOrUndefined$2 } from '@src/utility/hooks/isN
17
17
  import avatarDefault from '@core/assets/images/avatars/avatar-blank.png';
18
18
  import '@draft-js-plugins/mention/lib/plugin.css';
19
19
  import '@draft-js-plugins/emoji/lib/plugin.css';
20
+ import { useDispatch } from 'react-redux';
20
21
  import { useAppDispatch as useAppDispatch$1, useAppSelector as useAppSelector$1 } from '@store/configureStore';
21
22
  import { useLocation } from 'react-router-dom';
22
- import { useDispatch } from 'react-redux';
23
23
 
24
24
  var instances = 'ej2_instances';
25
25
  var uid = 0;
@@ -27368,6 +27368,16 @@ const encodeUUID = (uuid) => {
27368
27368
  return uuid;
27369
27369
  }
27370
27370
  };
27371
+ const isValidUrl = (string) => {
27372
+ try {
27373
+ // eslint-disable-next-line no-new
27374
+ new URL(string);
27375
+ return true;
27376
+ }
27377
+ catch (e) {
27378
+ return false;
27379
+ }
27380
+ };
27371
27381
  // ** Checks if the passed date is today
27372
27382
  const isToday = (value) => {
27373
27383
  const today = new Date();
@@ -72079,11 +72089,11 @@ const ChatLog = (props) => {
72079
72089
  useRef(null);
72080
72090
  const containerChatRef = useRef(null);
72081
72091
  const { connectHub, dataInfo, dataProfile, handleUser, handleUserSidebarRight, handleSidebar, handleSidebarRight, userSidebarLeft, dataHistory, chatGetType, approveUpdateStatus, handleModalAdministrative, setDataItem, handleModalAddUserGroup, handleModalGroup, typeOpenModalAdd, setTypeOpenModalAdd, unpinMessage, getPinnedMessages } = props;
72082
- dataHistory?.find((dt) => dt.id === active);
72092
+ const groupChatUsers = dataHistory?.find((dt) => dt.id === active);
72083
72093
  //const dataInfoAvatar = dataInfo && dataInfo.avatar ? dataInfo.avatar : DEFAULT_AVATAR
72084
72094
  useState([]);
72085
72095
  useState(false);
72086
- useState(false);
72096
+ const [openModalPreview, setOpenModalPreview] = useState(false);
72087
72097
  useState(false);
72088
72098
  useState(false);
72089
72099
  useState(false);
@@ -72094,7 +72104,7 @@ const ChatLog = (props) => {
72094
72104
  useState({ x: 0, y: 0 });
72095
72105
  useState(undefined);
72096
72106
  useState(undefined);
72097
- useState(null);
72107
+ const [selectedMessage, setSelectedMessage] = useState(null);
72098
72108
  useState(false);
72099
72109
  useState(true);
72100
72110
  useRef(0);
@@ -72105,184 +72115,41 @@ const ChatLog = (props) => {
72105
72115
  useState(false);
72106
72116
  useState(new Set());
72107
72117
  useState(true);
72108
- // // Load initial messages
72109
- // useEffect(() => {
72110
- // if (!checkScroll) {
72111
- // scrollToBottom()
72112
- // }
72113
- // }, [messageByGroup, checkScroll])
72114
- // const mentionItems = useMemo(() => {
72115
- // if (!groupChatUsers) { return [] }
72116
- // const users = Array.isArray(groupChatUsers.users) ? groupChatUsers.users : []
72117
- // const mappedUsers = users.map((user: any) => {
72118
- // const rawAvatar = user.avatar
72119
- // let avatar = DEFAULT_AVATAR
72120
- // if (rawAvatar) {
72121
- // avatar = isValidUrl(rawAvatar) ? rawAvatar : `${CDN_URL_VIEW}/${rawAvatar}`
72122
- // }
72123
- // return {
72124
- // ...user,
72125
- // avatar
72126
- // }
72127
- // })
72128
- // return [{ id: "all", name: "All" }, ...mappedUsers]
72129
- // }, [groupChatUsers])
72130
- // const toggleModalPreview = () => {
72131
- // setOpenModalPreview(!openModalPreview)
72132
- // }
72133
- // const handlePreview = useCallback((mes: any) => {
72134
- // setSelectedMessage(mes)
72135
- // setOpenModalPreview(true)
72136
- // }, [])
72137
- // const [isFetching, setIsFetching] = useState(false)
72138
- // const dispatch = useDispatch()
72139
- // const handleStartConversation = () => {
72140
- // if (
72141
- // !Object.keys({}).length &&
72142
- // !userSidebarLeft &&
72143
- // window.innerWidth < 992
72144
- // ) {
72145
- // handleSidebar()
72146
- // }
72147
- // }
72148
- // const uploadGroup = async (files: File[], type: 'image' | 'file') => {
72149
- // if (files.length === 0) { return }
72150
- // const batchId = newGuid()
72151
- // const errors: Array<{ file: File; error: any }> = []
72152
- // // Upload và gửi từng file một
72153
- // for (const file of files) {
72154
- // try {
72155
- // const result = await uploadWithRetry(file)
72156
- // if (result.success && chatRoomId) {
72157
- // const infoFiles = result.data?.infoFiles || []
72158
- // const items = type === 'image' ? infoFiles.map((x: any) => ({ ...x, batchId, createdDate: new Date().toISOString() })) : infoFiles
72159
- // // Gửi message ngay sau khi upload thành công
72160
- // connectHub(chatRoomId, {
72161
- // sendTo: dataInfo.id,
72162
- // content: '',
72163
- // type,
72164
- // path: JSON.stringify(items),
72165
- // typeChat
72166
- // })
72167
- // } else {
72168
- // errors.push({ file, error: result.error })
72169
- // }
72170
- // } catch (error) {
72171
- // errors.push({ file, error })
72172
- // }
72173
- // }
72174
- // // Thông báo lỗi nếu có
72175
- // if (errors.length > 0) {
72176
- // notificationError(`${errors.length}/${files.length} ${type === 'image' ? 'ảnh' : 'file'} upload thất bại`)
72177
- // }
72178
- // }
72179
- // const dataURLToFile = (dataUrl: string, fileName: string) => {
72180
- // const [header, data] = dataUrl.split(',')
72181
- // const mime = header.match(/data:(.*?);/)?.[1] || 'image/png'
72182
- // const binary = atob(data)
72183
- // const len = binary.length
72184
- // const u8 = new Uint8Array(len)
72185
- // for (let i = 0; i < len; i++) { u8[i] = binary.charCodeAt(i) }
72186
- // return new File([u8], fileName, { type: mime })
72187
- // }
72188
- // // ** Extract tagUserIds from editor mentions
72189
- // const extractTagUserIds = (): string[] => {
72190
- // const tagUserIds: string[] = []
72191
- // let hasAllMention = false
72192
- // editor.getEditorState().read(() => {
72193
- // const root = $getRoot()
72194
- // const nodes = root.getAllTextNodes()
72195
- // nodes.forEach((node) => {
72196
- // if ($isMentionNode(node)) {
72197
- // const userId = (node as any).__userId
72198
- // if (userId === 'all') {
72199
- // hasAllMention = true
72200
- // } else if (userId && !tagUserIds.includes(userId)) {
72201
- // tagUserIds.push(userId)
72202
- // }
72203
- // }
72204
- // })
72205
- // })
72206
- // // Nếu có @All, lấy tất cả userId trong nhóm
72207
- // if (hasAllMention && groupChatUsers?.users) {
72208
- // const allUserIds = groupChatUsers.users
72209
- // .map((user: any) => user.id)
72210
- // .filter((id: string) => id !== dataProfile?.id) // Loại bỏ chính mình
72211
- // return allUserIds
72212
- // }
72213
- // return tagUserIds
72214
- // }
72215
- // // ** Sends New Msg
72216
- // const handleSendMsg = (message: any) => {
72217
- // if (imgCopy.length > 0) {
72218
- // const imageFiles: File[] = []
72219
- // const otherFiles: File[] = []
72220
- // imgCopy.forEach((item: any, idx: number) => {
72221
- // const data = item?.data || item
72222
- // if (typeof data === 'string' && data.startsWith('data:image')) {
72223
- // imageFiles.push(dataURLToFile(data, item.name || `pasted_${Date.now()}_${idx}.png`))
72224
- // } else if (data instanceof File) {
72225
- // if (data.type?.startsWith('image')) {
72226
- // imageFiles.push(data)
72227
- // } else {
72228
- // otherFiles.push(data)
72229
- // }
72230
- // }
72231
- // })
72232
- // // Gửi ảnh trước rồi file
72233
- // uploadGroup(imageFiles, 'image')
72234
- // uploadGroup(otherFiles, 'file')
72235
- // }
72236
- // if (message.trim().length) {
72237
- // if (typingTimeoutRef.current) {
72238
- // clearTimeout(typingTimeoutRef.current)
72239
- // }
72240
- // isTypingRef.current = false
72241
- // if (connection && chatRoomId) {
72242
- // connection.invoke('StopTyping', chatRoomId).catch(console.error)
72243
- // }
72244
- // const tagUserIds = extractTagUserIds()
72245
- // const id = typeChat === 'CHAT' ? chatRoomId : contactId
72246
- // if (id) {
72247
- // connectHub(id, {
72248
- // sendTo: dataInfo.id,
72249
- // content: message.trim(),
72250
- // replyContent: replyMessage?.currentMessage ? JSON.stringify(replyMessage.currentMessage) : null,
72251
- // path: null,
72252
- // type: 'text',
72253
- // typeChat,
72254
- // tagUserIds
72255
- // })
72256
- // }
72257
- // }
72258
- // setImgCopy([])
72259
- // }
72260
- // const uploadWithRetry = async (file: File, maxRetries = 2) => {
72261
- // let lastError
72262
- // for (let attempt = 0; attempt <= maxRetries; attempt++) {
72263
- // try {
72264
- // const formDataFile = new FormData()
72265
- // formDataFile.append('files', file, file.name)
72266
- // const rs = await uploadMultiFileApi(formDataFile).unwrap()
72267
- // return { success: true, data: rs }
72268
- // } catch (error: any) {
72269
- // lastError = error
72270
- // // Không retry nếu là lỗi validation từ server
72271
- // if (error?.status === 400 || error?.status === 413) {
72272
- // break
72273
- // }
72274
- // if (attempt < maxRetries) {
72275
- // // Exponential backoff: 1s, 2s, 4s...
72276
- // await new Promise(resolve => setTimeout(resolve, 1000 * Math.pow(2, attempt))
72277
- // )
72278
- // }
72279
- // }
72280
- // }
72281
- // return {
72282
- // success: false,
72283
- // error: lastError?.message || 'Upload thất bại'
72284
- // }
72285
- // }
72118
+ const scrollToBottom = () => {
72119
+ const messagesEndEle = document.getElementById('messagesEnd');
72120
+ messagesEndEle?.scrollIntoView({ block: 'end', behavior: "instant" });
72121
+ };
72122
+ // Load initial messages
72123
+ useEffect(() => {
72124
+ if (!checkScroll) {
72125
+ scrollToBottom();
72126
+ }
72127
+ }, [messageByGroup, checkScroll]);
72128
+ useMemo(() => {
72129
+ if (!groupChatUsers) {
72130
+ return [];
72131
+ }
72132
+ const users = Array.isArray(groupChatUsers.users) ? groupChatUsers.users : [];
72133
+ const mappedUsers = users.map((user) => {
72134
+ const rawAvatar = user.avatar;
72135
+ let avatar = DEFAULT_AVATAR;
72136
+ if (rawAvatar) {
72137
+ avatar = isValidUrl(rawAvatar) ? rawAvatar : `${CDN_URL_VIEW}/${rawAvatar}`;
72138
+ }
72139
+ return {
72140
+ ...user,
72141
+ avatar
72142
+ };
72143
+ });
72144
+ return [{ id: "all", name: "All" }, ...mappedUsers];
72145
+ }, [groupChatUsers]);
72146
+ useCallback((mes) => {
72147
+ setSelectedMessage(mes);
72148
+ setOpenModalPreview(true);
72149
+ }, []);
72150
+ useState(false);
72151
+ useDispatch();
72152
+ //từ đây
72286
72153
  // const sendFile = async (event: any) => {
72287
72154
  // if (event.target.files && event.target.files[0]) {
72288
72155
  // const filesToUpload: File[] = Array.from(event.target.files)