{"version":3,"file":"AIUIEntrance.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/GenerativeUI/AIUIEntrance.tsx"],"sourcesContent":["import React, {\n  createContext,\n  type ReactNode,\n  useContext,\n  useEffect,\n  useMemo,\n  useState,\n} from 'react';\nimport clsx from 'clsx';\nimport { ThemeScopeProvider, useAIThemeScopeClassName } from '../themeScope';\n\n/**\n * Spaces out the parts of a reply's UI that come in together, so they come in one after another\n * rather than at once. The counterpart of `AIUICascade` in StreamChatAI.\n */\nexport class AIUICascade {\n  /** How long after the part before it a part comes in, in seconds. */\n  static readonly step = 0.1;\n  /** The longest a part waits for the parts before it, in seconds. */\n  static readonly longestWait = 0.4;\n\n  private next = 0;\n\n  /** How long a part that comes in at `now`, in seconds, waits for the parts before it. */\n  delay(\n    now = (typeof performance === 'undefined' ? Date.now() : performance.now()) / 1000,\n  ) {\n    const start = Math.min(Math.max(now, this.next), now + AIUICascade.longestWait);\n    this.next = start + AIUICascade.step;\n    return start - now;\n  }\n}\n\ntype AIUIStage = {\n  cascade: AIUICascade;\n  /** Whether what shows now comes in with the part around it, rather than on its own. */\n  isCarried: boolean;\n};\n\nconst AIUIStageContext = createContext<AIUIStage | undefined>(undefined);\n\n// How long a part waits for a frame before it shows anyway, in milliseconds.\nconst LONGEST_FRAME_WAIT = 50;\n\n// Calls back on the next frame, or a moment later where no frame comes: a page painting no\n// frames, such as one in the background or in some web views, still shows its UI.\nconst nextFrame = (callback: () => void) => {\n  let done = false;\n  const run = () => {\n    if (done) return;\n    done = true;\n    callback();\n  };\n  const frame =\n    typeof requestAnimationFrame === 'function' ? requestAnimationFrame(run) : undefined;\n  const timer = setTimeout(run, LONGEST_FRAME_WAIT);\n  return () => {\n    done = true;\n    if (frame !== undefined) cancelAnimationFrame(frame);\n    clearTimeout(timer);\n  };\n};\n\n// Shows its content, fading it in after the cascade's delay when it starts hidden.\nconst useEntrance = (startsHidden: boolean, cascade: AIUICascade | undefined) => {\n  const [entrance, setEntrance] = useState({ delay: 0, hasShown: !startsHidden });\n  useEffect(() => {\n    if (entrance.hasShown) return;\n    const delay = cascade?.delay() ?? 0;\n    // A frame later, so the hidden state is painted and the change to shown transitions.\n    return nextFrame(() => setEntrance({ delay, hasShown: true }));\n  }, [cascade, entrance.hasShown]);\n  return entrance;\n};\n\nconst EntranceBox = ({\n  children,\n  className,\n  delay,\n  hasShown,\n}: {\n  children: ReactNode;\n  delay: number;\n  hasShown: boolean;\n  className?: string;\n}) => (\n  <div\n    className={clsx(\n      'str-chat__ai-ui-entrance',\n      !hasShown && 'str-chat__ai-ui-entrance--hidden',\n      className,\n    )}\n    data-entrance={hasShown ? 'shown' : 'hidden'}\n    style={hasShown && delay > 0 ? { transitionDelay: `${delay}s` } : undefined}\n  >\n    {children}\n  </div>\n);\n\n/**\n * A reply's generated UI as a whole: it comes in when it first shows while the agent writes it,\n * and shows at once when it was written before, such as a reply from history.\n */\nexport const AIUIEntranceStage = ({\n  children,\n  className,\n  isStreaming,\n}: {\n  children: ReactNode;\n  /** Whether the agent is writing the UI as it first shows. */\n  isStreaming: boolean;\n  className?: string;\n}) => {\n  const [cascade] = useState(() => new AIUICascade());\n  // UI written before it showed shows whole, its parts with it.\n  const [isLive] = useState(isStreaming);\n  const { delay, hasShown } = useEntrance(isStreaming, cascade);\n  const stage = useMemo(\n    () => ({ cascade, isCarried: !isLive || !hasShown }),\n    [cascade, hasShown, isLive],\n  );\n  return (\n    <AIUIStageContext.Provider value={stage}>\n      <EntranceBox className={className} delay={delay} hasShown={hasShown}>\n        {children}\n      </EntranceBox>\n    </AIUIStageContext.Provider>\n  );\n};\n\n/**\n * What a part of a reply's generated UI needs to come in when it shows, for a renderer whose\n * part is its own element, which a wrapping box would take out of its layout: the props for that\n * element, and `scope`, to wrap what the part shows in, so what shows with it comes in with it.\n * The same entrance as `AIUIEntrance`'s.\n */\nexport const useAIUIEntrance = () => {\n  const stage = useContext(AIUIStageContext);\n  const { delay, hasShown } = useEntrance(!!stage && !stage.isCarried, stage?.cascade);\n  const inner = useMemo(\n    () => stage && { cascade: stage.cascade, isCarried: stage.isCarried || !hasShown },\n    [hasShown, stage],\n  );\n  return {\n    props: {\n      className: clsx(\n        'str-chat__ai-ui-entrance',\n        !hasShown && 'str-chat__ai-ui-entrance--hidden',\n      ),\n      'data-entrance': hasShown ? 'shown' : 'hidden',\n      style: hasShown && delay > 0 ? { transitionDelay: `${delay}s` } : undefined,\n    },\n    scope: (children: ReactNode) => (\n      <AIUIStageContext.Provider value={inner}>{children}</AIUIStageContext.Provider>\n    ),\n  };\n};\n\n/**\n * Brings a part of a reply's generated UI in when it shows, such as when the agent writes it:\n * the part fades in, rising into place and coming into focus, after the parts that came in just\n * before it. What shows together with the reply's UI, or with a part around it, comes in with\n * that, so a reply written before it showed shows at once. With reduced motion it only fades.\n *\n * Renderers give it to each part they place. Every app component has it.\n */\nexport const AIUIEntrance = ({\n  children,\n  className,\n}: {\n  children: ReactNode;\n  className?: string;\n}) => {\n  const themeScopeClassName = useAIThemeScopeClassName();\n  const { props, scope } = useAIUIEntrance();\n  return (\n    <ThemeScopeProvider>\n      <div {...props} className={clsx(themeScopeClassName, props.className, className)}>\n        {scope(children)}\n      </div>\n    </ThemeScopeProvider>\n  );\n};\n"],"mappings":";;;;;;;;;;AAeA,IAAa,cAAb,MAAa,YAAY;;CAEvB,OAAgB,OAAO;;CAEvB,OAAgB,cAAc;CAE9B,OAAe;;CAGf,MACE,OAAO,OAAO,gBAAgB,cAAc,KAAK,IAAI,IAAI,YAAY,IAAI,KAAK,KAC9E;EACA,MAAM,QAAQ,KAAK,IAAI,KAAK,IAAI,KAAK,KAAK,IAAI,GAAG,MAAM,YAAY,WAAW;EAC9E,KAAK,OAAO,QAAQ,YAAY;EAChC,OAAO,QAAQ;CACjB;AACF;AAQA,IAAM,mBAAmB,cAAqC,KAAA,CAAS;AAGvE,IAAM,qBAAqB;AAI3B,IAAM,aAAa,aAAyB;CAC1C,IAAI,OAAO;CACX,MAAM,YAAY;EAChB,IA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