SessionPluginReferenceIconProvider.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { useMemo, type ReactNode } from "react";
  2. import { usePluginReferenceCatalog } from "@/hooks/usePluginReferenceCatalog.js";
  3. import { PluginReferenceIconProvider } from "@/v4/pluginReferenceIconContext.js";
  4. import { buildSessionPluginIconMap } from "@/v4/pluginReferenceIconProjection.js";
  5. interface SessionPluginReferenceIconProviderProps {
  6. children: ReactNode;
  7. enabled: boolean;
  8. remoteSessionId?: string | null;
  9. sessionId: string | null;
  10. workspaceIdentity?: string;
  11. workspacePath: string;
  12. }
  13. interface ActiveSessionPluginReferenceIconProviderProps extends Omit<
  14. SessionPluginReferenceIconProviderProps,
  15. "enabled" | "sessionId"
  16. > {
  17. sessionId: string;
  18. }
  19. function ActiveSessionPluginReferenceIconProvider({
  20. children,
  21. remoteSessionId,
  22. sessionId,
  23. workspaceIdentity,
  24. workspacePath,
  25. }: ActiveSessionPluginReferenceIconProviderProps) {
  26. const catalog = usePluginReferenceCatalog(workspacePath, workspaceIdentity, sessionId, true, {
  27. dedupeSessionRequest: true,
  28. preferredRemoteSessionId: remoteSessionId ?? undefined,
  29. // Timeline 图标是可选展示;Session 尚未就绪或 remote 暂不可用时安静回退 Cable。
  30. suppressErrorLog: true,
  31. });
  32. const iconByPluginId = useMemo(
  33. () => buildSessionPluginIconMap(catalog.authority, catalog.entries),
  34. [catalog.authority, catalog.entries],
  35. );
  36. const projection = useMemo(
  37. () => (catalog.authority === "session" ? { sessionId, iconByPluginId } : null),
  38. [catalog.authority, iconByPluginId, sessionId],
  39. );
  40. return <PluginReferenceIconProvider value={projection}>{children}</PluginReferenceIconProvider>;
  41. }
  42. /**
  43. * 已发送 Plugin chip 的惰性 Session-authority 图标边界。
  44. *
  45. * 把 catalog hook 直接挂在 SessionPane 后,即使 disabled 也会先解析
  46. * workspace services,导致所有无 Plugin 引用的会话产生额外依赖与请求。拆成子组件后,
  47. * 只有确实含 plugin:// 用户消息且 Session snapshot 就绪时才挂载数据 hook。
  48. */
  49. export function SessionPluginReferenceIconBoundary({
  50. children,
  51. enabled,
  52. remoteSessionId,
  53. sessionId,
  54. workspaceIdentity,
  55. workspacePath,
  56. }: SessionPluginReferenceIconProviderProps) {
  57. if (!enabled || !sessionId) {
  58. return <PluginReferenceIconProvider value={null}>{children}</PluginReferenceIconProvider>;
  59. }
  60. return (
  61. <ActiveSessionPluginReferenceIconProvider
  62. remoteSessionId={remoteSessionId}
  63. sessionId={sessionId}
  64. workspaceIdentity={workspaceIdentity}
  65. workspacePath={workspacePath}
  66. >
  67. {children}
  68. </ActiveSessionPluginReferenceIconProvider>
  69. );
  70. }