@memori.ai/memori-react 8.15.1 → 8.16.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 (166) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/components/Chat/Chat.css +2 -1
  3. package/dist/components/Chat/Chat.js +17 -17
  4. package/dist/components/Chat/Chat.js.map +1 -1
  5. package/dist/components/ChatBubble/ChatBubble.css +1 -1
  6. package/dist/components/ContentPreviewModal/ContentPreviewModal.css +114 -0
  7. package/dist/components/ContentPreviewModal/ContentPreviewModal.d.ts +14 -0
  8. package/dist/components/ContentPreviewModal/ContentPreviewModal.js +18 -0
  9. package/dist/components/ContentPreviewModal/ContentPreviewModal.js.map +1 -0
  10. package/dist/components/ContentPreviewModal/index.d.ts +2 -0
  11. package/dist/components/ContentPreviewModal/index.js +9 -0
  12. package/dist/components/ContentPreviewModal/index.js.map +1 -0
  13. package/dist/components/FilePreview/FilePreview.css +1 -1
  14. package/dist/components/FilePreview/FilePreview.js +43 -13
  15. package/dist/components/FilePreview/FilePreview.js.map +1 -1
  16. package/dist/components/MediaWidget/DocumentCard.d.ts +3 -0
  17. package/dist/components/MediaWidget/DocumentCard.js +9 -0
  18. package/dist/components/MediaWidget/DocumentCard.js.map +1 -0
  19. package/dist/components/MediaWidget/MediaItemWidget.css +946 -19
  20. package/dist/components/MediaWidget/MediaItemWidget.d.ts +5 -36
  21. package/dist/components/MediaWidget/MediaItemWidget.js +295 -198
  22. package/dist/components/MediaWidget/MediaItemWidget.js.map +1 -1
  23. package/dist/components/MediaWidget/MediaItemWidget.types.d.ts +62 -0
  24. package/dist/components/MediaWidget/MediaItemWidget.types.js +3 -0
  25. package/dist/components/MediaWidget/MediaItemWidget.types.js.map +1 -0
  26. package/dist/components/MediaWidget/MediaItemWidget.utils.d.ts +23 -0
  27. package/dist/components/MediaWidget/MediaItemWidget.utils.js +162 -0
  28. package/dist/components/MediaWidget/MediaItemWidget.utils.js.map +1 -0
  29. package/dist/components/MediaWidget/MediaPreviewModal.d.ts +15 -0
  30. package/dist/components/MediaWidget/MediaPreviewModal.js +162 -0
  31. package/dist/components/MediaWidget/MediaPreviewModal.js.map +1 -0
  32. package/dist/components/MediaWidget/MediaWidget.js +1 -2
  33. package/dist/components/MediaWidget/MediaWidget.js.map +1 -1
  34. package/dist/components/Snippet/Snippet.css +64 -33
  35. package/dist/components/Snippet/Snippet.js +17 -4
  36. package/dist/components/Snippet/Snippet.js.map +1 -1
  37. package/dist/components/StartPanel/StartPanel.js +1 -2
  38. package/dist/components/StartPanel/StartPanel.js.map +1 -1
  39. package/dist/components/UploadButton/UploadButton.css +0 -5
  40. package/dist/components/layouts/WebsiteAssistant.js +8 -8
  41. package/dist/components/layouts/WebsiteAssistant.js.map +1 -1
  42. package/dist/components/layouts/chat.css +1 -1
  43. package/dist/components/layouts/website-assistant.css +405 -197
  44. package/dist/helpers/constants.js +0 -7
  45. package/dist/helpers/constants.js.map +1 -1
  46. package/dist/helpers/utils.d.ts +1 -0
  47. package/dist/helpers/utils.js +3 -1
  48. package/dist/helpers/utils.js.map +1 -1
  49. package/dist/index.js +43 -1
  50. package/dist/index.js.map +1 -1
  51. package/dist/styles.css +0 -2
  52. package/dist/version.d.ts +1 -0
  53. package/dist/version.js +5 -0
  54. package/dist/version.js.map +1 -0
  55. package/esm/components/Chat/Chat.css +2 -1
  56. package/esm/components/Chat/Chat.js +17 -17
  57. package/esm/components/Chat/Chat.js.map +1 -1
  58. package/esm/components/ChatBubble/ChatBubble.css +1 -1
  59. package/esm/components/ContentPreviewModal/ContentPreviewModal.css +114 -0
  60. package/esm/components/ContentPreviewModal/ContentPreviewModal.d.ts +14 -0
  61. package/esm/components/ContentPreviewModal/ContentPreviewModal.js +15 -0
  62. package/esm/components/ContentPreviewModal/ContentPreviewModal.js.map +1 -0
  63. package/esm/components/ContentPreviewModal/index.d.ts +2 -0
  64. package/esm/components/ContentPreviewModal/index.js +2 -0
  65. package/esm/components/ContentPreviewModal/index.js.map +1 -0
  66. package/esm/components/FilePreview/FilePreview.css +1 -1
  67. package/esm/components/FilePreview/FilePreview.js +44 -14
  68. package/esm/components/FilePreview/FilePreview.js.map +1 -1
  69. package/esm/components/MediaWidget/DocumentCard.d.ts +3 -0
  70. package/esm/components/MediaWidget/DocumentCard.js +5 -0
  71. package/esm/components/MediaWidget/DocumentCard.js.map +1 -0
  72. package/esm/components/MediaWidget/MediaItemWidget.css +946 -19
  73. package/esm/components/MediaWidget/MediaItemWidget.d.ts +5 -36
  74. package/esm/components/MediaWidget/MediaItemWidget.js +296 -197
  75. package/esm/components/MediaWidget/MediaItemWidget.js.map +1 -1
  76. package/esm/components/MediaWidget/MediaItemWidget.types.d.ts +62 -0
  77. package/esm/components/MediaWidget/MediaItemWidget.types.js +2 -0
  78. package/esm/components/MediaWidget/MediaItemWidget.types.js.map +1 -0
  79. package/esm/components/MediaWidget/MediaItemWidget.utils.d.ts +23 -0
  80. package/esm/components/MediaWidget/MediaItemWidget.utils.js +149 -0
  81. package/esm/components/MediaWidget/MediaItemWidget.utils.js.map +1 -0
  82. package/esm/components/MediaWidget/MediaPreviewModal.d.ts +15 -0
  83. package/esm/components/MediaWidget/MediaPreviewModal.js +157 -0
  84. package/esm/components/MediaWidget/MediaPreviewModal.js.map +1 -0
  85. package/esm/components/MediaWidget/MediaWidget.js +1 -2
  86. package/esm/components/MediaWidget/MediaWidget.js.map +1 -1
  87. package/esm/components/Snippet/Snippet.css +64 -33
  88. package/esm/components/Snippet/Snippet.js +18 -5
  89. package/esm/components/Snippet/Snippet.js.map +1 -1
  90. package/esm/components/StartPanel/StartPanel.js +1 -2
  91. package/esm/components/StartPanel/StartPanel.js.map +1 -1
  92. package/esm/components/UploadButton/UploadButton.css +0 -5
  93. package/esm/components/layouts/WebsiteAssistant.js +8 -8
  94. package/esm/components/layouts/WebsiteAssistant.js.map +1 -1
  95. package/esm/components/layouts/chat.css +1 -1
  96. package/esm/components/layouts/website-assistant.css +405 -197
  97. package/esm/helpers/constants.js +0 -7
  98. package/esm/helpers/constants.js.map +1 -1
  99. package/esm/helpers/utils.d.ts +1 -0
  100. package/esm/helpers/utils.js +1 -0
  101. package/esm/helpers/utils.js.map +1 -1
  102. package/esm/index.js +43 -1
  103. package/esm/index.js.map +1 -1
  104. package/esm/styles.css +0 -2
  105. package/esm/version.d.ts +1 -0
  106. package/esm/version.js +2 -0
  107. package/esm/version.js.map +1 -0
  108. package/package.json +5 -3
  109. package/src/components/Chat/Chat.css +2 -1
  110. package/src/components/Chat/Chat.stories.tsx +124 -0
  111. package/src/components/Chat/Chat.tsx +72 -71
  112. package/src/components/Chat/__snapshots__/Chat.test.tsx.snap +567 -1034
  113. package/src/components/ChatBubble/ChatBubble.css +1 -1
  114. package/src/components/ContentPreviewModal/ContentPreviewModal.css +114 -0
  115. package/src/components/ContentPreviewModal/ContentPreviewModal.tsx +69 -0
  116. package/src/components/ContentPreviewModal/index.ts +2 -0
  117. package/src/components/FilePreview/FilePreview.css +1 -1
  118. package/src/components/FilePreview/FilePreview.tsx +60 -37
  119. package/src/components/FilePreview/__snapshots__/FilePreview.test.tsx.snap +15 -105
  120. package/src/components/MediaWidget/DocumentCard.test.tsx +45 -0
  121. package/src/components/MediaWidget/DocumentCard.tsx +19 -0
  122. package/src/components/MediaWidget/MediaItemWidget.css +946 -19
  123. package/src/components/MediaWidget/MediaItemWidget.test.tsx +89 -1
  124. package/src/components/MediaWidget/MediaItemWidget.tsx +734 -461
  125. package/src/components/MediaWidget/MediaItemWidget.types.ts +65 -0
  126. package/src/components/MediaWidget/MediaItemWidget.utils.test.ts +324 -0
  127. package/src/components/MediaWidget/MediaItemWidget.utils.ts +194 -0
  128. package/src/components/MediaWidget/MediaPreviewModal.test.tsx +271 -0
  129. package/src/components/MediaWidget/MediaPreviewModal.tsx +423 -0
  130. package/src/components/MediaWidget/MediaWidget.stories.tsx +193 -0
  131. package/src/components/MediaWidget/MediaWidget.tsx +2 -4
  132. package/src/components/MediaWidget/__snapshots__/DocumentCard.test.tsx.snap +24 -0
  133. package/src/components/MediaWidget/__snapshots__/MediaItemWidget.test.tsx.snap +162 -170
  134. package/src/components/MediaWidget/__snapshots__/MediaWidget.test.tsx.snap +21 -63
  135. package/src/components/Snippet/Snippet.css +64 -33
  136. package/src/components/Snippet/Snippet.tsx +30 -21
  137. package/src/components/Snippet/__snapshots__/Snippet.test.tsx.snap +314 -297
  138. package/src/components/StartPanel/StartPanel.tsx +0 -9
  139. package/src/components/StartPanel/__snapshots__/StartPanel.test.tsx.snap +12 -636
  140. package/src/components/UploadButton/UploadButton.css +0 -5
  141. package/src/components/layouts/WebsiteAssistant.tsx +66 -62
  142. package/src/components/layouts/__snapshots__/Chat.test.tsx.snap +1 -53
  143. package/src/components/layouts/__snapshots__/FullPage.test.tsx.snap +2 -106
  144. package/src/components/layouts/__snapshots__/HiddenChat.test.tsx.snap +1 -53
  145. package/src/components/layouts/__snapshots__/Totem.test.tsx.snap +1 -53
  146. package/src/components/layouts/__snapshots__/WebsiteAssistant.test.tsx.snap +32 -33
  147. package/src/components/layouts/__snapshots__/ZoomedFullBody.test.tsx.snap +1 -53
  148. package/src/components/layouts/chat.css +1 -1
  149. package/src/components/layouts/layouts.stories.tsx +68 -0
  150. package/src/components/layouts/website-assistant.css +405 -197
  151. package/src/helpers/constants.ts +0 -7
  152. package/src/helpers/utils.ts +4 -0
  153. package/src/index.test.tsx +8 -0
  154. package/src/index.tsx +51 -1
  155. package/src/styles.css +0 -2
  156. package/src/version.ts +2 -0
  157. package/src/components/AttachmentLinkModal/AttachmentLinkModal.css +0 -68
  158. package/src/components/AttachmentLinkModal/AttachmentLinkModal.stories.tsx +0 -32
  159. package/src/components/AttachmentLinkModal/AttachmentLinkModal.test.tsx +0 -10
  160. package/src/components/AttachmentLinkModal/AttachmentLinkModal.tsx +0 -131
  161. package/src/components/AttachmentLinkModal/__snapshots__/AttachmentLinkModal.test.tsx.snap +0 -9
  162. package/src/components/MediaWidget/LinkItemWidget.css +0 -46
  163. package/src/components/MediaWidget/LinkItemWidget.stories.tsx +0 -61
  164. package/src/components/MediaWidget/LinkItemWidget.test.tsx +0 -33
  165. package/src/components/MediaWidget/LinkItemWidget.tsx +0 -204
  166. package/src/components/MediaWidget/__snapshots__/LinkItemWidget.test.tsx.snap +0 -253
