2023-01-13 23:05:28 +00:00
|
|
|
import { useContext, useRef, useState, useEffect } from 'react';
|
2022-08-24 20:30:48 +00:00
|
|
|
import { setNodeConfig } from 'api/setNodeConfig';
|
|
|
|
import { getNodeAccounts } from 'api/getNodeAccounts';
|
|
|
|
import { removeAccount } from 'api/removeAccount';
|
|
|
|
import { addAccountCreate } from 'api/addAccountCreate';
|
2023-01-13 23:05:28 +00:00
|
|
|
import { useNavigation, useLocation } from 'react-router-dom';
|
2022-08-24 20:30:48 +00:00
|
|
|
|
2023-01-13 23:05:28 +00:00
|
|
|
export function useDashboard() {
|
2022-08-24 20:30:48 +00:00
|
|
|
|
|
|
|
const [state, setState] = useState({
|
2022-09-01 07:22:43 +00:00
|
|
|
domain: "",
|
|
|
|
accountStorage: null,
|
|
|
|
keyType: null,
|
2022-11-10 05:11:49 +00:00
|
|
|
pushSupported: null,
|
2022-09-01 07:22:43 +00:00
|
|
|
enableImage: null,
|
|
|
|
enableAudio: null,
|
|
|
|
enableVideo: null,
|
2023-01-13 23:05:28 +00:00
|
|
|
|
|
|
|
errorMessage: null,
|
|
|
|
createToken: null,
|
2022-08-24 20:30:48 +00:00
|
|
|
showSettings: false,
|
2023-01-13 23:05:28 +00:00
|
|
|
showCreate: false,
|
2022-08-24 20:30:48 +00:00
|
|
|
busy: false,
|
|
|
|
accounts: [],
|
|
|
|
});
|
|
|
|
|
2023-01-13 23:05:28 +00:00
|
|
|
const navigate = useNavigation();
|
|
|
|
const location = useLocation();
|
|
|
|
const token = useRef();
|
2022-09-06 18:23:04 +00:00
|
|
|
|
2022-08-24 20:30:48 +00:00
|
|
|
const updateState = (value) => {
|
|
|
|
setState((s) => ({ ...s, ...value }));
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
setCreateLink: async () => {
|
|
|
|
if (!state.createBusy) {
|
2023-01-13 23:05:28 +00:00
|
|
|
updateState({ busy: true });
|
2022-08-24 20:30:48 +00:00
|
|
|
try {
|
2023-01-13 23:05:28 +00:00
|
|
|
const create = await addAccountCreate(token.current)
|
2022-08-24 20:30:48 +00:00
|
|
|
updateState({ createToken: create, showCreate: true });
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
window.alert(err);
|
|
|
|
}
|
2023-01-13 23:05:28 +00:00
|
|
|
updateState({ busy: false });
|
2022-08-24 20:30:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
setShowCreate: (showCreate) => {
|
|
|
|
updateState({ showCreate });
|
|
|
|
},
|
|
|
|
removeAccount: async (accountId) => {
|
|
|
|
await removeAccount(token, accountId);
|
|
|
|
actions.getAccounts();
|
|
|
|
},
|
2022-09-01 07:22:43 +00:00
|
|
|
setHost: (domain) => {
|
|
|
|
updateState({ domain });
|
2022-08-24 20:30:48 +00:00
|
|
|
},
|
2022-09-01 07:22:43 +00:00
|
|
|
setStorage: (accountStorage) => {
|
|
|
|
updateState({ accountStorage });
|
|
|
|
},
|
|
|
|
setKeyType: (keyType) => {
|
|
|
|
updateState({ keyType });
|
|
|
|
},
|
2022-11-10 05:11:49 +00:00
|
|
|
setPushSupported: (pushSupported) => {
|
|
|
|
updateState({ pushSupported });
|
|
|
|
},
|
2022-09-01 07:22:43 +00:00
|
|
|
setEnableImage: (enableImage) => {
|
|
|
|
updateState({ enableImage });
|
|
|
|
},
|
|
|
|
setEnableAudio: (enableAudio) => {
|
|
|
|
updateState({ enableAudio });
|
|
|
|
},
|
|
|
|
setEnableVideo: (enableVideo) => {
|
|
|
|
updateState({ enableVideo });
|
2022-08-24 20:30:48 +00:00
|
|
|
},
|
|
|
|
setShowSettings: (value) => {
|
|
|
|
updateState({ showSettings: value });
|
|
|
|
},
|
|
|
|
setSettings: async () => {
|
|
|
|
if (!state.busy) {
|
|
|
|
updateState({ busy: true });
|
|
|
|
try {
|
2022-11-10 05:11:49 +00:00
|
|
|
const { domain, keyType, accountStorage, pushSupported, enableImage, enableAudio, enableVideo } = state;
|
2023-01-13 23:05:28 +00:00
|
|
|
const storage = accountStorage * 1073741824;
|
|
|
|
const config = { domain, accountStorage: storage, keyType, enableImage, enableAudio, enableVideo, pushSupported };
|
|
|
|
await setNodeConfig(token.current, config);
|
|
|
|
updateState({ busy: false, showSettings: false });
|
2022-08-24 20:30:48 +00:00
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
console.log(err);
|
2023-01-13 23:05:28 +00:00
|
|
|
updateState({ busy: false });
|
|
|
|
throw new Error("failed to set settings");
|
2022-08-24 20:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getAccounts: async () => {
|
2023-01-13 23:05:28 +00:00
|
|
|
if (!state.busy) {
|
|
|
|
updateState({ busy: true });
|
2022-08-24 20:30:48 +00:00
|
|
|
try {
|
2023-01-13 23:05:28 +00:00
|
|
|
let accounts = await getNodeAccounts(token.current);
|
2022-08-24 20:30:48 +00:00
|
|
|
accounts.sort((a, b) => {
|
|
|
|
if (a.handle < b.handle) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.handle > b.handle) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
2023-01-13 23:05:28 +00:00
|
|
|
updateState({ busy: false, accounts });
|
2022-08-24 20:30:48 +00:00
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
console.log(err);
|
2023-01-13 23:05:28 +00:00
|
|
|
updateState({ busy: false, errorMessage: 'failed to load accounts' });
|
2022-08-24 20:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-09-06 18:23:04 +00:00
|
|
|
useEffect(() => {
|
2023-01-13 23:05:28 +00:00
|
|
|
const params = new URLSearchParams(location);
|
|
|
|
const pass = params.get("pass");
|
|
|
|
if (!pass) {
|
|
|
|
navigate('/admin');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
token.current = pass;
|
|
|
|
const config = JSON.parse(params.get("config"));
|
|
|
|
const { storage, domain, keyType, pushSupported, enableImage, enableAudio, enableVideo } = config;
|
|
|
|
const accountStorage = Math.ceil(accountStorage / 1073741824);
|
|
|
|
updateState({ domain, accountStorage, keyType, enableImage, enableAudio, enableVideo, pushSupported });
|
|
|
|
actions.getAccounts();
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2022-08-24 20:30:48 +00:00
|
|
|
return { state, actions };
|
|
|
|
}
|
|
|
|
|