mue/src/components/modals/main/settings/sections/Changelog.jsx

161 lines
4.2 KiB
React
Raw Normal View History

import variables from 'modules/variables';
import { PureComponent, createRef } from 'react';
import { MdOutlineWifiOff } from 'react-icons/md';
import Modal from 'react-modal';
2021-04-26 12:11:40 +00:00
import Lightbox from '../../marketplace/Lightbox';
2021-04-26 12:11:40 +00:00
2021-08-14 19:10:48 +00:00
export default class Changelog extends PureComponent {
2021-03-23 13:10:34 +00:00
constructor() {
super();
this.state = {
2021-04-26 12:11:40 +00:00
title: null,
showLightbox: false,
lightboxImg: null,
};
this.offlineMode = localStorage.getItem('offlineMode') === 'true';
this.controller = new AbortController();
2021-08-27 22:08:32 +00:00
this.changelog = createRef();
}
async getUpdate() {
const res = await fetch(variables.constants.BLOG_POST + '/index.json', {
signal: this.controller.signal,
});
if (res.status === 404) {
this.setState({ error: true });
return;
}
if (this.controller.signal.aborted === true) {
return;
}
2021-04-16 17:43:23 +00:00
const data = await res.json();
let date = new Date(data.date.split(' ')[0]);
date = date.toLocaleDateString(variables.languagecode.replace('_', '-'), {
year: 'numeric',
month: 'long',
day: 'numeric',
2021-04-26 12:11:40 +00:00
});
this.setState({
title: data.title,
date,
2021-04-16 17:43:23 +00:00
image: data.featured_image || null,
author: variables.getMessage('modals.main.settings.sections.changelog.by', {
author: data.authors.join(', '),
}),
content: data.markdown,
});
2021-04-26 12:11:40 +00:00
// lightbox etc
const images = this.changelog.current.getElementsByTagName('img');
const links = this.changelog.current.getElementsByTagName('a');
2021-04-26 12:11:40 +00:00
2021-05-26 21:28:26 +00:00
for (const img of images) {
2021-04-26 12:11:40 +00:00
img.draggable = false;
img.onclick = () => {
this.setState({
showLightbox: true,
lightboxImg: img.src,
2021-04-26 12:11:40 +00:00
});
};
}
2021-04-26 12:11:40 +00:00
2021-08-27 22:08:32 +00:00
// open in new tab
2021-04-26 12:11:40 +00:00
for (let link = 0; link < links.length; link++) {
links[link].target = '_blank';
links[link].rel = 'noopener noreferrer';
2021-04-26 12:11:40 +00:00
}
}
componentDidMount() {
2021-08-27 22:08:32 +00:00
if (navigator.onLine === false || this.offlineMode) {
2021-04-16 17:43:23 +00:00
return;
}
this.getUpdate();
}
componentWillUnmount() {
// stop making requests
this.controller.abort();
}
render() {
2021-04-16 17:43:23 +00:00
const errorMessage = (msg) => {
return (
<div className="emptyItems">
<div className="emptyMessage">{msg}</div>
2021-04-16 17:43:23 +00:00
</div>
);
};
if (navigator.onLine === false || this.offlineMode) {
return errorMessage(
<>
<MdOutlineWifiOff />
<h1>{variables.getMessage('modals.main.marketplace.offline.title')}</h1>
<p className="description">
{variables.getMessage('modals.main.marketplace.offline.description')}
</p>
</>,
);
2021-04-16 17:43:23 +00:00
}
if (this.state.error === true) {
return errorMessage(
<>
<MdOutlineWifiOff />
2022-11-06 11:59:59 +00:00
<span className="title">{variables.getMessage('modals.main.error_boundary.title')}</span>
<span className="subtitle">
{variables.getMessage('modals.main.error_boundary.message')}
</span>
</>,
);
}
2021-04-16 17:43:23 +00:00
if (!this.state.title) {
return errorMessage(
<div className="loaderHolder">
<div id="loader"></div>
<span className="subtitle">{variables.getMessage('modals.main.loading')}</span>
</div>,
);
2021-04-16 17:43:23 +00:00
}
return (
<div className="changelogtab" ref={this.changelog}>
2021-08-27 22:08:32 +00:00
<h1>{this.state.title}</h1>
<h5>
{this.state.author} {this.state.date}
</h5>
{this.state.image ? (
<img
draggable="false"
src={this.state.image}
alt={this.state.title}
className="updateImage"
/>
) : null}
<div className="updateChangelog">{this.state.content}</div>
<Modal
closeTimeoutMS={100}
onRequestClose={() => this.setState({ showLightbox: false })}
isOpen={this.state.showLightbox}
className="Modal lightBoxModal"
overlayClassName="Overlay resetoverlay"
ariaHideApp={false}
>
<Lightbox
modalClose={() => this.setState({ showLightbox: false })}
img={this.state.lightboxImg}
/>
</Modal>
2021-04-27 21:17:36 +00:00
</div>
);
}
}