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

130 lines
3.5 KiB
JavaScript
Raw Normal View History

import { useContext, 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';
import { ViewportContext } from 'context/ViewportContext';
2022-08-24 20:30:48 +00:00
export function useDashboard(token, config) {
const [state, setState] = useState({
domain: "",
accountStorage: null,
keyType: null,
enableImage: null,
enableAudio: null,
enableVideo: null,
2022-08-24 20:30:48 +00:00
showSettings: false,
busy: false,
loading: false,
accounts: [],
createBusy: false,
showCreate: false,
});
const viewport = useContext(ViewportContext);
2022-08-24 20:30:48 +00:00
const updateState = (value) => {
setState((s) => ({ ...s, ...value }));
}
const actions = {
setCreateLink: async () => {
if (!state.createBusy) {
updateState({ createBusy: true });
try {
let create = await addAccountCreate(token)
updateState({ createToken: create, showCreate: true });
}
catch (err) {
window.alert(err);
}
updateState({ createBusy: false });
}
},
setShowCreate: (showCreate) => {
updateState({ showCreate });
},
removeAccount: async (accountId) => {
await removeAccount(token, accountId);
actions.getAccounts();
},
setHost: (domain) => {
updateState({ domain });
2022-08-24 20:30:48 +00:00
},
setStorage: (accountStorage) => {
updateState({ accountStorage });
},
setKeyType: (keyType) => {
updateState({ keyType });
},
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 {
const { domain, keyType, accountStorage, enableImage, enableAudio, enableVideo } = state;
2022-08-24 20:30:48 +00:00
await setNodeConfig(token,
{ domain, accountStorage: accountStorage * 1073741824,
keyType, enableImage, enableAudio, enableVideo });
2022-08-24 20:30:48 +00:00
updateState({ showSettings: false });
}
catch(err) {
console.log(err);
window.alert(err);
}
updateState({ busy: false });
}
},
getAccounts: async () => {
if (!state.loading) {
updateState({ loading: true });
try {
let accounts = await getNodeAccounts(token);
accounts.sort((a, b) => {
if (a.handle < b.handle) {
return -1;
}
if (a.handle > b.handle) {
return 1;
}
return 0;
});
updateState({ accounts });
}
catch(err) {
console.log(err);
window.alert(err);
}
updateState({ loading: false });
}
},
};
useEffect(() => {
updateState({ display: viewport.state.display });
}, [viewport]);
2022-08-24 20:30:48 +00:00
useEffect(() => {
const { accountStorage, domain, keyType, enableImage, enableAudio, enableVideo } = config;
updateState({ domain, accountStorage: Math.ceil(accountStorage / 1073741824), keyType,
enableImage, enableAudio, enableVideo });
2022-08-24 20:30:48 +00:00
actions.getAccounts();
2022-09-02 05:35:28 +00:00
// eslint-disable-next-line
}, [config]);
2022-08-24 20:30:48 +00:00
return { state, actions };
}