11import { LinkPreviewsManager } from './linkPreviewsManager' ;
22import { AttachmentManager } from './attachmentManager' ;
33import { TextComposer } from './textComposer' ;
4- import { Channel } from '../channel' ;
54import { StateStore } from '../store' ;
65import { formatMessage , generateUUIDv4 } from '../utils' ;
6+ import type { Channel } from '../channel' ;
77import type {
8- DefaultGenerics ,
98 DraftMessage ,
109 DraftResponse ,
1110 EventTypes ,
12- ExtendableGenerics ,
1311 FormatMessageResponse ,
1412 MessageResponse ,
1513 MessageResponseBase ,
@@ -21,10 +19,10 @@ import { mergeWith } from '../utils/mergeWith';
2119 1/ decide whether lastChange timestamp is necessary
2220 */
2321
24- export type MessageComposerState < SCG extends ExtendableGenerics = DefaultGenerics > = {
22+ export type MessageComposerState = {
2523 id : string ;
2624 lastChange : Date | null ;
27- quotedMessage : FormatMessageResponse < SCG > | null ;
25+ quotedMessage : FormatMessageResponse | null ;
2826} ;
2927
3028export type MessageComposerConfig = {
@@ -34,26 +32,25 @@ export type MessageComposerConfig = {
3432 urlPreviewEnabled ?: boolean ;
3533} ;
3634
37- export type MessageComposerOptions < SCG extends ExtendableGenerics = DefaultGenerics > = {
38- channel : Channel < SCG > ;
39- composition ?: DraftResponse < SCG > | MessageResponse < SCG > | FormatMessageResponse < SCG > ;
35+ export type MessageComposerOptions = {
36+ channel : Channel ;
37+ composition ?: DraftResponse | MessageResponse | FormatMessageResponse ;
4038 config ?: Partial < MessageComposerConfig > ;
4139 threadId ?: string ;
4240} ;
4341
44- const isMessageDraft = < SCG extends ExtendableGenerics = DefaultGenerics > (
45- composition : DraftResponse < SCG > | MessageResponse < SCG > | FormatMessageResponse < SCG > ,
46- ) : composition is DraftResponse < SCG > => ! ! composition . message ;
42+ const isMessageDraft = ( composition : unknown ) : composition is DraftResponse =>
43+ ! ! ( composition as { message ?: DraftMessage } ) . message ;
4744
48- const initState = < SCG extends ExtendableGenerics = DefaultGenerics > (
49- composition ?: DraftResponse < SCG > | MessageResponse < SCG > | FormatMessageResponse < SCG > ,
50- ) : MessageComposerState < SCG > =>
45+ const initState = (
46+ composition ?: DraftResponse | MessageResponse | FormatMessageResponse ,
47+ ) : MessageComposerState =>
5148 composition
5249 ? {
5350 id : isMessageDraft ( composition ) ? composition . message . id : composition . id ,
5451 lastChange : new Date ( ) ,
5552 quotedMessage : composition . quoted_message
56- ? formatMessage ( composition . quoted_message as MessageResponseBase < SCG > )
53+ ? formatMessage ( composition . quoted_message as MessageResponseBase )
5754 : null ,
5855 }
5956 : {
@@ -67,28 +64,31 @@ const DEFAULT_COMPOSER_CONFIG: MessageComposerConfig = {
6764 urlPreviewEnabled : false ,
6865} ;
6966
70- export class MessageComposer < SCG extends ExtendableGenerics = DefaultGenerics > {
71- channel : Channel < SCG > ;
67+ export class MessageComposer {
68+ channel : Channel ;
7269 config : MessageComposerConfig ;
73- state : StateStore < MessageComposerState < SCG > > ;
74- attachmentManager : AttachmentManager < SCG > ;
75- linkPreviewsManager : LinkPreviewsManager < SCG > ;
76- // todo: mediaRecorder: MediaRecorderController<SCG> ;
77- textComposer : TextComposer < SCG > ;
70+ state : StateStore < MessageComposerState > ;
71+ attachmentManager : AttachmentManager ;
72+ linkPreviewsManager : LinkPreviewsManager ;
73+ // todo: mediaRecorder: MediaRecorderController;
74+ textComposer : TextComposer ;
7875 threadId : string | null ;
7976 private unsubscribeFunctions : Set < ( ) => void > = new Set ( ) ;
8077
81- constructor ( { channel, composition, config = { } , threadId } : MessageComposerOptions < SCG > ) {
78+ constructor ( { channel, composition, config = { } , threadId } : MessageComposerOptions ) {
8279 this . channel = channel ;
8380 this . threadId = threadId ?? null ;
8481 // todo: solve ts-ignore
85- // @ts -ignore
86- this . config = mergeWith ( config , DEFAULT_COMPOSER_CONFIG ) ;
87- const message = composition && ( isMessageDraft ( composition ) ? composition . message : composition ) ;
88- this . attachmentManager = new AttachmentManager < SCG > ( { channel, message } ) ;
89- this . linkPreviewsManager = new LinkPreviewsManager < SCG > ( { client : channel . getClient ( ) , message } ) ;
90- this . textComposer = new TextComposer < SCG > ( { composer : this , message } ) ;
91- this . state = new StateStore < MessageComposerState < SCG > > ( initState < SCG > ( composition ) ) ;
82+ this . config = mergeWith ( DEFAULT_COMPOSER_CONFIG , config ) ;
83+ const message =
84+ composition && ( isMessageDraft ( composition ) ? composition . message : composition ) ;
85+ this . attachmentManager = new AttachmentManager ( { channel, message } ) ;
86+ this . linkPreviewsManager = new LinkPreviewsManager ( {
87+ client : channel . getClient ( ) ,
88+ message,
89+ } ) ;
90+ this . textComposer = new TextComposer ( { composer : this , message } ) ;
91+ this . state = new StateStore < MessageComposerState > ( initState ( composition ) ) ;
9292 }
9393
9494 get client ( ) {
@@ -103,8 +103,12 @@ export class MessageComposer<SCG extends ExtendableGenerics = DefaultGenerics> {
103103 return this . state . getLatestValue ( ) . quotedMessage ;
104104 }
105105
106- initState = ( { composition } : { composition ?: DraftResponse < SCG > | MessageResponse < SCG > } = { } ) => {
107- const message = composition && ( composition . message as DraftMessage < SCG > | MessageResponseBase < SCG > ) ;
106+ initState = ( {
107+ composition,
108+ } : { composition ?: DraftResponse | MessageResponse } = { } ) => {
109+ const message = isMessageDraft ( composition )
110+ ? composition . message
111+ : ( composition as MessageResponse ) ;
108112 this . attachmentManager . initState ( { message } ) ;
109113 this . linkPreviewsManager . initState ( { message } ) ;
110114 this . textComposer . initState ( { message } ) ;
@@ -133,7 +137,12 @@ export class MessageComposer<SCG extends ExtendableGenerics = DefaultGenerics> {
133137
134138 private subscribeMessageUpdated = ( ) => {
135139 // todo: test the impact of 'reaction.new', 'reaction.deleted', 'reaction.updated'
136- const eventTypes : EventTypes [ ] = [ 'message.updated' , 'reaction.new' , 'reaction.deleted' , 'reaction.updated' ] ;
140+ const eventTypes : EventTypes [ ] = [
141+ 'message.updated' ,
142+ 'reaction.new' ,
143+ 'reaction.deleted' ,
144+ 'reaction.updated' ,
145+ ] ;
137146
138147 const unsubscribeFunctions = eventTypes . map (
139148 ( eventType ) =>
@@ -163,25 +172,32 @@ export class MessageComposer<SCG extends ExtendableGenerics = DefaultGenerics> {
163172
164173 private subscribeDraftUpdated = ( ) =>
165174 this . client . on ( 'draft.updated' , ( event ) => {
166- const draft = event . draft as DraftResponse < SCG > ;
167- if ( ! draft || draft . parent_id !== this . threadId || draft . message . id !== this . id ) return ;
175+ const draft = event . draft as DraftResponse ;
176+ if ( ! draft || draft . parent_id !== this . threadId || draft . message . id !== this . id )
177+ return ;
168178 this . initState ( { composition : draft } ) ;
169179 } ) . unsubscribe ;
170180
171181 private subscribeDraftDeleted = ( ) =>
172182 this . client . on ( 'draft.deleted' , ( event ) => {
173- const draft = event . draft as DraftResponse < SCG > ;
174- if ( ! draft || draft . parent_id !== this . threadId || draft . message . id !== this . id ) return ;
183+ const draft = event . draft as DraftResponse ;
184+ if ( ! draft || draft . parent_id !== this . threadId || draft . message . id !== this . id )
185+ return ;
175186 this . clear ( ) ;
176187 } ) . unsubscribe ;
177188
178189 private subscribeTextChanged = ( ) =>
179190 this . textComposer . state . subscribe ( ( nextValue , previousValue ) => {
180- if ( ! this . config . urlPreviewEnabled || ! nextValue . text || nextValue . text === previousValue ?. text ) return ;
191+ if (
192+ ! this . config . urlPreviewEnabled ||
193+ ! nextValue . text ||
194+ nextValue . text === previousValue ?. text
195+ )
196+ return ;
181197 this . linkPreviewsManager . findAndEnrichUrls ( nextValue . text ) ;
182198 } ) ;
183199
184- setQuotedMessage = ( quotedMessage : FormatMessageResponse < SCG > | null ) => {
200+ setQuotedMessage = ( quotedMessage : FormatMessageResponse | null ) => {
185201 this . state . partialNext ( { quotedMessage } ) ;
186202 } ;
187203
0 commit comments