Fix tsc and ESLint warnings

This commit is contained in:
Brian Bowman 2022-07-11 01:17:59 -05:00
parent 51d00add22
commit 53e2b0cbed
10 changed files with 82 additions and 45 deletions

View File

@ -33,6 +33,27 @@
"semi": [
"error",
"never"
],
"@typescript-eslint/ban-types": [
"warn",
{
"extendDefaults": true,
"types": {
"{}": false
}
}
],
"@typescript-eslint/no-unused-vars": [
"warn",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}
]
},
"settings": {
"react": {
"version": "detect"
}
}
}

View File

@ -56,7 +56,7 @@ class App extends React.Component<IProps, IState> {
console.log(payload)
})
listen('jar_extracted', ({ payload }) => {
listen('jar_extracted', ({ payload }: { payload: string}) => {
setConfigOption('grasscutter_path', payload)
})

View File

@ -40,7 +40,7 @@ function none() {
alert('none')
}
class Debug extends React.Component<any, any>{
class Debug extends React.Component{
render() {
return (
<div className="App">

View File

@ -11,10 +11,6 @@ import Server from '../../resources/icons/server.svg'
import './ServerLaunchSection.css'
import {dataDir} from '@tauri-apps/api/path'
interface IProps {
[key: string]: any
}
interface IState {
grasscutterEnabled: boolean;
buttonLabel: string;
@ -31,8 +27,8 @@ interface IState {
httpsEnabled: boolean;
}
export default class ServerLaunchSection extends React.Component<IProps, IState> {
constructor(props: IProps) {
export default class ServerLaunchSection extends React.Component<{}, IState> {
constructor(props: {}) {
super(props)
this.state = {

View File

@ -5,7 +5,6 @@ import closeIcon from '../../resources/icons/close.svg'
import minIcon from '../../resources/icons/min.svg'
import cogBtn from '../../resources/icons/cog.svg'
import downBtn from '../../resources/icons/download.svg'
import gameBtn from '../../resources/icons/game.svg'
import Tr from '../../utils/language'

View File

@ -3,7 +3,7 @@ import './BigButton.css'
interface IProps {
children: React.ReactNode;
onClick: () => any;
onClick: () => unknown;
id: string;
disabled?: boolean;
}
@ -23,7 +23,7 @@ export default class BigButton extends React.Component<IProps, IState> {
this.handleClick = this.handleClick.bind(this)
}
static getDerivedStateFromProps(props: IProps, state: IState) {
static getDerivedStateFromProps(props: IProps, _state: IState) {
return {
disabled: props.disabled
}

View File

@ -12,9 +12,7 @@ interface IProps {
id?: string;
clearable?: boolean;
customClearBehaviour?: () => void;
style?: {
[key: string]: any;
}
style?: React.CSSProperties;
}
interface IState {

View File

@ -8,7 +8,7 @@ import { dataDir } from '@tauri-apps/api/path'
import './Downloads.css'
import Divider from './Divider'
import { getConfigOption, setConfigOption } from '../../../utils/configuration'
import { getConfigOption } from '../../../utils/configuration'
import { invoke } from '@tauri-apps/api'
import { listen } from '@tauri-apps/api/event'
import HelpButton from '../common/HelpButton'
@ -183,7 +183,7 @@ export default class Downloads extends React.Component<IProps, IState> {
grasscutter_downloading: this.props.downloadManager.downloadingJar(),
resources_downloading: this.props.downloadManager.downloadingResources(),
repo_downloading: this.props.downloadManager.downloadingRepo(),
grasscutter_set: gc_path && gc_path !== '',
grasscutter_set: gc_path !== '',
})
}

View File

@ -11,8 +11,28 @@ interface IProps {
interface IState {
selected: string;
news: any;
commitList: any;
news?: JSX.Element;
commitList?: JSX.Element[];
}
interface GrasscutterAPIResponse {
commits: {
gc_stable: CommitResponse[];
gc_dev: CommitResponse[];
cultivation: CommitResponse[];
}
}
interface CommitResponse {
sha: string;
commit: Commit;
}
interface Commit {
author: {
name: string;
};
message: string;
}
export default class NewsSection extends React.Component<IProps, IState> {
@ -21,8 +41,6 @@ export default class NewsSection extends React.Component<IProps, IState> {
this.state = {
selected: props.selected || 'commits',
news: null,
commitList: null
}
this.setSelected = this.setSelected.bind(this)
@ -42,40 +60,41 @@ export default class NewsSection extends React.Component<IProps, IState> {
async showLatestCommits() {
if (!this.state.commitList) {
const commits: string = await invoke('req_get', { url: 'https://api.grasscutter.io/cultivation/query' })
let obj
const response: string = await invoke('req_get', { url: 'https://api.grasscutter.io/cultivation/query' })
let grasscutterApiResponse: GrasscutterAPIResponse | null = null
try {
obj = JSON.parse(commits)
grasscutterApiResponse = JSON.parse(response)
} catch(e) {
obj = {}
grasscutterApiResponse = null
}
// 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)
let commits: CommitResponse[]
if (grasscutterApiResponse?.commits == null) {
// If it didn't work, use official API
const response: string = await invoke('req_get', { url: 'https://api.github.com/repos/Grasscutters/Grasscutter/commits' })
commits = JSON.parse(response)
} else {
obj = obj.commits.gc_stable
commits = grasscutterApiResponse.commits.gc_stable
}
// Probably rate-limited
if (!Array.isArray(obj)) return
if (!Array.isArray(commits)) return
// Get only first 5
const commitsList = obj.slice(0, 10)
const commitsListHtml = commitsList.map((commit: any) => {
const commitsList = commits.slice(0, 10)
const commitsListHtml = commitsList.map((commitResponse: CommitResponse) => {
return (
<tr className="Commit" id="newsCommitsTable" key={commit.sha}>
<td className="CommitAuthor"><span>{commit.commit.author.name}</span></td>
<td className="CommitMessage"><span>{commit.commit.message}</span></td>
<tr className="Commit" id="newsCommitsTable" key={commitResponse.sha}>
<td className="CommitAuthor"><span>{commitResponse.commit.author.name}</span></td>
<td className="CommitMessage"><span>{commitResponse.commit.message}</span></td>
</tr>
)
})
this.setState({
commitList: commitsListHtml,
news: commitsListHtml
news: <>{commitsListHtml}</>
})
}
@ -83,12 +102,16 @@ export default class NewsSection extends React.Component<IProps, IState> {
}
async showNews() {
let news = <tr></tr>
let news: JSX.Element | JSX.Element[] = <tr></tr>
switch(this.state.selected) {
case 'commits':
news = await this.showLatestCommits()
case 'commits': {
const commits = await this.showLatestCommits()
if (commits != null) {
news = commits
}
break
}
case 'latest_version':
news = <tr><td>Latest version</td></tr>
@ -100,7 +123,7 @@ export default class NewsSection extends React.Component<IProps, IState> {
}
this.setState({
news
news: <>{news}</>
})
}

View File

@ -45,16 +45,16 @@ export interface Configuration {
debug_enabled: boolean
}
export async function setConfigOption(key: string, value: any): Promise<void> {
const config: any = await getConfig()
export async function setConfigOption<K extends keyof Configuration>(key: K, value: Configuration[K]): Promise<void> {
const config = await getConfig()
config[key] = value
await saveConfig(<Configuration> config)
}
export async function getConfigOption(key: string): Promise<any> {
const config: any = await getConfig()
const defaults: any = defaultConfig
export async function getConfigOption<K extends keyof Configuration>(key: K): Promise<Configuration[K]> {
const config = await getConfig()
const defaults = defaultConfig
return config[key] || defaults[key]
}