show commits in news section

This commit is contained in:
SpikeHD 2022-05-14 02:05:14 -07:00
parent 053c43a079
commit 3d62512814
5 changed files with 92 additions and 7 deletions

View File

@ -22,7 +22,7 @@
"download": "Download" "download": "Download"
}, },
"news": { "news": {
"latest_commits": "Commits", "latest_commits": "Recent Commits",
"latest_version": "Latest Version" "latest_version": "Latest Version"
} }
} }

View File

@ -3,7 +3,6 @@ all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows" windows_subsystem = "windows"
)] )]
use std::borrow::Borrow;
use open; use open;
use structs::{APIQuery}; use structs::{APIQuery};
@ -20,6 +19,7 @@ fn main() {
disconnect, disconnect,
run_program, run_program,
run_jar, run_jar,
req_get,
get_bg_file, get_bg_file,
downloader::download_file, downloader::download_file,
downloader::stop_download, downloader::stop_download,
@ -65,6 +65,15 @@ fn run_jar(path: String, execute_in: String) {
}; };
} }
#[tauri::command]
async fn req_get(url: String) -> String {
// Send a GET request to the specified URL.
let response = web::query(&url.to_string()).await;
// Send the response body back to the client.
return response;
}
#[tauri::command] #[tauri::command]
async fn get_bg_file() -> String { async fn get_bg_file() -> String {
let query = web::query("https://api.grasscutters.xyz/cultivation/query").await; let query = web::query("https://api.grasscutters.xyz/cultivation/query").await;

View File

@ -1,4 +1,8 @@
use reqwest::header::USER_AGENT;
pub(crate) async fn query(site: &str) -> String { pub(crate) async fn query(site: &str) -> String {
let response = reqwest::get(site).await.unwrap(); let client = reqwest::Client::new();
let response = client.get(site).header(USER_AGENT, "cultivation").send().await.unwrap();
return response.text().await.unwrap(); return response.text().await.unwrap();
} }

View File

@ -2,6 +2,7 @@
background-color: rgba(106, 105, 106, 0.6); background-color: rgba(106, 105, 106, 0.6);
display: flex; display: flex;
flex-direction: column;
position: absolute; position: absolute;
@ -17,7 +18,7 @@
display: flex; display: flex;
flex-direction: row; flex-direction: row;
justify-content: space-between; justify-content: space-around;
align-items: center; align-items: center;
font-weight: bold; font-weight: bold;
@ -42,4 +43,27 @@
.NewsTab.selected { .NewsTab.selected {
border-bottom: 1px solid #ffc61e; border-bottom: 1px solid #ffc61e;
}
.NewsContent {
overflow-y: auto;
}
.Commit {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: flex-start;
color: #fff;
margin: 8px;
}
.CommitAuthor {
font-weight: bold;
}
.CommitMessage {
width: 60%;
} }

View File

@ -1,3 +1,5 @@
/* eslint-disable indent */
import { invoke } from '@tauri-apps/api/tauri'
import React from 'react' import React from 'react'
import Tr from '../../../utils/language' import Tr from '../../../utils/language'
@ -9,6 +11,7 @@ interface IProps {
interface IState { interface IState {
selected: string; selected: string;
news: any;
} }
export default class NewsSection extends React.Component<IProps, IState> { export default class NewsSection extends React.Component<IProps, IState> {
@ -16,14 +19,58 @@ export default class NewsSection extends React.Component<IProps, IState> {
super(props) super(props)
this.state = { this.state = {
selected: props.selected || 'commits' selected: props.selected || 'commits',
news: null
} }
this.setSelected = this.setSelected.bind(this) this.setSelected = this.setSelected.bind(this)
this.showNews = this.showNews.bind(this)
} }
setSelected(item: string) { setSelected(item: string) {
this.setState({ selected: item }) this.setState({ selected: item })
this.showNews()
}
async showLatestCommits() {
const commits: string = await invoke('req_get', { url: 'https://api.github.com/repos/Grasscutters/Grasscutter/commits' })
const obj = JSON.parse(commits)
// Get only first 5
const commitsList = obj.slice(0, 5)
const commitsListHtml = commitsList.map((commit: any) => {
return (
<div className="Commit" key={commit.sha}>
<div className="CommitAuthor">{commit.commit.author.name}</div>
<div className="CommitMessage">{commit.commit.message.substring(0, 50) + '...'}</div>
</div>
)
})
return commitsListHtml
}
async showNews() {
let news = <div></div>
switch(this.state.selected) {
case 'commits':
news = await this.showLatestCommits()
break
case 'latest_version':
news = <div>Latest version</div>
break
default:
news = <div>Unknown</div>
break
}
this.setState({
news
})
} }
render() { render() {
@ -33,12 +80,13 @@ export default class NewsSection extends React.Component<IProps, IState> {
<div className={'NewsTab ' + (this.state.selected === 'commits' ? 'selected' : '')} id="commits" onClick={() => this.setSelected('commits')}> <div className={'NewsTab ' + (this.state.selected === 'commits' ? 'selected' : '')} id="commits" onClick={() => this.setSelected('commits')}>
<Tr text="news.latest_commits" /> <Tr text="news.latest_commits" />
</div> </div>
</div>
<div className="NewsTabs">
<div className={'NewsTab ' + (this.state.selected === 'latest_version' ? 'selected' : '')} id="latest_version" onClick={() => this.setSelected('latest_version')}> <div className={'NewsTab ' + (this.state.selected === 'latest_version' ? 'selected' : '')} id="latest_version" onClick={() => this.setSelected('latest_version')}>
<Tr text="news.latest_version" /> <Tr text="news.latest_version" />
</div> </div>
</div> </div>
<div className="NewsContent">
{this.state.news}
</div>
</div> </div>
) )
} }