@yushaw/sanqian-chat 0.2.21 → 0.2.22

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.
@@ -7869,6 +7869,7 @@ var ChatInput = (0, import_react23.memo)(
7869
7869
  const [text, setText] = (0, import_react23.useState)("");
7870
7870
  const textareaRef = (0, import_react23.useRef)(null);
7871
7871
  const submitInFlightRef = (0, import_react23.useRef)(false);
7872
+ const inputRevisionRef = (0, import_react23.useRef)(0);
7872
7873
  const canSend = (text.trim().length > 0 || allowEmptySubmit) && !disabled && !isLoading;
7873
7874
  const showStopButton = isStreaming && !!onStop;
7874
7875
  const showSpinner = isLoading && !showStopButton;
@@ -7893,13 +7894,24 @@ var ChatInput = (0, import_react23.memo)(
7893
7894
  e?.preventDefault();
7894
7895
  if (!canSend) return;
7895
7896
  if (submitInFlightRef.current) return;
7897
+ const draft = text;
7898
+ const payload = draft.trim();
7899
+ const submitRevision = inputRevisionRef.current;
7896
7900
  submitInFlightRef.current = true;
7901
+ setText("");
7902
+ const restoreDraft = () => {
7903
+ setText((current) => {
7904
+ if (inputRevisionRef.current !== submitRevision) return current;
7905
+ return current.length === 0 ? draft : current;
7906
+ });
7907
+ };
7897
7908
  try {
7898
- const result = await onSend(text.trim());
7899
- if (result !== false) {
7900
- setText("");
7909
+ const result = await onSend(payload);
7910
+ if (result === false) {
7911
+ restoreDraft();
7901
7912
  }
7902
7913
  } catch {
7914
+ restoreDraft();
7903
7915
  } finally {
7904
7916
  submitInFlightRef.current = false;
7905
7917
  }
@@ -7945,7 +7957,10 @@ var ChatInput = (0, import_react23.memo)(
7945
7957
  {
7946
7958
  ref: textareaRef,
7947
7959
  value: text,
7948
- onChange: (e) => setText(e.target.value),
7960
+ onChange: (e) => {
7961
+ inputRevisionRef.current += 1;
7962
+ setText(e.target.value);
7963
+ },
7949
7964
  onKeyDown: handleKeyDown,
7950
7965
  placeholder,
7951
7966
  disabled,
@@ -7797,6 +7797,7 @@ var ChatInput = memo7(
7797
7797
  const [text, setText] = useState14("");
7798
7798
  const textareaRef = useRef9(null);
7799
7799
  const submitInFlightRef = useRef9(false);
7800
+ const inputRevisionRef = useRef9(0);
7800
7801
  const canSend = (text.trim().length > 0 || allowEmptySubmit) && !disabled && !isLoading;
7801
7802
  const showStopButton = isStreaming && !!onStop;
7802
7803
  const showSpinner = isLoading && !showStopButton;
@@ -7821,13 +7822,24 @@ var ChatInput = memo7(
7821
7822
  e?.preventDefault();
7822
7823
  if (!canSend) return;
7823
7824
  if (submitInFlightRef.current) return;
7825
+ const draft = text;
7826
+ const payload = draft.trim();
7827
+ const submitRevision = inputRevisionRef.current;
7824
7828
  submitInFlightRef.current = true;
7829
+ setText("");
7830
+ const restoreDraft = () => {
7831
+ setText((current) => {
7832
+ if (inputRevisionRef.current !== submitRevision) return current;
7833
+ return current.length === 0 ? draft : current;
7834
+ });
7835
+ };
7825
7836
  try {
7826
- const result = await onSend(text.trim());
7827
- if (result !== false) {
7828
- setText("");
7837
+ const result = await onSend(payload);
7838
+ if (result === false) {
7839
+ restoreDraft();
7829
7840
  }
7830
7841
  } catch {
7842
+ restoreDraft();
7831
7843
  } finally {
7832
7844
  submitInFlightRef.current = false;
7833
7845
  }
@@ -7873,7 +7885,10 @@ var ChatInput = memo7(
7873
7885
  {
7874
7886
  ref: textareaRef,
7875
7887
  value: text,
7876
- onChange: (e) => setText(e.target.value),
7888
+ onChange: (e) => {
7889
+ inputRevisionRef.current += 1;
7890
+ setText(e.target.value);
7891
+ },
7877
7892
  onKeyDown: handleKeyDown,
7878
7893
  placeholder,
7879
7894
  disabled,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yushaw/sanqian-chat",
3
- "version": "0.2.21",
3
+ "version": "0.2.22",
4
4
  "description": "Floating chat window SDK for Sanqian AI Assistant",
5
5
  "main": "./dist/main/index.js",
6
6
  "types": "./dist/main/index.d.ts",