databag/net/web/src/dashboard/useDashboard.hook.js

184 lines
5.4 KiB
JavaScript
Raw Normal View History

2023-01-14 20:02:50 +00:00
import { useContext, useState, useEffect } from 'react';
2023-01-14 00:45:59 +00:00
import { getNodeConfig } from 'api/getNodeConfig';
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-14 00:45:59 +00:00
import { useNavigate } from 'react-router-dom';
import { AppContext } from 'context/AppContext';
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({
domain: "",
accountStorage: null,
keyType: null,
pushSupported: null,
allowUnsealed: null,
transformSupported: false,
enableImage: null,
enableAudio: null,
enableVideo: null,
2023-04-13 07:39:14 +00:00
enableIce: null,
iceUrl: null,
iceUsername: null,
icePassword: null,
2023-07-18 05:39:38 +00:00
enableOpenAccess: null,
openAccessLimit: null,
2023-01-13 23:05:28 +00:00
2023-01-14 04:20:18 +00:00
configError: false,
accountsError: false,
2023-01-13 23:05:28 +00:00
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-14 00:45:59 +00:00
const navigate = useNavigate();
const app = useContext(AppContext);
2022-08-24 20:30:48 +00:00
const updateState = (value) => {
setState((s) => ({ ...s, ...value }));
}
2023-01-14 00:45:59 +00:00
useEffect(() => {
if (!app.state.adminToken) {
2023-01-14 16:07:23 +00:00
navigate('/');
2023-01-14 00:45:59 +00:00
}
else {
syncConfig();
syncAccounts();
}
2023-01-14 20:02:50 +00:00
// eslint-disable-next-line
2023-01-14 00:45:59 +00:00
}, [app]);
2022-08-24 20:30:48 +00:00
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-14 00:45:59 +00:00
const create = await addAccountCreate(app.state.adminToken)
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) => {
2023-01-14 00:45:59 +00:00
await removeAccount(app.state.adminToken, accountId);
2023-01-15 05:11:10 +00:00
syncAccounts();
2022-08-24 20:30:48 +00:00
},
setHost: (domain) => {
updateState({ domain });
2022-08-24 20:30:48 +00:00
},
setStorage: (accountStorage) => {
updateState({ accountStorage });
},
setKeyType: (keyType) => {
updateState({ keyType });
},
setPushSupported: (pushSupported) => {
updateState({ pushSupported });
},
setAllowUnsealed: (allowUnsealed) => {
updateState({ allowUnsealed });
},
setEnableImage: (enableImage) => {
updateState({ enableImage });
},
setEnableAudio: (enableAudio) => {
updateState({ enableAudio });
},
setEnableVideo: (enableVideo) => {
updateState({ enableVideo });
2022-08-24 20:30:48 +00:00
},
2023-04-13 07:39:14 +00:00
setEnableIce: (enableIce) => {
updateState({ enableIce });
},
setIceUrl: (iceUrl) => {
updateState({ iceUrl });
},
setIceUsername: (iceUsername) => {
updateState({ iceUsername });
},
setIcePassword: (icePassword) => {
updateState({ icePassword });
},
2023-07-18 05:39:38 +00:00
setEnableOpenAccess: (enableOpenAccess) => {
updateState({ enableOpenAccess });
},
setOpenAccessLimit: (openAccessLimit) => {
updateState({ openAccessLimit });
},
2022-08-24 20:30:48 +00:00
setShowSettings: (value) => {
updateState({ showSettings: value });
},
2023-01-14 00:45:59 +00:00
logout: () => {
app.actions.clearAdmin();
},
2023-01-14 04:20:18 +00:00
reload: async () => {
await syncConfig();
2023-01-14 00:45:59 +00:00
await syncAccounts();
},
2022-08-24 20:30:48 +00:00
setSettings: async () => {
if (!state.busy) {
updateState({ busy: true });
try {
const { domain, keyType, accountStorage, pushSupported, transformSupported, allowUnsealed, enableImage, enableAudio, enableVideo, enableIce, iceUrl, iceUsername, icePassword, enableOpenAccess, openAccessLimit } = state;
2023-01-13 23:05:28 +00:00
const storage = accountStorage * 1073741824;
2024-01-14 07:24:07 +00:00
const config = { domain, accountStorage: storage, keyType, enableImage, enableAudio, enableVideo, pushSupported, transformSupported, allowUnsealed, enableIce, iceUrl, iceUsername, icePassword, enableOpenAccess, openAccessLimit };
2023-01-14 00:45:59 +00:00
await setNodeConfig(app.state.adminToken, config);
2023-01-13 23:05:28 +00:00
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
}
}
},
};
2023-01-14 00:45:59 +00:00
const syncConfig = async () => {
try {
const config = await getNodeConfig(app.state.adminToken);
const { storage, domain, keyType, pushSupported, transformSupported, allowUnsealed, enableImage, enableAudio, enableVideo, enableIce, iceUrl, iceUsername, icePassword, enableOpenAccess, openAccessLimit } = config;
2023-01-14 00:45:59 +00:00
const accountStorage = Math.ceil(storage / 1073741824);
updateState({ configError: false, domain, accountStorage, keyType, enableImage, enableAudio, enableVideo, pushSupported, transformSupported, allowUnsealed, enableIce, iceUrl, iceUsername, icePassword, enableOpenAccess, openAccessLimit });
2023-01-13 23:05:28 +00:00
}
2023-01-14 00:45:59 +00:00
catch(err) {
console.log(err);
2023-01-14 04:20:18 +00:00
updateState({ configError: true });
2023-01-14 00:45:59 +00:00
}
};
const syncAccounts = async () => {
try {
const accounts = await getNodeAccounts(app.state.adminToken);
accounts.sort((a, b) => {
if (a.handle < b.handle) {
return -1;
}
if (a.handle > b.handle) {
return 1;
}
return 0;
});
2023-01-14 04:20:18 +00:00
updateState({ accounstError: false, accounts });
2023-01-14 00:45:59 +00:00
}
catch(err) {
console.log(err);
2023-01-14 04:20:18 +00:00
updateState({ accountsError: true });
2023-01-14 00:45:59 +00:00
}
};
2022-08-24 20:30:48 +00:00
return { state, actions };
}