@webitel/ui-chats 0.1.40 → 0.1.42

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 (50) hide show
  1. package/package.json +17 -1
  2. package/src/adapters/chat-web-sdk/toChatMessage.ts +111 -0
  3. package/src/adapters/index.ts +12 -0
  4. package/src/types/index.ts +15 -0
  5. package/src/ui/index.ts +2 -0
  6. package/src/ui/messaging/components/chat-date-divider.vue +1 -1
  7. package/src/ui/messaging/components/scroll-to-bottom-btn.vue +0 -2
  8. package/src/ui/messaging/components/the-messages-container.vue +56 -46
  9. package/src/ui/messaging/composables/useChatScroll.ts +185 -75
  10. package/src/ui/messaging/composables/useObserveHeightUntilStable.ts +75 -0
  11. package/src/ui/messaging/composables/useScrollToBottomBtn.ts +51 -0
  12. package/src/ui/messaging/modules/message/components/chat-message.vue +1 -2
  13. package/src/ui/messaging/modules/message/components/details/chat-message-document.vue +1 -2
  14. package/src/ui/messaging/modules/message/components/details/chat-message-image.vue +0 -2
  15. package/src/ui/messaging/modules/message/components/details/chat-message-player.vue +1 -2
  16. package/src/ui/messaging/modules/message/components/details/chat-message-size-exceeded-error.vue +1 -1
  17. package/src/ui/messaging/modules/message/components/details/chat-message-time.vue +1 -1
  18. package/src/ui/messaging/types/ChatMessage.types.ts +3 -2
  19. package/src/ui/the-chat-container.vue +7 -0
  20. package/src/ui/utils/ResultCallbacks.types.ts +1 -1
  21. package/src/ui/utils/emitter.ts +1 -1
  22. package/types/adapters/chat-web-sdk/toChatMessage.d.ts +23 -0
  23. package/types/adapters/index.d.ts +8 -0
  24. package/types/types/index.d.ts +8 -0
  25. package/types/ui/chat-footer/modules/user-input/enums/ChatAction.enum.d.ts +6 -5
  26. package/types/ui/index.d.ts +7 -3
  27. package/types/ui/messaging/components/the-messages-container.vue.d.ts +5 -1
  28. package/types/ui/messaging/composables/useChatScroll.d.ts +13 -3
  29. package/types/ui/messaging/composables/useObserveHeightUntilStable.d.ts +11 -0
  30. package/types/ui/messaging/composables/useScrollToBottomBtn.d.ts +9 -0
  31. package/types/ui/messaging/modules/message/composables/useChatMessageFile.d.ts +2 -0
  32. package/types/ui/messaging/types/ChatMessage.types.d.ts +33 -32
  33. package/types/ui/the-chat-container.vue.d.ts +39 -49
  34. package/types/ui/utils/ResultCallbacks.types.d.ts +1 -1
  35. package/types/ui/utils/emitter.d.ts +1 -1
  36. package/types/ui/chat-container.vue.d.ts +0 -76
  37. package/types/ui/chat-footer/modules/user-input/types/ChatAction.types.d.ts +0 -10
  38. package/types/ui/chat-input/components/actions/attach-files-action.vue.d.ts +0 -29
  39. package/types/ui/chat-input/components/actions/emoji-picker-action.vue.d.ts +0 -24
  40. package/types/ui/chat-input/components/actions/send-message-action.vue.d.ts +0 -29
  41. package/types/ui/chat-input/components/chat-input-actions-bar.vue.d.ts +0 -43
  42. package/types/ui/chat-input/components/chat-input-actions-wrapper.vue.d.ts +0 -34
  43. package/types/ui/chat-input/components/chat-input.vue.d.ts +0 -42
  44. package/types/ui/chat-input/components/chat-text-field.vue.d.ts +0 -32
  45. package/types/ui/chat-input/enums/ChatAction.enum.d.ts +0 -6
  46. package/types/ui/media-viewer/media-viewer.vue.d.ts +0 -11
  47. package/types/ui/messaging/components/chat-messages-container.vue.d.ts +0 -11
  48. package/types/ui/messaging/components/the-chat-messages-container.vue.d.ts +0 -40
  49. package/types/ui/messaging/composebles/useChatScroll.d.ts +0 -12
  50. package/types/ui/messaging/modules/message/components/details/chat-message-avatar.vue.d.ts +0 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webitel/ui-chats",
