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

191 lines
5.4 KiB
JavaScript
Raw Normal View History

2022-10-10 18:43:29 +00:00
import { Button, Card, Container, Grid, Input, Link, Modal, Spacer, Switch, Text, textTransforms } from '@nextui-org/react';
2022-10-10 12:28:31 +00:00
import { Component } from 'react';
import NavBar from '../src/components/Navbar';
2022-10-10 12:14:25 +00:00
2022-10-10 18:43:29 +00:00
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
2022-10-10 12:28:31 +00:00
import styles from '../styles/main.module.css';
2022-10-10 12:14:25 +00:00
export default class Home extends Component {
2022-02-14 17:43:52 +00:00
constructor(props) {
super(props);
2022-10-10 17:38:06 +00:00
this.state = {
2022-10-10 19:01:11 +00:00
loading: true,
2022-10-10 17:38:06 +00:00
steamId: undefined,
2022-10-10 18:43:29 +00:00
isPreviewVisible: false,
previewUrl: undefined,
overlayUrl: undefined,
values: {
socketAddr: undefined,
useBeatLeader: false,
showPlayerStats: true,
showScoreInfo: false,
showSongInfo: false,
},
2022-10-10 17:38:06 +00:00
}
}
async componentDidMount() {
2022-10-10 12:14:25 +00:00
const urlSearchParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries());
if (params.id) {
document.location.href = "/overlay/"+ window.location.search
2022-10-10 17:38:06 +00:00
return;
2022-10-10 12:14:25 +00:00
}
2022-10-10 17:38:06 +00:00
2022-10-10 18:43:29 +00:00
if (localStorage.getItem('values') == undefined) {
localStorage.setItem('values', JSON.stringify({
steamId: this.state.steamId,
values: this.state.values
}));
} else {
const json = JSON.parse(localStorage.getItem('values'))
this.setState({ steamId: json.steamId, values: json.values });
}
2022-10-10 19:01:11 +00:00
this.setState({ loading: false });
2022-10-10 17:38:06 +00:00
}
loadPreview() {
2022-10-10 18:43:29 +00:00
this.setState({ isPreviewVisible: true, previewUrl: this.generateUrl(true) });
}
generateUrl(withTc = false) {
let values = "";
Object.entries(this.state.values).forEach(value => {
if (value[1] === undefined) {
return;
}
if (value[0] == "useBeatLeader" && value[1] === true) {
values += `&beatLeader=${value[1]}`;
return;
}
values += `&${value[0]}=${value[1]}`;
});
2022-10-10 17:38:06 +00:00
2022-10-10 18:50:02 +00:00
return window.location.origin + "/overlay?id=" + this.state.steamId + values + (withTc ? "&textColor=black" : "");
2022-10-10 17:38:06 +00:00
}
2022-10-10 18:43:29 +00:00
updateValue(key, value) {
let values = this.state.values;
values[key] = value;
this.setState({ values: values });
this.updateStorage();
}
updateStorage() {
setTimeout(() => {
localStorage.setItem('values', JSON.stringify({
steamId: this.state.steamId,
values: this.state.values
}));
}, 5);
}
render() {
2022-10-10 19:01:11 +00:00
return this.state.loading ? <h1>Loading...</h1> :
<div className={styles.main}>
2022-10-10 12:28:31 +00:00
<NavBar></NavBar>
<Container css={{
2022-10-10 17:38:06 +00:00
marginTop: '$8'
2022-10-10 12:28:31 +00:00
}}>
2022-10-10 18:43:29 +00:00
{/* Preview */}
{
this.state.isPreviewVisible ? <Modal
closeButton
open={this.state.isPreviewVisible}
width={"100%"}
blur
onClose={() => this.setState({ isPreviewVisible: false })}
>
<Modal.Header>
<Text size={18}>
Overlay Preview
</Text>
</Modal.Header>
<Modal.Body>
<iframe height={600} src={this.state.previewUrl}></iframe>
</Modal.Body>
</Modal> : <></>
}
2022-10-10 17:38:06 +00:00
<Grid.Container gap={2}>
2022-10-10 12:28:31 +00:00
<Grid xs={12}>
<Card>
<Card.Body>
2022-10-10 19:04:04 +00:00
<Text b>Welcome to the Setup panel</Text>
2022-10-10 17:38:06 +00:00
<Spacer y={2} />
<Input
underlined
labelPlaceholder="Ip Address (Only set if you stream on multiple devices)"
initialValue="localhost"
2022-10-10 18:43:29 +00:00
value={this.state.values.socketAddr}
onChange={event => this.updateValue("socketAddr", event.target.value)} checked={true}
2022-10-10 17:38:06 +00:00
/>
<Spacer y={2} />
<Input
underlined
labelPlaceholder="Steam Id"
initialValue=""
2022-10-10 18:43:29 +00:00
value={this.state.steamId}
onChange={event => {
this.setState({ steamId: event.target.value });
this.updateStorage();
}}
2022-10-10 17:38:06 +00:00
/>
2022-10-10 18:43:29 +00:00
<Spacer y={1} />
<Text>Do you want to use BeatLeader rather than ScoreSaber?</Text>
<Switch onChange={event => this.updateValue("useBeatLeader", event.target.checked)} checked={this.state.values.useBeatLeader} size="md" />
2022-10-10 17:38:06 +00:00
<Text>Do you want to show Player Stats (Current PP, global pos, etc)</Text>
2022-10-10 18:43:29 +00:00
<Switch onChange={event => this.updateValue("showPlayerStats", event.target.checked)} checked={this.state.values.showPlayerStats} size="md" />
<Text>Do you want to show Score Info (Current swing values, total score, etc)</Text>
<Switch onChange={event => this.updateValue("showScoreInfo", event.target.checked)} checked={this.state.values.showScoreInfo} size="md" />
2022-10-10 17:38:06 +00:00
<Text>Do you want to show Song Info (Song name, bsr, song art, etc)</Text>
2022-10-10 18:43:29 +00:00
<Switch onChange={event => this.updateValue("showSongInfo", event.target.checked)} checked={this.state.values.showSongInfo} size="md" />
2022-10-10 19:04:04 +00:00
<Spacer y={1} />
2022-10-10 17:38:06 +00:00
2022-10-10 18:43:29 +00:00
<Button.Group>
<Button flat auto onClick={() => {
if (!this.state.steamId) {
toast.error("Please provide a Steam ID");
return;
}
window.open(this.generateUrl(), "_blank");
}}>Open Overlay</Button>
<Button flat auto onClick={() => {
if (!this.state.steamId) {
toast.error("Please provide a Steam ID");
return;
}
this.loadPreview();
}}>Preview</Button>
</Button.Group>
2022-10-10 17:38:06 +00:00
2022-10-10 18:43:29 +00:00
{
this.state.overlayUrl !== undefined ?
<>
<Text b>
Url
</Text>
<Link href={this.state.overlayUrl}>{this.state.overlayUrl}</Link>
</>
: <></>
}
2022-10-10 12:28:31 +00:00
</Card.Body>
</Card>
</Grid>
</Grid.Container>
</Container>
2022-10-10 18:43:29 +00:00
<ToastContainer />
2022-10-10 12:14:25 +00:00
</div>
2022-10-10 19:01:11 +00:00
}
}