2023-02-20 22:11:30 +01:00
|
|
|
import { Flex, Tabs } from '@mantine/core';
|
2022-12-04 21:19:40 +01:00
|
|
|
import { UseFormReturnType } from '@mantine/form';
|
2023-02-20 22:11:30 +01:00
|
|
|
import { useDebouncedValue } from '@mantine/hooks';
|
|
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
import { useGetDashboardIcons } from '../../../../../../hooks/icons/useGetDashboardIcons';
|
2022-12-18 22:27:01 +01:00
|
|
|
import { AppType } from '../../../../../../types/app';
|
2023-02-20 22:11:30 +01:00
|
|
|
import { IconSelector } from './IconSelector';
|
2022-12-04 21:19:40 +01:00
|
|
|
|
|
|
|
|
interface AppearanceTabProps {
|
2022-12-18 22:27:01 +01:00
|
|
|
form: UseFormReturnType<AppType, (values: AppType) => AppType>;
|
|
|
|
|
disallowAppNameProgagation: () => void;
|
|
|
|
|
allowAppNamePropagation: boolean;
|
2022-12-04 21:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-11 19:32:51 +01:00
|
|
|
export const AppearanceTab = ({
|
|
|
|
|
form,
|
2022-12-18 22:27:01 +01:00
|
|
|
disallowAppNameProgagation,
|
|
|
|
|
allowAppNamePropagation,
|
2022-12-11 19:32:51 +01:00
|
|
|
}: AppearanceTabProps) => {
|
2023-02-20 22:11:30 +01:00
|
|
|
const { data, isLoading } = useGetDashboardIcons();
|
|
|
|
|
|
|
|
|
|
const [debouncedValue] = useDebouncedValue(form.values.name, 500);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (allowAppNamePropagation !== true) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const matchingDebouncedIcon = data
|
|
|
|
|
?.flatMap((x) => x.entries)
|
|
|
|
|
.find((x) => replaceCharacters(x.name.split('.')[0]) === replaceCharacters(debouncedValue));
|
|
|
|
|
|
|
|
|
|
if (!matchingDebouncedIcon) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
form.setFieldValue('appearance.iconUrl', matchingDebouncedIcon.url);
|
|
|
|
|
}, [debouncedValue]);
|
2022-12-05 20:04:08 +01:00
|
|
|
|
2022-12-04 21:19:40 +01:00
|
|
|
return (
|
|
|
|
|
<Tabs.Panel value="appearance" pt="lg">
|
2022-12-05 21:43:47 +01:00
|
|
|
<Flex gap={5}>
|
|
|
|
|
<IconSelector
|
2022-12-11 19:32:51 +01:00
|
|
|
form={form}
|
2023-02-20 22:11:30 +01:00
|
|
|
data={data}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
allowAppNamePropagation={allowAppNamePropagation}
|
|
|
|
|
disallowAppNameProgagation={disallowAppNameProgagation}
|
2022-12-05 21:43:47 +01:00
|
|
|
/>
|
|
|
|
|
</Flex>
|
2022-12-04 21:19:40 +01:00
|
|
|
</Tabs.Panel>
|
|
|
|
|
);
|
|
|
|
|
};
|
2022-12-05 20:04:08 +01:00
|
|
|
|
2023-02-20 22:11:30 +01:00
|
|
|
const replaceCharacters = (value: string) => value.toLowerCase().replaceAll('', '-');
|