@assistant-ui/react 0.5.9 → 0.5.11

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -1284,7 +1284,7 @@ var toContentPartStatus = (message, partIndex, part) => {
1284
1284
  return message.status;
1285
1285
  };
1286
1286
  var EMPTY_CONTENT = Object.freeze({ type: "text", text: "" });
1287
- var syncContentPart = ({ message }, useContentPart, partIndex) => {
1287
+ var getContentPartState = ({ message }, useContentPart, partIndex) => {
1288
1288
  let part = message.content[partIndex];
1289
1289
  if (!part) {
1290
1290
  if (message.content.length === 0 && partIndex === 0) {
@@ -1294,29 +1294,32 @@ var syncContentPart = ({ message }, useContentPart, partIndex) => {
1294
1294
  }
1295
1295
  }
1296
1296
  const status = toContentPartStatus(message, partIndex, part);
1297
- const currentState = useContentPart.getState();
1298
- if (currentState.part === part && currentState.status === status) return;
1299
- useContentPart.setState(
1300
- Object.freeze({
1301
- part,
1302
- status
1303
- })
1304
- );
1297
+ const currentState = useContentPart?.getState();
1298
+ if (currentState && currentState.part === part && currentState.status === status)
1299
+ return null;
1300
+ return Object.freeze({ part, status });
1305
1301
  };
1306
1302
  var useContentPartContext2 = (partIndex) => {
1307
1303
  const { useMessage } = useMessageContext();
1308
1304
  const [context] = (0, import_react34.useState)(() => {
1309
1305
  const useContentPart = (0, import_zustand10.create)(
1310
- () => ({})
1306
+ () => getContentPartState(useMessage.getState(), void 0, partIndex)
1311
1307
  );
1312
- syncContentPart(useMessage.getState(), useContentPart, partIndex);
1308
+ getContentPartState(useMessage.getState(), useContentPart, partIndex);
1313
1309
  return { useContentPart };
1314
1310
  });
1315
1311
  (0, import_react34.useEffect)(() => {
1316
- syncContentPart(useMessage.getState(), context.useContentPart, partIndex);
1317
- return useMessage.subscribe((message) => {
1318
- syncContentPart(message, context.useContentPart, partIndex);
1319
- });
1312
+ const syncContentPart = (message) => {
1313
+ const newState = getContentPartState(
1314
+ message,
1315
+ context.useContentPart,
1316
+ partIndex
1317
+ );
1318
+ if (!newState) return;
1319
+ context.useContentPart.setState(newState, true);
1320
+ };
1321
+ syncContentPart(useMessage.getState());
1322
+ return useMessage.subscribe(syncContentPart);
1320
1323
  }, [context, useMessage, partIndex]);
1321
1324
  return context;
1322
1325
  };
@@ -1913,16 +1916,15 @@ var import_jsx_runtime23 = require("react/jsx-runtime");
1913
1916
  var getIsLast = (messages, message) => {
1914
1917
  return messages[messages.length - 1]?.id === message.id;
1915
1918
  };
1916
- var syncMessage = (messages, getBranches, useMessage, messageIndex) => {
1919
+ var getMessageState = (messages, getBranches, useMessage, messageIndex) => {
1917
1920
  const parentId = messages[messageIndex - 1]?.id ?? null;
1918
1921
  const message = messages[messageIndex];
1919
- if (!message) return;
1920
1922
  const isLast = getIsLast(messages, message);
1921
1923
  const branches = getBranches(message.id);
1922
- const currentState = useMessage.getState();
1923
- if (currentState.message === message && currentState.parentId === parentId && currentState.branches === branches && currentState.isLast === isLast)
1924
- return;
1925
- useMessage.setState({
1924
+ const currentState = useMessage?.getState();
1925
+ if (currentState && currentState.message === message && currentState.parentId === parentId && currentState.branches === branches && currentState.isLast === isLast)
1926
+ return null;
1927
+ return Object.freeze({
1926
1928
  message,
1927
1929
  parentId,
1928
1930
  branches,
@@ -1932,7 +1934,14 @@ var syncMessage = (messages, getBranches, useMessage, messageIndex) => {
1932
1934
  var useMessageContext2 = (messageIndex) => {
1933
1935
  const { useThreadMessages, useThreadActions } = useThreadContext();
1934
1936
  const [context] = (0, import_react48.useState)(() => {
1935
- const useMessage = (0, import_zustand13.create)(() => ({}));
1937
+ const useMessage = (0, import_zustand13.create)(
1938
+ () => getMessageState(
1939
+ useThreadMessages.getState(),
1940
+ useThreadActions.getState().getBranches,
1941
+ void 0,
1942
+ messageIndex
1943
+ )
1944
+ );
1936
1945
  const useMessageUtils = makeMessageUtilsStore();
1937
1946
  const useEditComposer = makeEditComposerStore({
1938
1947
  onEdit: () => {
@@ -1960,23 +1969,24 @@ var useMessageContext2 = (messageIndex) => {
1960
1969
  });
1961
1970
  }
1962
1971
  });
1963
- syncMessage(
1964
- useThreadMessages.getState(),
1965
- useThreadActions.getState().getBranches,
1966
- useMessage,
1967
- messageIndex
1968
- );
1969
1972
  return { useMessage, useMessageUtils, useEditComposer };
1970
1973
  });
1971
1974
  (0, import_react48.useEffect)(() => {
1972
- return useThreadMessages.subscribe((thread) => {
1973
- syncMessage(
1975
+ const syncMessage = (thread) => {
1976
+ const newState = getMessageState(
1974
1977
  thread,
1975
1978
  useThreadActions.getState().getBranches,
1976
1979
  context.useMessage,
1977
1980
  messageIndex
1978
1981
  );
1979
- });
1982
+ if (!newState) return;
1983
+ context.useMessage.setState(
1984
+ newState,
1985
+ true
1986
+ );
1987
+ };
1988
+ syncMessage(useThreadMessages.getState());
1989
+ return useThreadMessages.subscribe(syncMessage);
1980
1990
  }, [useThreadMessages, useThreadActions, context, messageIndex]);
1981
1991
  return context;
1982
1992
  };