From 3d62512814514c368cfdeb1da13ff36e68d01749 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Sat, 14 May 2022 02:05:14 -0700 Subject: [PATCH] show commits in news section --- lang/en.json | 2 +- src-tauri/src/main.rs | 11 +++++- src-tauri/src/web.rs | 6 ++- src/ui/components/news/NewsSection.css | 26 ++++++++++++- src/ui/components/news/NewsSection.tsx | 54 ++++++++++++++++++++++++-- 5 files changed, 92 insertions(+), 7 deletions(-) diff --git a/lang/en.json b/lang/en.json index b4268c9..f7852ab 100644 --- a/lang/en.json +++ b/lang/en.json @@ -22,7 +22,7 @@ "download": "Download" }, "news": { - "latest_commits": "Commits", + "latest_commits": "Recent Commits", "latest_version": "Latest Version" } } \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 05114be..b53862d 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,7 +3,6 @@ all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows" )] -use std::borrow::Borrow; use open; use structs::{APIQuery}; @@ -20,6 +19,7 @@ fn main() { disconnect, run_program, run_jar, + req_get, get_bg_file, downloader::download_file, 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] async fn get_bg_file() -> String { let query = web::query("https://api.grasscutters.xyz/cultivation/query").await; diff --git a/src-tauri/src/web.rs b/src-tauri/src/web.rs index f667221..7dcd631 100644 --- a/src-tauri/src/web.rs +++ b/src-tauri/src/web.rs @@ -1,4 +1,8 @@ +use reqwest::header::USER_AGENT; + 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(); } \ No newline at end of file diff --git a/src/ui/components/news/NewsSection.css b/src/ui/components/news/NewsSection.css index 5fe54e4..1c920b1 100644 --- a/src/ui/components/news/NewsSection.css +++ b/src/ui/components/news/NewsSection.css @@ -2,6 +2,7 @@ background-color: rgba(106, 105, 106, 0.6); display: flex; + flex-direction: column; position: absolute; @@ -17,7 +18,7 @@ display: flex; flex-direction: row; - justify-content: space-between; + justify-content: space-around; align-items: center; font-weight: bold; @@ -42,4 +43,27 @@ .NewsTab.selected { 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%; } \ No newline at end of file diff --git a/src/ui/components/news/NewsSection.tsx b/src/ui/components/news/NewsSection.tsx index a4c3c45..4a55823 100644 --- a/src/ui/components/news/NewsSection.tsx +++ b/src/ui/components/news/NewsSection.tsx @@ -1,3 +1,5 @@ +/* eslint-disable indent */ +import { invoke } from '@tauri-apps/api/tauri' import React from 'react' import Tr from '../../../utils/language' @@ -9,6 +11,7 @@ interface IProps { interface IState { selected: string; + news: any; } export default class NewsSection extends React.Component { @@ -16,14 +19,58 @@ export default class NewsSection extends React.Component { super(props) this.state = { - selected: props.selected || 'commits' + selected: props.selected || 'commits', + news: null } this.setSelected = this.setSelected.bind(this) + this.showNews = this.showNews.bind(this) } setSelected(item: string) { 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 ( +
+
{commit.commit.author.name}
+
{commit.commit.message.substring(0, 50) + '...'}
+
+ ) + }) + + return commitsListHtml + } + + 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() { @@ -33,12 +80,13 @@ export default class NewsSection extends React.Component {
this.setSelected('commits')}>
- -
this.setSelected('latest_version')}>
+
+ {this.state.news} +
) }