Skip to content

fix: add image preview and download link for upload input v3#257

Merged
smarcet merged 7 commits into
mainfrom
fix/upload-input-v3-preview-download
Jun 1, 2026
Merged

fix: add image preview and download link for upload input v3#257
smarcet merged 7 commits into
mainfrom
fix/upload-input-v3-preview-download

Conversation

@tomrndom
Copy link
Copy Markdown
Contributor

@tomrndom tomrndom commented May 27, 2026

ref: https://app.clickup.com/t/86b9axe92

image

Signed-off-by: Tomás Castillo tcastilloboireau@gmail.com

Summary by CodeRabbit

  • New Features

    • Uploaded files show image thumbnails that open in a new tab; filenames are clickable download links with download behavior.
  • Bug Fixes

    • Previews reliably choose an available file URL and fall back when the primary link is unavailable; preview images update correctly after load errors.
  • Tests

    • Added tests covering file preview rendering, download-link attributes, and preview error handling.
  • Documentation

    • Added translation strings for the V3 upload input UI.

Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 27, 2026

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Uploaded-file rows now show linked image thumbnails (ProgressiveImg) with a local file_icon fallback, normalize and rewrite preview URLs, convert filenames into download/open links, add tests for preview/download behavior, and update Jest asset mock mapping.

Changes

Uploaded file thumbnail and download rendering

Layer / File(s) Summary
Imports and prop formatting
src/components/inputs/upload-input-v3/index.js
Adds i18n import, ProgressiveImg and file_icon imports and updates onError default prop formatting.
Add English translations
src/i18n/en.json
Adds upload_input_v3 translation block with no_post_url, upload_disabled, click_upload, drag_and_drop, see_preview, preview_file, and complete keys.
Translate dropzone UI strings
src/components/inputs/upload-input-v3/index.js
Replace hard-coded dropzone messages (no-post-URL, upload disabled, click/drag guidance) with T.translate(...) calls.
Thumbnail rendering and download link behavior
src/components/inputs/upload-input-v3/index.js
Resolve src from private_url/public_url/file_url (handle src === '#'), rewrite ?dl=0?raw=1 for previews, render a linked ProgressiveImg thumbnail with file_icon placeholder, and render filename as an anchor that opens/downloads the resolved src.
Tests and Jest asset mock path
src/components/inputs/upload-input-v3/__tests__/upload-input-v3.test.js, package.json
Add File Preview and Download tests covering preview/link attributes, private→public fallback for downloads, filename download link, and image error fallback; update Jest moduleNameMapper to point to <rootDir>/src/__mocks__/fileMock.js for asset imports.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • smarcet
  • santipalenque

Poem

