@webitel/ui-chats 0.0.16 → 0.0.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webitel/ui-chats",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "Reusable Webitel Frontend Code for Chats UI",
5
5
  "workspaces": [
6
6
  "../../",
package/src/ui/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { ChatAction } from "./chat-footer/modules/user-input/enums/ChatAction.enum";
2
+ export { MessageAction } from "./messaging/modules/message/enums/MessageAction.enum";
2
3
  export type { ChatMessageType } from "./messaging/types/ChatMessage.types";
3
4
  export { default as ChatContainer } from "./the-chat-container.vue";
@@ -15,6 +15,7 @@
15
15
  :message="message"
16
16
  :show-avatar="showAvatar(index)"
17
17
  :without-avatars="props.withoutAvatars"
18
+ @[MessageAction.ClickOnImage]="clickOnImage(message)"
18
19
  >
19
20
  <template #before-message>
20
21
  <chat-date-divider
@@ -26,7 +27,7 @@
26
27
  </div>
27
28
  <scroll-to-bottom-btn
28
29
  v-if="showScrollToBottomBtn"
29
- :new-message-count="newUnseenMessages"
30
+ :new-message-count="newUnseenMessagesCount"
30
31
  @scroll="scrollToBottom('smooth')"
31
32
  />
32
33
  </section>
@@ -34,12 +35,13 @@
34
35
 
35
36
  <script setup lang="ts">
36
37
  import type { Emitter } from "mitt";
37
- import { inject, useTemplateRef } from "vue";
38
+ import { computed, inject, nextTick, onMounted, useTemplateRef } from "vue";
38
39
 
39
40
  import type { UiChatsEmitterEvents } from "../../utils/emitter";
40
41
  import { useChatScroll } from "../composables/useChatScroll";
41
42
  import ChatMessage from "../modules/message/components/chat-message.vue";
42
43
  import { useChatMessages } from "../modules/message/composables/useChatMessage";
44
+ import { MessageAction } from "../modules/message/enums/MessageAction.enum";
43
45
  import type { ChatMessageType } from "../types/ChatMessage.types";
44
46
  import ChatDateDivider from "./chat-date-divider.vue";
45
47
  import ScrollToBottomBtn from "./scroll-to-bottom-btn.vue";
@@ -62,15 +64,28 @@ const { showAvatar, showChatDate } = useChatMessages(props.messages);
62
64
 
63
65
  const {
64
66
  showScrollToBottomBtn,
65
- newUnseenMessages,
67
+ newUnseenMessagesCount,
66
68
  scrollToBottom,
67
69
  handleChatScroll,
68
70
  handleChatResize,
69
- } = useChatScroll(messagesContainer, props.messages);
71
+ } = useChatScroll(
72
+ messagesContainer,
73
+ computed(() => props.messages),
74
+ );
70
75
 
71
76
  function focusOnInput() {
72
77
  uiChatsEmitter?.on("focusOnTextField", focus);
73
78
  }
79
+
80
+ function clickOnImage(message: ChatMessageType) {
81
+ uiChatsEmitter!.emit("clickChatMessageImage", message);
82
+ }
83
+
84
+ onMounted(() => {
85
+ nextTick(() => {
86
+ scrollToBottom();
87
+ });
88
+ });
74
89
  </script>
75
90
 
76
91
  <style scoped lang="scss">
@@ -5,27 +5,23 @@ import type { ChatMessageType } from "../types/ChatMessage.types";
5
5
 
