@iblai/web-containers 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/dist/cjs/_virtual/index2.js +2 -2
  2. package/dist/cjs/_virtual/index3.js +2 -2
  3. package/dist/cjs/components/canvas/binary-artifact-utils.js +261 -36
  4. package/dist/cjs/components/canvas/binary-artifact-utils.js.map +1 -1
  5. package/dist/cjs/components/canvas/binary-canvas-component.js +571 -53
  6. package/dist/cjs/components/canvas/binary-canvas-component.js.map +1 -1
  7. package/dist/cjs/components/canvas/canvas-version-menu.js +41 -0
  8. package/dist/cjs/components/canvas/canvas-version-menu.js.map +1 -0
  9. package/dist/cjs/components/canvas/canvas-view.js +25 -37
  10. package/dist/cjs/components/canvas/canvas-view.js.map +1 -1
  11. package/dist/cjs/components/canvas/code-view.js +143 -0
  12. package/dist/cjs/components/canvas/code-view.js.map +1 -0
  13. package/dist/cjs/components/canvas/copy-text-button.js +26 -0
  14. package/dist/cjs/components/canvas/copy-text-button.js.map +1 -0
  15. package/dist/cjs/components/canvas/docx-preview.js +287 -0
  16. package/dist/cjs/components/canvas/docx-preview.js.map +1 -0
  17. package/dist/cjs/components/canvas/html-preview.js +503 -0
  18. package/dist/cjs/components/canvas/html-preview.js.map +1 -0
  19. package/dist/cjs/components/canvas/office-file-limits.js +302 -0
  20. package/dist/cjs/components/canvas/office-file-limits.js.map +1 -0
  21. package/dist/cjs/components/canvas/pdf-preview.js +459 -0
  22. package/dist/cjs/components/canvas/pdf-preview.js.map +1 -0
  23. package/dist/cjs/components/canvas/session-canvas-cards.js +62 -15
  24. package/dist/cjs/components/canvas/session-canvas-cards.js.map +1 -1
  25. package/dist/cjs/components/canvas/session-canvas-placement.js +1 -0
  26. package/dist/cjs/components/canvas/session-canvas-placement.js.map +1 -1
  27. package/dist/cjs/components/canvas/text-file-canvas.js +260 -0
  28. package/dist/cjs/components/canvas/text-file-canvas.js.map +1 -0
  29. package/dist/cjs/components/canvas/text-file-kinds.js +329 -0
  30. package/dist/cjs/components/canvas/text-file-kinds.js.map +1 -0
  31. package/dist/cjs/components/canvas/use-artifact-versions.js +136 -0
  32. package/dist/cjs/components/canvas/use-artifact-versions.js.map +1 -0
  33. package/dist/cjs/components/canvas/xlsx-preview.js +139 -0
  34. package/dist/cjs/components/canvas/xlsx-preview.js.map +1 -0
  35. package/dist/cjs/components/chat/chat-export.js.map +1 -1
  36. package/dist/cjs/components/chat/chat-host-context.js.map +1 -1
  37. package/dist/cjs/components/chat/chat-messages/canvas-message-preview.js +9 -4
  38. package/dist/cjs/components/chat/chat-messages/canvas-message-preview.js.map +1 -1
  39. package/dist/cjs/components/chat/chat-messages/message-preview.js +85 -55
  40. package/dist/cjs/components/chat/chat-messages/message-preview.js.map +1 -1
  41. package/dist/cjs/components/chat/index.js +212 -46
  42. package/dist/cjs/components/chat/index.js.map +1 -1
  43. package/dist/cjs/components/modals/user-profile-modal.js +5 -0
  44. package/dist/cjs/components/modals/user-profile-modal.js.map +1 -1
  45. package/dist/cjs/components/platform-sidebar/index.js +6 -0
  46. package/dist/cjs/components/platform-sidebar/index.js.map +1 -1
  47. package/dist/cjs/hooks/use-iframe-message-handler.js +8 -0
  48. package/dist/cjs/hooks/use-iframe-message-handler.js.map +1 -1
  49. package/dist/cjs/i18n/messages/en.json.js +26 -14
  50. package/dist/cjs/i18n/messages/en.json.js.map +1 -1
  51. package/dist/cjs/i18n/messages/es.json.js +26 -14
  52. package/dist/cjs/i18n/messages/es.json.js.map +1 -1
  53. package/dist/cjs/i18n/messages/fr.json.js +26 -14
  54. package/dist/cjs/i18n/messages/fr.json.js.map +1 -1
  55. package/dist/cjs/i18n/messages/zh.json.js +26 -14
  56. package/dist/cjs/i18n/messages/zh.json.js.map +1 -1
  57. package/dist/cjs/index.js +7 -0
  58. package/dist/cjs/index.js.map +1 -1
  59. package/dist/cjs/lib/html-preview-messages.js +405 -0
  60. package/dist/cjs/lib/html-preview-messages.js.map +1 -0
  61. package/dist/cjs/lib/html-preview-page-prelude.js +449 -0
  62. package/dist/cjs/lib/html-preview-page-prelude.js.map +1 -0
  63. package/dist/cjs/node_modules/.pnpm/css-mediaquery@0.1.2/node_modules/css-mediaquery/index.js +1 -1
  64. package/dist/cjs/node_modules/.pnpm/matchmediaquery@0.4.2/node_modules/matchmediaquery/index.js +1 -1
  65. package/dist/cjs/node_modules/.pnpm/pdfjs-dist@4.8.69/node_modules/pdfjs-dist/build/pdf.js +72 -36
  66. package/dist/cjs/node_modules/.pnpm/pdfjs-dist@4.8.69/node_modules/pdfjs-dist/build/pdf.js.map +1 -1
  67. package/dist/cjs/node_modules/.pnpm/prop-types@15.8.1/node_modules/prop-types/index.js +1 -1
  68. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Outline.js +113 -0
  69. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Outline.js.map +1 -0
  70. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/OutlineContext.js +8 -0
  71. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/OutlineContext.js.map +1 -0
  72. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/OutlineItem.js +82 -0
  73. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/OutlineItem.js.map +1 -0
  74. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Ref.js +18 -0
  75. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Ref.js.map +1 -0
  76. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Thumbnail.js +54 -0
  77. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Thumbnail.js.map +1 -0
  78. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/index.js +16 -0
  79. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/index.js.map +1 -1
  80. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/shared/hooks/useCachedValue.js +20 -0
  81. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/shared/hooks/useCachedValue.js.map +1 -0
  82. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/shared/hooks/useOutlineContext.js +11 -0
  83. package/dist/cjs/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/shared/hooks/useOutlineContext.js.map +1 -0
  84. package/dist/cjs/web-containers/tsconfig.build.tsbuildinfo +1 -1
  85. package/dist/esm/components/canvas/binary-artifact-utils.js +248 -36
  86. package/dist/esm/components/canvas/binary-artifact-utils.js.map +1 -1
  87. package/dist/esm/components/canvas/binary-canvas-component.js +574 -56
  88. package/dist/esm/components/canvas/binary-canvas-component.js.map +1 -1
  89. package/dist/esm/components/canvas/canvas-version-menu.js +38 -0
  90. package/dist/esm/components/canvas/canvas-version-menu.js.map +1 -0
  91. package/dist/esm/components/canvas/canvas-view.js +26 -38
  92. package/dist/esm/components/canvas/canvas-view.js.map +1 -1
  93. package/dist/esm/components/canvas/code-view.js +140 -0
  94. package/dist/esm/components/canvas/code-view.js.map +1 -0
  95. package/dist/esm/components/canvas/copy-text-button.js +24 -0
  96. package/dist/esm/components/canvas/copy-text-button.js.map +1 -0
  97. package/dist/esm/components/canvas/docx-preview.js +285 -0
  98. package/dist/esm/components/canvas/docx-preview.js.map +1 -0
  99. package/dist/esm/components/canvas/html-preview.js +501 -0
  100. package/dist/esm/components/canvas/html-preview.js.map +1 -0
  101. package/dist/esm/components/canvas/office-file-limits.js +296 -0
  102. package/dist/esm/components/canvas/office-file-limits.js.map +1 -0
  103. package/dist/esm/components/canvas/pdf-preview.js +456 -0
  104. package/dist/esm/components/canvas/pdf-preview.js.map +1 -0
  105. package/dist/esm/components/canvas/session-canvas-cards.js +63 -16
  106. package/dist/esm/components/canvas/session-canvas-cards.js.map +1 -1
  107. package/dist/esm/components/canvas/session-canvas-placement.js +1 -0
  108. package/dist/esm/components/canvas/session-canvas-placement.js.map +1 -1
  109. package/dist/esm/components/canvas/text-file-canvas.js +258 -0
  110. package/dist/esm/components/canvas/text-file-canvas.js.map +1 -0
  111. package/dist/esm/components/canvas/text-file-kinds.js +322 -0
  112. package/dist/esm/components/canvas/text-file-kinds.js.map +1 -0
  113. package/dist/esm/components/canvas/use-artifact-versions.js +134 -0
  114. package/dist/esm/components/canvas/use-artifact-versions.js.map +1 -0
  115. package/dist/esm/components/canvas/xlsx-preview.js +137 -0
  116. package/dist/esm/components/canvas/xlsx-preview.js.map +1 -0
  117. package/dist/esm/components/chat/chat-export.js.map +1 -1
  118. package/dist/esm/components/chat/chat-host-context.js.map +1 -1
  119. package/dist/esm/components/chat/chat-messages/canvas-message-preview.js +10 -5
  120. package/dist/esm/components/chat/chat-messages/canvas-message-preview.js.map +1 -1
  121. package/dist/esm/components/chat/chat-messages/message-preview.js +85 -55
  122. package/dist/esm/components/chat/chat-messages/message-preview.js.map +1 -1
  123. package/dist/esm/components/chat/index.js +212 -46
  124. package/dist/esm/components/chat/index.js.map +1 -1
  125. package/dist/esm/components/modals/user-profile-modal.js +5 -0
  126. package/dist/esm/components/modals/user-profile-modal.js.map +1 -1
  127. package/dist/esm/components/platform-sidebar/index.js +6 -0
  128. package/dist/esm/components/platform-sidebar/index.js.map +1 -1
  129. package/dist/esm/hooks/use-iframe-message-handler.js +8 -0
  130. package/dist/esm/hooks/use-iframe-message-handler.js.map +1 -1
  131. package/dist/esm/i18n/messages/en.json.js +27 -14
  132. package/dist/esm/i18n/messages/en.json.js.map +1 -1
  133. package/dist/esm/i18n/messages/es.json.js +27 -14
  134. package/dist/esm/i18n/messages/es.json.js.map +1 -1
  135. package/dist/esm/i18n/messages/fr.json.js +27 -14
  136. package/dist/esm/i18n/messages/fr.json.js.map +1 -1
  137. package/dist/esm/i18n/messages/zh.json.js +27 -14
  138. package/dist/esm/i18n/messages/zh.json.js.map +1 -1
  139. package/dist/esm/index.js +2 -0
  140. package/dist/esm/index.js.map +1 -1
  141. package/dist/esm/lib/html-preview-messages.js +393 -0
  142. package/dist/esm/lib/html-preview-messages.js.map +1 -0
  143. package/dist/esm/lib/html-preview-page-prelude.js +444 -0
  144. package/dist/esm/lib/html-preview-page-prelude.js.map +1 -0
  145. package/dist/esm/node_modules/.pnpm/pdfjs-dist@4.8.69/node_modules/pdfjs-dist/build/pdf.js +37 -37
  146. package/dist/esm/node_modules/.pnpm/pdfjs-dist@4.8.69/node_modules/pdfjs-dist/build/pdf.js.map +1 -1
  147. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Outline.js +111 -0
  148. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Outline.js.map +1 -0
  149. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/OutlineContext.js +6 -0
  150. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/OutlineContext.js.map +1 -0
  151. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/OutlineItem.js +80 -0
  152. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/OutlineItem.js.map +1 -0
  153. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Ref.js +16 -0
  154. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Ref.js.map +1 -0
  155. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Thumbnail.js +52 -0
  156. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/Thumbnail.js.map +1 -0
  157. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/index.js +8 -0
  158. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/index.js.map +1 -1
  159. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/shared/hooks/useCachedValue.js +18 -0
  160. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/shared/hooks/useCachedValue.js.map +1 -0
  161. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/shared/hooks/useOutlineContext.js +9 -0
  162. package/dist/esm/node_modules/.pnpm/react-pdf@9.2.1_@types_react@19.1.17_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-pdf/dist/esm/shared/hooks/useOutlineContext.js.map +1 -0
  163. package/dist/esm/web-containers/tsconfig.build.tsbuildinfo +1 -1
  164. package/dist/index.d.ts +90 -7
  165. package/dist/next/index.d.ts +9 -6
  166. package/dist/package.json +4 -1
  167. package/package.json +6 -3
  168. package/dist/cjs/components/chat/chat-binary-artifact-utils.js +0 -110
  169. package/dist/cjs/components/chat/chat-binary-artifact-utils.js.map +0 -1
  170. package/dist/esm/components/chat/chat-binary-artifact-utils.js +0 -105
  171. package/dist/esm/components/chat/chat-binary-artifact-utils.js.map +0 -1