3
- "version": "0.1.40",
3
+ "version": "0.1.42",
4
4
  "description": "Reusable Webitel Frontend Code for Chats UI",
5
5
  "workspaces": [
6
6
  "../../",
@@ -10,6 +10,7 @@
10
10
  "scripts": {
11
11
  "dev": "vite",
12
12
  "build:types": "vue-tsc -p ./tsconfig.build.json",
13
+ "typecheck": "vue-tsc --noEmit -p ./tsconfig.typecheck.json",
13
14
  "lint:fix": "npx biome check --write ./src",
14
15
  "biome:ci:gh": "biome ci ./src --reporter=github",
15
16
  "test:unit": "node -e \"process.exit(0)\"",
@@ -26,6 +27,7 @@
26
27
  "devDependencies": {
27
28
  "@biomejs/biome": "2.4.16",
28
29
  "@types/node": "^24.10.0",
30
+ "@webitel/chat-web-sdk": "^0.0.19",
29
31
  "@vitejs/plugin-vue": "^6.0.1",
30
32
  "@vue/tsconfig": "^0.8.1",
31
33
  "mitt": "^3",
@@ -37,11 +39,17 @@
37
39
  },
38
40
  "peerDependencies": {
39
41
  "@webitel/api-services": "^0.1.0",
42
+ "@webitel/chat-web-sdk": "^0.0.19",
40
43
  "@webitel/styleguide": "~26.2",
41
44
  "@webitel/ui-sdk": "~26.6",
42
45
  "mitt": "^3",
43
46
  "vue": "^3.5"
44
47
  },
48
+ "peerDependenciesMeta": {
49
+ "@webitel/chat-web-sdk": {
50
+ "optional": true
51
+ }
52
+ },
45
53
  "type": "module",
46
54
  "main": "index.ts",
47
55
  "types": "index.d.ts",
@@ -59,6 +67,14 @@
59
67
  "./ui": {
60
68
  "types": "./types/ui/index.d.ts",
61
69
  "import": "./src/ui/index.ts"
70
+ },
71
+ "./types": {
72
+ "types": "./types/types/index.d.ts",
73
+ "import": "./src/types/index.ts"
74
+ },
75
+ "./adapters": {
76
+ "types": "./types/adapters/index.d.ts",
77
+ "import": "./src/adapters/index.ts"
62
78
  }
63
79
  },
64
80
  "author": "webitel",
@@ -0,0 +1,111 @@
1
+ import type { IMessage } from '@webitel/chat-web-sdk';
2
+
3
+ import type { ChatMember, ChatMessageFile, ChatMessageType } from '../../types';
4
+
5
+ /**
6
+ * Sub-shapes of a chat-web-sdk message, derived via indexed access so this
7
+ * adapter depends on a single package (`@webitel/chat-web-sdk`) and stays in
8
+ * sync automatically when the SDK reshapes them.
9
+ */
10
+ type SdkMember = NonNullable<IMessage['sender']>;
11
+ type SdkImage = NonNullable<IMessage['images']>[number];
12
+ type SdkDocument = NonNullable<IMessage['documents']>[number];
13
+
14
+ export interface ToChatMessageOptions {
15
+ /**
16
+ * Decide whether a message is authored by the current agent / "self" side.
17
+ * Result is written to `ChatMember.self`, which drives outgoing-bubble
18
+ * alignment in the UI. The message alone cannot know who "self" is — that
19
+ * is presentation context the caller owns (e.g. compare `sender.contact`
20
+ * against the logged-in account).
21
+ */
22
+ isSelf?: (message: IMessage) => boolean;
23
+ }
24
+
25
+ /**
26
+ * Parse a chat-web-sdk Unix-ms timestamp (`string`) into the numeric ms the
27
+ * UI contract expects. Returns `0` for missing / non-numeric input.
28
+ */
29
+ const toTimestamp = (value: string | undefined): number => {
30
+ if (value == null) return 0;
31
+ const parsed = Number(value);
32
+ return Number.isFinite(parsed) ? parsed : 0;
33
+ };
34
+
35
+ const mapImage = (image: SdkImage): ChatMessageFile => ({
36
+ id: image.id,
37
+ mime: image.mime,
38
+ url: image.url,
39
+ });
40
+
41
+ const mapDocument = (document: SdkDocument): ChatMessageFile => ({
42
+ id: document.id,
43
+ name: document.name,
44
+ size: document.size,
45
+ mime: document.mime,
46
+ url: document.url,
47
+ });
48
+
49
+ /**
50
+ * A UI message carries at most one `file`, while the SDK exposes separate
51
+ * `images[]` / `documents[]` arrays. Pick the first available attachment,
52
+ * preferring images.
53
+ */
54
+ const mapAttachment = (message: IMessage): ChatMessageFile | undefined => {
55
+ const image = message.images?.[0];
56
+ if (image) return mapImage(image);
57
+
58
+ const document = message.documents?.[0];
59
+ if (document) return mapDocument(document);
60
+
61
+ return undefined;
62
+ };
63
+
64
+ const mapMember = (
65
+ sender: SdkMember | undefined,
66
+ self: boolean | undefined,
67
+ ): ChatMember => {
68
+ const contact = sender?.contact;
69
+
70
+ return {
71
+ id: sender?.id ?? '',
72
+ name: contact?.name ?? '',
73
+ // UI derives bot rendering from `type === 'bot'`; otherwise keep the
74
+ // SDK channel type ('telegram', 'webchat', …).
75
+ type: contact?.isBot ? 'bot' : (contact?.type ?? ''),
76
+ externalId: contact?.sub ?? contact?.iss,
77
+ self,
78
+ };
79
+ };
80
+
81
+ /**
82
+ * Map a single `@webitel/chat-web-sdk` message (`IMessage`) into the
83
+ * `@webitel/ui-chats` presentation contract (`ChatMessageType`).
84
+ *
85
+ * Anti-corruption layer: keeps `ui-chats` free of any backend/SDK coupling.
86
+ */
87
+ export const mapMessageToChatMessage = (
88
+ message: IMessage,
89
+ options: ToChatMessageOptions = {},
90
+ ): ChatMessageType => {
91
+ const createdAt = toTimestamp(message.createdAt);
92
+
93
+ return {
94
+ id: message.id,
95
+ text: message.body,
96
+ createdAt,
97
+ date: createdAt,
98
+ updatedAt: message.editedAt ? toTimestamp(message.editedAt) : undefined,
99
+ file: mapAttachment(message),
100
+ member: mapMember(message.sender, options.isSelf?.(message)),
101
+ };
102
+ };
103
+
104
+ /**
105
+ * Batch variant of {@link mapMessageToChatMessage} for message history pages.
106
+ */
107
+ export const mapMessagesToChatMessages = (
108
+ messages: readonly IMessage[],
109
+ options?: ToChatMessageOptions,
110
+ ): ChatMessageType[] =>
111
+ messages.map((message) => mapMessageToChatMessage(message, options));
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Adapters mapping external data sources into the `@webitel/ui-chats`
3
+ * presentation contract (`ChatMessageType`, …).
4
+ *
5
+ * Consuming this entry pulls `@webitel/chat-web-sdk` (declared as an optional
6
+ * peerDependency). The core `@webitel/ui-chats/ui` entry stays free of it.
7
+ */
8
+ export {
9
+ mapMessagesToChatMessages,
10
+ mapMessageToChatMessage,
11
+ type ToChatMessageOptions,
12
+ } from './chat-web-sdk/toChatMessage';
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Public data contract of `@webitel/ui-chats`.
3
+ *
4
+ * These are the plain, transport-agnostic shapes the UI components accept.
5
+ * External data sources (e.g. `@webitel/chat-web-sdk`) must be mapped into
6
+ * them — see `@webitel/ui-chats/adapters`.
7
+ */
8
+ export type {
9
+ ChatMember,
10
+ ChatMessageChatInfo,
11
+ ChatMessageFile,
12
+ ChatMessageType,
13
+ ChatVia,
14
+ ContactInfo,
15
+ } from '../ui/messaging/types/ChatMessage.types';
package/src/ui/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export { ChatAction } from './chat-footer/modules/user-input/enums/ChatAction.enum';
2
+ export { useChatScroll } from './messaging/composables/useChatScroll';
3
+ export { useObserveHeightUntilStable } from './messaging/composables/useObserveHeightUntilStable';
2
4
  export { useChatMessageFile } from './messaging/modules/message/composables/useChatMessageFile';
3
5
  export { MessageAction } from './messaging/modules/message/enums/MessageAction.enum';
4
6
  export type {
@@ -19,7 +19,7 @@ const props = defineProps<{
19
19
 
20
20
  const { t } = useI18n();
21
21
 
22
- const formattedDate = computed<Date>(() => {
22
+ const formattedDate = computed<string>(() => {
23
23
  const chatDate = formatDate(+props.date, FormatDateMode.DATE);
24
24
  const today = formatDate(Date.now(), FormatDateMode.DATE);
25
25
 
@@ -17,8 +17,6 @@
17
17
  </template>
18
18
 
19
19
  <script setup lang="ts">
20
- import { defineEmits } from 'vue';
21
-
22
20
  const props = withDefaults(
23
21
  defineProps<{
24
22
  newMessageCount?: number;
@@ -7,31 +7,35 @@
7
7
  ref="messages-container"
8
8
  class="the-chat-messages-container__wrapper wt-scrollbar"
9
9
  @scroll="handleChatScroll"
10
- @resize="handleChatResize"
11
10
  >
12
- <chat-observer
13
- v-if="props.next"
14
- :next="props.next"
15
- :loading="props.isLoading"
16
- @[ChatAction.LoadNextMessages]="handleLoadNextMessages"
17
- />
18
- <chat-message
19
- v-for="(message, index) of props.messages"
20
- :key="message.id"
21
- :message="message"
22
- :agent-name="props.agentName"
23
- :show-avatar="showAvatar(index)"
24
- :without-avatars="props.withoutAvatars"
25
- :username="props.contact?.name"
26
- @[MessageAction.ClickOnImage]="clickOnImage(message)"
11
+ <div
12
+ ref="chat-content"
13
+ class="the-chat-messages-container__content"
27
14
  >
28
- <template #before-message>
29
- <chat-date-divider
30
- v-if="showChatDate(index)"
31
- :date="message.createdAt"
32
- />
33
- </template>
34
- </chat-message>
15
+ <chat-observer
16
+ v-if="props.next"
17
+ :next="props.next"
18
+ :loading="props.isLoading"
19
+ @[ChatAction.LoadNextMessages]="handleLoadNextMessages"
20
+ />
21
+ <chat-message
22
+ v-for="(message, index) of props.messages"
23
+ :key="message.id"
24
+ :message="message"
25
+ :agent-name="props.agentName"
26
+ :show-avatar="showAvatar(index)"
27
+ :without-avatars="props.withoutAvatars"
28
+ :username="props.contact?.name?.commonName"
29
+ @[MessageAction.ClickOnImage]="clickOnImage(message)"
30
+ >
31
+ <template #before-message>
32
+ <chat-date-divider
33
+ v-if="showChatDate(index)"
34
+ :date="message.createdAt"
35
+ />
36
+ </template>
37
+ </chat-message>
38
+ </div>
35
39
  </div>
36
40
  <scroll-to-bottom-btn
37
41
  v-if="showScrollToBottomBtn"
@@ -47,17 +51,11 @@
47
51
  >
48
52
  import { WebitelContactsContact } from '@webitel/api-services/gen/models';
49
53
  import type { Emitter } from 'mitt';
50
- import {
51
- computed,
52
- defineProps,
53
- inject,
54
- nextTick,
55
- onMounted,
56
- useTemplateRef,
57
- } from 'vue';
54
+ import { computed, inject, useTemplateRef } from 'vue';
58
55
  import { ChatAction } from '../../chat-footer/modules/user-input/enums/ChatAction.enum';
59
56
  import type { UiChatsEmitterEvents } from '../../utils/emitter';
60
57
  import { useChatScroll } from '../composables/useChatScroll';
58
+ import { useObserveHeightUntilStable } from '../composables/useObserveHeightUntilStable';
61
59
  import ChatMessage from '../modules/message/components/chat-message.vue';
62
60
  import { useChatMessages } from '../modules/message/composables/useChatMessage';
63
61
  import { MessageAction } from '../modules/message/enums/MessageAction.enum';
@@ -76,17 +74,22 @@ const props = withDefaults(
76
74
  withoutAvatars?: boolean;
77
75
  agentName?: string;
78
76
  contact?: WebitelContactsContact;
77
+ chatId?: string;
78
+ isChatClosed?: boolean;
79
79
  }>(),
80
80
  {
81
81
  next: false,
82
82
  isLoading: false,
83
83
  withoutAvatars: false,
84
+ chatId: '',
85
+ isChatClosed: false,
84
86
  },
85
87
  );
86
88
 
87
89
  const emit = defineEmits<(e: typeof ChatAction.LoadNextMessages) => void>();
88
90
 
89
- const messagesContainer = useTemplateRef('messages-container');
91
+ const chatContainer = useTemplateRef('messages-container');
92
+ const chatContent = useTemplateRef('chat-content');
90
93
 
91
94
  const { showAvatar, showChatDate } = useChatMessages(
92
95
  computed(() => props.messages),
@@ -98,11 +101,21 @@ const {
98
101
  scrollToBottom,
99
102
  loadNextMessages,
100
103
  handleChatScroll,
101
- handleChatResize,
102
- } = useChatScroll(
103
- messagesContainer,
104
- computed(() => props.messages), // props values reactivity https://stackoverflow.com/questions/72408463/use-props-in-composables-vue3 @author ye.pohranichna
105
- computed(() => props.isLoading),
104
+ } = useChatScroll({
105
+ chatContainer,
106
+ chatContent,
107
+ messages: computed(() => props.messages), // props values reactivity https://stackoverflow.com/questions/72408463/use-props-in-composables-vue3 @author ye.pohranichna
108
+ chatId: computed(() => props.chatId),
109
+ isChatClosed: computed(() => props.isChatClosed),
110
+ isLoading: computed(() => props.isLoading),
111
+ onBeforeStart: ({ scrollToBottom }) => {
112
+ scrollToBottom();
113
+ startObserve();
114
+ },
115
+ });
116
+
117
+ const { startObserve } = useObserveHeightUntilStable(chatContainer, () =>
118
+ scrollToBottom('instant'),
106
119
  );
107
120
 
108
121
  function handleLoadNextMessages() {
@@ -117,13 +130,7 @@ function clickOnImage(message: ChatMessageType) {
117
130
  uiChatsEmitter?.emit('clickChatMessageImage', message);
118
131
  }
119
132
 
120
- onMounted(async () => {
121
- /* TODO: add loader on all chat(in the-chat-container?) and remove timeout after that */
122
- await nextTick();
123
- setTimeout(() => {
124
- scrollToBottom();
125
- }, 500);
126
- });
133
+ // TODO: add loader for all chats(in the-chat-container or in calling component?)
127
134
  </script>
128
135
 
129
136
  <style
@@ -138,14 +145,17 @@ onMounted(async () => {
138
145
  }
139
146
 
140
147
  .the-chat-messages-container__wrapper {
141
- display: flex;
142
- flex-direction: column;
143
148
  box-sizing: border-box;
144
149
  overflow-x: hidden;
145
150
  overflow-y: auto;
146
151
  width: 100%;
147
152
  padding-right: var(--scrollbar-width); // scrollbar offset
148
153
  scrollbar-gutter: stable both-edges;
154
+ }
155
+
156
+ .the-chat-messages-container__content {
157
+ display: flex;
158
+ flex-direction: column;
149
159
  gap: var(--spacing-xs);
150
160
  }
151
161
  </style>