stream-chat 9.0.1 → 9.1.0

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/dist/esm/index.js CHANGED
@@ -6073,6 +6073,10 @@ var pollStateChangeValidators = {
6073
6073
  max_votes_allowed: ({ data, value }) => {
6074
6074
  if (data.enforce_unique_vote && value)
6075
6075
  return { max_votes_allowed: "Enforce unique vote is enabled" };
6076
+ const numericMatch = value.match(/^[0-9]+$/);
6077
+ if (!numericMatch && value) {
6078
+ return { max_votes_allowed: "Only numbers are allowed" };
6079
+ }
6076
6080
  if (value?.length > 1 && !value.match(VALID_MAX_VOTES_VALUE_REGEX))
6077
6081
  return { max_votes_allowed: "Type a number from 2 to 10" };
6078
6082
  return { max_votes_allowed: void 0 };
@@ -6187,7 +6191,7 @@ var createPollComposerStateMiddleware = ({
6187
6191
  forward
6188
6192
  }) => {
6189
6193
  if (!state.targetFields) return forward();
6190
- const { previousState } = state;
6194
+ const { previousState, injectedFieldErrors } = state;
6191
6195
  const finalValidators = {
6192
6196
  ...pollStateChangeValidators,
6193
6197
  ...defaultPollFieldChangeEventValidators,
@@ -6207,7 +6211,7 @@ var createPollComposerStateMiddleware = ({
6207
6211
  nextState: {
6208
6212
  ...previousState,
6209
6213
  data: { ...previousState.data, ...newData },
6210
- errors: { ...previousState.errors, ...newErrors }
6214
+ errors: { ...previousState.errors, ...newErrors, ...injectedFieldErrors }
6211
6215
  }
6212
6216
  });
6213
6217
  },
@@ -6233,7 +6237,11 @@ var createPollComposerStateMiddleware = ({
6233
6237
  nextState: {
6234
6238
  ...previousState,
6235
6239
  data: { ...previousState.data, ...newData },
6236
- errors: { ...previousState.errors, ...newErrors }
6240
+ errors: {
6241
+ ...previousState.errors,
6242
+ ...newErrors,
6243
+ ...state.injectedFieldErrors
6244
+ }
6237
6245
  }
6238
6246
  });
6239
6247
  }
@@ -6294,13 +6302,20 @@ var PollComposer = class {
6294
6302
  this.initState = () => {
6295
6303
  this.state.next(this.initialState);
6296
6304
  };
6297
- this.updateFields = async (data) => {
6305
+ /**
6306
+ * Updates specified fields and generates relevant errors
6307
+ * @param data
6308
+ * @param injectedFieldErrors - errors produced externally that will take precedence over the errors generated in the middleware chaing
6309
+ */
6310
+ // FIXME: change method params to a single object with the next major release
6311
+ this.updateFields = async (data, injectedFieldErrors) => {
6298
6312
  const { state, status } = await this.stateMiddlewareExecutor.execute({
6299
6313
  eventName: "handleFieldChange",
6300
6314
  initialValue: {
6301
6315
  nextState: { ...this.state.getLatestValue() },
6302
6316
  previousState: { ...this.state.getLatestValue() },
6303
- targetFields: data
6317
+ targetFields: data,
6318
+ injectedFieldErrors
6304
6319
  }
6305
6320
  });
6306
6321
  if (status === "discard") return;
@@ -6772,11 +6787,48 @@ var createDraftMessageComposerStateCompositionMiddleware = (composer) => ({
6772
6787
  }
6773
6788
  });
6774
6789
 
6790
+ // src/messageComposer/middleware/messageComposer/pollOnly.ts
6791
+ var pollLocalMessageNullifiedFields = {
6792
+ attachments: [],
6793
+ mentioned_users: [],
6794
+ parent_id: void 0,
6795
+ quoted_message: void 0,
6796
+ text: ""
6797
+ };
6798
+ var createPollOnlyCompositionMiddleware = (composer) => ({
6799
+ id: "stream-io/message-composer-middleware/poll-only",
6800
+ handlers: {
6801
+ compose: ({
6802
+ state,
6803
+ complete,
6804
+ forward
6805
+ }) => {
6806
+ const pollId = composer.pollId;
6807
+ const isEditingMessage = !!composer.editedMessage;
6808
+ const isComposingThreadReply = !!composer.threadId;
6809
+ if (!pollId || isComposingThreadReply || isEditingMessage) return forward();
6810
+ return complete({
6811
+ ...state,
6812
+ localMessage: {
6813
+ ...state.localMessage,
6814
+ ...pollLocalMessageNullifiedFields,
6815
+ poll_id: pollId
6816
+ },
6817
+ message: {
6818
+ id: state.localMessage.id,
6819
+ poll_id: pollId
6820
+ }
6821
+ });
6822
+ }
6823
+ }
6824
+ });
6825
+
6775
6826
  // src/messageComposer/middleware/messageComposer/MessageComposerMiddlewareExecutor.ts
6776
6827
  var MessageComposerMiddlewareExecutor = class extends MiddlewareExecutor {
6777
6828
  constructor({ composer }) {
6778
6829
  super();
6779
6830
  this.use([
6831
+ createPollOnlyCompositionMiddleware(composer),
6780
6832
  createTextComposerCompositionMiddleware(composer),
6781
6833
  createAttachmentsCompositionMiddleware(composer),
6782
6834
  createLinkPreviewsCompositionMiddleware(composer),
@@ -7628,27 +7680,8 @@ var TextComposerMiddlewareExecutor = class extends MiddlewareExecutor {
7628
7680
  eventName,
7629
7681
  initialValue: initialState
7630
7682
  });
7631
- if (result && result.state.suggestions) {
7632
- try {
7633
- const searchResult = await withCancellation(
7634
- "textComposer-suggestions-search",
7635
- async () => {
7636
- await result.state.suggestions?.searchSource.search(
7637
- result.state.suggestions?.query
7638
- );
7639
- }
7640
- );
7641
- if (searchResult === "canceled") return { ...result, status: "discard" };
7642
- } catch (error) {
7643
- return {
7644
- ...result,
7645
- state: {
7646
- ...result.state,
7647
- suggestions: void 0
7648
- }
7649
- };
7650
- }
7651
- }
7683
+ const { query, searchSource } = result.state.suggestions ?? {};
7684
+ searchSource?.search(query)?.catch(console.error);
7652
7685
  return result;
7653
7686
  }
7654
7687
  };
@@ -8326,15 +8359,17 @@ var initState5 = (composition) => {
8326
8359
  const quotedMessage = composition.quoted_message;
8327
8360
  let message;
8328
8361
  let draftId = null;
8362
+ let id = MessageComposer.generateId();
8329
8363
  if (compositionIsDraftResponse(composition)) {
8330
8364
  message = composition.message;
8331
8365
  draftId = composition.message.id;
8332
8366
  } else {
8333
8367
  message = composition;
8368
+ id = composition.id;
8334
8369
  }
8335
8370
  return {
8336
8371
  draftId,
8337
- id: message.id,
8372
+ id,
8338
8373
  quotedMessage: quotedMessage ? formatMessage(quotedMessage) : null,
8339
8374
  pollId: message.poll_id ?? null
8340
8375
  };
@@ -8357,6 +8392,7 @@ var _MessageComposer = class _MessageComposer {
8357
8392
  this.attachmentManager.initState({ message });
8358
8393
  this.linkPreviewsManager.initState({ message });
8359
8394
  this.textComposer.initState({ message });
8395
+ this.pollComposer.initState();
8360
8396
  this.customDataManager.initState({ message });
8361
8397
  this.state.next(initState5(composition));
8362
8398
  if (composition && !compositionIsDraftResponse(composition) && message && isLocalMessage(message)) {
@@ -8517,11 +8553,6 @@ var _MessageComposer = class _MessageComposer {
8517
8553
  this.state.partialNext({ quotedMessage });
8518
8554
  };
8519
8555
  this.clear = () => {
8520
- this.attachmentManager.initState();
8521
- this.linkPreviewsManager.initState();
8522
- this.textComposer.initState();
8523
- this.pollComposer.initState();
8524
- this.customDataManager.initState();
8525
8556
  this.initState();
8526
8557
  };
8527
8558
  this.restore = () => {
@@ -8596,6 +8627,7 @@ var _MessageComposer = class _MessageComposer {
8596
8627
  try {
8597
8628
  const { poll } = await this.client.createPoll(composition.data);
8598
8629
  this.state.partialNext({ pollId: poll.id });
8630
+ this.pollComposer.initState();
8599
8631
  } catch (error) {
8600
8632
  this.client.notifications.add({
8601
8633
  message: "Failed to create the poll",
@@ -14814,7 +14846,7 @@ var StreamChat = class _StreamChat {
14814
14846
  if (this.userAgent) {
14815
14847
  return this.userAgent;
14816
14848
  }
14817
- const version = "9.0.1";
14849
+ const version = "9.1.0";
14818
14850
  const clientBundle = "browser-esm";
14819
14851
  let userAgentString = "";
14820
14852
  if (this.sdkIdentifier) {