BotTopicPreparation.tsx 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { pendingTopicPreparations } from "@/v4/topicPreparationRenderUnits.js";
  2. import { useContext, useMemo } from "react";
  3. import { LoaderCircleIcon } from "lucide-react";
  4. import type { BotTopicPreparation as Preparation, FeishuBotProvider } from "@zcode/shared";
  5. import type { ConversationRow } from "@zcode/shared/zcode-protocol-v4";
  6. import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip.js";
  7. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  8. import { DeliveryContext } from "@/v4/botGroupDeliveryContext.js";
  9. import { ConversationUserInputBubble } from "@/v4/ConversationUserInputBubble.js";
  10. const EMPTY_ROWS: readonly ConversationRow[] = [];
  11. const EMPTY_PREPARATIONS: readonly Preparation[] = [];
  12. export function useBotTopicPreparations(): readonly Preparation[] {
  13. const value = useContext(DeliveryContext);
  14. if (!value?.context?.group?.threadId || value.context.activeTaskId !== value.taskId)
  15. return EMPTY_PREPARATIONS;
  16. return value.context.group.preparation ?? EMPTY_PREPARATIONS;
  17. }
  18. export function BotTopicPreparationList({
  19. items,
  20. acceptedRows = EMPTY_ROWS,
  21. provider,
  22. }: {
  23. items: readonly Preparation[];
  24. acceptedRows?: readonly ConversationRow[];
  25. provider?: FeishuBotProvider;
  26. }) {
  27. const { intl } = useZCodeIntl();
  28. const pending = useMemo(
  29. () => pendingTopicPreparations(items, acceptedRows),
  30. [items, acceptedRows],
  31. );
  32. if (!pending.length) return null;
  33. return (
  34. <div className="flex flex-col gap-4" aria-live="polite">
  35. {pending.map((item) => (
  36. <div
  37. key={item.messageId}
  38. data-topic-preparation={item.messageId}
  39. className="flex min-w-0 flex-col items-end"
  40. >
  41. <div
  42. data-topic-preparation-source="true"
  43. className="mb-2 flex min-w-0 max-w-full items-center gap-1.5 text-ui-sm text-foreground-subtle"
  44. >
  45. <span className="min-w-0 truncate" title={item.senderName}>
  46. {provider
  47. ? `${provider === "lark" ? "Lark" : intl.formatMessage({ id: "bots.group.provider.feishu" })} · `
  48. : ""}
  49. {item.senderName}
  50. </span>
  51. {item.status !== "failed" ? (
  52. <Tooltip>
  53. <TooltipTrigger asChild>
  54. <span
  55. tabIndex={0}
  56. role="img"
  57. aria-label={intl.formatMessage({ id: `chat.topicPreparation.${item.status}` })}
  58. data-topic-preparation-loading="true"
  59. className="inline-flex size-4 shrink-0 items-center justify-center rounded-sm focus-visible:outline focus-visible:outline-ring"
  60. >
  61. <LoaderCircleIcon
  62. aria-hidden="true"
  63. className="size-3 animate-spin motion-reduce:animate-none"
  64. />
  65. </span>
  66. </TooltipTrigger>
  67. <TooltipContent>
  68. {intl.formatMessage({ id: `chat.topicPreparation.${item.status}` })}
  69. </TooltipContent>
  70. </Tooltip>
  71. ) : null}
  72. </div>
  73. <ConversationUserInputBubble
  74. text={item.text}
  75. contentParts={item.contentParts}
  76. rowId={item.messageId}
  77. />
  78. </div>
  79. ))}
  80. </div>
  81. );
  82. }
  83. export function BotTopicPreparation({ items }: { items: readonly Preparation[] }) {
  84. const value = useContext(DeliveryContext);
  85. if (!value?.context?.group?.threadId || value.context.activeTaskId !== value.taskId) return null;
  86. return <BotTopicPreparationList items={items} provider={value.provider} />;
  87. }