Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyanndavis committed Nov 11, 2024
1 parent 0dbcb0e commit 56d9c39
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions workbench/src/renderer/components/Changelog/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import pkg from '../../../../package.json';
import { ipcMainChannels } from '../../../main/ipcMainChannels';

const { ipcRenderer } = window.Workbench.electron;
const { logger } = window.Workbench;

export default function Changelog(props) {
const { t } = useTranslation();
Expand All @@ -17,30 +18,36 @@ export default function Changelog(props) {
// Load HTML from external file (which is generated by Python build process).
useEffect(() => {
async function loadHtml() {
const baseUrl = await ipcRenderer.invoke(ipcMainChannels.BASE_URL)
fetch(`${baseUrl}/changelog.html`)
.then(response => response.text())
.then(htmlString => {
// Find the section whose heading explicitly matches the current version.
const versionStr = pkg.version;
const escapedVersionStr = versionStr.split('.').join('\\.');
const sectionRegex = new RegExp(
`<section.*?>[\\s]*?<h1>${escapedVersionStr}\\b[\\s\\S]*?</h1>[\\s\\S]*?</section>`
const baseUrl = await ipcRenderer.invoke(ipcMainChannels.BASE_URL);
const response = await fetch(`${baseUrl}/changelog.html`);
if (!response.ok) {
logger.debug(`Error fetching changelog HTML: ${response.status} ${response.statusText}`);
return;
}
try {
const htmlString = await response.text();
// Find the section whose heading explicitly matches the current version.
const versionStr = pkg.version;
const escapedVersionStr = versionStr.split('.').join('\\.');
const sectionRegex = new RegExp(
`<section.*?>[\\s]*?<h1>${escapedVersionStr}\\b[\\s\\S]*?</h1>[\\s\\S]*?</section>`
);
const sectionMatches = htmlString.match(sectionRegex);
if (sectionMatches && sectionMatches.length) {
let latestVersionSection = sectionMatches[0];
const linkRegex = /<a\shref/g;
// Ensure all links open in a new window and are styled with a relevant icon.
latestVersionSection = latestVersionSection.replaceAll(
linkRegex,
'<a target="_blank" class="link-external" href'
);
const sectionMatches = htmlString.match(sectionRegex);
if (sectionMatches && sectionMatches.length) {
let latestVersionSection = sectionMatches[0];
const linkRegex = /<a\shref/g;
// Ensure all links open in a new window and are styled with a relevant icon.
latestVersionSection = latestVersionSection.replaceAll(
linkRegex,
'<a target="_blank" class="link-external" href'
);
setHtmlContent({
__html: latestVersionSection
});
}
});
setHtmlContent({
__html: latestVersionSection
});
}
} catch(error) {
logger.debug(error);
}
}
loadHtml();
}, []);
Expand Down

0 comments on commit 56d9c39

Please sign in to comment.