Skip to content

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

Open
tomrndom wants to merge 4 commits into
mainfrom
fix/upload-input-v3-preview-download
Open

fix: add image preview and download link for upload input v3#257
tomrndom wants to merge 4 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 now show image thumbnails that open the file in a new tab; filenames are clickable download links for direct downloading.
  • Bug Fixes

    • Thumbnails use a fallback icon when previews fail to load and reliably resolve the correct file URL when the primary link is unavailable.

Review Change Stack

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

coderabbitai Bot commented May 27, 2026

📝 Walkthrough

Walkthrough

Uploaded-file rows now show linked image thumbnails (ProgressiveImg) with a local file_icon fallback, normalize and rewrite file preview URLs, convert filenames into download 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 ProgressiveImg and file_icon imports and updates onError default prop formatting.
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 tiny preview hops into view,
A click will open, a link to pursue,
If images stumble, an icon will stay,
Files now preview and download with play! 🎨

🚥 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 changes: adding image preview and download link functionality to the upload input v3 component, which aligns with all three modified files.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/upload-input-v3-preview-download

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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
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