From 147ca53629cbccfa117b755678941c97b9348312 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 2 Dec 2024 13:41:31 +1100 Subject: [PATCH] Build required widget (#8610) * Implement "required for build orders" dashboard widget * Allow dashboard widges to be optionally disabled * Fix for enabled check --- .../components/dashboard/DashboardWidget.tsx | 10 ++++------ .../dashboard/DashboardWidgetLibrary.tsx | 19 ++++++++++++++----- .../widgets/QueryCountDashboardWidget.tsx | 3 +++ src/frontend/src/hooks/UseDashboardItems.tsx | 4 +++- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/frontend/src/components/dashboard/DashboardWidget.tsx b/src/frontend/src/components/dashboard/DashboardWidget.tsx index 044c9f13e6dd..c7180477b1f1 100644 --- a/src/frontend/src/components/dashboard/DashboardWidget.tsx +++ b/src/frontend/src/components/dashboard/DashboardWidget.tsx @@ -15,6 +15,7 @@ export interface DashboardWidgetProps { label: string; title: string; description: string; + enabled?: boolean; minWidth?: number; minHeight?: number; render: () => JSX.Element; @@ -35,12 +36,9 @@ export default function DashboardWidget({ removing: boolean; onRemove: () => void; }>) { - // TODO: Implement visibility check - // if (!props?.visible?.() == false) { - // return null; - // } - - // TODO: Add button to remove widget (if "editing") + if (item.enabled == false) { + return null; + } return ( diff --git a/src/frontend/src/components/dashboard/DashboardWidgetLibrary.tsx b/src/frontend/src/components/dashboard/DashboardWidgetLibrary.tsx index c8c26138a0fa..7eed187908f7 100644 --- a/src/frontend/src/components/dashboard/DashboardWidgetLibrary.tsx +++ b/src/frontend/src/components/dashboard/DashboardWidgetLibrary.tsx @@ -1,6 +1,7 @@ import { t } from '@lingui/macro'; import { ModelType } from '../../enums/ModelType'; +import { useGlobalSettingsState } from '../../states/SettingsState'; import type { DashboardWidgetProps } from './DashboardWidget'; import ColorToggleDashboardWidget from './widgets/ColorToggleWidget'; import GetStartedWidget from './widgets/GetStartedWidget'; @@ -13,6 +14,8 @@ import QueryCountDashboardWidget from './widgets/QueryCountDashboardWidget'; * @returns A list of built-in dashboard widgets which display the number of results for a particular query */ export function BuiltinQueryCountWidgets(): DashboardWidgetProps[] { + const globalSettings = useGlobalSettingsState.getState(); + return [ QueryCountDashboardWidget({ label: 'sub-prt', @@ -38,22 +41,28 @@ export function BuiltinQueryCountWidgets(): DashboardWidgetProps[] { modelType: ModelType.part, params: { low_stock: true, active: true } }), - // TODO: Required for build orders + QueryCountDashboardWidget({ + title: t`Required for Build Orders`, + label: 'bld-req', + description: t`Show parts which are required for active build orders`, + modelType: ModelType.part, + params: { stock_to_build: true } + }), QueryCountDashboardWidget({ title: t`Expired Stock Items`, label: 'exp-stk', description: t`Show the number of stock items which have expired`, modelType: ModelType.stockitem, - params: { expired: true } - // TODO: Hide if expiry is disabled + params: { expired: true }, + enabled: globalSettings.isSet('STOCK_ENABLE_EXPIRY') }), QueryCountDashboardWidget({ title: t`Stale Stock Items`, label: 'stl-stk', description: t`Show the number of stock items which are stale`, modelType: ModelType.stockitem, - params: { stale: true } - // TODO: Hide if expiry is disabled + params: { stale: true }, + enabled: globalSettings.isSet('STOCK_ENABLE_EXPIRY') }), QueryCountDashboardWidget({ title: t`Active Build Orders`, diff --git a/src/frontend/src/components/dashboard/widgets/QueryCountDashboardWidget.tsx b/src/frontend/src/components/dashboard/widgets/QueryCountDashboardWidget.tsx index 6a3ec97d5601..0ec30de04adf 100644 --- a/src/frontend/src/components/dashboard/widgets/QueryCountDashboardWidget.tsx +++ b/src/frontend/src/components/dashboard/widgets/QueryCountDashboardWidget.tsx @@ -102,18 +102,21 @@ export default function QueryCountDashboardWidget({ title, description, modelType, + enabled = true, params }: { label: string; title: string; description: string; modelType: ModelType; + enabled?: boolean; params: any; }): DashboardWidgetProps { return { label: label, title: title, description: description, + enabled: enabled, minWidth: 2, minHeight: 1, render: () => ( diff --git a/src/frontend/src/hooks/UseDashboardItems.tsx b/src/frontend/src/hooks/UseDashboardItems.tsx index 45a3542902a2..4eb506f98214 100644 --- a/src/frontend/src/hooks/UseDashboardItems.tsx +++ b/src/frontend/src/hooks/UseDashboardItems.tsx @@ -93,7 +93,9 @@ export function useDashboardItems(): DashboardLibraryProps { }, [pluginQuery, inventreeContext]); const items: DashboardWidgetProps[] = useMemo(() => { - return [...builtin, ...pluginDashboardItems]; + const widgets = [...builtin, ...pluginDashboardItems]; + + return widgets.filter((item) => item.enabled ?? true); }, [builtin, pluginDashboardItems]); const loaded: boolean = useMemo(() => {