/* eslint-disable indent */ import { invoke } from '@tauri-apps/api/tauri' import React from 'react' import Tr from '../../../utils/language' import './NewsSection.css' interface IProps { selected?: string; } interface IState { selected: string; news: any; commitList: any; } export default class NewsSection extends React.Component { constructor(props: IProps) { super(props) this.state = { selected: props.selected || 'commits', news: null, commitList: null } this.setSelected = this.setSelected.bind(this) this.showNews = this.showNews.bind(this) } componentDidMount() { // Call showNews off the bat this.showNews() } setSelected(item: string) { this.setState({ selected: item }, () => { this.showNews() }) } async showLatestCommits() { if (!this.state.commitList) { const commits: string = await invoke('req_get', { url: 'https://api.grasscutter.io/cultivation/query' }) let obj try { obj = JSON.parse(commits) } catch(e) { obj = {} } // If it didn't work, use official API if (!obj.commits) { const commits: string = await invoke('req_get', { url: 'https://api.github.com/repos/Grasscutters/Grasscutter/commits' }) obj = JSON.parse(commits) } else { const decoded: string = await invoke('base64_decode', { encoded: obj.commits }) const commitData = JSON.parse(decoded) obj = commitData.gc_stable } // Probably rate-limited if (!Array.isArray(obj)) return // Get only first 5 const commitsList = obj.slice(0, 10) const commitsListHtml = commitsList.map((commit: any) => { return ( {commit.commit.author.name} {commit.commit.message} ) }) this.setState({ commitList: commitsListHtml, news: commitsListHtml }) } return this.state.commitList } async showNews() { let news = switch(this.state.selected) { case 'commits': news = await this.showLatestCommits() break case 'latest_version': news = Latest version break default: news = Unknown break } this.setState({ news }) } render() { return (
this.setSelected('commits')}>
this.setSelected('latest_version')}>
{this.state.news}
) } }