refactor(widgets): replace beszelSystem option with generic integrationSelect
This commit is contained in:
5 files changed
+62
-33
No files matched your search
@@ -16,7 +16,7 @@ import { WidgetTextInput } from "./widget-text-input";
|
||||
import { WidgetUmamiEventInput } from "./widget-umami-event-input";
|
||||
import { WidgetUmamiEventNamesInput } from "./widget-umami-event-names-input";
|
||||
import { WidgetUmamiWebsiteInput } from "./widget-umami-website-input";
|
||||
import { WidgetBeszelSystemInput } from "./widget-beszel-system-input";
|
||||
import { WidgetIntegrationSelectInput } from "./widget-integration-select-input";
|
||||
|
||||
const mapping = {
|
||||
anchorNote: WidgetAnchorNoteInput,
|
||||
@@ -36,7 +36,7 @@ const mapping = {
|
||||
umamiEventNames: WidgetUmamiEventNamesInput,
|
||||
umamiWebsite: WidgetUmamiWebsiteInput,
|
||||
customWidgetSelect: WidgetCustomWidgetSelectInput,
|
||||
beszelSystem: WidgetBeszelSystemInput,
|
||||
integrationSelect: WidgetIntegrationSelectInput,
|
||||
} satisfies Record<WidgetOptionType, unknown>;
|
||||
|
||||
export const getInputForType = <TType extends WidgetOptionType>(type: TType) => {
|
||||
|
||||
+17
-20
@@ -2,28 +2,27 @@
|
||||
|
||||
import { Select, Text } from "@mantine/core";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import { useScopedI18n } from "@homarr/translation/client";
|
||||
|
||||
import type { CommonWidgetInputProps } from "./common";
|
||||
import { useWidgetInputTranslation } from "./common";
|
||||
import { useFormContext } from "./form";
|
||||
|
||||
export const WidgetBeszelSystemInput = ({ property, kind }: CommonWidgetInputProps<"beszelSystem">) => {
|
||||
const t = useScopedI18n("widget.beszelSystemStats.option.systemId");
|
||||
const useIntegrationSelectTranslation = () =>
|
||||
useScopedI18n("widget.integrationSelect");
|
||||
|
||||
export const WidgetIntegrationSelectInput = ({
|
||||
property,
|
||||
kind,
|
||||
options,
|
||||
}: CommonWidgetInputProps<"integrationSelect">) => {
|
||||
const t = useIntegrationSelectTranslation();
|
||||
const tInput = useWidgetInputTranslation(kind, property);
|
||||
const form = useFormContext();
|
||||
|
||||
const integrationIds = form.values.integrationIds;
|
||||
const integrationIds = form.values.integrationIds as string[];
|
||||
|
||||
const {
|
||||
data: systemsResult = [],
|
||||
isPending,
|
||||
isError,
|
||||
} = clientApi.widget.beszel.getSystems.useQuery(
|
||||
{ integrationIds },
|
||||
{ enabled: integrationIds.length > 0, staleTime: 30_000 },
|
||||
);
|
||||
const { data: selectData = [], isPending, isError } = options.useOptions(integrationIds);
|
||||
|
||||
if (integrationIds.length === 0) {
|
||||
return (
|
||||
@@ -33,20 +32,18 @@ export const WidgetBeszelSystemInput = ({ property, kind }: CommonWidgetInputPro
|
||||
);
|
||||
}
|
||||
|
||||
const systems = systemsResult.flatMap((r) => r.systems.map((s) => ({ value: s.id, label: s.name })));
|
||||
|
||||
return (
|
||||
<Select
|
||||
label={tInput("label")}
|
||||
description={tInput("description")}
|
||||
description={options.withDescription ? tInput("description") : undefined}
|
||||
placeholder={(isPending && t("loading")) || t("placeholder")}
|
||||
clearable
|
||||
searchable
|
||||
nothingFoundMessage={t("noSystems")}
|
||||
data={systems}
|
||||
clearable={options.clearable}
|
||||
searchable={options.searchable}
|
||||
nothingFoundMessage={t("noResults")}
|
||||
data={selectData}
|
||||
disabled={isError}
|
||||
error={(isError && t("loadError")) || undefined}
|
||||
{...form.getInputProps(`options.${property}`)}
|
||||
/>
|
||||
);
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user