mirror of
1
Fork 0
gotosocial/web/source/settings-panel/index.js

178 lines
4.7 KiB
JavaScript
Raw Normal View History

2022-09-07 13:51:16 +02:00
/*
2022-09-09 20:43:55 +02:00
GoToSocial
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
2022-09-07 13:51:16 +02:00
2022-09-09 20:43:55 +02:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2022-09-07 13:51:16 +02:00
2022-09-09 20:43:55 +02:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
2022-09-07 13:51:16 +02:00
2022-09-09 20:43:55 +02:00
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2022-09-07 13:51:16 +02:00
*/
"use strict";
const Promise = require("bluebird");
const React = require("react");
2022-09-16 15:49:02 +02:00
const ReactDom = require("react-dom/client");
2022-09-09 20:43:55 +02:00
const Redux = require("react-redux");
2022-09-13 22:09:09 +02:00
const { Switch, Route, Redirect } = require("wouter");
2022-09-09 20:43:55 +02:00
const { Provider } = require("react-redux");
const { PersistGate } = require("redux-persist/integration/react");
2022-09-07 13:51:16 +02:00
2022-09-09 20:43:55 +02:00
const { store, persistor } = require("./redux");
const api = require("./lib/api");
2022-09-15 20:02:55 +02:00
const oauth = require("./redux/reducers/oauth").actions;
const { AuthenticationError } = require("./lib/errors");
2022-09-09 20:43:55 +02:00
const Login = require("./components/login");
2022-09-07 13:51:16 +02:00
2022-09-10 19:23:50 +02:00
require("./style.css");
2022-09-09 01:39:43 +02:00
2022-09-09 20:43:55 +02:00
// TODO: nested categories?
2022-09-07 13:51:16 +02:00
const nav = {
2022-09-09 01:39:43 +02:00
"User": {
2022-09-13 18:35:27 +02:00
"Profile": require("./user/profile.js"),
"Settings": require("./user/settings.js"),
2022-09-09 01:39:43 +02:00
},
"Admin": {
2022-09-15 20:02:55 +02:00
adminOnly: true,
2022-09-13 18:35:27 +02:00
"Instance Settings": require("./admin/settings.js"),
2022-09-16 15:49:27 +02:00
"Actions": require("./admin/actions"),
2022-09-13 18:35:27 +02:00
"Federation": require("./admin/federation.js"),
2022-09-13 21:33:13 +02:00
"Custom Emoji": require("./admin/emoji.js"),
2022-09-09 01:39:43 +02:00
}
2022-09-07 13:51:16 +02:00
};
2022-09-15 20:02:55 +02:00
const { sidebar, panelRouter } = require("./lib/get-views")(nav);
2022-09-07 13:51:16 +02:00
function App() {
const dispatch = Redux.useDispatch();
2022-09-15 20:02:55 +02:00
const { loginState, isAdmin } = Redux.useSelector((state) => state.oauth);
const reduxTempStatus = Redux.useSelector((state) => state.temporary.status);
2022-09-15 20:02:55 +02:00
const [errorMsg, setErrorMsg] = React.useState();
const [tokenChecked, setTokenChecked] = React.useState(false);
React.useEffect(() => {
2022-09-15 17:27:32 +02:00
if (loginState == "login" || loginState == "callback") {
Promise.try(() => {
// Process OAUTH authorization token from URL if available
if (loginState == "callback") {
let urlParams = new URLSearchParams(window.location.search);
let code = urlParams.get("code");
2022-09-15 20:02:55 +02:00
2022-09-15 17:27:32 +02:00
if (code == undefined) {
setErrorMsg(new Error("Waiting for OAUTH callback but no ?code= provided. You can try logging in again:"));
} else {
return dispatch(api.oauth.tokenize(code));
}
}
2022-09-15 17:27:32 +02:00
}).then(() => {
// Fetch current instance info
return dispatch(api.instance.fetch());
}).then(() => {
// Check currently stored auth token for validity if available
2022-09-11 19:05:06 +02:00
return dispatch(api.user.fetchAccount());
2022-09-15 17:27:32 +02:00
}).then(() => {
setTokenChecked(true);
2022-09-15 20:02:55 +02:00
return dispatch(api.oauth.checkIfAdmin());
2022-09-15 17:27:32 +02:00
}).catch((e) => {
2022-09-15 20:02:55 +02:00
if (e instanceof AuthenticationError) {
dispatch(oauth.remove());
e.message = "Stored OAUTH token no longer valid, please log in again.";
}
2022-09-15 17:27:32 +02:00
setErrorMsg(e);
2022-09-16 18:50:25 +02:00
console.error(e);
2022-09-15 17:27:32 +02:00
});
}
}, []);
let ErrorElement = null;
if (errorMsg != undefined) {
ErrorElement = (
<div className="error">
<b>{errorMsg.type}</b>
<span>{errorMsg.message}</span>
</div>
);
}
const LogoutElement = (
2022-09-15 20:02:55 +02:00
<button className="logout" onClick={() => { dispatch(api.oauth.logout()); }}>
Log out
</button>
);
if (reduxTempStatus != undefined) {
return (
<section>
{reduxTempStatus}
</section>
);
} else if (tokenChecked && loginState == "login") {
2022-09-07 13:51:16 +02:00
return (
<>
<div className="sidebar">
2022-09-15 20:02:55 +02:00
{sidebar.all}
{isAdmin && sidebar.admin}
{LogoutElement}
2022-09-07 13:51:16 +02:00
</div>
2022-09-10 19:23:50 +02:00
<section className="with-sidebar">
{ErrorElement}
2022-09-07 13:51:16 +02:00
<Switch>
2022-09-15 20:02:55 +02:00
{panelRouter.all}
{isAdmin && panelRouter.admin}
<Route> {/* default route */}
2022-09-13 22:09:09 +02:00
<Redirect to="/settings/user" />
</Route>
2022-09-07 13:51:16 +02:00
</Switch>
</section>
</>
);
} else if (loginState == "none") {
return (
2022-09-15 20:02:55 +02:00
<Login error={ErrorElement} />
);
2022-09-07 13:51:16 +02:00
} else {
let status;
2022-09-15 20:02:55 +02:00
if (loginState == "login") {
status = "Verifying stored login...";
} else if (loginState == "callback") {
status = "Processing OAUTH callback...";
}
2022-09-10 19:23:50 +02:00
return (
<section>
<div>
{status}
</div>
{ErrorElement}
{LogoutElement}
</section>
2022-09-10 19:23:50 +02:00
);
2022-09-07 13:51:16 +02:00
}
2022-09-07 13:51:16 +02:00
}
2022-09-09 20:43:55 +02:00
function Main() {
return (
<Provider store={store}>
<PersistGate loading={"loading..."} persistor={persistor}>
<App />
</PersistGate>
</Provider>
);
}
2022-09-16 15:49:02 +02:00
const root = ReactDom.createRoot(document.getElementById("root"));
root.render(<React.StrictMode><Main /></React.StrictMode>);