@homecode/ui 5.3.0 → 5.3.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.
@@ -167,6 +167,8 @@ function Autocomplete(props) {
167
167
  return true;
168
168
  };
169
169
  const handleSelect = (option) => {
170
+ if (!option)
171
+ return;
170
172
  if (selectable) {
171
173
  setSelectedId(option.id);
172
174
  setSelectedLabel(option.label);
@@ -196,7 +198,12 @@ function Autocomplete(props) {
196
198
  const { focusedIndex, setFocusedIndex } = useListKeyboardControl({
197
199
  isActive: isPopupOpenForInput,
198
200
  itemsCount: displayItems.length,
199
- onSelect: index => handleSelect(displayItems[index]),
201
+ onSelect: index => {
202
+ const option = displayItems[index];
203
+ if (!option)
204
+ return;
205
+ handleSelect(option);
206
+ },
200
207
  });
201
208
  const fetchOptionsCore = useCallback(async (filter, offset) => {
202
209
  const requestKey = `${filter}:${offset}`;
@@ -1,6 +1,16 @@
1
1
  /** Keep in sync with `PromptComposer.styl` max/min heights. */
2
2
  const PROMPT_COMPOSER_MAX_HEIGHT_PX = 200;
3
3
  const PROMPT_COMPOSER_MIN_HEIGHT_PX = 40;
4
+ function safePromptComposerEditorText(editor) {
5
+ if (!editor || editor.isDestroyed)
6
+ return null;
7
+ try {
8
+ return editor.getText?.() ?? null;
9
+ }
10
+ catch {
11
+ return null;
12
+ }
13
+ }
4
14
  function promptComposerSafeEditorDom(editor) {
5
15
  if (!editor || editor.isDestroyed)
6
16
  return null;
@@ -41,4 +51,4 @@ function syncPromptComposerHeight(el, text) {
41
51
  contentHeight > PROMPT_COMPOSER_MAX_HEIGHT_PX ? 'auto' : 'hidden';
42
52
  }
43
53
 
44
- export { PROMPT_COMPOSER_MAX_HEIGHT_PX, PROMPT_COMPOSER_MIN_HEIGHT_PX, promptComposerSafeEditorDom, syncPromptComposerHeight };
54
+ export { PROMPT_COMPOSER_MAX_HEIGHT_PX, PROMPT_COMPOSER_MIN_HEIGHT_PX, promptComposerSafeEditorDom, safePromptComposerEditorText, syncPromptComposerHeight };
@@ -85,9 +85,13 @@ function createPromptComposerHandle(editor) {
85
85
  return {
86
86
  insertAtCaret: (content, options) => insertPromptComposerContentAtCaret(editor, content, options),
87
87
  focus: () => {
88
+ if (editor.isDestroyed)
89
+ return;
88
90
  editor.commands.focus();
89
91
  },
90
92
  reset: () => {
93
+ if (editor.isDestroyed)
94
+ return;
91
95
  editor.chain().clearContent().focus().run();
92
96
  },
93
97
  getText: () => editor.getText(),
@@ -4,7 +4,7 @@ import { useEditor } from '@tiptap/react';
4
4
  import StarterKit from '@tiptap/starter-kit';
5
5
  import { DEFAULT_CHAT_SLASH_ITEMS } from '../../tiptap/slash-mention/defaultChatSlashItems.js';
6
6
  import { createSlashMentionExtension } from '../../tiptap/slash-mention/createSlashMentionExtension.js';
7
- import { promptComposerSafeEditorDom, syncPromptComposerHeight } from './PromptComposer.helpers.js';
7
+ import { safePromptComposerEditorText, promptComposerSafeEditorDom, syncPromptComposerHeight } from './PromptComposer.helpers.js';
8
8
  import { PROMPT_COMPOSER_EMPTY_DOC, promptComposerParagraphDoc } from './promptComposerDoc.js';
9
9
 
10
10
  const PROMPT_COMPOSER_EDITOR_CLASS = 'promptComposerEditor';
@@ -67,7 +67,9 @@ function usePromptComposerEditor({ disabled, placeholder, slashCommandItems, onS
67
67
  const [text, setText] = useState('');
68
68
  const editorDomRef = useRef(null);
69
69
  const bindEditorDom = useCallback(({ editor }) => {
70
- const nextText = editor.getText();
70
+ const nextText = safePromptComposerEditorText(editor);
71
+ if (nextText == null)
72
+ return;
71
73
  setText(nextText);
72
74
  queueMicrotask(() => {
73
75
  const dom = promptComposerSafeEditorDom(editor);
@@ -119,8 +121,10 @@ function usePromptComposerEditor({ disabled, placeholder, slashCommandItems, onS
119
121
  handleKeyDown: handleEditorKeyDown,
120
122
  },
121
123
  onTransaction: ({ editor: activeEditor }) => {
124
+ const nextText = safePromptComposerEditorText(activeEditor);
125
+ if (nextText == null)
126
+ return;
122
127
  const dom = promptComposerSafeEditorDom(activeEditor);
123
- const nextText = activeEditor.getText();
124
128
  if (dom) {
125
129
  syncPromptComposerHeight(dom, nextText);
126
130
  }
@@ -152,23 +156,36 @@ function usePromptComposerEditor({ disabled, placeholder, slashCommandItems, onS
152
156
  };
153
157
  }, [editor]);
154
158
  useEffect(() => {
155
- if (!editor || prefillMessage == null)
159
+ if (!editor || prefillMessage == null || editor.isDestroyed)
160
+ return;
161
+ const current = safePromptComposerEditorText(editor);
162
+ if (current == null)
156
163
  return;
157
- const current = editor.getText();
158
164
  const next = prefillMessage;
159
165
  if (current === next)
160
166
  return;
161
- if (next === '') {
162
- editor.commands.setContent(PROMPT_COMPOSER_EMPTY_DOC);
167
+ try {
168
+ if (next === '') {
169
+ editor.commands.setContent(PROMPT_COMPOSER_EMPTY_DOC);
170
+ }
171
+ else {
172
+ editor.commands.setContent(promptComposerParagraphDoc(next));
173
+ }
163
174
  }
164
- else {
165
- editor.commands.setContent(promptComposerParagraphDoc(next));
175
+ catch {
176
+ return;
166
177
  }
167
- setText(editor.getText());
178
+ const synced = safePromptComposerEditorText(editor);
179
+ if (synced != null)
180
+ setText(synced);
168
181
  queueMicrotask(() => {
182
+ if (editor.isDestroyed)
183
+ return;
169
184
  const dom = promptComposerSafeEditorDom(editor);
170
- if (dom)
171
- syncPromptComposerHeight(dom, editor.getText());
185
+ const heightText = safePromptComposerEditorText(editor);
186
+ if (dom && heightText != null) {
187
+ syncPromptComposerHeight(dom, heightText);
188
+ }
172
189
  if (!disabled && !editor.isDestroyed) {
173
190
  editor.chain().focus('end').run();
174
191
  }
@@ -26,7 +26,11 @@ const useListKeyboardControl = ({ isActive, itemsCount, onSelect, }) => {
26
26
  return;
27
27
  }
28
28
  if (e.key === 'Enter') {
29
- onSelect(newIndex);
29
+ if (newIndex < 0 || newIndex >= itemsCount)
30
+ return;
31
+ e.preventDefault();
32
+ e.stopPropagation();
33
+ onSelect?.(newIndex);
30
34
  return;
31
35
  }
32
36
  }, [focusedIndex, itemsCount, onSelect]);
@@ -37,10 +41,14 @@ const useListKeyboardControl = ({ isActive, itemsCount, onSelect, }) => {
37
41
  isActive,
38
42
  });
39
43
  useEffect(() => {
40
- if (itemsCount < focusedIndex) {
44
+ if (itemsCount <= 0) {
45
+ setFocusedIndex(-1);
46
+ return;
47
+ }
48
+ if (focusedIndex >= itemsCount) {
41
49
  setFocusedIndex(itemsCount - 1);
42
50
  }
43
- }, [itemsCount]);
51
+ }, [itemsCount, focusedIndex]);
44
52
  return {
45
53
  focusedIndex,
46
54
  setFocusedIndex,
@@ -1,6 +1,10 @@
1
1
  /** Keep in sync with `PromptComposer.styl` max/min heights. */
2
2
  export declare const PROMPT_COMPOSER_MAX_HEIGHT_PX = 200;
3
3
  export declare const PROMPT_COMPOSER_MIN_HEIGHT_PX = 40;
4
+ export declare function safePromptComposerEditorText(editor: {
5
+ readonly isDestroyed?: boolean;
6
+ getText?: () => string;
7
+ } | null | undefined): string | null;
4
8
  export declare function promptComposerSafeEditorDom(editor: {
5
9
  readonly isDestroyed?: boolean;
6
10
  } | null | undefined): HTMLElement | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homecode/ui",
3
- "version": "5.3.0",
3
+ "version": "5.3.2",
4
4
  "description": "React UI components library",
5
5
  "scripts": {
6
6
  "tests": "jest",