@@ -5,69 +5,459 @@ var React = require('react');
5
5
  var context = require('../../i18n/context.js');
6
6
  require('../../packages/i18n/dist/index.esm.js');
7
7
  var dataLayer = require('@iblai/data-layer');
8
+ var webUtils = require('@iblai/web-utils');
9
+ var utils = require('../../lib/utils.js');
8
10
  var button = require('../chat/ui/button.js');
11
+ var tabs = require('../ui/tabs.js');
12
+ var chatHostContext = require('../chat/chat-host-context.js');
9
13
  var canvasExportHandlers = require('./canvas-export-handlers.js');
10
14
  var binaryArtifactUtils = require('./binary-artifact-utils.js');
15
+ var canvasVersionMenu = require('./canvas-version-menu.js');
16
+ var codeView = require('./code-view.js');
17
+ var copyTextButton = require('./copy-text-button.js');
18
+ var docxPreview = require('./docx-preview.js');
19
+ var htmlPreview = require('./html-preview.js');
20
+ var pdfPreview = require('./pdf-preview.js');
21
+ var textFileKinds = require('./text-file-kinds.js');
22
+ var useArtifactVersions = require('./use-artifact-versions.js');
23
+ var xlsxPreview = require('./xlsx-preview.js');
11
24
  var fileArchive = require('../../node_modules/.pnpm/lucide-react@0.528.0_react@19.1.0/node_modules/lucide-react/dist/esm/icons/file-archive.js');
12
25
  var download = require('../../node_modules/.pnpm/lucide-react@0.528.0_react@19.1.0/node_modules/lucide-react/dist/esm/icons/download.js');
13
26
  var x = require('../../node_modules/.pnpm/lucide-react@0.528.0_react@19.1.0/node_modules/lucide-react/dist/esm/icons/x.js');
14
27
 