6
6
  export const useChatScroll = (
7
7
  element: Ref<HTMLElement | null> = null,
8
- chatMessages: ChatMessageType[] = [],
8
+ messages: Ref<ChatMessageType[]> | ComputedRef<ChatMessageType[]>,
9
9
  ) => {
10
+ /* @author ye.pohranichna
11
+ why 136px? because: https://webitel.atlassian.net/browse/WTEL-7136 */
10
12
  const defaultThreshold = 136;
11
- const { arrivedState } = useScroll(element.value);
13
+ const { arrivedState } = useScroll(element);
12
14
 
13
- const newUnseenMessages = ref<number>(0);
15
+ const newUnseenMessagesCount = ref<number>(0);
14
16
  const showScrollToBottomBtn = ref<boolean>(false);
15
17
  /* @author ye.pohranichna
16
- the distance where the scrollToBottomBtn must be shown/hide.
17
- why defaultThreshold=136px? because: https://webitel.atlassian.net/browse/WTEL-7136 */
18
+ the distance where the scrollToBottomBtn must be shown/hide. */
18
19
  const threshold = ref<number>(defaultThreshold);
19
- const messages = ref<ChatMessageType[]>(chatMessages);
20
-
21
- const lastMessage = computed<ChatMessageType>(
22
- () => messages.value[messages.value?.length - 1],
23
- );
24
20
 
25
- const isLastMessageIsMy = computed<boolean>(() =>
26
- Boolean(lastMessage.value?.member?.self),
21
+ const isLastMessageIsMy = computed<boolean>(
22
+ () => lastMessage.value?.member?.self,
27
23
  );
28
-
24
+ const lastMessage = computed<ChatMessageType>(() => messages.value?.at(-1));
29
25
  const handleChatScroll = () => {
30
26
  const wrapper = element.value;
31
27
  if (!wrapper) return;
@@ -42,13 +38,23 @@ export const useChatScroll = (
42
38
  };
43
39
 
44
40
  const handleShowScrollToBottomBtn = (el: HTMLElement) => {
45
- resetScrollToBottomBtn();
41
+ if (arrivedState.bottom) {
42
+ resetScrollToBottomBtn();
43
+ return;
44
+ /* @author ye.pohranichna
45
+ quit the function because we are already at the bottom */
46
+ }
46
47
 
47
48
  const { scrollTop, scrollHeight, clientHeight } = el;
48
49
  const distanceFromBottom = scrollHeight - (scrollTop + clientHeight);
49
50
  showScrollToBottomBtn.value = distanceFromBottom > threshold.value;
50
51
  };
51
52
 
53
+ const resetScrollToBottomBtn = (): void => {
54
+ newUnseenMessagesCount.value = 0;
55
+ showScrollToBottomBtn.value = false;
56
+ };
57
+
52
58
  const updateThreshold = (el: HTMLElement) => {
53
59
  /* @author ye.pohranichna
54
60
  need to update if clientHeight was changed
@@ -56,23 +62,11 @@ export const useChatScroll = (
56
62
  threshold.value = Math.max(defaultThreshold, el.clientHeight * 0.3);
57
63
  };
58
64
 
59
- const resetScrollToBottomBtn = (): void => {
60
- if (arrivedState.bottom && newUnseenMessages.value) {
61
- /* @author ye.pohranichna
62
- hide the btn and reset new messages count, when we scroll to the bottom without btn */
63
- newUnseenMessages.value = 0;
64
- showScrollToBottomBtn.value = false;
65
- /* @author ye.pohranichna
66
- quit the function because we are already at the bottom */
67
- return;
68
- }
69
- };
70
-
71
- const scrollToBottomAfterNewMessage = () => {
65
+ const handleBtnAfterNewMessage = () => {
72
66
  if (arrivedState.bottom || isLastMessageIsMy.value) {
73
67
  scrollToBottom("smooth");
74
68
  } else {
75
- newUnseenMessages.value += 1;
69
+ newUnseenMessagesCount.value += 1;
76
70
  }
77
71
  };
78
72
 
@@ -82,7 +76,7 @@ export const useChatScroll = (
82
76
  behavior: behavior === "instant" ? "auto" : behavior,
83
77
  });
84
78
 
85
- newUnseenMessages.value = 0;
79
+ newUnseenMessagesCount.value = 0;
86
80
  showScrollToBottomBtn.value = false;
87
81
  };
88
82
 
@@ -90,7 +84,7 @@ export const useChatScroll = (
90
84
  () => messages.value?.length,
91
85
  (newValue, oldValue) => {
92
86
  const newMessageReceived = newValue - oldValue === 1; // when chat have just 1 new message @author ye.pohranichna
93
- if (newMessageReceived) scrollToBottomAfterNewMessage();
87
+ if (newMessageReceived) handleBtnAfterNewMessage();
94
88
  },
95
89
  {
96
90
  flush: "post",
@@ -99,7 +93,7 @@ export const useChatScroll = (
99
93
 
100
94
  return {
101
95
  showScrollToBottomBtn,
102
- newUnseenMessages,
96
+ newUnseenMessagesCount,
103
97
  scrollToBottom,
104
98
  handleChatScroll,
105
99
  handleChatResize,
@@ -32,7 +32,7 @@
32
32
  <message-image
33
33
  v-else-if="image"
34
34
  :file="image"
35
- @open="emit('open-image')"
35
+ @open="emit(MessageAction.ClickOnImage)"
36
36
  />
37
37
  <message-document
38
38
  v-else-if="document"
@@ -58,6 +58,7 @@ import { computed, defineEmits, defineProps, inject } from "vue";
58
58
 
59
59
  import type { ChatMessageType } from "../../../types/ChatMessage.types";
60
60
  import { useChatMessageFile } from "../composables/useChatMessageFile";
61
+ import { MessageAction } from "../enums/MessageAction.enum";
61
62
  import MessageAvatar from "./details/chat-message-avatar.vue";
62
63
  import MessageBlockedError from "./details/chat-message-blocked-error.vue";
63
64
  import MessageDocument from "./details/chat-message-document.vue";
@@ -81,10 +82,8 @@ const props = withDefaults(
81
82
  );
82
83
 
83
84
  const emit = defineEmits<{
84
- "open-image": [];
85
- "initialized-player": [
86
- object,
87
- ];
85
+ (e: typeof MessageAction.ClickOnImage): void;
86
+ (e: typeof MessageAction.InitializedPlayer, player: object): void;
88
87
  }>();
89
88
 
90
89
  const size = inject<ComponentSize>("size");
@@ -108,7 +107,7 @@ const getClientUsername = computed<string>(() => {
108
107
  });
109
108
 
110
109
  function handlePlayerInitialize(player) {
111
- emit("initialized-player", {
110
+ emit(MessageAction.InitializedPlayer, {
112
111
  player,
113
112
  });
114
113
  }
@@ -0,0 +1,6 @@
1
+ export const MessageAction = {
2
+ ClickOnImage: "clickOnImage",
3
+ InitializedPlayer: "initializedPlayer",
4
+ } as const;
5
+
6
+ export type MessageAction = (typeof MessageAction)[keyof typeof MessageAction];
@@ -58,6 +58,7 @@ import {
58
58
  import Dropzone from "./messaging/components/dropzone.vue";
59
59
  import ChatMessagesContainer from "./messaging/components/the-chat-messages-container.vue";
60
60
  import { useDropzoneHandlers } from "./messaging/composables/useDropzoneHandlers";
61
+ import { MessageAction } from "./messaging/modules/message/enums/MessageAction.enum";
61
62
  import type { ChatMessageType } from "./messaging/types/ChatMessage.types";
62
63
  import { createUiChatsEmitter } from "./utils/emitter";
63
64
  import type { ResultCallbacks } from "./utils/ResultCallbacks.types";
@@ -89,6 +90,7 @@ const emit = defineEmits<{
89
90
  files: File[],
90
91
  options: ResultCallbacks,
91
92
  ): void;
93
+ (e: typeof MessageAction.ClickOnImage, message: ChatMessageType): void;
92
94
  }>();
93
95
 
94
96
  const slots = defineSlots<
@@ -103,6 +105,9 @@ const uiChatsEmitter = createUiChatsEmitter();
103
105
  provide("size", props.size);
104
106
  provide("uiChatsEmitter", uiChatsEmitter);
105
107
 
108
+ uiChatsEmitter?.on("clickChatMessageImage", (message) => {
109
+ emit(MessageAction.ClickOnImage, message);
110
+ });
106
111
  const { isDropzoneVisible, handleDragLeave } = useDropzoneHandlers();
107
112
 
108
113
  const draft = ref<string>("");
@@ -1,10 +1,13 @@
1
1
  import mitt from "mitt";
2
2
 
3
+ import { ChatMessageType } from "../../../types/ui";
4
+
3
5
  export type UiChatsEmitterEvents = {
4
6
  insertAtCursor: {
5
7
  text: string;
6
8
  };
7
9
  focusOnTextField: undefined;
10
+ clickChatMessageImage: ChatMessageType;
8
11
  };
9
12
 
10
13
  export const createUiChatsEmitter = () => {
@@ -1,10 +1,10 @@
1
1
  export declare const ChatAction: {
2
- readonly SendMessage: "sendMessage";
3
- readonly AttachFiles: "attachFiles";
4
- readonly EmojiPicker: "emojiPicker";
5
- readonly QuickReplies: "quickReplies";
2
+ readonly SendMessage: "sendMessage";
3
+ readonly AttachFiles: "attachFiles";
4
+ readonly EmojiPicker: "emojiPicker";
5
+ readonly QuickReplies: "quickReplies";
6
6
  };
7
7
  export type ChatAction = (typeof ChatAction)[keyof typeof ChatAction];
8
8
  export type SharedActionSlots = {
9
- [key in `action:${ChatAction}`]?: () => any;
9
+ [key in `action:${ChatAction}`]?: () => any;
10
10
  };
@@ -1,8 +1,8 @@
1
- import { type Ref } from "vue";
1
+ import { type ComputedRef, type Ref } from "vue";
2
2
  import type { ChatMessageType } from "../types/ChatMessage.types";
3
- export declare const useChatScroll: (element?: Ref<HTMLElement | null>, chatMessages?: ChatMessageType[]) => {
3
+ export declare const useChatScroll: (element: Ref<HTMLElement | null>, messages: Ref<ChatMessageType[]> | ComputedRef<ChatMessageType[]>) => {
4
4
  showScrollToBottomBtn: Ref<boolean, boolean>;
5
- newUnseenMessages: Ref<number, number>;
5
+ newUnseenMessagesCount: Ref<number, number>;
6
6
  scrollToBottom: (behavior?: ScrollBehavior) => void;
7
7
  handleChatScroll: () => void;
8
8
  handleChatResize: () => void;
@@ -11,12 +11,12 @@ type __VLS_Slots = {} & {
11
11
  } & {
12
12
  'after-message'?: (props: typeof __VLS_44) => any;
13
13
  };
14
- declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
15
- "open-image": () => any;
16
- "initialized-player": (args_0: object) => any;
14
+ declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {} & {
15
+ clickOnImage: () => any;
16
+ initializedPlayer: (player: object) => any;
17
17
  }, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
18
- "onOpen-image"?: () => any;
19
- "onInitialized-player"?: (args_0: object) => any;
18
+ onClickOnImage?: () => any;
19
+ onInitializedPlayer?: (player: object) => any;
20
20
  }>, {
21
21
  username: string;
22
22
  showAvatar: boolean;
@@ -0,0 +1,5 @@
1
+ export declare const MessageAction: {
2
+ readonly ClickOnImage: "clickOnImage";
3
+ readonly InitializedPlayer: "initializedPlayer";
4
+ };
5
+ export type MessageAction = (typeof MessageAction)[keyof typeof MessageAction];
@@ -1,44 +1,44 @@
1
1
  export interface ChatMessageType {
2
- id: number;
3
- date?: number;
4
- file?: ChatMessageFile;
5
- member: ChatMember;
6
- peer?: ChatMember;
7
- chat?: ChatMessageChatInfo;
8
- createdAt: number;
9
- channelId?: string;
10
- updatedAt?: number;
11
- contact?: null | ContactInfo;
12
- text?: string;
2
+ id: number;
3
+ date?: number;
4
+ file?: ChatMessageFile;
5
+ member: ChatMember;
6
+ peer?: ChatMember;
7
+ chat?: ChatMessageChatInfo;
8
+ createdAt: number;
9
+ channelId?: string;
10
+ updatedAt?: number;
11
+ contact?: null | ContactInfo;
12
+ text?: string;
13
13
  }
14
14
  export type ContactInfo = {
15
- id: string;
16
- name?: string;
15
+ id: string;
16
+ name?: string;
17
17
  };
18
18
  export type ChatMessageFile = {
19
- id?: string;
20
- name?: string;
21
- size?: string;
22
- mime?: string;
23
- url?: string;
24
- streamUrl?: string;
19
+ id?: string;
20
+ name?: string;
21
+ size?: string;
22
+ mime?: string;
23
+ url?: string;
24
+ streamUrl?: string;
25
25
  };
26
26
  export type ChatMember = {
27
- id: number;
28
- name: string;
29
- type: string;
30
- userId?: number;
31
- externalId?: string;
32
- via?: ChatVia;
33
- self?: boolean;
27
+ id: number;
28
+ name: string;
29
+ type: string;
30
+ userId?: number;
31
+ externalId?: string;
32
+ via?: ChatVia;
33
+ self?: boolean;
34
34
  };
35
35
  export type ChatMessageChatInfo = {
36
- id: string;
37
- via: ChatVia;
36
+ id: string;
37
+ via: ChatVia;
38
38
  };
39
39
  export type ChatVia = {
40
- id: number;
41
- name: string;
42
- type: string;
43
- messenger?: string;
40
+ id: number;
41
+ name: string;
42
+ type: string;
43
+ messenger?: string;
44
44
  };
@@ -1,33 +1,58 @@
1
1
  import { ComponentSize } from "@webitel/ui-sdk/enums";
2
- import { ChatAction, type SharedActionSlots } from "./chat-footer/modules/user-input/enums/ChatAction.enum";
2
+ import {
3
+ ChatAction,
4
+ type SharedActionSlots,
5
+ } from "./chat-footer/modules/user-input/enums/ChatAction.enum";
3
6
  import type { ChatMessageType } from "./messaging/types/ChatMessage.types";
4
7
  import type { ResultCallbacks } from "./utils/ResultCallbacks.types";
5
8
  type __VLS_Props = {
6
- messages: ChatMessageType[];
7
- chatActions?: ChatAction[];
8
- size?: ComponentSize;
9
- withoutAvatars?: boolean;
9
+ messages: ChatMessageType[];
10
+ chatActions?: ChatAction[];
11
+ size?: ComponentSize;
12
+ withoutAvatars?: boolean;
10
13
  };
11
14
  type __VLS_Slots = {
12
- main: () => any;
13
- footer: () => any;
15
+ main: () => any;
16
+ footer: () => any;
14
17
  } & SharedActionSlots;
15
- declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {} & {
16
- "action:sendMessage": (text: string, options: ResultCallbacks) => any;
17
- "action:attachFiles": (files: File[], options: ResultCallbacks) => any;
18
- }, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
19
- "onAction:sendMessage"?: (text: string, options: ResultCallbacks) => any;
20
- "onAction:attachFiles"?: (files: File[], options: ResultCallbacks) => any;
21
- }>, {
22
- size: ComponentSize;
23
- withoutAvatars: boolean;
24
- chatActions: ChatAction[];
25
- }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
18
+ declare const __VLS_base: import("vue").DefineComponent<
19
+ __VLS_Props,
20
+ {},
21
+ {},
22
+ {},
23
+ {},
24
+ import("vue").ComponentOptionsMixin,
25
+ import("vue").ComponentOptionsMixin,
26
+ {} & {
27
+ "action:sendMessage": (text: string, options: ResultCallbacks) => any;
28
+ "action:attachFiles": (files: File[], options: ResultCallbacks) => any;
29
+ },
30
+ string,
31
+ import("vue").PublicProps,
32
+ Readonly<__VLS_Props> &
33
+ Readonly<{
34
+ "onAction:sendMessage"?: (text: string, options: ResultCallbacks) => any;
35
+ "onAction:attachFiles"?: (files: File[], options: ResultCallbacks) => any;
36
+ }>,
37
+ {
38
+ size: ComponentSize;
39
+ withoutAvatars: boolean;
40
+ chatActions: ChatAction[];
41
+ },
42
+ {},
43
+ {},
44
+ {},
45
+ string,
46
+ import("vue").ComponentProvideOptions,
47
+ false,
48
+ {},
49
+ any
50
+ >;
26
51
  declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
27
52
  declare const _default: typeof __VLS_export;
28
53
  export default _default;
29
54
  type __VLS_WithSlots<T, S> = T & {
30
- new (): {
31
- $slots: S;
32
- };
55
+ new (): {
56
+ $slots: S;
57
+ };
33
58
  };
@@ -1,7 +1,9 @@
1
+ import { ChatMessageType } from "../../../types/ui";
1
2
  export type UiChatsEmitterEvents = {
2
3
  insertAtCursor: {
3
4
  text: string;
4
5
  };
5
6
  focusOnTextField: undefined;
7
+ clickChatMessageImage: ChatMessageType;
6
8
  };
7
9
  export declare const createUiChatsEmitter: () => import("mitt").Emitter<UiChatsEmitterEvents>;