@@ -0,0 +1,65 @@
1
+ import type { Medium } from '@memori.ai/memori-api-client/dist/types';
2
+
3
+ export type LinkPreviewInfo = {
4
+ title?: string;
5
+ siteName?: string;
6
+ description?: string;
7
+ mediaType?: string;
8
+ image?: string;
9
+ imageWidth?: number;
10
+ imageHeight?: number;
11
+ favicon?: string;
12
+ images?: string[];
13
+ video?: string;
14
+ videos?: string[];
15
+ };
16
+
17
+ /** @deprecated Use LinkPreviewInfo */
18
+ export type ILinkPreviewInfo = LinkPreviewInfo;
19
+
20
+ export type MediaItem = Medium & { type?: string };
21
+
22
+ export interface MediaItemWidgetProps {
23
+ items: MediaItem[];
24
+ sessionID?: string;
25
+ tenantID?: string;
26
+ translateTo?: string;
27
+ baseURL?: string;
28
+ apiURL?: string;
29
+ customMediaRenderer?: (mimeType: string) => JSX.Element | null;
30
+ fromUser?: boolean;
31
+ descriptionOneLine?: boolean;
32
+ onLinkPreviewInfo?: (linkPreviewInfo: LinkPreviewInfo) => void;
33
+ }
34
+
35
+ export interface RenderMediaItemProps {
36
+ isChild?: boolean;
37
+ item: MediaItem;
38
+ sessionID?: string;
39
+ tenantID?: string;
40
+ preview?: boolean;
41
+ baseURL?: string;
42
+ apiURL?: string;
43
+ /** Called when user opens a media item (e.g. image) in the preview modal. Receives the clicked item so the correct one opens even when mediumIDs duplicate. */
44
+ onClick?: (item: MediaItem) => void;
45
+ customMediaRenderer?: (mimeType: string) => JSX.Element | null;
46
+ descriptionOneLine?: boolean;
47
+ onLinkPreviewInfo?: (linkPreviewInfo: LinkPreviewInfo) => void;
48
+ }
49
+
50
+ export interface RenderSnippetItemProps {
51
+ item: Medium & { type: string };
52
+ sessionID?: string;
53
+ tenantID?: string;
54
+ baseURL?: string;
55
+ apiURL?: string;
56
+ /** Called when user opens a snippet in the preview modal. Receives the clicked item. */
57
+ onClick?: (item: Medium & { type: string }) => void;
58
+ }
59
+
60
+ export interface DocumentCardProps {
61
+ title: string;
62
+ badge: string;
63
+ meta?: string | null;
64
+ icon: JSX.Element;
65
+ }
@@ -0,0 +1,324 @@
1
+ import {
2
+ formatBytes,
3
+ getFileExtensionFromUrl,
4
+ getFileExtensionFromMime,
5
+ countLines,
6
+ shouldUseDarkFileCard,
7
+ fetchLinkPreview,
8
+ getContentSize,
9
+ isValidUrl,
10
+ normalizeUrl,
11
+ getImageDisplaySource,
12
+ FILE_EXTENSIONS_DARK_CARD,
13
+ FILE_MIME_TYPES_DARK_CARD,
14
+ TEXT_FILE_EXTENSIONS,
15
+ IMAGE_MIME_TYPES,
16
+ FALLBACK_IMAGE_BASE64,
17
+ } from './MediaItemWidget.utils';
18
+ import type { Medium } from '@memori.ai/memori-api-client/dist/types';
19
+
20
+ describe('MediaItemWidget.utils', () => {
21
+ describe('formatBytes', () => {
22
+ it('returns "0 Bytes" for undefined or 0', () => {
23
+ expect(formatBytes(undefined)).toBe('0 Bytes');
24
+ expect(formatBytes(0)).toBe('0 Bytes');
25
+ });
26
+
27
+ it('formats bytes correctly', () => {
28
+ expect(formatBytes(1)).toBe('1 Bytes');
29
+ expect(formatBytes(1024)).toBe('1 KB');
30
+ expect(formatBytes(1536)).toBe('1.5 KB');
31
+ expect(formatBytes(1024 * 1024)).toBe('1 MB');
32
+ expect(formatBytes(1024 * 1024 * 1024)).toBe('1 GB');
33
+ });
34
+ });
35
+
36
+ describe('getFileExtensionFromUrl', () => {
37
+ it('returns null for undefined or empty url', () => {
38
+ expect(getFileExtensionFromUrl(undefined)).toBeNull();
39
+ expect(getFileExtensionFromUrl('')).toBeNull();
40
+ });
41
+
42
+ it('extracts extension from url', () => {
43
+ expect(getFileExtensionFromUrl('https://example.com/file.pdf')).toBe('PDF');
44
+ expect(getFileExtensionFromUrl('https://example.com/doc.xlsx?token=abc')).toBe('XLSX');
45
+ expect(getFileExtensionFromUrl('image.png')).toBe('PNG');
46
+ expect(getFileExtensionFromUrl('file.Md')).toBe('MD');
47
+ });
48
+
49
+ it('returns null when no extension', () => {
50
+ expect(getFileExtensionFromUrl('https://example.com/path')).toBeNull();
51
+ });
52
+ });
53
+
54
+ describe('getFileExtensionFromMime', () => {
55
+ it('maps known mime types to extensions', () => {
56
+ expect(getFileExtensionFromMime('application/pdf')).toBe('PDF');
57
+ expect(getFileExtensionFromMime('text/html')).toBe('HTML');
58
+ expect(getFileExtensionFromMime('text/plain')).toBe('TXT');
59
+ expect(getFileExtensionFromMime('application/json')).toBe('JSON');
60
+ expect(getFileExtensionFromMime('application/vnd.ms-excel')).toBe('XLS');
61
+ expect(getFileExtensionFromMime('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')).toBe('XLSX');
62
+ });
63
+
64
+ it('uses subtype when not in MIME_TO_EXT map', () => {
65
+ expect(getFileExtensionFromMime('application/octet-stream')).toBe('OCTET-STREAM');
66
+ expect(getFileExtensionFromMime('image/jpeg')).toBe('JPEG');
67
+ });
68
+
69
+ it('falls back to FILE when mime has no subtype', () => {
70
+ expect(getFileExtensionFromMime('unknown')).toBe('FILE');
71
+ });
72
+ });
73
+
74
+ describe('countLines', () => {
75
+ it('returns 0 for undefined or empty', () => {
76
+ expect(countLines(undefined)).toBe(0);
77
+ expect(countLines('')).toBe(0);
78
+ });
79
+
80
+ it('counts lines with \\n', () => {
81
+ expect(countLines('a')).toBe(1);
82
+ expect(countLines('a\nb')).toBe(2);
83
+ expect(countLines('a\nb\nc')).toBe(3);
84
+ });
85
+
86
+ it('counts lines with \\r\\n', () => {
87
+ expect(countLines('a\r\nb')).toBe(2);
88
+ });
89
+
90
+ it('counts lines with \\r', () => {
91
+ expect(countLines('a\rb')).toBe(2);
92
+ });
93
+ });
94
+
95
+ describe('shouldUseDarkFileCard', () => {
96
+ const dummyItem: Medium = {
97
+ mediumID: 'id',
98
+ mimeType: 'text/plain',
99
+ title: 'File',
100
+ url: 'https://example.com/file.txt',
101
+ };
102
+
103
+ it('returns true when file extension is in FILE_EXTENSIONS_DARK_CARD', () => {
104
+ (FILE_EXTENSIONS_DARK_CARD as readonly string[]).forEach((ext) => {
105
+ expect(shouldUseDarkFileCard(dummyItem, ext, 'application/octet-stream')).toBe(true);
106
+ });
107
+ });
108
+
109
+ it('returns true when mime type is in FILE_MIME_TYPES_DARK_CARD', () => {
110
+ (FILE_MIME_TYPES_DARK_CARD as readonly string[]).forEach((mime) => {
111
+ expect(shouldUseDarkFileCard(dummyItem, null, mime)).toBe(true);
112
+ });
113
+ });
114
+
115
+ it('returns false for unknown extension and mime', () => {
116
+ expect(shouldUseDarkFileCard(dummyItem, null, 'application/octet-stream')).toBe(false);
117
+ expect(shouldUseDarkFileCard(dummyItem, 'XYZ', 'application/octet-stream')).toBe(false);
118
+ });
119
+ });
120
+
121
+ describe('fetchLinkPreview', () => {
122
+ const originalFetch = globalThis.fetch;
123
+
124
+ afterEach(() => {
125
+ globalThis.fetch = originalFetch;
126
+ });
127
+
128
+ it('returns link preview when fetch succeeds', async () => {
129
+ const mockData = {
130
+ title: 'Example',
131
+ description: 'A site',
132
+ image: 'https://example.com/og.png',
133
+ };
134
+ globalThis.fetch = jest.fn().mockResolvedValue({
135
+ ok: true,
136
+ json: () => Promise.resolve(mockData),
137
+ });
138
+
139
+ const result = await fetchLinkPreview('https://example.com');
140
+ expect(result).toEqual(mockData);
141
+ expect(globalThis.fetch).toHaveBeenCalledWith(
142
+ expect.stringContaining('/api/linkpreview/')
143
+ );
144
+ });
145
+
146
+ it('uses provided baseUrl', async () => {
147
+ globalThis.fetch = jest.fn().mockResolvedValue({ ok: true, json: () => Promise.resolve({}) });
148
+ await fetchLinkPreview('https://example.com', 'https://custom.api');
149
+ expect(globalThis.fetch).toHaveBeenCalledWith(
150
+ expect.stringMatching(/^https:\/\/custom\.api\/api\/linkpreview\//)
151
+ );
152
+ });
153
+
154
+ it('returns null when fetch fails', async () => {
155
+ const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
156
+ globalThis.fetch = jest.fn().mockRejectedValue(new Error('Network error'));
157
+
158
+ const result = await fetchLinkPreview('https://example.com');
159
+ expect(result).toBeNull();
160
+ consoleSpy.mockRestore();
161
+ });
162
+ });
163
+
164
+ describe('getContentSize', () => {
165
+ it('returns blob size when content is present', () => {
166
+ const item: Medium = {
167
+ mediumID: 'id',
168
+ mimeType: 'text/plain',
169
+ title: 'File',
170
+ content: 'hello',
171
+ };
172
+ expect(getContentSize(item)).toBe(5);
173
+ });
174
+
175
+ it('returns properties.size when no content', () => {
176
+ const item: Medium = {
177
+ mediumID: 'id',
178
+ mimeType: 'application/pdf',
179
+ title: 'Doc',
180
+ url: 'https://example.com/doc.pdf',
181
+ properties: { size: 1024 },
182
+ };
183
+ expect(getContentSize(item)).toBe(1024);
184
+ });
185
+
186
+ it('returns undefined when no content and no size property', () => {
187
+ const item: Medium = {
188
+ mediumID: 'id',
189
+ mimeType: 'text/plain',
190
+ title: 'File',
191
+ };
192
+ expect(getContentSize(item)).toBeUndefined();
193
+ });
194
+ });
195
+
196
+ describe('isValidUrl', () => {
197
+ it('returns false for undefined or empty', () => {
198
+ expect(isValidUrl(undefined)).toBe(false);
199
+ expect(isValidUrl('')).toBe(false);
200
+ });
201
+
202
+ it('returns true for valid URLs', () => {
203
+ expect(isValidUrl('https://example.com')).toBe(true);
204
+ expect(isValidUrl('http://localhost:3000')).toBe(true);
205
+ expect(isValidUrl('https://memori.ai/path')).toBe(true);
206
+ });
207
+
208
+ it('returns false for invalid URLs', () => {
209
+ expect(isValidUrl('not a url')).toBe(false);
210
+ expect(isValidUrl('ftp://')).toBe(false);
211
+ });
212
+ });
213
+
214
+ describe('normalizeUrl', () => {
215
+ it('returns undefined for undefined or empty', () => {
216
+ expect(normalizeUrl(undefined)).toBeUndefined();
217
+ expect(normalizeUrl('')).toBe('');
218
+ });
219
+
220
+ it('leaves http URLs unchanged', () => {
221
+ expect(normalizeUrl('https://example.com')).toBe('https://example.com');
222
+ expect(normalizeUrl('http://example.com')).toBe('http://example.com');
223
+ });
224
+
225
+ it('prepends https when no protocol', () => {
226
+ expect(normalizeUrl('example.com')).toBe('https://example.com');
227
+ expect(normalizeUrl('memori.ai/path')).toBe('https://memori.ai/path');
228
+ });
229
+ });
230
+
231
+ describe('getImageDisplaySource', () => {
232
+ it('returns resource URL when resourceUrl is valid', () => {
233
+ const item: Medium & { type?: string } = {
234
+ mediumID: 'id',
235
+ mimeType: 'image/png',
236
+ title: 'Image',
237
+ url: 'https://example.com/img.png',
238
+ };
239
+ const resourceUrl = 'https://cdn.example.com/img.png';
240
+ expect(getImageDisplaySource(item, resourceUrl)).toEqual({
241
+ src: resourceUrl,
242
+ isRgb: false,
243
+ });
244
+ });
245
+
246
+ it('returns item.url when resourceUrl is empty but item.url is valid', () => {
247
+ const item: Medium & { type?: string } = {
248
+ mediumID: 'id',
249
+ mimeType: 'image/jpeg',
250
+ title: 'Image',
251
+ url: 'https://example.com/photo.jpg',
252
+ };
253
+ expect(getImageDisplaySource(item, '')).toEqual({
254
+ src: 'https://example.com/photo.jpg',
255
+ isRgb: false,
256
+ });
257
+ });
258
+
259
+ it('returns rgb/rgba as src and isRgb true', () => {
260
+ const item: Medium & { type?: string } = {
261
+ mediumID: 'id',
262
+ mimeType: 'image/png',
263
+ title: 'Swatch',
264
+ url: 'rgb(255, 0, 0)',
265
+ };
266
+ expect(getImageDisplaySource(item, '')).toEqual({
267
+ src: 'rgb(255, 0, 0)',
268
+ isRgb: true,
269
+ });
270
+ const itemRgba: Medium & { type?: string } = {
271
+ ...item,
272
+ url: 'rgba(0, 128, 255, 0.5)',
273
+ };
274
+ expect(getImageDisplaySource(itemRgba, '')).toEqual({
275
+ src: 'rgba(0, 128, 255, 0.5)',
276
+ isRgb: true,
277
+ });
278
+ });
279
+
280
+ it('returns base64 data URL when content is present and no valid URL', () => {
281
+ const item: Medium & { type?: string } = {
282
+ mediumID: 'id',
283
+ mimeType: 'image/png',
284
+ title: 'Inline',
285
+ content: 'abc123',
286
+ };
287
+ expect(getImageDisplaySource(item, '')).toEqual({
288
+ src: 'data:image/png;base64,abc123',
289
+ isRgb: false,
290
+ });
291
+ });
292
+
293
+ it('returns undefined src when no valid URL, no rgb, no content', () => {
294
+ const item: Medium & { type?: string } = {
295
+ mediumID: 'id',
296
+ mimeType: 'image/png',
297
+ title: 'Missing',
298
+ };
299
+ expect(getImageDisplaySource(item, '')).toEqual({
300
+ src: undefined,
301
+ isRgb: false,
302
+ });
303
+ });
304
+ });
305
+
306
+ describe('constants', () => {
307
+ it('FALLBACK_IMAGE_BASE64 is a data URL', () => {
308
+ expect(FALLBACK_IMAGE_BASE64).toMatch(/^data:image\/svg\+xml;base64,/);
309
+ });
310
+
311
+ it('TEXT_FILE_EXTENSIONS includes expected extensions', () => {
312
+ expect(TEXT_FILE_EXTENSIONS).toContain('TXT');
313
+ expect(TEXT_FILE_EXTENSIONS).toContain('HTML');
314
+ expect(TEXT_FILE_EXTENSIONS).toContain('MD');
315
+ expect(TEXT_FILE_EXTENSIONS).toContain('JSON');
316
+ });
317
+
318
+ it('IMAGE_MIME_TYPES includes image types', () => {
319
+ expect(IMAGE_MIME_TYPES).toContain('image/jpeg');
320
+ expect(IMAGE_MIME_TYPES).toContain('image/png');
321
+ expect(IMAGE_MIME_TYPES).toContain('image/gif');
322
+ });
323
+ });
324
+ });
@@ -0,0 +1,194 @@
1
+ import type { Medium } from '@memori.ai/memori-api-client/dist/types';
2
+ import type { LinkPreviewInfo } from './MediaItemWidget.types';
3
+
4
+ export const FILE_EXTENSIONS_DARK_CARD = [
5
+ 'TXT',
6
+ 'HTML',
7
+ 'PDF',
8
+ 'DOC',
9
+ 'DOCX',
10
+ 'XLS',
11
+ 'XLSX',
12
+ 'JSON',
13
+ 'XML',
14
+ 'MD',
15
+ 'CSS',
16
+ 'JS',
17
+ 'TS',
18
+ 'PY',
19
+ ] as const;
20
+
21
+ export const FILE_MIME_TYPES_DARK_CARD = [
22
+ 'application/pdf',
23
+ 'application/msword',
24
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
25
+ 'application/vnd.ms-excel',
26
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
27
+ 'text/html',
28
+ 'text/plain',
29
+ 'text/css',
30
+ 'text/javascript',
31
+ 'application/json',
32
+ 'application/xml',
33
+ 'text/markdown',
34
+ ] as const;
35
+
36
+ export const TEXT_FILE_EXTENSIONS = [
37
+ 'TXT',
38
+ 'HTML',
39
+ 'MD',
40
+ 'CSS',
41
+ 'JS',
42
+ 'TS',
43
+ 'PY',
44
+ 'JSON',
45
+ 'XML',
46
+ ] as const;
47
+
48
+ export const IMAGE_MIME_TYPES = [
49
+ 'image/jpeg',
50
+ 'image/png',
51
+ 'image/jpg',
52
+ 'image/gif',
53
+ ] as const;
54
+
55
+ const MIME_TO_EXT: Record<string, string> = {
56
+ 'application/pdf': 'PDF',
57
+ 'application/msword': 'DOC',
58
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
59
+ 'DOCX',
60
+ 'application/vnd.ms-excel': 'XLS',
61
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'XLSX',
62
+ 'text/html': 'HTML',
63
+ 'text/plain': 'TXT',
64
+ 'text/css': 'CSS',
65
+ 'text/javascript': 'JS',
66
+ 'application/json': 'JSON',
67
+ 'application/xml': 'XML',
68
+ 'text/markdown': 'MD',
69
+ };
70
+
71
+ export const FALLBACK_IMAGE_BASE64 =
72
+ 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cmVjdCB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCIgZmlsbD0iI2YwZjBmMCIvPjx0ZXh0IHg9IjUwJSIgeT0iNTAlIiBmb250LWZhbWlseT0iQXJpYWwsIHNhbnMtc2VyaWYiIGZvbnQtc2l6ZT0iMTQiIGZpbGw9IiM5OTk5OTkiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGR5PSIuM2VtIj5JbWFnZSBub3QgYXZhaWxhYmxlPC90ZXh0Pjwvc3ZnPg==';
73
+
74
+ export function formatBytes(bytes: number | undefined): string {
75
+ if (!bytes || bytes === 0) return '0 Bytes';
76
+ const k = 1024;
77
+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];
78
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
79
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
80
+ }
81
+
82
+ export function getFileExtensionFromUrl(
83
+ url: string | undefined
84
+ ): string | null {
85
+ if (!url) return null;
86
+ const match = url.match(/\.([a-zA-Z0-9]+)(?:\?|$)/);
87
+ return match ? match[1].toUpperCase() : null;
88
+ }
89
+
90
+ export function getFileExtensionFromMime(mimeType: string): string {
91
+ return (
92
+ MIME_TO_EXT[mimeType] ||
93
+ mimeType.split('/')[1]?.toUpperCase() ||
94
+ 'FILE'
95
+ );
96
+ }
97
+
98
+ export function countLines(content: string | undefined): number {
99
+ if (!content) return 0;
100
+ return content.split(/\r\n|\r|\n/).length;
101
+ }
102
+
103
+ export function shouldUseDarkFileCard(
104
+ _item: Medium,
105
+ fileExtension: string | null,
106
+ mimeType: string
107
+ ): boolean {
108
+ if (
109
+ fileExtension &&
110
+ (FILE_EXTENSIONS_DARK_CARD as readonly string[]).includes(fileExtension)
111
+ ) {
112
+ return true;
113
+ }
114
+ return (FILE_MIME_TYPES_DARK_CARD as readonly string[]).includes(mimeType);
115
+ }
116
+
117
+ const LINK_PREVIEW_BASE_URL = 'https://aisuru.com';
118
+
119
+ export async function fetchLinkPreview(
120
+ url: string,
121
+ baseUrl?: string
122
+ ): Promise<LinkPreviewInfo | null> {
123
+ try {
124
+ const res = await fetch(
125
+ `${baseUrl || LINK_PREVIEW_BASE_URL}/api/linkpreview/${encodeURIComponent(url)}`
126
+ );
127
+ const data: LinkPreviewInfo = await res.json();
128
+ return data;
129
+ } catch (err) {
130
+ console.error('fetchLinkPreview', err);
131
+ return null;
132
+ }
133
+ }
134
+
135
+ export function getContentSize(item: Medium): number | undefined {
136
+ if (item.content != null) {
137
+ return new Blob([item.content]).size;
138
+ }
139
+ return item.properties?.size as number | undefined;
140
+ }
141
+
142
+ export function isValidUrl(urlString: string | undefined): boolean {
143
+ if (!urlString) return false;
144
+ try {
145
+ new URL(urlString);
146
+ return true;
147
+ } catch {
148
+ return false;
149
+ }
150
+ }
151
+
152
+ export function normalizeUrl(url: string | undefined): string | undefined {
153
+ if (!url || url.length === 0) return url;
154
+ return url.startsWith('http') ? url : `https://${url}`;
155
+ }
156
+
157
+ // ---------------------------------------------------------------------------
158
+ // Image source resolution (single source of truth for widget + modal)
159
+ // ---------------------------------------------------------------------------
160
+
161
+ export type ImageDisplaySource = {
162
+ /** Resolved URL or data URL for <img src>; undefined if no displayable source */
163
+ src: string | undefined;
164
+ /** True when item.url is a CSS color (rgb/rgba) — render as colored swatch, not <img> */
165
+ isRgb: boolean;
166
+ };
167
+
168
+ /**
169
+ * Resolves the display source for an image medium. Used by both the grid thumbnail
170
+ * and the preview modal so they stay in sync (resource URL, base64 content, or rgb/rgba).
171
+ */
172
+ export function getImageDisplaySource(
173
+ item: Medium & { type?: string },
174
+ resourceUrl: string
175
+ ): ImageDisplaySource {
176
+ const hasValidUrl =
177
+ isValidUrl(resourceUrl) || isValidUrl(item.url);
178
+ const isRgb =
179
+ !!item.url &&
180
+ (item.url.startsWith('rgb(') || item.url.startsWith('rgba('));
181
+
182
+ let src: string | undefined;
183
+ if (hasValidUrl) {
184
+ src = resourceUrl || item.url;
185
+ } else if (isRgb) {
186
+ src = item.url;
187
+ } else if (item.content) {
188
+ src = `data:${item.mimeType};base64,${item.content}`;
189
+ } else {
190
+ src = undefined;
191
+ }
192
+
193
+ return { src, isRgb };
194
+ }