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

57 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-05-12 22:19:51 +02:00
import { Center, Loader, Select, Tooltip } from '@mantine/core';
2022-12-04 17:36:30 +01:00
import { useQuery } from '@tanstack/react-query';
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();
//const loadConfig = useConfigStore((x) => x.loadConfig);
const { data: configs, isLoading, isError } = useConfigsQuery();
const [activeConfig, setActiveConfig] = useState(configName);
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
2022-12-04 17:36:30 +01:00
setActiveConfig(value);
/*
loadConfig(e ?? 'default');
setCookie('config-name', e ?? 'default', {
maxAge: 60 * 60 * 24 * 30,
sameSite: 'strict',
});
*/
};
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 (
<Select
2022-08-22 09:50:54 +02:00
label={t('configSelect.label')}
2022-12-04 17:36:30 +01:00
value={activeConfig}
onChange={onConfigChange}
data={configs}
/>
);
}
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[];