Files
Homarr/src/components/Dashboard/Wrappers/gridstack/store.tsx

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-01-06 22:46:07 +01:00
import { useMantineTheme } from '@mantine/core';
2023-02-11 08:58:09 +09:00
import { create } from 'zustand';
import { useConfigContext } from '../../../../config/provider';
import { GridstackBreakpoints } from '../../../../constants/gridstack-breakpoints';
2023-01-06 22:46:07 +01:00
export const useGridstackStore = create<GridstackStoreType>((set, get) => ({
mainAreaWidth: null,
currentShapeSize: null,
setMainAreaWidth: (w: number) =>
set((v) => ({ ...v, mainAreaWidth: w, currentShapeSize: getCurrentShapeSize(w) })),
}));
interface GridstackStoreType {
mainAreaWidth: null | number;
currentShapeSize: null | 'sm' | 'md' | 'lg';
setMainAreaWidth: (width: number) => void;
}
export const useNamedWrapperColumnCount = (): 'small' | 'medium' | 'large' | null => {
2023-01-06 22:46:07 +01:00
const mainAreaWidth = useGridstackStore((x) => x.mainAreaWidth);
if (!mainAreaWidth) return null;
2023-03-03 00:37:22 +09:00
if (mainAreaWidth >= 1400) return 'large';
2023-03-03 00:37:22 +09:00
if (mainAreaWidth >= 800) return 'medium';
2023-01-06 22:46:07 +01:00
return 'small';
};
2023-01-06 22:46:07 +01:00
export const useWrapperColumnCount = () => {
const { config } = useConfigContext();
if (!config) {
return null;
}
switch (useNamedWrapperColumnCount()) {
case 'large':
return config.settings.customization.gridstack?.columnCountLarge ?? 12;
case 'medium':
return config.settings.customization.gridstack?.columnCountMedium ?? 6;
case 'small':
return config.settings.customization.gridstack?.columnCountSmall ?? 3;
default:
return null;
}
2023-01-06 22:46:07 +01:00
};
function getCurrentShapeSize(size: number) {
return size >= GridstackBreakpoints.large
? 'lg'
: size >= GridstackBreakpoints.medium
? 'md'
: 'sm';
2023-01-06 22:46:07 +01:00
}