Files
Homarr/src/components/Config/ConfigChanger.tsx

76 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-01-06 12:04:37 +09:00
import { Center, Dialog, Loader, Notification, Select, Tooltip } from '@mantine/core';
import { useToggle } from '@mantine/hooks';
2022-12-04 17:36:30 +01:00
import { useQuery } from '@tanstack/react-query';
2023-01-06 12:04:37 +09:00
import { setCookie } from 'cookies-next';
2022-08-22 09:50:54 +02:00
import { useTranslation } from 'next-i18next';
2022-12-04 17:36:30 +01:00
import { useState } from 'react';
import { useConfigContext } from '../../config/provider';
export default function ConfigChanger() {
2022-08-22 09:50:54 +02:00
const { t } = useTranslation('settings/general/config-changer');
2022-12-04 17:36:30 +01:00
const { name: configName } = useConfigContext();
2023-01-06 12:04:37 +09:00
// const loadConfig = useConfigStore((x) => x.loadConfig);
2022-12-04 17:36:30 +01:00
const { data: configs, isLoading, isError } = useConfigsQuery();
const [activeConfig, setActiveConfig] = useState(configName);
2023-01-06 12:04:37 +09:00
const [isRefreshing, toggle] = useToggle();
2022-12-04 17:36:30 +01:00
const onConfigChange = (value: string) => {
// TODO: check what should happen here with @manuel-rw
// Wheter it should check for the current url and then load the new config only on index
2022-12-04 18:20:25 +01:00
// Or it should always load the selected config and open index or ? --> change url to page
2023-01-06 12:04:37 +09:00
setCookie('config-name', value ?? 'default', {
maxAge: 60 * 60 * 24 * 30,
sameSite: 'strict',
});
2022-12-04 17:36:30 +01:00
setActiveConfig(value);
2023-01-06 12:04:37 +09:00
toggle();
// Use timeout to wait for the cookie to be set
setTimeout(() => {
window.location.reload();
}, 1000);
2022-12-04 17:36:30 +01:00
};
2022-08-22 09:50:54 +02:00
// If configlist is empty, return a loading indicator
2022-12-04 17:36:30 +01:00
if (isLoading || !configs || configs?.length === 0 || !configName) {
2022-05-12 22:19:51 +02:00
return (
<Tooltip label={"Loading your configs. This doesn't load in vercel."}>
<Center>
2022-05-12 22:19:51 +02:00
<Loader />
</Center>
</Tooltip>
2022-05-12 22:19:51 +02:00
);
}
2022-12-04 17:36:30 +01:00
return (
2023-01-06 12:04:37 +09:00
<>
<Select
label={t('configSelect.label')}
value={activeConfig}
onChange={onConfigChange}
data={configs}
/>
<Dialog
position={{ top: 0, left: 0 }}
unstyled
opened={isRefreshing}
onClose={() => toggle()}
size="lg"
radius="md"
>
<Notification loading title={t('configSelect.loadingNew')} radius="md" disallowClose>
{t('configSelect.pleaseWait')}
</Notification>
</Dialog>
</>
);
}
2022-12-04 17:36:30 +01:00
2022-12-22 11:30:50 +09:00
const useConfigsQuery = () =>
useQuery({
2022-12-04 17:36:30 +01:00
queryKey: ['config/get-all'],
queryFn: fetchConfigs,
});
const fetchConfigs = async () => (await (await fetch('/api/configs')).json()) as string[];