From 4ecde2b05056f0c08ab7501788398f7802987606 Mon Sep 17 00:00:00 2001 From: chohongm Date: Wed, 30 Aug 2023 14:11:16 +0900 Subject: [PATCH 1/4] Update ChannelPreview to support MFM lastMessage. --- .../__tests__/ChannelPreview.spec.js | 48 +++++++++++++++++-- .../components/ChannelPreview/utils.js | 18 +++++-- .../ChannelList/stories/index.stories.js | 2 +- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/modules/ChannelList/components/ChannelPreview/__tests__/ChannelPreview.spec.js b/src/modules/ChannelList/components/ChannelPreview/__tests__/ChannelPreview.spec.js index 776610522..4cc661bcd 100644 --- a/src/modules/ChannelList/components/ChannelPreview/__tests__/ChannelPreview.spec.js +++ b/src/modules/ChannelList/components/ChannelPreview/__tests__/ChannelPreview.spec.js @@ -13,9 +13,45 @@ jest.setSystemTime(new Date('March 2, 2022 08:15:52')); describe('ChannelPreview', () => { test('utils/getLastMessage returns lastMessage', function () { const text = 'example-text'; - const channel = {}; - const channel2 = { lastMessage: { message: '' } }; - const channel3 = { lastMessage: { message: text } }; + const channel = { + lastMessage: { + isUserMessage: () => true, + isFileMessage: () => false, + isMultipleFilesMessage: () => false, + } + }; + const channel2 = { + lastMessage: { + message: '', + isUserMessage: () => true, + isFileMessage: () => false, + isMultipleFilesMessage: () => false, + } + }; + const channel3 = { + lastMessage: { + message: text, + isUserMessage: () => true, + isFileMessage: () => false, + isMultipleFilesMessage: () => false, + } + }; + const channel4 = { + lastMessage: { + name: 'file1', + isUserMessage: () => false, + isFileMessage: () => true, + isMultipleFilesMessage: () => false, + } + }; + const channel5 = { + lastMessage: { + fileInfoList: [{ fileName: 'file1' }, { fileName: 'file2' }], + isUserMessage: () => false, + isFileMessage: () => false, + isMultipleFilesMessage: () => true, + } + }; expect( getLastMessage(channel) ).toBe(''); @@ -25,6 +61,12 @@ describe('ChannelPreview', () => { expect( getLastMessage(channel3) ).toBe(text); + expect( + getLastMessage(channel4) + ).toBe('file1'); + expect( + getLastMessage(channel5) + ).toBe('file1'); }); test('utils/getChannelTitle returns channelTitle', function () { diff --git a/src/modules/ChannelList/components/ChannelPreview/utils.js b/src/modules/ChannelList/components/ChannelPreview/utils.js index 2228bac17..d5ed2579d 100644 --- a/src/modules/ChannelList/components/ChannelPreview/utils.js +++ b/src/modules/ChannelList/components/ChannelPreview/utils.js @@ -5,6 +5,9 @@ import isYesterday from 'date-fns/isYesterday'; import { truncateString } from '../../../../utils'; import { LabelStringSet } from '../../../../ui/Label'; +import {match} from "ts-pattern"; +import {TOKEN_TYPES} from "../../../Message/utils/tokens/types"; +import {BaseMessage, MessageType} from "@sendbird/chat/message"; /* eslint-disable default-param-last */ export const getChannelTitle = (channel = {}, currentUserId, stringSet = LabelStringSet) => { @@ -51,13 +54,18 @@ export const getTotalMembers = (channel) => ( : 0 ); -const getPrettyLastMessage = (message = {}) => { +const getPrettyLastMessage = (message = null) => { const MAXLEN = 30; - const { messageType, name } = message; - if (messageType === 'file') { - return truncateString(name, MAXLEN); + if (!message) return ''; + if (message.isFileMessage()) { + return truncateString(message.name, MAXLEN); } - return message.message; + if (message.isMultipleFilesMessage()) { + const { fileInfoList } = message; + if (fileInfoList.length > 0) return fileInfoList[0].fileName; + return ''; + } + return message.message ?? ''; }; export const getLastMessage = (channel) => (channel?.lastMessage ? getPrettyLastMessage(channel?.lastMessage) : ''); diff --git a/src/modules/ChannelList/stories/index.stories.js b/src/modules/ChannelList/stories/index.stories.js index eb33ddcbf..2d7bc13ff 100644 --- a/src/modules/ChannelList/stories/index.stories.js +++ b/src/modules/ChannelList/stories/index.stories.js @@ -2,7 +2,7 @@ import React, { useState, useCallback } from 'react'; import Sendbird from '../../../lib/Sendbird'; const appId = process.env.STORYBOOK_APP_ID;; -const userId = 'sendbird'; +const userId = 'sendbird'; // Change this to 'sendbird1' for testing MultipleFilesMessage. import ChannelList from '../../ChannelList'; import { getSdk } from '../../../lib/selectors'; From 901daa879ded6a5b32cfafcfb9a9632a3f487772 Mon Sep 17 00:00:00 2001 From: chohongm Date: Wed, 30 Aug 2023 14:15:21 +0900 Subject: [PATCH 2/4] minor fix --- .eslintrc.js | 30 +++++++++---------- .../components/ChannelPreview/utils.js | 4 --- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 9ee082d15..1246f5be7 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -12,7 +12,7 @@ module.exports = { Atomics: 'readonly', SharedArrayBuffer: 'readonly', }, - parser: "@babel/eslint-parser", + parser: '@babel/eslint-parser', parserOptions: { ecmaFeatures: { jsx: true, @@ -20,31 +20,31 @@ module.exports = { ecmaVersion: 2018, sourceType: 'module', }, - plugins: [ 'react', 'babel', '@typescript-eslint' ], + plugins: ['react', 'babel', '@typescript-eslint'], rules: { // uncomment 'linebreak-style' to build in windows - its not adviced to commit from windows // read more - https://community.perforce.com/s/article/3096 // ['linebreak-style']: 0, - "import/extensions": [ - "error", - "ignorePackages", + 'import/extensions': [ + 'error', + 'ignorePackages', { - "js": "never", - "jsx": "never", - "ts": "never", - "tsx": "never", + js: 'never', + jsx: 'never', + ts: 'never', + tsx: 'never', }, ], - "react/forbid-prop-types": 0, + 'react/forbid-prop-types': 0, // we don't want to force to define function component only - "react/function-component-definition": "off", - "no-unused-expressions": "off", - "@typescript-eslint/no-unused-expressions": ["error"], + 'react/function-component-definition': 'off', + 'no-unused-expressions': 'off', + '@typescript-eslint/no-unused-expressions': ['error'], }, settings: { - "import/resolver": { + 'import/resolver': { node: { - extensions: [".js", ".jsx", ".ts", ".tsx"], + extensions: ['.js', '.jsx', '.ts', '.tsx'], }, }, }, diff --git a/src/modules/ChannelList/components/ChannelPreview/utils.js b/src/modules/ChannelList/components/ChannelPreview/utils.js index d5ed2579d..3a801334f 100644 --- a/src/modules/ChannelList/components/ChannelPreview/utils.js +++ b/src/modules/ChannelList/components/ChannelPreview/utils.js @@ -2,12 +2,8 @@ import isToday from 'date-fns/isToday'; import format from 'date-fns/format'; import isThisYear from 'date-fns/isThisYear'; import isYesterday from 'date-fns/isYesterday'; - import { truncateString } from '../../../../utils'; import { LabelStringSet } from '../../../../ui/Label'; -import {match} from "ts-pattern"; -import {TOKEN_TYPES} from "../../../Message/utils/tokens/types"; -import {BaseMessage, MessageType} from "@sendbird/chat/message"; /* eslint-disable default-param-last */ export const getChannelTitle = (channel = {}, currentUserId, stringSet = LabelStringSet) => { From 2ee1c79e4348d1f1d8183f5ea52b4dd832bdc5d9 Mon Sep 17 00:00:00 2001 From: Liam Hongman Cho Date: Wed, 30 Aug 2023 15:29:35 +0900 Subject: [PATCH 3/4] Update src/modules/ChannelList/components/ChannelPreview/utils.js Co-authored-by: HoonBaek --- src/modules/ChannelList/components/ChannelPreview/utils.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/modules/ChannelList/components/ChannelPreview/utils.js b/src/modules/ChannelList/components/ChannelPreview/utils.js index 3a801334f..b5207cd0e 100644 --- a/src/modules/ChannelList/components/ChannelPreview/utils.js +++ b/src/modules/ChannelList/components/ChannelPreview/utils.js @@ -58,7 +58,9 @@ const getPrettyLastMessage = (message = null) => { } if (message.isMultipleFilesMessage()) { const { fileInfoList } = message; - if (fileInfoList.length > 0) return fileInfoList[0].fileName; + if (fileInfoList.length > 0) { + return truncateString(fileInfoList[0].fileName, MAXLEN); + } return ''; } return message.message ?? ''; From 8a06589732befa11bc4c16b8dad29fd6a4432696 Mon Sep 17 00:00:00 2001 From: chohongm Date: Wed, 30 Aug 2023 15:30:41 +0900 Subject: [PATCH 4/4] minor --- src/modules/ChannelList/stories/index.stories.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/ChannelList/stories/index.stories.js b/src/modules/ChannelList/stories/index.stories.js index 2d7bc13ff..eb33ddcbf 100644 --- a/src/modules/ChannelList/stories/index.stories.js +++ b/src/modules/ChannelList/stories/index.stories.js @@ -2,7 +2,7 @@ import React, { useState, useCallback } from 'react'; import Sendbird from '../../../lib/Sendbird'; const appId = process.env.STORYBOOK_APP_ID;; -const userId = 'sendbird'; // Change this to 'sendbird1' for testing MultipleFilesMessage. +const userId = 'sendbird'; import ChannelList from '../../ChannelList'; import { getSdk } from '../../../lib/selectors';