28
+ // An html file is held and saved as bytes of no type, whether it came as
29
+ // text, inline or from a link. Typed as html, a blob: address of it would be
30
+ // a page of this app's own origin for anything that opened it.
31
+ const HTML_FILE_BLOB_TYPE = 'application/octet-stream';
32
+ const HTML_MIME_TYPE = /^\s*(?:text\/html|application\/xhtml\+xml)\b/i;
33
+ // The same for bytes of any name that a server or the backend calls html.
34
+ const neverHtml = (mimeType) => mimeType && HTML_MIME_TYPE.test(mimeType) ? HTML_FILE_BLOB_TYPE : mimeType;
35
+ // What a browser runs as a page when it is sent to the file's link. Opened
36
+ // away, script in such a file would run for the person who pressed Export, in
37
+ // a tab or in the system browser, so its link is never opened: the file is
38
+ // saved from its bytes or not at all.
39
+ const PAGE_EXTENSIONS = new Set([
40
+ 'html',
41
+ 'htm',
42
+ 'xhtml',
43
+ 'xht',
44
+ 'shtml',
45
+ 'svg',
46
+ 'svgz',
47
+ 'xml',
48
+ 'xsl',
49
+ 'xslt',
50
+ 'mht',
51
+ 'mhtml',
52
+ ]);
53
+ const PAGE_MIME_TYPES = new Set([
54
+ 'text/html',
55
+ 'application/xhtml+xml',
56
+ 'image/svg+xml',
57
+ 'application/xml',
58
+ 'text/xml',
59
+ 'application/xslt+xml',
60
+ ]);
61
+ const suffixOf = (name) => { var _a; return (_a = /\.([a-z0-9]+)$/.exec((name !== null && name !== void 0 ? name : '').trim().toLowerCase())) === null || _a === void 0 ? void 0 : _a[1]; };
62
+ // Storage can pick the type it answers with from the name the file is stored under.
63
+ const linkSuffix = (link) => {
64
+ try {
65
+ return suffixOf(decodeURIComponent(new URL(link, 'https://relative.invalid').pathname));
66
+ }
67
+ catch (_a) {
68
+ return undefined;
69
+ }
70
+ };
71
+ // The sub-tabs of the agent settings (Settings, Privacy, MCP) look like this.
72
+ const VIEW_TABS_LIST_CLASS = 'inline-flex h-auto max-w-full justify-start overflow-x-auto rounded-md bg-gray-100 p-1';
73
+ const VIEW_TAB_TRIGGER_CLASS = 'px-3 py-1.5 text-sm font-medium text-gray-700 data-[state=active]:bg-white data-[state=active]:text-gray-900';
74
+ const BODY_CLASS = 'flex min-h-0 flex-1 flex-col overflow-hidden bg-gray-50';
75
+ /** The viewer's body: the panel of the selected tab when there are tabs. */
76
+ function ViewBody({ tabbed, tab, children, }) {
77
+ if (!tabbed)
78
+ return jsxRuntime.jsx("div", { className: BODY_CLASS, children: children });
79
+ return (jsxRuntime.jsx(tabs.TabsContent, { value: tab, forceMount: true, className: utils.cn(BODY_CLASS, 'mt-0 focus-visible:ring-inset'), children: children }));
80
+ }
81
+ const opensAsPage = (info) => [info.extension, suffixOf(info.title), linkSuffix(info.link)].some((suffix) => suffix !== undefined && PAGE_EXTENSIONS.has(suffix)) ||
82
+ info.mimeTypes.some((mimeType) => PAGE_MIME_TYPES.has((mimeType !== null && mimeType !== void 0 ? mimeType : '').split(';')[0].trim().toLowerCase()));
83
+ // A version row is saved data, and not always the string its type promises.
84
+ const textOf = (value) => (typeof value === 'string' ? value : '');
85
+ const describeArtifact = (artifact, dmUrl) => ({
86
+ source: binaryArtifactUtils.resolveBinarySource(artifact, dmUrl),
87
+ title: artifact.title,
88
+ extension: artifact.file_extension,
89
+ mimeType: artifact.mime_type,
90
+ });
91
+ const readDetail = async ({ fetchArtifact, artifactId, org, userId, dmUrl, }) => describeArtifact((await fetchArtifact({ id: artifactId, org, userId }).unwrap()), dmUrl);
15
92
  /**
16
- * Read-only canvas for binary artifacts (pdf, images, …). Unlike the
17
- * rich-text canvas there is no editing of any kind: no formatting toolbar,
18
- * no highlight-to-edit, no AI rewrite controls, no version editing. The
19
- * only actions are viewing (when the browser can render the file) and a
20
- * plain export of the original bytes.
21
- *
22
- * The bytes are served base64-encoded by the artifact detail endpoint only
23
- * (`binary_content` is nulled on every embedded/streamed payload), so they
24
- * are fetched here on mount.
93
+ * The file's link as a shared chat holds it now. The chat is read a page at
94
+ * a time from its newest messages, as the chat itself reads it, until a
95
+ * message names the file.
96
+ */
97
+ const readSharedSessionLink = async ({ readSharedSession, artifactId, org, sessionId, }) => {
98
+ if (!readSharedSession || !sessionId)
99
+ return null;
100
+ for (let page = 1;; page += 1) {
101
+ const data = await readSharedSession({
102
+ org,
103
+ sessionId,
104
+ page,
105
+ pageSize: dataLayer.CHAT_HISTORY_PAGE_SIZE,
106
+ }).unwrap();
107
+ const url = binaryArtifactUtils.findBinaryUrlInMessages(data === null || data === void 0 ? void 0 : data.results, artifactId);
108
+ if (url || !(data === null || data === void 0 ? void 0 : data.next))
109
+ return url;
110
+ }
111
+ };
112
+ /**
113
+ * A new link for a file whose link ran out or was refused. A shared chat is
114
+ * asked first: it is the only source its viewer is sure to be allowed. Of the
115
+ * artifact endpoints, the session's list carries the link without the file,
116
+ * so it comes before the detail endpoint, which still sends the whole file
117
+ * inline beside the link.
25
118
  */
