@sendbird/uikit-utils 1.0.0-rc.1

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.
Files changed (81) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +9 -0
  3. package/lib/commonjs/hooks/index.js +88 -0
  4. package/lib/commonjs/hooks/index.js.map +1 -0
  5. package/lib/commonjs/index.js +223 -0
  6. package/lib/commonjs/index.js.map +1 -0
  7. package/lib/commonjs/sendbird/attrs.js +57 -0
  8. package/lib/commonjs/sendbird/attrs.js.map +1 -0
  9. package/lib/commonjs/sendbird/channel.js +53 -0
  10. package/lib/commonjs/sendbird/channel.js.map +1 -0
  11. package/lib/commonjs/sendbird/message.js +111 -0
  12. package/lib/commonjs/sendbird/message.js.map +1 -0
  13. package/lib/commonjs/shared/arrayToMap.js +54 -0
  14. package/lib/commonjs/shared/arrayToMap.js.map +1 -0
  15. package/lib/commonjs/shared/conditionChaining.js +15 -0
  16. package/lib/commonjs/shared/conditionChaining.js.map +1 -0
  17. package/lib/commonjs/shared/logger.js +98 -0
  18. package/lib/commonjs/shared/logger.js.map +1 -0
  19. package/lib/commonjs/shared/pubsub.js +54 -0
  20. package/lib/commonjs/shared/pubsub.js.map +1 -0
  21. package/lib/commonjs/shared/regex.js +102 -0
  22. package/lib/commonjs/shared/regex.js.map +1 -0
  23. package/lib/commonjs/types.js +6 -0
  24. package/lib/commonjs/types.js.map +1 -0
  25. package/lib/commonjs/ui-format/common.js +106 -0
  26. package/lib/commonjs/ui-format/common.js.map +1 -0
  27. package/lib/commonjs/ui-format/groupChannel.js +80 -0
  28. package/lib/commonjs/ui-format/groupChannel.js.map +1 -0
  29. package/lib/module/hooks/index.js +60 -0
  30. package/lib/module/hooks/index.js.map +1 -0
  31. package/lib/module/index.js +15 -0
  32. package/lib/module/index.js.map +1 -0
  33. package/lib/module/sendbird/attrs.js +49 -0
  34. package/lib/module/sendbird/attrs.js.map +1 -0
  35. package/lib/module/sendbird/channel.js +36 -0
  36. package/lib/module/sendbird/channel.js.map +1 -0
  37. package/lib/module/sendbird/message.js +85 -0
  38. package/lib/module/sendbird/message.js.map +1 -0
  39. package/lib/module/shared/arrayToMap.js +45 -0
  40. package/lib/module/shared/arrayToMap.js.map +1 -0
  41. package/lib/module/shared/conditionChaining.js +7 -0
  42. package/lib/module/shared/conditionChaining.js.map +1 -0
  43. package/lib/module/shared/logger.js +90 -0
  44. package/lib/module/shared/logger.js.map +1 -0
  45. package/lib/module/shared/pubsub.js +45 -0
  46. package/lib/module/shared/pubsub.js.map +1 -0
  47. package/lib/module/shared/regex.js +78 -0
  48. package/lib/module/shared/regex.js.map +1 -0
  49. package/lib/module/types.js +2 -0
  50. package/lib/module/types.js.map +1 -0
  51. package/lib/module/ui-format/common.js +85 -0
  52. package/lib/module/ui-format/common.js.map +1 -0
  53. package/lib/module/ui-format/groupChannel.js +59 -0
  54. package/lib/module/ui-format/groupChannel.js.map +1 -0
  55. package/lib/typescript/src/hooks/index.d.ts +11 -0
  56. package/lib/typescript/src/index.d.ts +15 -0
  57. package/lib/typescript/src/sendbird/attrs.d.ts +43 -0
  58. package/lib/typescript/src/sendbird/channel.d.ts +18 -0
  59. package/lib/typescript/src/sendbird/message.d.ts +20 -0
  60. package/lib/typescript/src/shared/arrayToMap.d.ts +5 -0
  61. package/lib/typescript/src/shared/conditionChaining.d.ts +2 -0
  62. package/lib/typescript/src/shared/logger.d.ts +38 -0
  63. package/lib/typescript/src/shared/pubsub.d.ts +8 -0
  64. package/lib/typescript/src/shared/regex.d.ts +11 -0
  65. package/lib/typescript/src/types.d.ts +79 -0
  66. package/lib/typescript/src/ui-format/common.d.ts +43 -0
  67. package/lib/typescript/src/ui-format/groupChannel.d.ts +5 -0
  68. package/package.json +75 -0
  69. package/src/hooks/index.ts +65 -0
  70. package/src/index.ts +57 -0
  71. package/src/sendbird/attrs.ts +44 -0
  72. package/src/sendbird/channel.ts +37 -0
  73. package/src/sendbird/message.ts +101 -0
  74. package/src/shared/arrayToMap.ts +70 -0
  75. package/src/shared/conditionChaining.ts +6 -0
  76. package/src/shared/logger.ts +59 -0
  77. package/src/shared/pubsub.ts +42 -0
  78. package/src/shared/regex.ts +74 -0
  79. package/src/types.ts +101 -0
  80. package/src/ui-format/common.ts +73 -0
  81. package/src/ui-format/groupChannel.ts +47 -0
