This repository has been archived on 2023-11-06. You can view files and clone it, but cannot push or open issues or pull requests.
beatsaber-overlay/pages/index.js

89 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-02-14 17:43:52 +00:00
import Head from 'next/head'
import { Component } from 'react'
import Avatar from '../components/Avatar';
import PlayerStats from '../components/PlayerStats';
2022-02-14 17:43:52 +00:00
// Why do u have to proxy requests... it's so dumb LOL
const API_URL = "https://bangor375.herokuapp.com/https://scoresaber.com/api/player/%s/full";
2022-02-14 17:43:52 +00:00
export default class Home extends Component {
2022-02-14 17:43:52 +00:00
constructor(props) {
super(props);
2022-02-14 17:43:52 +00:00
this.state = {
loading: true,
id: undefined,
isValidScoresaber: true,
data: undefined
}
}
2022-02-14 17:43:52 +00:00
async componentDidMount() {
const urlSearchParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries());
2022-02-14 17:43:52 +00:00
const id = params.id;
if (!id) { // Check if the id pararm is valid
this.setState({ loading: false, isValidScoresaber: false });
return;
}
2022-02-14 17:43:52 +00:00
await this.updateData(id);
setTimeout(async () => {
if (!this.state.isValidScoresaber) {
return;
}
await this.updateData(id);
}, 30_000); // Update the scoresaber data every 30 seconds.
}
2022-02-14 17:43:52 +00:00
async updateData(id) {
const data = await fetch(API_URL.replace("%s", id), {
mode: 'cors'
});
if (data.status == 422) { // invalid scoresaber
this.setState({ loading: false, isValidScoresaber: false });
return;
}
const json = await data.json();
if (json.errorMessage) {
this.setState({ loading: false, isValidScoresaber: false });
return;
}
this.setState({ loading: false, id: id, data: json });
console.log(json);
}
render() {
const { loading, isValidScoresaber, data } = this.state;
return <>
{ loading ?
<>
<p>Loading...</p>
</>
: !isValidScoresaber ?
<div className={'invalid-player'}>
<p>Provide a valid scoresaber id</p>
<p>Example: {document.location.origin}?id=76561198449412074</p>
<div className={'info'}>
<p>This is currently just a simple overlay displaying ScoreSaber stats.</p>
<p>If you have any suggestions you can message me on discord @ Fascinated#4719</p>
</div>
</div> :
<div className={'overlay'}>
<div className={'player-stats-container'}>
<Avatar url={data.profilePicture} />
<PlayerStats
pp={data.pp.toLocaleString()}
globalPos={data.rank.toLocaleString()}
country={data.country}
countryRank={data.countryRank.toLocaleString()}
/>
</div>
</div>
}
</>
}
}