@assistant-ui/react 0.5.10 → 0.5.11

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -1192,7 +1192,7 @@ var toContentPartStatus = (message, partIndex, part) => {
1192
1192
  return message.status;
1193
1193
  };
1194
1194
  var EMPTY_CONTENT = Object.freeze({ type: "text", text: "" });
1195
- var syncContentPart = ({ message }, useContentPart, partIndex) => {
1195
+ var getContentPartState = ({ message }, useContentPart, partIndex) => {
1196
1196
  let part = message.content[partIndex];
1197
1197
  if (!part) {
1198
1198
  if (message.content.length === 0 && partIndex === 0) {
@@ -1202,29 +1202,32 @@ var syncContentPart = ({ message }, useContentPart, partIndex) => {
1202
1202
  }
1203
1203
  }
1204
1204
  const status = toContentPartStatus(message, partIndex, part);
1205
- const currentState = useContentPart.getState();
1206
- if (currentState.part === part && currentState.status === status) return;
1207
- useContentPart.setState(
1208
- Object.freeze({
1209
- part,
1210
- status
1211
- })
1212
- );
1205
+ const currentState = useContentPart?.getState();
1206
+ if (currentState && currentState.part === part && currentState.status === status)
1207
+ return null;
1208
+ return Object.freeze({ part, status });
1213
1209
  };
1214
1210
  var useContentPartContext2 = (partIndex) => {
1215
1211
  const { useMessage } = useMessageContext();
1216
1212
  const [context] = useState4(() => {
1217
1213
  const useContentPart = create10(
1218
- () => ({})
1214
+ () => getContentPartState(useMessage.getState(), void 0, partIndex)
1219
1215
  );
1220
- syncContentPart(useMessage.getState(), useContentPart, partIndex);
1216
+ getContentPartState(useMessage.getState(), useContentPart, partIndex);
1221
1217
  return { useContentPart };
1222
1218
  });
1223
1219
  useEffect6(() => {
1224
- syncContentPart(useMessage.getState(), context.useContentPart, partIndex);
1225
- return useMessage.subscribe((message) => {
1226
- syncContentPart(message, context.useContentPart, partIndex);
1227
- });
1220
+ const syncContentPart = (message) => {
1221
+ const newState = getContentPartState(
1222
+ message,
1223
+ context.useContentPart,
1224
+ partIndex
1225
+ );
1226
+ if (!newState) return;
1227
+ context.useContentPart.setState(newState, true);
1228
+ };
1229
+ syncContentPart(useMessage.getState());
1230
+ return useMessage.subscribe(syncContentPart);
1228
1231
  }, [context, useMessage, partIndex]);
1229
1232
  return context;
1230
1233
  };
@@ -1828,16 +1831,15 @@ import { jsx as jsx23 } from "react/jsx-runtime";
1828
1831
  var getIsLast = (messages, message) => {
1829
1832
  return messages[messages.length - 1]?.id === message.id;
1830
1833
  };
1831
- var syncMessage = (messages, getBranches, useMessage, messageIndex) => {
1834
+ var getMessageState = (messages, getBranches, useMessage, messageIndex) => {
1832
1835
  const parentId = messages[messageIndex - 1]?.id ?? null;
1833
1836
  const message = messages[messageIndex];
1834
- if (!message) return;
1835
1837
  const isLast = getIsLast(messages, message);
1836
1838
  const branches = getBranches(message.id);
1837
- const currentState = useMessage.getState();
1838
- if (currentState.message === message && currentState.parentId === parentId && currentState.branches === branches && currentState.isLast === isLast)
1839
- return;
1840
- useMessage.setState({
1839
+ const currentState = useMessage?.getState();
1840
+ if (currentState && currentState.message === message && currentState.parentId === parentId && currentState.branches === branches && currentState.isLast === isLast)
1841
+ return null;
1842
+ return Object.freeze({
1841
1843
  message,
1842
1844
  parentId,
1843
1845
  branches,
@@ -1847,7 +1849,14 @@ var syncMessage = (messages, getBranches, useMessage, messageIndex) => {
1847
1849
  var useMessageContext2 = (messageIndex) => {
1848
1850
  const { useThreadMessages, useThreadActions } = useThreadContext();
1849
1851
  const [context] = useState6(() => {
1850
- const useMessage = create13(() => ({}));
1852
+ const useMessage = create13(
1853
+ () => getMessageState(
1854
+ useThreadMessages.getState(),
1855
+ useThreadActions.getState().getBranches,
1856
+ void 0,
1857
+ messageIndex
1858
+ )
1859
+ );
1851
1860
  const useMessageUtils = makeMessageUtilsStore();
1852
1861
  const useEditComposer = makeEditComposerStore({
1853
1862
  onEdit: () => {
@@ -1875,23 +1884,24 @@ var useMessageContext2 = (messageIndex) => {
1875
1884
  });
1876
1885
  }
1877
1886
  });
1878
- syncMessage(
1879
- useThreadMessages.getState(),
1880
- useThreadActions.getState().getBranches,
1881
- useMessage,
1882
- messageIndex
1883
- );
1884
1887
  return { useMessage, useMessageUtils, useEditComposer };
1885
1888
  });
1886
1889
  useEffect10(() => {
1887
- return useThreadMessages.subscribe((thread) => {
1888
- syncMessage(
1890
+ const syncMessage = (thread) => {
1891
+ const newState = getMessageState(
1889
1892
  thread,
1890
1893
  useThreadActions.getState().getBranches,
1891
1894
  context.useMessage,
1892
1895
  messageIndex
1893
1896
  );
1894
- });
1897
+ if (!newState) return;
1898
+ context.useMessage.setState(
1899
+ newState,
1900
+ true
1901
+ );
1902
+ };
1903
+ syncMessage(useThreadMessages.getState());
1904
+ return useThreadMessages.subscribe(syncMessage);
1895
1905
  }, [useThreadMessages, useThreadActions, context, messageIndex]);
1896
1906
  return context;
1897
1907
  };