@@ -0,0 +1,47 @@
1
+ import type { Locale } from 'date-fns';
2
+ import format from 'date-fns/format';
3
+ import isToday from 'date-fns/isToday';
4
+ import isYesterday from 'date-fns/isYesterday';
5
+
6
+ import type { SendbirdGroupChannel } from '../types';
7
+ import { truncate } from './common';
8
+
9
+ export const getGroupChannelTitle = (
10
+ currentUserId: string,
11
+ channel: SendbirdGroupChannel,
12
+ EMPTY_USERNAME = '(No name)',
13
+ NO_MEMBERS = '(No members)',
14
+ DEFAULT_CHANNEL_NAME = 'Group Channel',
15
+ ) => {
16
+ if (channel.name !== DEFAULT_CHANNEL_NAME && channel.name !== '') return channel.name;
17
+ if (channel.memberCount === 1) return NO_MEMBERS;
18
+ return channel.members
19
+ .filter(({ userId }) => userId !== currentUserId)
20
+ .map(({ nickname }) => nickname || EMPTY_USERNAME)
21
+ .join(', ');
22
+ };
23
+
24
+ export const getGroupChannelPreviewTime = (channel: SendbirdGroupChannel, locale?: Locale) => {
25
+ const timestamp = channel.lastMessage?.createdAt || channel.joinedAt * 1000 || channel.createdAt;
26
+ if (isToday(timestamp)) return format(timestamp, 'p', { locale });
27
+ if (isYesterday(timestamp)) return 'Yesterday';
28
+ return format(timestamp, 'MMM dd', { locale });
29
+ };
30
+
31
+ export const getGroupChannelLastMessage = (channel: SendbirdGroupChannel, EMPTY_MESSAGE = '', MAX_LEN = 15) => {
32
+ const message = channel.lastMessage;
33
+ if (!message) return EMPTY_MESSAGE;
34
+
35
+ if (message.isFileMessage()) {
36
+ const extIdx = message.name.lastIndexOf('.');
37
+ if (extIdx > -1) {
38
+ const file = message.name.slice(0, extIdx);
39
+ const ext = message.name.slice(extIdx);
40
+ return truncate(file, { maxLen: MAX_LEN }) + ext;
41
+ }
42
+
43
+ return truncate(message.name, { maxLen: MAX_LEN });
44
+ }
45
+
46
+ return message.message ?? EMPTY_MESSAGE;
47
+ };