mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-10 15:35:55 +01:00
✨ Add possibiltiy to add widgets to dashboard
This commit is contained in:
@@ -6,6 +6,7 @@ import { useStyles } from './styles';
|
||||
|
||||
interface GenericAvailableElementTypeProps {
|
||||
name: string;
|
||||
handleAddition: () => Promise<void>;
|
||||
description?: string;
|
||||
image: string | TablerIcon;
|
||||
disabled?: boolean;
|
||||
@@ -16,6 +17,7 @@ export const GenericAvailableElementType = ({
|
||||
description,
|
||||
image,
|
||||
disabled,
|
||||
handleAddition,
|
||||
}: GenericAvailableElementTypeProps) => {
|
||||
const { classes } = useStyles();
|
||||
|
||||
@@ -39,7 +41,15 @@ export const GenericAvailableElementType = ({
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
<Button disabled={disabled} variant="light" size="xs" mt="auto" radius="md" fullWidth>
|
||||
<Button
|
||||
disabled={disabled}
|
||||
onClick={handleAddition}
|
||||
variant="light"
|
||||
size="xs"
|
||||
mt="auto"
|
||||
radius="md"
|
||||
fullWidth
|
||||
>
|
||||
Add to Dashboard
|
||||
</Button>
|
||||
</Stack>
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
import { Grid, Text } from '@mantine/core';
|
||||
import {
|
||||
IconArrowsUpDown,
|
||||
IconCalendarTime,
|
||||
IconClock,
|
||||
IconCloudRain,
|
||||
IconFileDownload,
|
||||
} from '@tabler/icons';
|
||||
import { useTranslation } from 'next-i18next';
|
||||
import { useConfigContext } from '../../../../../../config/provider';
|
||||
import widgets from '../../../../../../widgets';
|
||||
import { GenericAvailableElementType } from '../Shared/GenericElementType';
|
||||
import { SelectorBackArrow } from '../Shared/SelectorBackArrow';
|
||||
import { WidgetElementType } from './WidgetElementType';
|
||||
|
||||
@@ -20,6 +13,7 @@ export const AvailableIntegrationElements = ({
|
||||
onClickBack,
|
||||
}: AvailableIntegrationElementsProps) => {
|
||||
const { t } = useTranslation('layout/element-selector/selector');
|
||||
const activeWidgets = useConfigContext().config?.widgets ?? [];
|
||||
return (
|
||||
<>
|
||||
<SelectorBackArrow onClickBack={onClickBack} />
|
||||
@@ -30,9 +24,11 @@ export const AvailableIntegrationElements = ({
|
||||
</Text>
|
||||
|
||||
<Grid>
|
||||
{Object.entries(widgets).map(([k, v]) => (
|
||||
<WidgetElementType key={k} id={k} image={v.icon} />
|
||||
))}
|
||||
{Object.entries(widgets)
|
||||
.filter(([widgetId]) => !activeWidgets.some((aw) => aw.id === widgetId))
|
||||
.map(([k, v]) => (
|
||||
<WidgetElementType key={k} id={k} image={v.icon} widget={v} />
|
||||
))}
|
||||
</Grid>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -1,19 +1,63 @@
|
||||
import { Button, Card, Center, Grid, Stack, Text } from '@mantine/core';
|
||||
import { useModals } from '@mantine/modals';
|
||||
import { TablerIcon } from '@tabler/icons';
|
||||
import { useTranslation } from 'next-i18next';
|
||||
import Image from 'next/image';
|
||||
import React, { ReactNode } from 'react';
|
||||
import { AvailableElementTypes } from '../Overview/AvailableElementsOverview';
|
||||
import { useConfigContext } from '../../../../../../config/provider';
|
||||
import { useConfigStore } from '../../../../../../config/store';
|
||||
import { IWidget, IWidgetDefinition } from '../../../../../../widgets/widgets';
|
||||
import { GenericAvailableElementType } from '../Shared/GenericElementType';
|
||||
|
||||
interface WidgetElementTypeProps {
|
||||
id: string;
|
||||
image: string | TablerIcon;
|
||||
disabled?: boolean;
|
||||
widget: IWidgetDefinition;
|
||||
}
|
||||
|
||||
export const WidgetElementType = ({ id, image, disabled }: WidgetElementTypeProps) => {
|
||||
export const WidgetElementType = ({ id, image, disabled, widget }: WidgetElementTypeProps) => {
|
||||
const { closeModal } = useModals();
|
||||
const { t } = useTranslation(`modules/${id}`);
|
||||
const { name: configName, config } = useConfigContext();
|
||||
const updateConfig = useConfigStore((x) => x.updateConfig);
|
||||
|
||||
if (!configName) return null;
|
||||
|
||||
const getLowestWrapper = () => {
|
||||
return config?.wrappers.sort((a, b) => a.position - b.position)[0];
|
||||
};
|
||||
|
||||
const handleAddition = async () => {
|
||||
updateConfig(configName, (prev) => ({
|
||||
...prev,
|
||||
widgets: [
|
||||
...prev.widgets.filter((w) => w.id !== widget.id),
|
||||
{
|
||||
id: widget.id,
|
||||
properties: Object.entries(widget.options).reduce((prev, [k, v]) => {
|
||||
prev[k] = v.defaultValue;
|
||||
return prev;
|
||||
}, {} as IWidget<string, any>['properties']),
|
||||
area: {
|
||||
type: 'wrapper',
|
||||
properties: {
|
||||
id: getLowestWrapper()?.id ?? '',
|
||||
},
|
||||
},
|
||||
shape: {
|
||||
location: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
},
|
||||
size: {
|
||||
width: widget.gridstack.minWidth,
|
||||
height: widget.gridstack.minHeight,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
}));
|
||||
// TODO: safe to file system
|
||||
closeModal('selectElement');
|
||||
};
|
||||
|
||||
return (
|
||||
<GenericAvailableElementType
|
||||
@@ -21,6 +65,7 @@ export const WidgetElementType = ({ id, image, disabled }: WidgetElementTypeProp
|
||||
description={t('descriptor.description')}
|
||||
image={image}
|
||||
disabled={disabled}
|
||||
handleAddition={handleAddition}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user