stream-chat-react 13.0.0 → 13.0.2

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.
@@ -8,3 +8,4 @@ export * from './components';
8
8
  export * from './UnsupportedAttachment';
9
9
  export * from './FileAttachment';
10
10
  export * from './utils';
11
+ export { useAudioController } from './hooks/useAudioController';
@@ -8,3 +8,4 @@ export * from './components';
8
8
  export * from './UnsupportedAttachment';
9
9
  export * from './FileAttachment';
10
10
  export * from './utils';
11
+ export { useAudioController } from './hooks/useAudioController';
@@ -24,7 +24,7 @@ export const useChat = ({ client, defaultLanguage = 'en', i18nInstance, initialN
24
24
  useEffect(() => {
25
25
  if (!client)
26
26
  return;
27
- const version = "13.0.0";
27
+ const version = "13.0.2";
28
28
  const userAgent = client.getUserAgent();
29
29
  if (!userAgent.includes('stream-chat-react')) {
30
30
  // result looks like: 'stream-chat-react-2.3.2-stream-chat-javascript-client-browser-2.2.2'
@@ -57,16 +57,20 @@ export const FormDialog = ({ className, close, fields, onSubmit, shouldDisableSu
57
57
  return (React.createElement("div", { className: clsx('str-chat__dialog str-chat__dialog--form', className) },
58
58
  React.createElement("div", { className: 'str-chat__dialog__body' },
59
59
  title && React.createElement("div", { className: 'str-chat__dialog__title' }, title),
60
- React.createElement("form", { autoComplete: 'off' }, Object.entries(fields).map(([id, fieldConfig]) => (React.createElement("div", { className: 'str-chat__dialog__field', key: `dialog-field-${id}` },
61
- fieldConfig.label && (React.createElement("label", { className: clsx(`str-chat__dialog__title str-chat__dialog__title--${id}`), htmlFor: id }, fieldConfig.label)),
62
- React.createElement(fieldConfig.element, {
63
- id,
64
- ...fieldConfig.props,
65
- onChange: handleChange,
66
- value: value[id],
67
- }),
68
- React.createElement(FieldError, { text: fieldErrors[id]?.message })))))),
69
- React.createElement("div", { className: 'str-chat__dialog__controls' },
70
- React.createElement("button", { className: 'str-chat__dialog__controls-button str-chat__dialog__controls-button--cancel', onClick: close }, t('Cancel')),
71
- React.createElement("button", { className: 'str-chat__dialog__controls-button str-chat__dialog__controls-button--submit', disabled: Object.keys(fieldErrors).length > 0 || shouldDisableSubmitButton?.(value), onClick: handleSubmit, type: 'submit' }, t('Send')))));
60
+ React.createElement("form", { autoComplete: 'off', onSubmit: (e) => {
61
+ e.preventDefault();
62
+ handleSubmit();
63
+ } },
64
+ Object.entries(fields).map(([id, fieldConfig]) => (React.createElement("div", { className: 'str-chat__dialog__field', key: `dialog-field-${id}` },
65
+ fieldConfig.label && (React.createElement("label", { className: clsx(`str-chat__dialog__title str-chat__dialog__title--${id}`), htmlFor: id }, fieldConfig.label)),
66
+ React.createElement(fieldConfig.element, {
67
+ id,
68
+ ...fieldConfig.props,
69
+ onChange: handleChange,
70
+ value: value[id],
71
+ }),
72
+ React.createElement(FieldError, { text: fieldErrors[id]?.message })))),
73
+ React.createElement("div", { className: 'str-chat__dialog__controls' },
74
+ React.createElement("button", { className: 'str-chat__dialog__controls-button str-chat__dialog__controls-button--cancel', onClick: close }, t('Cancel')),
75
+ React.createElement("button", { className: 'str-chat__dialog__controls-button str-chat__dialog__controls-button--submit', disabled: Object.keys(fieldErrors).length > 0 || shouldDisableSubmitButton?.(value), type: 'submit' }, t('Send')))))));
72
76
  };
@@ -1,7 +1,24 @@
1
1
  import { useCallback } from 'react';
2
+ import { MessageComposer } from 'stream-chat';
2
3
  import { useMessageComposer } from './useMessageComposer';
3
4
  import { useChannelActionContext } from '../../../context/ChannelActionContext';
4
5
  import { useTranslationContext } from '../../../context/TranslationContext';
6
+ const takeStateSnapshot = (messageComposer) => {
7
+ const textComposerState = messageComposer.textComposer.state.getLatestValue();
8
+ const attachmentManagerState = messageComposer.attachmentManager.state.getLatestValue();
9
+ const linkPreviewsManagerState = messageComposer.linkPreviewsManager.state.getLatestValue();
10
+ const pollComposerState = messageComposer.pollComposer.state.getLatestValue();
11
+ const customDataManagerState = messageComposer.customDataManager.state.getLatestValue();
12
+ const state = messageComposer.state.getLatestValue();
13
+ return () => {
14
+ messageComposer.state.next(state);
15
+ messageComposer.textComposer.state.next(textComposerState);
16
+ messageComposer.attachmentManager.state.next(attachmentManagerState);
17
+ messageComposer.linkPreviewsManager.state.next(linkPreviewsManagerState);
18
+ messageComposer.pollComposer.state.next(pollComposerState);
19
+ messageComposer.customDataManager.state.next(customDataManagerState);
20
+ };
21
+ };
5
22
  export const useSubmitHandler = (props) => {
6
23
  const { clearEditingState, overrideSubmitHandler } = props;
7
24
  const { addNotification, editMessage, sendMessage } = useChannelActionContext('useSubmitHandler');
@@ -23,7 +40,20 @@ export const useSubmitHandler = (props) => {
23
40
  }
24
41
  }
25
42
  else {
43
+ const restoreComposerStateSnapshot = takeStateSnapshot(messageComposer);
26
44
  try {
45
+ // FIXME: once MessageComposer has sendMessage method, then the following condition should be encapsulated by it
46
+ // keep attachments, text, quoted message (treat them as draft) ... if sending a poll
47
+ const sentPollMessage = !!message.poll_id;
48
+ if (sentPollMessage) {
49
+ messageComposer.state.partialNext({
50
+ id: MessageComposer.generateId(),
51
+ pollId: null,
52
+ });
53
+ }
54
+ else {
55
+ messageComposer.clear();
56
+ }
27
57
  // todo: get rid of overrideSubmitHandler once MessageComposer supports submission flow
28
58
  if (overrideSubmitHandler) {
29
59
  await overrideSubmitHandler({
@@ -36,11 +66,11 @@ export const useSubmitHandler = (props) => {
36
66
  else {
37
67
  await sendMessage({ localMessage, message, options: sendOptions });
38
68
  }
39
- messageComposer.clear();
40
69
  if (messageComposer.config.text.publishTypingEvents)
41
70
  await messageComposer.channel.stopTyping();
42
71
  }
43
72
  catch (err) {
73
+ restoreComposerStateSnapshot();
44
74
  addNotification(t('Send message request failed'), 'error');
45
75
  }
46
76
  }
@@ -7,7 +7,7 @@ import { useStateStore } from '../../../../store';
7
7
  import { usePollContext, useTranslationContext } from '../../../../context';
8
8
  const pollStateSelector = (nextValue) => ({
9
9
  name: nextValue.name,
10
- options: nextValue.options,
10
+ options: [...nextValue.options],
11
11
  vote_counts_by_option: nextValue.vote_counts_by_option,
12
12
  });
13
13
  export const PollResults = ({ close }) => {
@@ -28,8 +28,15 @@ export const MultipleAnswersField = () => {
28
28
  React.createElement("input", { id: 'max_votes_allowed', onBlur: () => {
29
29
  pollComposer.handleFieldBlur('max_votes_allowed');
30
30
  }, onChange: (e) => {
31
+ const nativeFieldValidation = !e.target.validity.valid
32
+ ? {
33
+ max_votes_allowed: t('Only numbers are allowed'),
34
+ }
35
+ : undefined;
31
36
  pollComposer.updateFields({
32
- max_votes_allowed: e.target.value,
33
- });
37
+ max_votes_allowed: !nativeFieldValidation
38
+ ? e.target.value
39
+ : pollComposer.max_votes_allowed,
40
+ }, nativeFieldValidation);
34
41
  }, placeholder: t('Maximum number of votes (from 2 to 10)'), type: 'number', value: max_votes_allowed }))))));
35
42
  };
@@ -15,13 +15,15 @@ export const PollHeader = () => {
15
15
  const selectionInstructions = useMemo(() => {
16
16
  if (is_closed)
17
17
  return t('Vote ended');
18
- if (enforce_unique_vote)
18
+ if (enforce_unique_vote || options.length === 1)
19
19
  return t('Select one');
20
20
  if (max_votes_allowed)
21
21
  return t('Select up to {{count}}', {
22
22
  count: max_votes_allowed > options.length ? options.length : max_votes_allowed,
23
23
  });
24
- return t('Select one or more');
24
+ if (options.length > 1)
25
+ return t('Select one or more');
26
+ return '';
25
27
  }, [is_closed, enforce_unique_vote, max_votes_allowed, options.length, t]);
26
28
  if (!name)
27
29
  return;
@@ -26,11 +26,11 @@ const configStateSelector = (state) => ({
26
26
  * In the long term, the fix should happen by handling keypress, but changing this has unknown implications.
27
27
  */
28
28
  const defaultShouldSubmit = (event) => event.key === 'Enter' && !event.shiftKey && !event.nativeEvent.isComposing;
29
- export const TextareaComposer = ({ className, closeSuggestionsOnClickOutside, containerClassName, listClassName, maxRows: maxRowsProp = 1, minRows: minRowsProp, onBlur, onChange, onKeyDown, onScroll, onSelect, placeholder: placeholderProp, shouldSubmit: shouldSubmitProp, ...restTextareaProps }) => {
29
+ export const TextareaComposer = ({ className, closeSuggestionsOnClickOutside, containerClassName, listClassName, maxRows: maxRowsProp, minRows: minRowsProp, onBlur, onChange, onKeyDown, onScroll, onSelect, placeholder: placeholderProp, shouldSubmit: shouldSubmitProp, ...restTextareaProps }) => {
30
30
  const { t } = useTranslationContext();
31
31
  const { AutocompleteSuggestionList = DefaultSuggestionList } = useComponentContext();
32
32
  const { additionalTextareaProps, cooldownRemaining, handleSubmit, maxRows: maxRowsContext, minRows: minRowsContext, onPaste, shouldSubmit: shouldSubmitContext, textareaRef, } = useMessageInputContext();
33
- const maxRows = maxRowsProp ?? maxRowsContext;
33
+ const maxRows = maxRowsProp ?? maxRowsContext ?? 1;
34
34
  const minRows = minRowsProp ?? minRowsContext;
35
35
  const placeholder = placeholderProp ?? additionalTextareaProps?.placeholder;
36
36
  const shouldSubmit = shouldSubmitProp ?? shouldSubmitContext ?? defaultShouldSubmit;
@@ -68,16 +68,13 @@ export const TextareaComposer = ({ className, closeSuggestionsOnClickOutside, co
68
68
  onKeyDown(event);
69
69
  return;
70
70
  }
71
- if (event.key === 'Enter') {
72
- // allow next line only on Shift + Enter. Enter is reserved for submission.
73
- event.preventDefault();
74
- }
75
71
  if (textComposer.suggestions &&
76
72
  textComposer.suggestions.searchSource.items?.length) {
77
73
  if (event.key === 'Escape')
78
74
  return textComposer.closeSuggestions();
79
75
  const loadedItems = textComposer.suggestions.searchSource.items;
80
76
  if (event.key === 'Enter') {
77
+ event.preventDefault();
81
78
  textComposer.handleSelect(loadedItems[focusedItemIndex]);
82
79
  }
83
80
  if (event.key === 'ArrowUp') {