Files
twenty_crm/front/src/modules/ui/input/hooks/useLazyLoadIcon.ts
Thaïs f35ea19f4d feat: get object metadata from backend in Object Detail and New Field… (#2122)
* feat: get object metadata from backend in Object Detail and New Field - Step 1

Closes #2008

* refactor: add useLazyLoadIcon hook
2023-10-19 16:58:18 +02:00

22 lines
587 B
TypeScript

import { useEffect, useState } from 'react';
import { IconComponent } from '@/ui/display/icon/types/IconComponent';
export const useLazyLoadIcon = (iconKey: string) => {
const [Icon, setIcon] = useState<IconComponent | undefined>();
const [isLoadingIcon, setIsLoadingIcon] = useState(true);
useEffect(() => {
if (!iconKey) return;
import(`@tabler/icons-react/dist/esm/icons/${iconKey}.js`).then(
(lazyLoadedIcon) => {
setIcon(lazyLoadedIcon.default);
setIsLoadingIcon(false);
},
);
}, [iconKey]);
return { Icon, isLoadingIcon };
};