🐰 A preview hops in, soft and bright,
A click will open files into light,
If images trip, an icon will stay,
Filenames offer a download way,
Hooray—thumbnails now lead the display!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding image preview and download functionality to the upload input v3 component, which is directly supported by the file modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/upload-input-v3-preview-download

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/components/inputs/upload-input-v3/index.js`:
- Around line 386-387: The preview/download anchors currently render with
href={src} even when src is empty, producing broken links; update the render
logic in the UploadInputV3 component (the JSX that outputs the <a href={src}
...> preview and download anchors) to conditionally render those <a> elements
only when src is truthy, and otherwise render a non-interactive fallback (e.g.,
a <span> or icon button with no href and appropriate aria-disabled) so the UI
isn’t clickable when no URL exists; apply this gate to both the preview anchor
and the download anchor render paths that reference the src variable.
- Around line 390-391: The onError handler for the image fallback currently sets
e.target.src = file_icon but leaves the same handler in place, risking an
infinite loop if the fallback also fails; update the onError callback used on
the image element (the onError prop in this component) to first remove or
nullify the handler (e.g., e.target.onerror = null or e.currentTarget.onerror =
null) and then set the fallback src, and optionally guard by checking if
e.target.src is already the fallback before assigning to avoid reassigning the
same value.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 95d83902-9a42-48cc-8c43-de00acf18754

📥 Commits

Reviewing files that changed from the base of the PR and between 051c04b and 4804426.

📒 Files selected for processing (1)
  • src/components/inputs/upload-input-v3/index.js

Comment on lines +386 to +387
<a href={src} target="_blank" rel="noreferrer" title="Preview file">
<img
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Avoid rendering preview/download anchors when URL is missing.

Line 386 and Line 399 can render <a> with an empty/undefined href, which creates broken interactive UI (click does nothing). Gate anchors on src and render plain text/icon when absent.

Proposed fix
-<a href={src} target="_blank" rel="noreferrer" title="Preview file">
-  <img
-    src={previewSrc}
-    alt={filename}
-    onError={(e) => { e.target.src = file_icon; }}
-    style={{ width: 70, height: 70, objectFit: 'contain', display: 'block', borderRadius: 4 }}
-  />
-</a>
+{src ? (
+  <a href={src} target="_blank" rel="noreferrer" title="Preview file">
+    <img
+      src={previewSrc}
+      alt={filename}
+      onError={(e) => { e.currentTarget.onerror = null; e.currentTarget.src = file_icon; }}
+      style={{ width: 70, height: 70, objectFit: 'contain', display: 'block', borderRadius: 4 }}
+    />
+  </a>
+) : (
+  <img
+    src={file_icon}
+    alt={filename}
+    style={{ width: 70, height: 70, objectFit: 'contain', display: 'block', borderRadius: 4 }}
+  />
+)}
@@
-<Typography
-  component="a"
-  href={src}
-  target="_blank"
-  rel="noreferrer"
-  download
+<Typography
+  component={src ? "a" : "span"}
+  href={src || undefined}
+  target={src ? "_blank" : undefined}
+  rel={src ? "noreferrer" : undefined}
+  download={Boolean(src)}

Also applies to: 398-403

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/inputs/upload-input-v3/index.js` around lines 386 - 387, The
preview/download anchors currently render with href={src} even when src is
empty, producing broken links; update the render logic in the UploadInputV3
component (the JSX that outputs the <a href={src} ...> preview and download
anchors) to conditionally render those <a> elements only when src is truthy, and
otherwise render a non-interactive fallback (e.g., a <span> or icon button with
no href and appropriate aria-disabled) so the UI isn’t clickable when no URL
exists; apply this gate to both the preview anchor and the download anchor
render paths that reference the src variable.

Comment thread src/components/inputs/upload-input-v3/index.js Outdated
tomrndom added 2 commits May 26, 2026 23:19
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
let src = file?.private_url || file?.public_url;
if (src === '#') src = file?.public_url;
// custom replace for dropbox case ( download vs raw)
const previewSrc = src ? src.replace("?dl=0", "?raw=1") : filename;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomrndom this logic is not correct u are asumming that files are always an image, file can have any ext please do check v2 implementation that use https://github.com/OpenStackweb/openstack-uicore-foundation/blob/main/src/components/progressive-img/index.js

Copy link
Copy Markdown
Collaborator

@smarcet smarcet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomrndom please review coments

@smarcet smarcet requested a review from santipalenque May 27, 2026 12:54
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/components/inputs/upload-input-v3/__tests__/upload-input-v3.test.js (1)

327-333: ⚡ Quick win

Strengthen fallback assertion to avoid false positives.

The current check passes for any non-original src (including empty/invalid). Assert the expected fallback asset value instead.

Suggested test tightening
 test('preview image falls back to file_icon on load error', () => {
   const files = [{ filename: 'document.pdf', size: 102400, public_url: 'https://cdn.example.com/document.pdf' }];
   render(<UploadInputV3 {...defaultProps} value={files} />);
   const img = screen.getByRole('img', { name: 'document.pdf' });
   fireEvent.error(img);
-  expect(img).not.toHaveAttribute('src', 'https://cdn.example.com/document.pdf');
+  expect(img.getAttribute('src')).toContain('fileMock');
 });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/inputs/upload-input-v3/__tests__/upload-input-v3.test.js`
around lines 327 - 333, The test "preview image falls back to file_icon on load
error" currently asserts the src is not the original URL which can yield false
positives; update it to assert the exact fallback asset is used after error:
after firing fireEvent.error(img) assert that img has the expected fallback src
(the component's file_icon asset) by checking expect(img).toHaveAttribute('src',
'<expected file_icon path or variable>') or compare against the imported/defined
file_icon constant used by UploadInputV3 so the assertion explicitly verifies
the fallback image is rendered.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/components/inputs/upload-input-v3/__tests__/upload-input-v3.test.js`:
- Around line 327-333: The test "preview image falls back to file_icon on load
error" currently asserts the src is not the original URL which can yield false
positives; update it to assert the exact fallback asset is used after error:
after firing fireEvent.error(img) assert that img has the expected fallback src
(the component's file_icon asset) by checking expect(img).toHaveAttribute('src',
'<expected file_icon path or variable>') or compare against the imported/defined
file_icon constant used by UploadInputV3 so the assertion explicitly verifies
the fallback image is rendered.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9cc56f35-7ec0-4294-8f40-a0c9bb669ec3

