2022-04-24 02:49:27 +00:00
|
|
|
import { useEffect, useState, useRef, useContext } from 'react';
|
2022-07-13 06:48:27 +00:00
|
|
|
import { getAvailable } from 'api/getAvailable';
|
|
|
|
import { setLogin } from 'api/setLogin';
|
2022-11-11 20:42:26 +00:00
|
|
|
import { clearLogin } from 'api/clearLogin';
|
2022-06-08 08:25:41 +00:00
|
|
|
import { setAccountAccess } from 'api/setAccountAccess';
|
2022-06-08 19:52:06 +00:00
|
|
|
import { addAccount } from 'api/addAccount';
|
|
|
|
import { getUsername } from 'api/getUsername';
|
2022-04-24 02:49:27 +00:00
|
|
|
import { AccountContext } from './AccountContext';
|
|
|
|
import { ProfileContext } from './ProfileContext';
|
|
|
|
import { ArticleContext } from './ArticleContext';
|
|
|
|
import { GroupContext } from './GroupContext';
|
|
|
|
import { CardContext } from './CardContext';
|
|
|
|
import { ChannelContext } from './ChannelContext';
|
2022-07-11 07:19:59 +00:00
|
|
|
import { StoreContext } from './StoreContext';
|
2022-07-20 22:49:42 +00:00
|
|
|
import { UploadContext } from './UploadContext';
|
2022-03-17 21:42:51 +00:00
|
|
|
|
2022-03-17 07:14:34 +00:00
|
|
|
export function useAppContext() {
|
2022-11-11 20:42:26 +00:00
|
|
|
const [state, setState] = useState({
|
|
|
|
});
|
2022-04-24 02:49:27 +00:00
|
|
|
const [appRevision, setAppRevision] = useState();
|
2022-03-17 21:42:51 +00:00
|
|
|
|
2022-11-11 20:42:26 +00:00
|
|
|
const appName = "Databag";
|
|
|
|
const appVersion = "1.0.0";
|
|
|
|
const userAgent = window.navigator.userAgent;
|
|
|
|
|
2022-03-17 07:14:34 +00:00
|
|
|
const ws = useRef(null);
|
|
|
|
const revision = useRef(null);
|
2022-04-25 21:29:57 +00:00
|
|
|
|
2022-03-17 07:14:34 +00:00
|
|
|
const updateState = (value) => {
|
|
|
|
setState((s) => ({ ...s, ...value }))
|
|
|
|
}
|
2022-05-16 06:33:17 +00:00
|
|
|
|
2022-07-20 22:49:42 +00:00
|
|
|
const uploadContext = useContext(UploadContext);
|
2022-07-11 07:19:59 +00:00
|
|
|
const storeContext = useContext(StoreContext);
|
2022-04-24 02:49:27 +00:00
|
|
|
const accountContext = useContext(AccountContext);
|
|
|
|
const profileContext = useContext(ProfileContext);
|
|
|
|
const channelContext = useContext(ChannelContext);
|
|
|
|
const cardContext = useContext(CardContext);
|
|
|
|
const groupContext = useContext(GroupContext);
|
|
|
|
const articleContext = useContext(ArticleContext);
|
|
|
|
|
2022-03-17 21:42:51 +00:00
|
|
|
const resetData = () => {
|
|
|
|
revision.current = null;
|
2022-05-16 06:33:17 +00:00
|
|
|
accountContext.actions.clearToken();
|
|
|
|
profileContext.actions.clearToken();
|
|
|
|
articleContext.actions.clearToken();
|
|
|
|
groupContext.actions.clearToken();
|
|
|
|
cardContext.actions.clearToken();
|
|
|
|
channelContext.actions.clearToken();
|
2022-03-26 08:37:47 +00:00
|
|
|
setState({});
|
2022-03-17 21:42:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 20:20:10 +00:00
|
|
|
const actions = {
|
2022-03-17 07:14:34 +00:00
|
|
|
logout: () => {
|
2022-08-04 22:20:48 +00:00
|
|
|
appLogout();
|
2022-07-11 07:19:59 +00:00
|
|
|
storeContext.actions.clear();
|
2022-07-20 22:49:42 +00:00
|
|
|
uploadContext.actions.clear();
|
2022-03-17 21:42:51 +00:00
|
|
|
resetData();
|
2022-03-21 05:12:42 +00:00
|
|
|
},
|
2022-08-03 20:20:10 +00:00
|
|
|
access: async (token) => {
|
2022-08-04 22:20:48 +00:00
|
|
|
await appAccess(token)
|
2022-08-03 20:20:10 +00:00
|
|
|
},
|
|
|
|
login: async (username, password) => {
|
2022-08-04 22:20:48 +00:00
|
|
|
await appLogin(username, password)
|
2022-08-03 20:20:10 +00:00
|
|
|
},
|
|
|
|
create: async (username, password, token) => {
|
2022-08-04 22:20:48 +00:00
|
|
|
await appCreate(username, password, token)
|
2022-08-03 20:20:10 +00:00
|
|
|
},
|
|
|
|
username: getUsername,
|
|
|
|
available: getAvailable,
|
2022-03-15 08:05:44 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 20:14:37 +00:00
|
|
|
const appCreate = async (username, password, token) => {
|
|
|
|
await addAccount(username, password, token);
|
2022-11-11 20:42:26 +00:00
|
|
|
let access = await setLogin(username, password, appName, appVersion, userAgent)
|
2022-08-03 20:20:10 +00:00
|
|
|
updateState({ access: access.appToken });
|
2022-07-21 20:14:37 +00:00
|
|
|
storeContext.actions.setValue('login:timestamp', access.created);
|
|
|
|
setWebsocket(access.appToken)
|
|
|
|
localStorage.setItem("session", JSON.stringify({
|
2022-08-04 22:20:48 +00:00
|
|
|
access: access.appToken,
|
2022-07-21 20:14:37 +00:00
|
|
|
timestamp: access.created,
|
|
|
|
}));
|
|
|
|
return access.created;
|
|
|
|
}
|
|
|
|
|
2022-08-03 20:20:10 +00:00
|
|
|
const appLogin = async (username, password) => {
|
2022-11-11 20:42:26 +00:00
|
|
|
let access = await setLogin(username, password, appName, appVersion, userAgent)
|
2022-08-03 20:20:10 +00:00
|
|
|
updateState({ access: access.appToken });
|
2022-07-21 20:14:37 +00:00
|
|
|
storeContext.actions.setValue('login:timestamp', access.created);
|
|
|
|
setWebsocket(access.appToken)
|
|
|
|
localStorage.setItem("session", JSON.stringify({
|
2022-08-04 22:20:48 +00:00
|
|
|
access: access.appToken,
|
2022-07-21 20:14:37 +00:00
|
|
|
timestamp: access.created,
|
|
|
|
}));
|
|
|
|
return access.created;
|
|
|
|
}
|
|
|
|
|
|
|
|
const appAccess = async (token) => {
|
2022-11-11 20:42:26 +00:00
|
|
|
let access = await setAccountAccess(token, appName, appVersion, userAgent)
|
2022-10-20 21:35:46 +00:00
|
|
|
updateState({ access: access.appToken });
|
|
|
|
storeContext.actions.setValue('login:timestamp', access.created);
|
|
|
|
setWebsocket(access.appToken)
|
|
|
|
localStorage.setItem("session", JSON.stringify({
|
|
|
|
access: access.appToken,
|
|
|
|
timestamp: access.created,
|
|
|
|
}));
|
|
|
|
return access.created;
|
2022-07-21 20:14:37 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 20:42:26 +00:00
|
|
|
const appLogout = async () => {
|
|
|
|
try {
|
|
|
|
await clearLogin(state.access);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2022-08-03 20:20:10 +00:00
|
|
|
updateState({ access: null });
|
2022-07-21 20:14:37 +00:00
|
|
|
clearWebsocket()
|
|
|
|
localStorage.removeItem("session");
|
|
|
|
}
|
|
|
|
|
2022-04-24 02:49:27 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (appRevision) {
|
|
|
|
accountContext.actions.setRevision(appRevision.account);
|
|
|
|
profileContext.actions.setRevision(appRevision.profile);
|
|
|
|
articleContext.actions.setRevision(appRevision.article);
|
|
|
|
groupContext.actions.setRevision(appRevision.group);
|
|
|
|
cardContext.actions.setRevision(appRevision.card);
|
|
|
|
channelContext.actions.setRevision(appRevision.channel);
|
|
|
|
}
|
2022-09-02 05:35:28 +00:00
|
|
|
// eslint-disable-next-line
|
2022-04-24 02:49:27 +00:00
|
|
|
}, [appRevision]);
|
|
|
|
|
2022-03-17 07:14:34 +00:00
|
|
|
const setWebsocket = (token) => {
|
2022-04-24 02:49:27 +00:00
|
|
|
|
|
|
|
accountContext.actions.setToken(token);
|
|
|
|
profileContext.actions.setToken(token);
|
|
|
|
articleContext.actions.setToken(token);
|
|
|
|
groupContext.actions.setToken(token);
|
|
|
|
cardContext.actions.setToken(token);
|
|
|
|
channelContext.actions.setToken(token);
|
|
|
|
|
2022-08-31 21:57:17 +00:00
|
|
|
let protocol;
|
2022-08-31 22:01:23 +00:00
|
|
|
if (window.location.protocol === 'http:') {
|
2022-08-31 21:57:17 +00:00
|
|
|
protocol = 'ws://';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
protocol = 'wss://';
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.current = new WebSocket(protocol + window.location.host + "/status");
|
2022-03-15 08:05:44 +00:00
|
|
|
ws.current.onmessage = (ev) => {
|
2022-04-08 04:57:45 +00:00
|
|
|
try {
|
2022-04-25 21:29:57 +00:00
|
|
|
let rev = JSON.parse(ev.data);
|
|
|
|
setAppRevision(rev);
|
2022-06-29 06:44:56 +00:00
|
|
|
updateState({ disconnected: false });
|
2022-03-17 07:14:34 +00:00
|
|
|
}
|
2022-04-08 04:57:45 +00:00
|
|
|
catch (err) {
|
|
|
|
console.log(err);
|
2022-03-17 07:14:34 +00:00
|
|
|
}
|
2022-03-15 08:05:44 +00:00
|
|
|
}
|
2022-03-23 18:39:31 +00:00
|
|
|
ws.current.onclose = (e) => {
|
2022-06-29 06:44:56 +00:00
|
|
|
updateState({ disconnected: true });
|
2022-03-23 18:39:31 +00:00
|
|
|
console.log(e)
|
2022-03-15 08:05:44 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
if (ws.current != null) {
|
|
|
|
ws.current.onmessage = () => {}
|
|
|
|
ws.current.onclose = () => {}
|
|
|
|
ws.current.onopen = () => {}
|
|
|
|
ws.current.onerror = () => {}
|
2022-03-17 07:14:34 +00:00
|
|
|
setWebsocket(token);
|
2022-03-15 08:05:44 +00:00
|
|
|
}
|
2022-10-28 17:07:58 +00:00
|
|
|
}, 1000)
|
2022-03-15 08:05:44 +00:00
|
|
|
}
|
|
|
|
ws.current.onopen = () => {
|
|
|
|
ws.current.send(JSON.stringify({ AppToken: token }))
|
|
|
|
}
|
2022-03-23 18:39:31 +00:00
|
|
|
ws.current.error = (e) => {
|
2022-06-29 06:44:56 +00:00
|
|
|
updateState({ disconnected: true });
|
2022-03-23 18:39:31 +00:00
|
|
|
console.log(e)
|
2022-03-15 08:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 07:14:34 +00:00
|
|
|
const clearWebsocket = () => {
|
|
|
|
ws.current.onclose = () => {}
|
|
|
|
ws.current.close()
|
|
|
|
ws.current = null
|
|
|
|
}
|
|
|
|
|
2022-03-15 03:45:54 +00:00
|
|
|
useEffect(() => {
|
2022-03-15 08:05:44 +00:00
|
|
|
const storage = localStorage.getItem('session');
|
|
|
|
if (storage != null) {
|
|
|
|
try {
|
|
|
|
const session = JSON.parse(storage)
|
2022-08-04 22:20:48 +00:00
|
|
|
if (session?.access) {
|
|
|
|
setState({ access: session.access })
|
|
|
|
setWebsocket(session.access);
|
2022-03-15 08:05:44 +00:00
|
|
|
} else {
|
2022-03-17 07:14:34 +00:00
|
|
|
setState({})
|
2022-03-15 08:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
console.log(err)
|
2022-03-17 07:14:34 +00:00
|
|
|
setState({})
|
2022-03-15 08:05:44 +00:00
|
|
|
}
|
2022-03-15 03:45:54 +00:00
|
|
|
} else {
|
2022-03-17 07:14:34 +00:00
|
|
|
setState({})
|
2022-03-15 03:45:54 +00:00
|
|
|
}
|
2022-09-02 05:35:28 +00:00
|
|
|
// eslint-disable-next-line
|
2022-03-15 03:45:54 +00:00
|
|
|
}, []);
|
2022-03-15 08:05:44 +00:00
|
|
|
|
2022-04-27 17:33:51 +00:00
|
|
|
if (state == null) {
|
|
|
|
return {};
|
2022-03-16 07:21:53 +00:00
|
|
|
}
|
2022-08-03 20:20:10 +00:00
|
|
|
return { state, actions }
|
2022-03-15 03:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|