26
- function BinaryCanvasComponent({ title, onClose, artifactId, org, userId, fileExtension, mimeType, }) {
119
+ const renewSource = async (lookup) => {
27
120
  var _a;
121
+ const { listArtifacts, artifactId, org, userId, sessionId, dmUrl } = lookup;
122
+ const shared = binaryArtifactUtils.resolveBinarySource({ binary_url: await readSharedSessionLink(lookup).catch(() => null) }, dmUrl);
123
+ if (shared.kind === 'url')
124
+ return { source: shared };
125
+ if (sessionId) {
126
+ try {
127
+ const page = await listArtifacts({
128
+ org,
129
+ userId,
130
+ params: { session_id: sessionId },
131
+ }).unwrap();
132
+ const row = ((_a = page === null || page === void 0 ? void 0 : page.results) !== null && _a !== void 0 ? _a : []).find((item) => item.id === artifactId);
133
+ if (row === null || row === void 0 ? void 0 : row.binary_url)
134
+ return describeArtifact(row, dmUrl);
135
+ }
136
+ catch (_b) {
137
+ // The detail endpoint below is the other way to a link.
138
+ }
139
+ }
140
+ return readDetail(lookup);
141
+ };
142
+ /**
143
+ * Reads a file link. No cookies and no headers: the link carries its own
144
+ * permission, and storage refuses a request that also brings a sign-in header.
145
+ * The error of a failed read is dropped, never logged: it can name the link.
146
+ */
147
+ const fetchLinkBlob = async (url) => {
148
+ try {
149
+ const response = await fetch(url, { credentials: 'omit' });
150
+ if (!response.ok)
151
+ return 'refused';
152
+ return { blob: await response.blob() };
153
+ }
154
+ catch (_a) {
155
+ return 'unreachable';
156
+ }
157
+ };
158
+ // The desktop app's own `fetch` keeps a copy on disk of whatever it reads from
159
+ // storage, under the link it was read from, and logs that link. So there a
160
+ // storage link is shown, played or opened, and never read.
161
+ const mayReadLink = (url, dmUrl) => !webUtils.isTauri() || binaryArtifactUtils.isDmHostedUrl(url, dmUrl);
162
+ const withMimeType = (blob, mimeType) => {
163
+ const type = neverHtml(mimeType || blob.type);
164
+ return type && blob.type !== type ? blob.slice(0, blob.size, type) : blob;
165
+ };
166
+ // `openExternalUrl` replaces the current page on the web, which would take
167
+ // the person out of the chat the file belongs to.
168
+ const openLinkAway = async (url) => {
169
+ if (webUtils.isTauri()) {
170
+ await webUtils.openExternalUrl(url);
171
+ return;
172
+ }
173
+ window.open(url, '_blank', 'noopener,noreferrer');
174
+ };
175
+ const LoadingSpinner = () => (jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-center", "data-testid": "binary-canvas-loading", children: jsxRuntime.jsx("div", { className: "h-10 w-10 animate-spin rounded-full border-4 border-blue-500 border-t-transparent" }) }));
176
+ /**
177
+ * Read-only canvas for binary artifacts (pdf, images, video, sound, Word,
178
+ * Excel, …). Unlike the rich-text canvas there is no editing of any kind: no
179
+ * formatting toolbar, no highlight-to-edit, no AI rewrite controls, no
180
+ * version editing. The only actions are viewing (when the file can be shown
181
+ * here) and a plain export of the original bytes.
182
+ *
183
+ * The file comes from its link (`binary_url`): the one the caller holds, or
184
+ * a new one when that has run out. A backend without links still sends the
185
+ * file inline on the detail endpoint. An svg and an html file are text
186
+ * artifacts: their text is the file, given by the caller (`content`) or read
187
+ * from the detail endpoint.
188
+ */
189
+ function BinaryCanvasComponent({ title, onClose, artifactId, org, userId, fileExtension, mimeType, binaryUrl: binaryUrlGiven, content: contentGiven, sessionId, versionUnavailable: versionUnavailableGiven, sharedSession, versionNumber, readOnly, }) {
190
+ var _a, _b, _c, _d;
28
191
  const t = context.useT();
192
+ const { config } = chatHostContext.useChatHost();
193
+ const dmUrl = ((_a = config === null || config === void 0 ? void 0 : config.dmUrl) === null || _a === void 0 ? void 0 : _a.call(config)) || dataLayer.getDmBaseUrl();
29
194
  const [fetchArtifact] = dataLayer.useLazyGetArtifactQuery();
30
- const [blob, setBlob] = React.useState(null);
195
+ const [listArtifacts] = dataLayer.useLazyListArtifactsQuery();
196
+ const [readSharedSession] = dataLayer.useLazyGetSharedSessionIdQuery();
197
+ const [file, setFile] = React.useState(null);
31
198
  const [resolvedTitle, setResolvedTitle] = React.useState(title);
32
199
  const [resolvedExtension, setResolvedExtension] = React.useState(fileExtension);
33
200
  const [loadState, setLoadState] = React.useState('loading');
34
201
  // The file arrived but can't be displayed (malformed LLM output, render
35
202
  // failure) — export still works, so this is softer than loadState 'error'.
36
203
  const [previewFailed, setPreviewFailed] = React.useState(false);
204
+ // Why a Word or Excel file was not drawn, which picks the sentence shown.
205
+ const [tooLargeToPreview, setTooLargeToPreview] = React.useState(false);
206
+ // An html file that cannot be run here (no preview page, the desktop app,
207
+ // no answer): its text is shown or Retry is offered, and Export still saves it.
208
+ const [pageUnavailable, setPageUnavailable] = React.useState(null);
209
+ // A pdf whose bytes are in hand but that pdf.js cannot draw here is shown in a frame instead.
210
+ const [pdfInFrame, setPdfInFrame] = React.useState(false);
211
+ const [tab, setTab] = React.useState('preview');
212
+ const pageSerialRef = React.useRef(0);
37
213
  const [reloadToken, setReloadToken] = React.useState(0);
214
+ // The `binaryUrl` whose picture, video or sound would not load: the next load goes past it.
215
+ const [staleLink, setStaleLink] = React.useState(null);
216
+ const renewedForRef = React.useRef(null);
217
+ const exportingRef = React.useRef(false);
218
+ // Export could neither read the file nor save it.
219
+ const [saveFailed, setSaveFailed] = React.useState(false);
220
+ // Once the person has stepped through the versions, the list says which
221
+ // one is on screen; until then the caller does.
222
+ const [stepped, setStepped] = React.useState(false);
223
+ // The latest file arrived as bytes or with a link, whatever its name says.
224
+ const [cameAsBytes, setCameAsBytes] = React.useState(false);
225
+ // A host that keeps this viewer mounted and hands it another file, or
226
+ // another version of this one, decides what is on screen again. Object.is:
227
+ // an id that is NaN never equals itself, and this would then never settle.
228
+ const [opened, setOpened] = React.useState({ artifactId, versionNumber });
229
+ const sameArtifact = Object.is(opened.artifactId, artifactId);
230
+ if (!sameArtifact || !Object.is(opened.versionNumber, versionNumber)) {
231
+ setOpened({ artifactId, versionNumber });
232
+ setStepped(false);
233
+ if (!sameArtifact) {
234
+ setCameAsBytes(false);
235
+ setTab('preview');
236
+ }
237
+ }
238
+ const versions = useArtifactVersions.useArtifactVersions({
239
+ artifactId,
240
+ org,
241
+ userId,
242
+ initialVersionNumber: versionNumber,
243
+ enabled: !sharedSession,
244
+ });
245
+ const earlier = versions.isViewingCurrent ? null : versions.viewing;
246
+ // A file that is text on the wire keeps every version's text; any other
247
+ // file keeps only its latest bytes. The name alone does not say which: a
248
+ // caller that calls an earlier version unavailable holds a file of bytes.
249
+ const keepsEveryVersion = !versionUnavailableGiven &&
250
+ !cameAsBytes &&
251
+ binaryArtifactUtils.isTextViewerFile({
252
+ fileExtension: binaryArtifactUtils.resolveEffectiveFileExtension(fileExtension, title),
253
+ binaryUrl: binaryUrlGiven,
254
+ });
255
+ const steppedVersions = React.useMemo(() => ({
256
+ ...versions,
257
+ goPrevious: () => {
258
+ setStepped(true);
259
+ versions.goPrevious();
260
+ },
261
+ goNext: () => {
262
+ setStepped(true);
263
+ versions.goNext();
264
+ },
265
+ backToLatest: () => {
266
+ setStepped(true);
267
+ versions.backToLatest();
268
+ },
269
+ restore: async () => {
270
+ setStepped(true);
271
+ return versions.restore();
272
+ },
273
+ }), [versions]);
274
+ const versionUnavailable = stepped
275
+ ? earlier !== null && !keepsEveryVersion
276
+ : Boolean(versionUnavailableGiven);
277
+ // An earlier version's text comes from the list once the person has stepped
278
+ // to it, and also when the caller opened it holding no text (a version
279
+ // saved as a partial update): reading the file would show the latest one
280
+ // under the earlier-version bar. Empty is then the whole file, not "none".
281
+ const earlierText = keepsEveryVersion && earlier !== null && (stepped || !contentGiven)
282
+ ? textOf(earlier.content)
283
+ : null;
284
+ const latestText = stepped
285
+ ? keepsEveryVersion && textOf((_b = versions.viewing) === null || _b === void 0 ? void 0 : _b.content)
286
+ : contentGiven;
287
+ // null: no text is held, so the file is read.
288
+ const heldText = earlierText !== null && earlierText !== void 0 ? earlierText : (latestText || null);
289
+ // The caller's link is the latest file's, or none for an earlier version.
290
+ const binaryUrl = (stepped && !keepsEveryVersion ? undefined : binaryUrlGiven) || undefined;
291
+ const lookup = React.useMemo(() => artifactId && org && userId
292
+ ? {
293
+ artifactId,
294
+ org,
295
+ userId,
296
+ sessionId,
297
+ dmUrl,
298
+ fetchArtifact,
299
+ listArtifacts,
300
+ readSharedSession: sharedSession ? readSharedSession : undefined,
301
+ }
302
+ : null, [
303
+ artifactId,
304
+ org,
305
+ userId,
306
+ sessionId,
307
+ dmUrl,
308
+ fetchArtifact,
309
+ listArtifacts,
310
+ sharedSession,
311
+ readSharedSession,
312
+ ]);
313
+ // One automatic renewal per link the caller gave. A host that feeds the
314
+ // renewed link back through `binaryUrl` would otherwise renew without end.
315
+ const claimRenewal = () => {
316
+ const given = binaryUrl !== null && binaryUrl !== void 0 ? binaryUrl : '';
317
+ if (renewedForRef.current !== null && renewedForRef.current !== given)
318
+ return false;
319
+ renewedForRef.current = given;
320
+ return true;
321
+ };
38
322
  React.useEffect(() => {
323
+ setSaveFailed(false);
324
+ if (versionUnavailable)
325
+ return;
39
326
  let cancelled = false;
40
- const load = async () => {
41
- if (!artifactId || !org || !userId) {
42
- setLoadState('error');
43
- return;
327
+ const present = async (found, fromProp) => {
328
+ var _a, _b;
329
+ const extension = found.extension || fileExtension || undefined;
330
+ const shownTitle = found.title || title;
331
+ const isHtml = binaryArtifactUtils.isHtmlExtension(binaryArtifactUtils.resolveEffectiveFileExtension(extension, shownTitle));
332
+ const type = isHtml
333
+ ? HTML_FILE_BLOB_TYPE
334
+ : neverHtml(binaryArtifactUtils.resolveBinaryMimeType(extension, (_a = found.mimeType) !== null && _a !== void 0 ? _a : mimeType));
335
+ const kindFor = (mime) => binaryArtifactUtils.resolveBinaryPreviewKind(mime, binaryArtifactUtils.resolveEffectiveFileExtension(extension, shownTitle));
336
+ const show = (loaded) => {
337
+ if (loaded.mode === 'direct' || loaded.text === undefined)
338
+ setCameAsBytes(true);
339
+ setFile(loaded);
340
+ setResolvedTitle(shownTitle);
341
+ setResolvedExtension(extension);
342
+ setLoadState('ready');
343
+ };
344
+ const { source } = found;
345
+ // Only the artifact's own text is ever run as a page, never what a link
346
+ // answered with. Text that is not markup (an html file edited in the
347
+ // editor holds markdown) has nothing to run.
348
+ if (source.kind === 'text' && isHtml) {
349
+ const isPage = binaryArtifactUtils.looksLikeMarkup(source.content);
350
+ setPreviewFailed(false);
351
+ show({
352
+ mode: 'blob',
353
+ blob: new Blob([source.content], { type: HTML_FILE_BLOB_TYPE }),
354
+ kind: isPage ? 'html' : 'none',
355
+ page: isPage ? { html: source.content, serial: ++pageSerialRef.current } : undefined,
356
+ text: source.content,
357
+ });
358
+ return 'shown';
44
359
  }
360
+ let bytes;
361
+ if (source.kind === 'url') {
362
+ const kind = kindFor(type);
363
+ const fromDm = binaryArtifactUtils.isDmHostedUrl(source.url, dmUrl);
364
+ const drawnFromBytes = kind === 'docx' || kind === 'xlsx' || kind === 'pdf';
365
+ // The DM lets another site read its files but refuses to be framed;
366
+ // storage can be framed but lets no other site read. So a DM link is
367
+ // read into a blob, and a storage link is handed to the browser as is.
368
+ // That holds for video and sound too: the DM does not send a file in
369
+ // parts, and Safari and the desktop app play nothing from a server
370
+ // that does not, while a blob can always be read in parts. Word,
371
+ // Excel and pdf files are drawn from their bytes, so a storage link
372
+ // is tried once in case its bucket allows the read. A pdf that cannot
373
+ // be read is still framed from its link, as the browser can show it.
374
+ const fetched = (fromDm || drawnFromBytes) && mayReadLink(source.url, dmUrl)
375
+ ? await fetchLinkBlob(source.url)
376
+ : null;
377
+ if (cancelled)
378
+ return 'cancelled';
379
+ // An answer that refuses is about the link, wherever it is: a bucket
380
+ // that lets no other site read gives no answer at all. A read that
381
+ // could not be made says nothing about the link.
382
+ if (fetched === 'refused')
383
+ return 'refused';
384
+ if (fetched === null || typeof fetched === 'string') {
385
+ show({
386
+ mode: 'direct',
387
+ url: source.url,
388
+ mimeType: type,
389
+ namedType: (_b = found.mimeType) !== null && _b !== void 0 ? _b : mimeType,
390
+ fromProp,
391
+ kind: kind === 'docx' || kind === 'xlsx' ? 'none' : kind,
392
+ });
393
+ return 'shown';
394
+ }
395
+ bytes = withMimeType(fetched.blob, type);
396
+ }
397
+ else {
398
+ bytes = binaryArtifactUtils.inlineSourceToBlob(source, type !== null && type !== void 0 ? type : 'application/octet-stream');
399
+ if (!bytes)
400
+ throw new Error('Artifact has no displayable content');
401
+ }
402
+ // Malformed LLM output (invalid SVG XML, non-PDF bytes, …) must show
403
+ // a clear message instead of the browser's raw parse error.
404
+ const issue = await binaryArtifactUtils.getPreviewIssue(bytes, bytes.type);
405
+ if (cancelled)
406
+ return 'cancelled';
407
+ setPreviewFailed(issue !== null);
408
+ show({
409
+ mode: 'blob',
410
+ blob: bytes,
411
+ kind: kindFor(bytes.type),
412
+ text: source.kind === 'text' ? source.content : undefined,
413
+ });
414
+ return 'shown';
415
+ };
416
+ const load = async () => {
45
417
  setLoadState('loading');
46
418
  setPreviewFailed(false);
419
+ setTooLargeToPreview(false);
420
+ setPageUnavailable(null);
421
+ setPdfInFrame(false);
422
+ setFile(null);
47
423
  try {
48
- const artifact = (await fetchArtifact({
49
- id: artifactId,
50
- org,
51
- userId,
52
- }).unwrap());
53
- if (cancelled)
54
- return;
55
- // Binary artifacts carry base64 `binary_content`; text-based viewable
56
- // files (svg) carry their source in `content` — handle both.
57
- const file = binaryArtifactUtils.artifactFileToBlob(artifact, fileExtension, mimeType);
58
- if (!file) {
59
- throw new Error('Artifact has no displayable content');
424
+ // A link that is not a web address counts as no link at all.
425
+ const givenSource = binaryUrl
426
+ ? binaryArtifactUtils.resolveBinarySource({ binary_url: binaryUrl }, dmUrl)
427
+ : null;
428
+ const given = (givenSource === null || givenSource === void 0 ? void 0 : givenSource.kind) === 'url' ? givenSource.url : null;
429
+ const renewFirst = given !== null &&
430
+ lookup !== null &&
431
+ (binaryUrl === staleLink || (binaryArtifactUtils.isBinaryUrlExpired(given) && claimRenewal()));
432
+ let found;
433
+ let fromProp = false;
434
+ if (given !== null && !renewFirst) {
435
+ found = { source: { kind: 'url', url: given } };
436
+ fromProp = true;
60
437
  }
61
- // Malformed LLM output (invalid SVG XML, non-PDF bytes, …) must show
62
- // a clear message instead of the browser's raw parse error.
63
- const issue = await binaryArtifactUtils.getPreviewIssue(file.blob, file.mimeType);
64
- if (cancelled)
438
+ else if (given === null &&
439
+ heldText !== null &&
440
+ binaryArtifactUtils.isTextViewerExtension(binaryArtifactUtils.resolveEffectiveFileExtension(fileExtension, title))) {
441
+ found = { source: { kind: 'text', content: heldText } };
442
+ }
443
+ else if (lookup === null) {
444
+ setLoadState('error');
65
445
  return;
66
- setPreviewFailed(issue !== null);
67
- setBlob(file.blob);
68
- setResolvedTitle(artifact.title || title);
69
- setResolvedExtension(file.extension);
70
- setLoadState('ready');
446
+ }
447
+ else {
448
+ found = await (given !== null ? renewSource(lookup) : readDetail(lookup));
449
+ if (cancelled)
450
+ return;
451
+ }
452
+ let outcome = await present(found, fromProp);
453
+ if (outcome === 'refused' && fromProp && lookup !== null && claimRenewal()) {
454
+ const renewed = await renewSource(lookup);
455
+ if (cancelled)
456
+ return;
457
+ outcome = await present(renewed, false);
458
+ }
459
+ if (outcome === 'refused')
460
+ throw new Error('The file link was refused');
71
461
  }
72
462
  catch (error) {
73
463
  if (cancelled)
@@ -80,7 +470,21 @@ function BinaryCanvasComponent({ title, onClose, artifactId, org, userId, fileEx
80
470
  return () => {
81
471
  cancelled = true;
82
472
  };
83
- }, [artifactId, org, userId, reloadToken, fetchArtifact]);
473
+ }, [
474
+ binaryUrl,
475
+ heldText,
476
+ staleLink,
477
+ lookup,
478
+ dmUrl,
479
+ fileExtension,
480
+ mimeType,
481
+ versionUnavailable,
482
+ reloadToken,
483
+ ]);
484
+ const page = (file === null || file === void 0 ? void 0 : file.mode) === 'blob' ? file.page : undefined;
485
+ // A page is handed to its frame as text, so it needs no address of its own.
486
+ const blob = (file === null || file === void 0 ? void 0 : file.mode) === 'blob' && !page ? file.blob : null;
487
+ const directUrl = (file === null || file === void 0 ? void 0 : file.mode) === 'direct' ? file.url : null;
84
488
  // Object URL for the embedded viewer. Created and revoked inside one
85
489
  // effect so each mount gets a fresh URL — a memoized URL would be revoked
86
490
  // by StrictMode's cleanup pass and hand the viewer a dead blob: URL.
@@ -88,6 +492,22 @@ function BinaryCanvasComponent({ title, onClose, artifactId, org, userId, fileEx
88
492
  React.useEffect(() => {
89
493
  if (!blob)
90
494
  return;
495
+ // An svg can hold script. A blob: address is of this app's origin, so a
496
+ // person who opens the picture in a tab of its own would run that script
497
+ // as the app; a data: address has no origin of its own.
498
+ if (blob.type === 'image/svg+xml') {
499
+ let live = true;
500
+ const reader = new FileReader();
501
+ reader.onload = () => {
502
+ if (live && typeof reader.result === 'string')
503
+ setBlobUrl(reader.result);
504
+ };
505
+ reader.readAsDataURL(blob);
506
+ return () => {
507
+ live = false;
508
+ setBlobUrl(null);
509
+ };
510
+ }
91
511
  const url = URL.createObjectURL(blob);
92
512
  setBlobUrl(url);
93
513
  return () => {
@@ -95,25 +515,123 @@ function BinaryCanvasComponent({ title, onClose, artifactId, org, userId, fileEx
95
515
  URL.revokeObjectURL(url);
96
516
  };
97
517
  }, [blob]);
98
- const effectiveMime = (_a = blob === null || blob === void 0 ? void 0 : blob.type) !== null && _a !== void 0 ? _a : binaryArtifactUtils.resolveBinaryMimeType(fileExtension, mimeType);
99
- const isPdf = effectiveMime === 'application/pdf';
100
- const isImage = binaryArtifactUtils.isImageMimeType(effectiveMime);
101
- const canPreview = isPdf || isImage;
102
- const handleExport = () => {
103
- if (!blob)
518
+ const viewerUrl = directUrl !== null && directUrl !== void 0 ? directUrl : blobUrl;
519
+ const kind = (_c = file === null || file === void 0 ? void 0 : file.kind) !== null && _c !== void 0 ? _c : 'none';
520
+ const saveFile = (_d = config === null || config === void 0 ? void 0 : config.saveFile) !== null && _d !== void 0 ? _d : canvasExportHandlers.downloadBlob;
521
+ const handleExport = async () => {
522
+ var _a;
523
+ if (!file || exportingRef.current)
524
+ return;
525
+ const filename = binaryArtifactUtils.buildBinaryFilename(resolvedTitle, resolvedExtension);
526
+ exportingRef.current = true;
527
+ setSaveFailed(false);
528
+ try {
529
+ if (file.mode === 'blob') {
530
+ await saveFile(file.blob, filename);
531
+ return;
532
+ }
533
+ const read = async (url) => mayReadLink(url, dmUrl) ? fetchLinkBlob(url) : 'unreachable';
534
+ const renewedLink = async () => {
535
+ var _a;
536
+ const renewed = lookup ? (_a = (await renewSource(lookup).catch(() => null))) === null || _a === void 0 ? void 0 : _a.source : null;
537
+ return (renewed === null || renewed === void 0 ? void 0 : renewed.kind) === 'url' ? renewed.url : null;
538
+ };
539
+ let link = file.url;
540
+ // A file can stay open longer than its link lasts.
541
+ const ranOut = binaryArtifactUtils.isBinaryUrlExpired(link);
542
+ if (ranOut)
543
+ link = (_a = (await renewedLink())) !== null && _a !== void 0 ? _a : link;
544
+ let fetched = await read(link);
545
+ // An answer that refuses means the link is dead, so a new one is tried
546
+ // once before the link is opened away, where a dead one shows an error.
547
+ if (fetched === 'refused' && !ranOut) {
548
+ const renewed = await renewedLink();
549
+ if (renewed && renewed !== link) {
550
+ link = renewed;
551
+ fetched = await read(link);
552
+ }
553
+ }
554
+ if (fetched === 'refused' || fetched === 'unreachable') {
555
+ const runsWhenOpened = opensAsPage({
556
+ extension: binaryArtifactUtils.resolveEffectiveFileExtension(resolvedExtension, resolvedTitle),
557
+ title: resolvedTitle,
558
+ link,
559
+ mimeTypes: [file.mimeType, file.namedType],
560
+ });
561
+ if (runsWhenOpened) {
562
+ setSaveFailed(true);
563
+ return;
564
+ }
565
+ await openLinkAway(link);
566
+ return;
567
+ }
568
+ await saveFile(withMimeType(fetched.blob, file.mimeType), filename);
569
+ }
570
+ catch (_b) {
571
+ console.error('[BinaryCanvas] Failed to save the file');
572
+ setSaveFailed(true);
573
+ }
574
+ finally {
575
+ exportingRef.current = false;
576
+ }
577
+ };
578
+ // A picture, video or sound that will not load from the link the caller
579
+ // gave may only need a new link. A file whose bytes are in hand does not.
580
+ const handlePreviewError = () => {
581
+ if ((file === null || file === void 0 ? void 0 : file.mode) === 'direct' && file.fromProp && lookup && claimRenewal()) {
582
+ setStaleLink(binaryUrl !== null && binaryUrl !== void 0 ? binaryUrl : null);
104
583
  return;
105
- canvasExportHandlers.downloadBlob(blob, binaryArtifactUtils.buildBinaryFilename(resolvedTitle, resolvedExtension));
584
+ }
585
+ setPreviewFailed(true);
586
+ };
587
+ const handleOfficePreviewError = (reason) => {
588
+ setTooLargeToPreview(reason === 'too-large');
589
+ setPreviewFailed(true);
590
+ };
591
+ const handlePageUnavailable = React.useCallback((reason) => setPageUnavailable(reason), []);
592
+ const handlePdfFailed = React.useCallback(() => setPreviewFailed(true), []);
593
+ const handlePdfTooLarge = React.useCallback(() => {
594
+ setTooLargeToPreview(true);
595
+ setPreviewFailed(true);
596
+ }, []);
597
+ const handlePdfUnavailable = React.useCallback(() => setPdfInFrame(true), []);
598
+ const handleRetry = () => {
599
+ renewedForRef.current = null;
600
+ setStaleLink(null);
601
+ setReloadToken((v) => v + 1);
106
602
  };
107
603
  const displayTitle = (resolvedTitle === null || resolvedTitle === void 0 ? void 0 : resolvedTitle.trim()) || t('canvasBinaryCanvas.untitledFile');
108
- return (jsxRuntime.jsxs("div", { className: "flex h-full w-full flex-col overflow-hidden bg-white", "data-testid": "binary-canvas", children: [jsxRuntime.jsxs("div", { className: "flex min-h-[50px] flex-shrink-0 items-center gap-1 border-b border-gray-200 bg-white px-2 py-2 sm:min-h-[60px] sm:px-3 sm:py-3 md:px-4", children: [jsxRuntime.jsxs("div", { className: "mr-2 flex min-w-0 flex-1 items-center gap-1 overflow-hidden sm:gap-2", children: [jsxRuntime.jsx(fileArchive.default, { className: "h-4 w-4 flex-shrink-0 text-blue-600 sm:h-5 sm:w-5" }), jsxRuntime.jsx("span", { className: "block truncate text-left text-xs font-medium text-gray-900 sm:text-sm md:text-base", "data-testid": "binary-canvas-title", children: displayTitle })] }), jsxRuntime.jsxs("div", { className: "ml-1 flex flex-shrink-0 items-center gap-1 sm:gap-2", children: [jsxRuntime.jsxs(button.Button, { variant: "outline", size: "sm", className: "flex items-center gap-1 border-blue-200 bg-transparent px-1 text-xs whitespace-nowrap text-blue-600 sm:px-2 md:px-3", onClick: handleExport, disabled: loadState !== 'ready' || !blob, "data-testid": "binary-canvas-export", children: [jsxRuntime.jsx(download.default, { className: "h-3.5 w-3.5 sm:mr-1" }), jsxRuntime.jsx("span", { className: "hidden sm:inline", children: t('canvasBinaryCanvas.export') })] }), onClose && (jsxRuntime.jsx(button.Button, { variant: "ghost", size: "sm", className: "h-8 w-8 p-0", onClick: onClose, "data-testid": "binary-canvas-close", children: jsxRuntime.jsx(x.default, { className: "h-4 w-4" }) }))] })] }), jsxRuntime.jsxs("div", { className: "flex min-h-0 flex-1 flex-col overflow-hidden bg-gray-50", children: [loadState === 'loading' && (jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-center", "data-testid": "binary-canvas-loading", children: jsxRuntime.jsx("div", { className: "h-10 w-10 animate-spin rounded-full border-4 border-blue-500 border-t-transparent" }) })), loadState === 'error' && (jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col items-center justify-center gap-3 p-6 text-center", "data-testid": "binary-canvas-error", children: [jsxRuntime.jsx(fileArchive.default, { className: "h-10 w-10 text-gray-400" }), jsxRuntime.jsx("p", { className: "text-sm text-gray-600", children: t('canvasBinaryCanvas.loadError') }), jsxRuntime.jsx(button.Button, { variant: "outline", size: "sm", onClick: () => setReloadToken((v) => v + 1), "data-testid": "binary-canvas-retry", children: t('canvasBinaryCanvas.retry') })] })), loadState === 'ready' && !previewFailed && blobUrl && isPdf && (jsxRuntime.jsx("iframe", { src: blobUrl, title: displayTitle, className: "h-full w-full flex-1 border-0", "data-testid": "binary-canvas-pdf" })), loadState === 'ready' && !previewFailed && blobUrl && isImage && (jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-center overflow-auto p-4", children: jsxRuntime.jsx("img", { src: blobUrl, alt: displayTitle, className: "max-h-full max-w-full object-contain", "data-testid": "binary-canvas-image", onError: () => setPreviewFailed(true) }) })), loadState === 'ready' && (previewFailed || !canPreview) && (
109
- /* Two ways here: the file itself is malformed / failed to render
110
- (previewFailed), or this type has no in-canvas viewer at all
111
- (defensive — the chat chip offers Download for those instead of
112
- opening the canvas). Either way the original file still
113
- downloads fine. */
114
- jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col items-center justify-center gap-3 p-6 text-center", "data-testid": previewFailed ? 'binary-canvas-preview-error' : 'binary-canvas-fallback', children: [jsxRuntime.jsx(fileArchive.default, { className: "h-10 w-10 text-gray-400" }), jsxRuntime.jsx("p", { className: "text-sm text-gray-600", children: previewFailed
115
- ? t('canvasBinaryCanvas.previewFailed')
116
- : t('canvasBinaryCanvas.previewUnavailable') })] }))] })] }));
604
+ const view = versionUnavailable ? 'versionUnavailable' : loadState;
605
+ // An html page and an svg are text as well as something to look at. Where
606
+ // the page cannot be run or the picture cannot be drawn, the text is shown.
607
+ const sourceText = (file === null || file === void 0 ? void 0 : file.mode) === 'blob' ? file.text : undefined;
608
+ const hasPreview = (Boolean(page) && pageUnavailable !== 'not-here') || (kind === 'image' && !previewFailed);
609
+ const shownCode = view === 'ready' && (tab === 'code' || !hasPreview) ? sourceText : undefined;
610
+ const showsCode = shownCode !== undefined;
611
+ const showsTabs = view === 'ready' && sourceText !== undefined && hasPreview;
612
+ const previewShown = view === 'ready' && !previewFailed && !showsCode;
613
+ const tabs$1 = [
614
+ { id: 'preview', label: t('canvasBinaryCanvas.preview') },
615
+ { id: 'code', label: t('canvasBinaryCanvas.code') },
616
+ ];
617
+ return (jsxRuntime.jsxs("div", { className: "flex h-full w-full flex-col overflow-hidden bg-white", "data-testid": "binary-canvas", children: [jsxRuntime.jsxs("div", { className: "flex min-h-[50px] flex-shrink-0 items-center gap-1 border-b border-gray-200 bg-white px-2 py-2 sm:min-h-[60px] sm:px-3 sm:py-3 md:px-4", children: [jsxRuntime.jsxs("div", { className: "mr-2 flex min-w-0 flex-1 items-center gap-1 overflow-hidden sm:gap-2", children: [jsxRuntime.jsx(fileArchive.default, { className: "h-4 w-4 flex-shrink-0 text-blue-600 sm:h-5 sm:w-5" }), jsxRuntime.jsx("span", { className: "block truncate text-left text-xs font-medium text-gray-900 sm:text-sm md:text-base", "data-testid": "binary-canvas-title", children: displayTitle }), jsxRuntime.jsx(canvasVersionMenu.CanvasVersionMenu, { versions: steppedVersions, disabled: view === 'loading' })] }), jsxRuntime.jsxs("div", { className: "ml-1 flex flex-shrink-0 items-center gap-1 sm:gap-2", children: [shownCode !== undefined && jsxRuntime.jsx(copyTextButton.CopyTextButton, { text: shownCode }), jsxRuntime.jsxs(button.Button, { variant: "outline", size: "sm", className: "flex items-center gap-1 border-blue-200 bg-transparent px-1 text-xs whitespace-nowrap text-blue-600 sm:px-2 md:px-3", onClick: () => void handleExport(), disabled: view !== 'ready' || !file, "aria-label": t('canvasBinaryCanvas.export'), "data-testid": "binary-canvas-export", children: [jsxRuntime.jsx(download.default, { className: "h-3.5 w-3.5 sm:mr-1", "aria-hidden": "true" }), jsxRuntime.jsx("span", { className: "hidden sm:inline", children: t('canvasBinaryCanvas.export') })] }), onClose && (jsxRuntime.jsx(button.Button, { variant: "ghost", size: "sm", className: "h-8 w-8 p-0", onClick: onClose, "aria-label": t('canvasBinaryCanvas.close'), "data-testid": "binary-canvas-close", children: jsxRuntime.jsx(x.default, { className: "h-4 w-4", "aria-hidden": "true" }) }))] })] }), saveFailed && (jsxRuntime.jsx("div", { role: "status", className: "flex-shrink-0 border-b border-gray-200 bg-gray-50 px-3 py-2 text-xs text-gray-700 sm:px-4 sm:text-sm", "data-testid": "binary-canvas-save-failed", children: t('canvasBinaryCanvas.saveFailed') })), jsxRuntime.jsx(canvasVersionMenu.CanvasVersionBanner, { versions: steppedVersions, readOnly: readOnly, restorable: keepsEveryVersion }), jsxRuntime.jsxs(tabs.Tabs, { value: tab, onValueChange: (value) => setTab(value), className: "flex min-h-0 flex-1 flex-col", children: [showsTabs && (jsxRuntime.jsx("div", { className: "flex flex-shrink-0 items-center border-b border-gray-200 bg-white px-2 py-2 sm:px-3 md:px-4", children: jsxRuntime.jsx(tabs.TabsList, { "aria-label": t('canvasBinaryCanvas.viewLabel'), className: VIEW_TABS_LIST_CLASS, "data-testid": "binary-canvas-view-tabs", children: tabs$1.map(({ id, label }) => (jsxRuntime.jsx(tabs.TabsTrigger, { value: id, className: VIEW_TAB_TRIGGER_CLASS, "data-testid": `binary-canvas-view-${id}`, children: label }, id))) }) })), jsxRuntime.jsxs(ViewBody, { tabbed: showsTabs, tab: tab, children: [view === 'versionUnavailable' && (jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col items-center justify-center gap-3 p-6 text-center", "data-testid": "binary-canvas-version-unavailable", children: [jsxRuntime.jsx(fileArchive.default, { className: "h-10 w-10 text-gray-400" }), jsxRuntime.jsx("p", { className: "text-sm text-gray-600", children: t('canvasBinaryCanvas.earlierVersionUnavailable') })] })), view === 'loading' && jsxRuntime.jsx(LoadingSpinner, {}), view === 'error' && (jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col items-center justify-center gap-3 p-6 text-center", "data-testid": "binary-canvas-error", children: [jsxRuntime.jsx(fileArchive.default, { className: "h-10 w-10 text-gray-400" }), jsxRuntime.jsx("p", { className: "text-sm text-gray-600", children: t('canvasBinaryCanvas.loadError') }), jsxRuntime.jsx(button.Button, { variant: "outline", size: "sm", onClick: handleRetry, "data-testid": "binary-canvas-retry", children: t('canvasBinaryCanvas.retry') })] })), shownCode !== undefined && (jsxRuntime.jsx(codeView.CodeView, { text: shownCode, language: textFileKinds.describeTextFile({ fileExtension: resolvedExtension, title: resolvedTitle })
618
+ .language, label: displayTitle })), previewShown && blob && kind === 'pdf' && !pdfInFrame && (jsxRuntime.jsx(pdfPreview.PdfPreview, { blob: blob, title: displayTitle, onFailed: handlePdfFailed, onTooLarge: handlePdfTooLarge, onUnavailable: handlePdfUnavailable })), previewShown && viewerUrl && kind === 'pdf' && (!blob || pdfInFrame) && (jsxRuntime.jsx("iframe", { src: viewerUrl, title: displayTitle, referrerPolicy: "no-referrer", className: "h-full w-full flex-1 border-0", "data-testid": "binary-canvas-pdf", "data-sentry-block": true })), previewShown && viewerUrl && kind === 'image' && (jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-center overflow-auto p-4", children: jsxRuntime.jsx("img", { src: viewerUrl, alt: displayTitle, referrerPolicy: "no-referrer", className: "max-h-full max-w-full object-contain", "data-testid": "binary-canvas-image", "data-sentry-block": true, onError: handlePreviewError }) })), previewShown && viewerUrl && kind === 'video' && (jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-center overflow-auto p-4", children: jsxRuntime.jsx("video", { src: viewerUrl, controls: true, playsInline: true, preload: "metadata", "aria-label": displayTitle, className: "max-h-full max-w-full", "data-testid": "binary-canvas-video", "data-sentry-block": true, onError: handlePreviewError }) })), previewShown && viewerUrl && kind === 'audio' && (jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-center overflow-auto p-4", children: jsxRuntime.jsx("audio", { src: viewerUrl, controls: true, preload: "metadata", "aria-label": displayTitle, className: "w-full max-w-md", "data-testid": "binary-canvas-audio", "data-sentry-block": true, onError: handlePreviewError }) })), previewShown && blob && kind === 'docx' && (jsxRuntime.jsx(docxPreview.DocxPreview, { blob: blob, title: displayTitle, onError: handleOfficePreviewError })), previewShown && blob && kind === 'xlsx' && (jsxRuntime.jsx(xlsxPreview.XlsxPreview, { blob: blob, title: displayTitle, onError: handleOfficePreviewError })), previewShown && page && pageUnavailable === 'no-answer' && (jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col items-center justify-center gap-3 p-6 text-center", "data-testid": "binary-canvas-page-error", children: [jsxRuntime.jsx(fileArchive.default, { className: "h-10 w-10 text-gray-400" }), jsxRuntime.jsx("p", { className: "text-sm text-gray-600", children: t('canvasBinaryCanvas.pageNotShown') }), jsxRuntime.jsx(button.Button, { variant: "outline", size: "sm", onClick: handleRetry, "data-testid": "binary-canvas-retry", children: t('canvasBinaryCanvas.retry') })] })), view === 'ready' && !previewFailed && page && !pageUnavailable && (
619
+ // Kept running under the Code tab: taking it away would start the
620
+ // page again. The attribute, with no display class beside it to
621
+ // outrank it, hides it under any stylesheet.
622
+ jsxRuntime.jsx("div", { hidden: showsCode, className: showsCode ? undefined : 'flex min-h-0 flex-1 flex-col', children: jsxRuntime.jsx(htmlPreview.HtmlPreview, { html: page.html, title: displayTitle, onUnavailable: handlePageUnavailable }, page.serial) })), view === 'ready' && !showsCode && (previewFailed || kind === 'none') && (
623
+ /* Three ways here: the file is malformed, failed to render or is
624
+ too large to draw (previewFailed); its type has no viewer here
625
+ (an archive, a slide deck); or it is a Word or Excel file whose
626
+ bytes could not be read to draw it. Either way the original file
627
+ still downloads fine. A page that cannot be run and a picture
628
+ that cannot be drawn from the file's own text show that text
629
+ instead (showsCode). */
630
+ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col items-center justify-center gap-3 p-6 text-center", "data-testid": previewFailed ? 'binary-canvas-preview-error' : 'binary-canvas-fallback', children: [jsxRuntime.jsx(fileArchive.default, { className: "h-10 w-10 text-gray-400" }), jsxRuntime.jsx("p", { className: "text-sm text-gray-600", children: previewFailed
631
+ ? t(tooLargeToPreview
632
+ ? 'canvasBinaryCanvas.previewTooLarge'
633
+ : 'canvasBinaryCanvas.previewFailed')
634
+ : t('canvasBinaryCanvas.previewUnavailable') })] }))] })] })] }));
117
635
  }
118
636
 
119
637
  exports.BinaryCanvasComponent = BinaryCanvasComponent;