📥 Commits

Reviewing files that changed from the base of the PR and between 4804426 and 427d0b8.

📒 Files selected for processing (3)
  • package.json
  • src/components/inputs/upload-input-v3/__tests__/upload-input-v3.test.js
  • src/components/inputs/upload-input-v3/index.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/inputs/upload-input-v3/index.js

Copy link
Copy Markdown
Contributor

@santipalenque santipalenque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other than seba's comment this looks good to me

@tomrndom tomrndom requested a review from smarcet May 27, 2026 15:16
Comment thread src/components/inputs/upload-input-v3/index.js Outdated
Copy link
Copy Markdown
Collaborator

@smarcet smarcet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomrndom please review

tomrndom added 2 commits June 1, 2026 10:26
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/i18n/en.json (1)

142-149: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Fix invalid JSON in src/i18n/en.json (breaks i18n loading)

jq reports a parse error at line 150, column 15 (Expected separator between values), consistent with upload_input_v3 missing its closing } and trailing comma before the "grid_filter" section.

🐛 Proposed fix
   "upload_input_v3": {
     "no_post_url": "No Post URL",
     "upload_disabled": "Upload has been disabled by administrators.",
     "click_upload": "Click to upload",
     "drag_and_drop": "or drag and drop",
     "see_preview": "See Preview",
     "preview_file": "Preview file",
     "complete": "Complete"
-  "grid_filter": {
+  },
+  "grid_filter": {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/i18n/en.json` around lines 142 - 149, The JSON for the "upload_input_v3"
object is malformed (missing its closing brace and the trailing comma before the
next "grid_filter" section), causing i18n load errors; fix by closing the
"upload_input_v3" object with a } and adding the required comma after that
object so the subsequent "grid_filter" key is a valid sibling, ensuring the file
becomes valid JSON (locate the "upload_input_v3" block in src/i18n/en.json to
apply the change).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/i18n/en.json`:
- Around line 142-149: The JSON for the "upload_input_v3" object is malformed
(missing its closing brace and the trailing comma before the next "grid_filter"
section), causing i18n load errors; fix by closing the "upload_input_v3" object
with a } and adding the required comma after that object so the subsequent
"grid_filter" key is a valid sibling, ensuring the file becomes valid JSON
(locate the "upload_input_v3" block in src/i18n/en.json to apply the change).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0248b2cd-0545-4a0b-94ce-36d252963731

📥 Commits

Reviewing files that changed from the base of the PR and between c26cfd8 and da2977c.

📒 Files selected for processing (2)
  • package.json
  • src/i18n/en.json
✅ Files skipped from review due to trivial changes (1)
  • package.json

@smarcet smarcet force-pushed the fix/upload-input-v3-preview-download branch from da2977c to 8efd84c Compare June 1, 2026 18:57
@smarcet smarcet requested review from santipalenque and smarcet June 1, 2026 18:58
Copy link
Copy Markdown
Collaborator

@smarcet smarcet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@smarcet smarcet merged commit 101173c into main Jun 1, 2026
5 checks passed
smarcet added a commit that referenced this pull request Jun 1, 2026
* fix: add image preview and download link for upload input v3

Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>

* fix: add unit test cases for preview image and download link

Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>

* fix: adjust route for fileMock at jest config

Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>

* fix: change preview component, fix src, adjust tests

Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>

* fix: add literals to i18n file

Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>

* fix: adjust test file to use i18n file

Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>

---------

Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
Co-authored-by: sebastian marcet <smarcet@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants