Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show error when image fails to load #466

Merged
merged 2 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion client/src/BadgePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ import Form from 'react-bootstrap/esm/Form';
* Image for previewing a badge with a custom icon, or placeholder text if no image is uploaded
*/
class BadgePreview extends React.Component<{ url: string, label: string }> {
static handleError = (event: React.SyntheticEvent<HTMLImageElement>) => {
const target = event.target as HTMLImageElement;
if (!target.src.includes("critical")) {
target.src = "https://custom-icon-badges.herokuapp.com/badge/failed%20to%20load-try%20compressing%20the%20image%20to%20make%20it%20smaller-critical?logo=x-circle-fill";
}
}

render = () => {
const { url, label } = this.props;
return (
<Form.Group controlId="formFile" className="mb-3 d-flex align-items-center flex-column">
<h3>{label}</h3>
{url ? <img className="m-2" src={url} alt="badge preview" /> : <Card.Text className="text-muted m-2">Upload a file to see a preview</Card.Text>}
{url ? <img className="m-2" src={url} alt="badge preview" onError={BadgePreview.handleError} /> : <Card.Text className="text-muted m-2">Upload a file to see a preview</Card.Text>}
</Form.Group>
);
}
Expand Down
14 changes: 8 additions & 6 deletions client/src/UploadForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class UploadForm extends React.Component<{}, { slug: string, type: string, data:
slug: fileName.split(".")[0],
type: match[1],
data: match[2],
previewUrl: this.buildShieldUrl(dataUrl),
previewUrl: UploadForm.buildShieldUrl(dataUrl),
message: { type: "", content: <div /> },
});
};
Expand Down Expand Up @@ -115,15 +115,17 @@ class UploadForm extends React.Component<{}, { slug: string, type: string, data:
});
};

buildShieldUrl = (
static buildShieldUrl = (
dataUrl: string = "",
text: string = "Preview",
color: string = "#E61B23"
label: string = "badge",
message: string = "preview",
color: string = "success"
): string => {
const encodedDataUrl = encodeURIComponent(dataUrl);
const encodedText = encodeURIComponent(text);
const encodedLabel = encodeURIComponent(label);
const encodedMessage = encodeURIComponent(message);
const encodedColor = encodeURIComponent(color);
return `https://img.shields.io/badge/${encodedText}-${encodedColor}.svg?logo=${encodedDataUrl}`;
return `https://img.shields.io/badge/${encodedLabel}-${encodedMessage}-${encodedColor}.svg?logo=${encodedDataUrl}`;
};

render = () => {
Expand Down