BetterDiscordApp-rauenzi/src/ui/publicservers/menu.js

270 lines
9.3 KiB
JavaScript
Raw Normal View History

2019-06-15 04:11:19 +02:00
import {React, WebpackModules} from "modules";
2019-05-28 23:27:25 +02:00
import SettingsTitle from "../settings/title";
import ServerCard from "./card";
2019-06-20 20:03:48 +02:00
import Manager from "./manager";
2019-05-28 23:27:25 +02:00
2019-06-19 05:09:49 +02:00
const SettingsView = WebpackModules.getByDisplayName("SettingsView");
2019-06-10 05:40:35 +02:00
export default class PublicServers extends React.Component {
2019-05-28 20:19:48 +02:00
constructor(props) {
super(props);
2019-06-10 05:40:35 +02:00
this.state = {
2019-06-19 05:09:49 +02:00
selectedCategory: "All",
2019-06-10 05:40:35 +02:00
title: "Loading...",
loading: true,
servers: [],
next: null,
connection: {
state: 0,
user: null
}
};
2019-05-28 20:19:48 +02:00
this.close = this.close.bind(this);
this.changeCategory = this.changeCategory.bind(this);
this.search = this.search.bind(this);
this.searchKeyDown = this.searchKeyDown.bind(this);
this.checkConnection = this.checkConnection.bind(this);
this.connect = this.connect.bind(this);
}
componentDidMount() {
this.checkConnection();
2019-06-20 20:03:48 +02:00
}
2019-05-28 20:19:48 +02:00
close() {
2019-06-15 04:11:19 +02:00
this.props.close();
2019-05-28 20:19:48 +02:00
}
search(query, clear) {
$.ajax({
method: "GET",
2019-06-19 05:09:49 +02:00
url: `${this.endPoint}${query}${query ? "&schema=new" : "?schema=new"}`,
2019-05-28 20:19:48 +02:00
success: data => {
let servers = data.results.reduce((arr, server) => {
server.joined = false;
arr.push(server);
2019-06-19 05:09:49 +02:00
// arr.push(<ServerCard server={server} join={this.join}/>);
2019-05-28 20:19:48 +02:00
return arr;
}, []);
if (!clear) {
2019-06-19 05:09:49 +02:00
servers = this.state.servers.concat(servers);
2019-05-28 20:19:48 +02:00
}
else {
2019-06-19 05:09:49 +02:00
//servers.unshift(this.bdServer);
2019-05-28 20:19:48 +02:00
}
2019-06-19 05:09:49 +02:00
console.log(data);
2019-05-28 20:19:48 +02:00
let end = data.size + data.from;
data.next = `?from=${end}`;
2019-06-19 05:09:49 +02:00
if (this.state.term) data.next += `&term=${this.state.term}`;
if (this.state.selectedCategory) data.next += `&category=${this.state.selectedCategory}`;
2019-05-28 20:19:48 +02:00
if (end >= data.total) {
end = data.total;
data.next = null;
}
2019-06-19 05:09:49 +02:00
let title = `Showing 1-${end} of ${data.total} results in ${this.state.selectedCategory}`;
if (this.state.term) title += ` for ${this.state.term}`;
2019-05-28 20:19:48 +02:00
2019-06-19 05:09:49 +02:00
this.setState({
2019-05-28 20:19:48 +02:00
loading: false,
title: title,
servers: servers,
next: data.next
});
if (clear) {
2019-06-19 05:09:49 +02:00
//console.log(this);
// this.refs.sbv.refs.contentScroller.scrollTop = 0;
2019-05-28 20:19:48 +02:00
}
},
error: () => {
2019-06-19 05:09:49 +02:00
this.setState({
2019-05-28 20:19:48 +02:00
loading: false,
title: "Failed to load servers. Check console for details"
});
}
});
}
2019-06-20 20:03:48 +02:00
async connect() {
await Manager.connect();
this.checkConnection();
2019-05-28 20:19:48 +02:00
}
get windowOptions() {
return {
width: 500,
height: 550,
backgroundColor: "#282b30",
show: true,
resizable: false,
maximizable: false,
minimizable: false,
alwaysOnTop: true,
frame: false,
2019-05-28 20:55:07 +02:00
center: false,
webPreferences: {
nodeIntegration: false
}
2019-05-28 20:19:48 +02:00
};
}
get bdServer() {
const server = {
2019-05-28 20:19:48 +02:00
name: "BetterDiscord",
online: "7500+",
members: "20000+",
categories: ["community", "programming", "support"],
description: "Official BetterDiscord server for support etc",
identifier: "86004744966914048",
iconUrl: "https://cdn.discordapp.com/icons/86004744966914048/292e7f6bfff2b71dfd13e508a859aedd.webp",
nativejoin: true,
invite_code: "0Tmfo5ZbORCRqbAd",
pinned: true
};
2019-06-20 20:03:48 +02:00
return React.createElement(ServerCard, {server: server, pinned: true});
2019-05-28 20:19:48 +02:00
}
get endPoint() {
return "https://search.discordservers.com";
}
get joinEndPoint() {
2019-05-28 20:55:07 +02:00
return "https://j.discordservers.com";
2019-05-28 20:19:48 +02:00
}
get connectEndPoint() {
return "https://join.discordservers.com/connect";
}
2019-06-20 20:03:48 +02:00
async checkConnection() {
const userData = await Manager.checkConnection();
if (!userData) {
return this.setState({loading: true, connection: {state: 1, user: null}});
2019-05-28 20:19:48 +02:00
}
2019-06-20 20:03:48 +02:00
this.setState({connection: {state: 2, user: userData}});
this.search("", true);
2019-05-28 20:19:48 +02:00
}
render() {
2019-06-20 20:03:48 +02:00
const categories = this.categoryButtons.map(name => ({
section: name,
2019-06-20 04:19:34 +02:00
label: name,
2019-06-20 20:03:48 +02:00
element: () => this.content
})
);
2019-06-19 05:09:49 +02:00
return React.createElement(SettingsView, {
onClose: this.close,
2019-06-20 20:03:48 +02:00
onSetSection: this.changeCategory,
2019-06-19 05:09:49 +02:00
section: this.state.selectedCategory,
sections: [
2019-06-20 20:03:48 +02:00
{section: "HEADER", label: "Search"},
2019-06-19 05:09:49 +02:00
{section: "CUSTOM", element: () => this.searchInput},
{section: "HEADER", label: "Categories"},
2019-06-20 20:03:48 +02:00
...categories,
{section: "DIVIDER"},
{section: "HEADER", label: React.createElement("a", {href: "https://discordservers.com", target: "_blank"}, "Discordservers.com")},
{section: "DIVIDER"},
{section: "CUSTOM", element: () => this.connection}
2019-06-19 05:09:49 +02:00
],
theme: "dark"
});
// return React.createElement(StandardSidebarView, {id: "pubslayer", ref: "sbv", notice: null, theme: "dark", closeAction: this.close, content: this.content, sidebar: this.sidebar});
2019-05-28 20:19:48 +02:00
}
get searchInput() {
2019-05-30 07:06:17 +02:00
return React.createElement(
2019-05-28 20:19:48 +02:00
"div",
{className: "ui-form-item"},
2019-05-30 07:06:17 +02:00
React.createElement(
2019-05-28 20:19:48 +02:00
"div",
{className: "ui-text-input flex-vertical", style: {width: "172px", marginLeft: "10px"}},
2019-06-19 05:09:49 +02:00
React.createElement("input", {onKeyDown: this.searchKeyDown, onChange: () => {}, type: "text", className: "input default", placeholder: "Search...", maxLength: "50"})
2019-05-28 20:19:48 +02:00
)
);
}
searchKeyDown(e) {
2019-06-19 05:09:49 +02:00
if (this.state.loading || e.which !== 13) return;
this.setState({
2019-05-28 20:19:48 +02:00
loading: true,
title: "Loading...",
term: e.target.value
});
let query = `?term=${e.target.value}`;
2019-06-20 20:03:48 +02:00
if (this.state.selectedCategory !== "All") {
2019-06-19 05:09:49 +02:00
query += `&category=${this.state.selectedCategory}`;
2019-05-28 20:19:48 +02:00
}
2019-06-19 05:09:49 +02:00
this.search(query, true);
2019-05-28 20:19:48 +02:00
}
get categoryButtons() {
2019-05-28 20:55:07 +02:00
return ["All", "FPS Games", "MMO Games", "Strategy Games", "MOBA Games", "RPG Games", "Tabletop Games", "Sandbox Games", "Simulation Games", "Music", "Community", "Language", "Programming", "Other"];
2019-05-28 20:19:48 +02:00
}
changeCategory(id) {
2019-06-19 05:09:49 +02:00
if (this.state.loading) return;
// this.refs.searchinput.value = "";
this.setState({
2019-05-28 20:19:48 +02:00
loading: true,
selectedCategory: id,
title: "Loading...",
term: null
});
2019-06-20 20:03:48 +02:00
if (id === "All") {
2019-06-19 05:09:49 +02:00
this.search("", true);
2019-05-28 20:19:48 +02:00
return;
}
2019-06-19 05:09:49 +02:00
this.search(`?category=${this.state.selectedCategory.replace(" ", "%20")}`, true);
2019-05-28 20:19:48 +02:00
}
get content() {
2019-06-20 20:03:48 +02:00
const pinned = this.state.selectedCategory == "All" || this.state.connection.state === 1 ? this.bdServer : null;
const servers = this.state.servers.map((server) => {
return React.createElement(ServerCard, {key: server.identifier, server: server});
});
2019-06-19 05:09:49 +02:00
return [React.createElement(SettingsTitle, {text: this.state.title}),
2019-06-20 20:03:48 +02:00
pinned,
servers,
this.state.next ? this.nextButton : null,
2019-06-19 05:09:49 +02:00
this.state.servers.length > 0 && React.createElement(SettingsTitle, {text: this.state.title})];
2019-05-28 20:19:48 +02:00
}
2019-06-20 20:03:48 +02:00
get nextButton() {
return React.createElement("button", {
type: "button",
onClick: () => {
if (this.state.loading) return;
this.setState({loading: true});
this.search(this.state.next, false);
},
className: "ui-button filled brand small grow",
style: {width: "100%", marginTop: "10px", marginBottom: "10px"}
},
React.createElement("div", {className: "ui-button-contents"}, this.state.loading ? "Loading" : "Load More")
2019-05-28 20:19:48 +02:00
);
}
get connection() {
2019-06-19 05:09:49 +02:00
const {connection} = this.state;
2019-05-30 07:06:17 +02:00
if (connection.state !== 2) return React.createElement("span", null);
2019-05-28 20:19:48 +02:00
2019-06-20 20:03:48 +02:00
return React.createElement("span", null,
React.createElement("span", {style: {color: "#b9bbbe", fontSize: "10px", marginLeft: "10px"}},
2019-05-28 20:19:48 +02:00
"Connected as: ",
`${connection.user.username}#${connection.user.discriminator}`
),
2019-06-20 20:03:48 +02:00
React.createElement("div", {style: {padding: "5px 10px 0 10px"}},
React.createElement("button",
2019-05-28 20:19:48 +02:00
{style: {width: "100%", minHeight: "20px"}, type: "button", className: "ui-button filled brand small grow"},
2019-06-20 20:03:48 +02:00
React.createElement("div", {className: "ui-button-contents", onClick: this.connect}, "Reconnect")
2019-05-28 20:19:48 +02:00
)
)
);
2019-06-20 20:03:48 +02:00
}
2019-05-28 20:55:07 +02:00
}