Files
Homarr/src/components/Dashboard/Views/DashboardView.tsx

68 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-01-06 22:46:07 +01:00
import { Center, Group, Loader, Stack } from '@mantine/core';
import { useEffect, useMemo, useRef } from 'react';
2022-12-04 17:36:30 +01:00
import { useConfigContext } from '../../../config/provider';
2023-01-06 22:46:07 +01:00
import { useResize } from '../../../hooks/use-resize';
2022-12-23 17:29:58 +01:00
import { useScreenSmallerThan } from '../../../hooks/useScreenSmallerThan';
2022-12-10 22:14:31 +01:00
import { CategoryType } from '../../../types/category';
import { WrapperType } from '../../../types/wrapper';
import { DashboardCategory } from '../Wrappers/Category/Category';
2023-01-06 22:46:07 +01:00
import { useGridstackStore } from '../Wrappers/gridstack/store';
2022-12-04 17:36:30 +01:00
import { DashboardSidebar } from '../Wrappers/Sidebar/Sidebar';
2022-12-10 22:14:31 +01:00
import { DashboardWrapper } from '../Wrappers/Wrapper/Wrapper';
2022-12-04 17:36:30 +01:00
export const DashboardView = () => {
const wrappers = useWrapperItems();
2022-12-16 20:18:20 +01:00
const layoutSettings = useConfigContext()?.config?.settings.customization.layout;
2022-12-18 21:21:23 +01:00
const doNotShowSidebar = useScreenSmallerThan('md');
2023-01-06 22:46:07 +01:00
const notReady = typeof doNotShowSidebar === 'undefined';
const mainAreaRef = useRef<HTMLDivElement>(null);
const { width } = useResize(mainAreaRef, [doNotShowSidebar]);
const setMainAreaWidth = useGridstackStore(x => x.setMainAreaWidth);
const mainAreaWidth = useGridstackStore(x => x.mainAreaWidth);
useEffect(() => {
2023-01-06 23:50:08 +01:00
if (width === 0) return;
2023-01-06 22:46:07 +01:00
setMainAreaWidth(width);
}, [width]);
2022-12-04 17:36:30 +01:00
return (
<Group align="top" h="100%">
2023-01-06 22:46:07 +01:00
{notReady ? <Center w="100%">
<Loader />
</Center> : <>
2023-01-06 23:50:08 +01:00
{layoutSettings?.enabledLeftSidebar && !doNotShowSidebar ? (
<DashboardSidebar location="left" isGridstackReady={!!mainAreaWidth} />
2022-12-16 20:18:20 +01:00
) : null}
2023-01-06 22:46:07 +01:00
<Stack ref={mainAreaRef} mx={-10} style={{ flexGrow: 1 }}>
{!mainAreaWidth ? null : wrappers.map((item) =>
2022-12-10 22:14:31 +01:00
item.type === 'category' ? (
<DashboardCategory key={item.id} category={item as unknown as CategoryType} />
) : (
<DashboardWrapper key={item.id} wrapper={item as WrapperType} />
)
2022-12-04 17:36:30 +01:00
)}
</Stack>
2023-01-06 23:50:08 +01:00
{layoutSettings?.enabledRightSidebar && !doNotShowSidebar ? (
<DashboardSidebar location="right" isGridstackReady={!!mainAreaWidth} />
2022-12-16 20:18:20 +01:00
) : null}
2023-01-06 22:46:07 +01:00
</>
}
2022-12-04 17:36:30 +01:00
</Group>
);
};
const useWrapperItems = () => {
const { config } = useConfigContext();
return useMemo(
() =>
config
? [
...config.categories.map((c) => ({ ...c, type: 'category' })),
...config.wrappers.map((w) => ({ ...w, type: 'wrapper' })),
].sort((a, b) => a.position - b.position)
: [],
[config?.categories, config?.wrappers]
);
};