feat(integrations): add search.ch integration with timetable widget (#5180)

This commit is contained in:
Meier Lukas authored and GitHub committed 2026-04-03 20:23:15 +02:00
1 parent 279d830b79
commit abb2dfc92a
19 files changed
+535

No files matched your search

+2
View File
@@ -1,5 +1,6 @@
import type { WidgetOptionType } from "../options";
import { WidgetAppInput } from "./widget-app-input";
import { WidgetDynamicSelectInput } from "./widget-dynamic-select-input";
import { WidgetLocationInput } from "./widget-location-input";
import { WidgetMultiTextInput } from "./widget-multi-text-input";
import { WidgetMultiReleasesRepositoriesInput } from "./widget-multiReleasesRepositories-input";
@@ -23,6 +24,7 @@ const mapping = {
app: WidgetAppInput,
sortableItemList: WidgetSortedItemListInput,
multiReleasesRepositories: WidgetMultiReleasesRepositoriesInput,
dynamicSelect: WidgetDynamicSelectInput,
} satisfies Record<WidgetOptionType, unknown>;
export const getInputForType = <TType extends WidgetOptionType>(type: TType) => {
@@ -0,0 +1,92 @@
"use client";
import { useState } from "react";
import { Group, Loader, Select } from "@mantine/core";
import { useDebouncedValue } from "@mantine/hooks";
import { IconCheck } from "@tabler/icons-react";
import { translateIfNecessary } from "@homarr/translation";
import { useI18n } from "@homarr/translation/client";
import type { CommonWidgetInputProps } from "./common";
import { useWidgetInputTranslation } from "./common";
import { useFormContext } from "./form";
export interface DynamicSelectOption {
value: string;
label: string;
}
export const WidgetDynamicSelectInput = ({ property, kind, options }: CommonWidgetInputProps<"dynamicSelect">) => {
const t = useI18n();
const tWidget = useWidgetInputTranslation(kind, property);
const form = useFormContext();
const inputProps = form.getInputProps(`options.${property}`);
const [search, setSearch] = useState("");
const [debouncedSearch] = useDebouncedValue(search, 300);
const { isPending, options: selectOptions } = options.useOptions(debouncedSearch, form.values.integrationIds);
const value = inputProps.value as DynamicSelectOption | null;
const onChange = inputProps.onChange as (value: DynamicSelectOption | null) => void;
return (
<Select
label={tWidget("label")}
data={selectOptions
.map((option) =>
typeof option === "string"
? option
: {
value: option.value,
label: translateIfNecessary(t, option.label) ?? option.value,
},
)
.concat(
typeof value === "object" &&
value !== null &&
!selectOptions.some((option) =>
typeof option === "string" ? option === value.value : option.value === value.value,
)
? {
value: value.value,
label: value.label,
}
: [],
)}
searchValue={search}
onSearchChange={setSearch}
leftSection={isPending && <Loader size="xs" />}
renderOption={({ option, checked }) => {
return (
<Group flex="1" gap="xs">
{option.label}
{checked && (
<IconCheck
style={{ marginInlineStart: "auto" }}
color="currentColor"
opacity={0.6}
size={18}
stroke={1.5}
/>
)}
</Group>
);
}}
description={options.withDescription ? tWidget("description") : undefined}
searchable
{...inputProps}
value={value?.value ?? null}
onChange={(value) =>
onChange(
value
? {
value,
label:
selectOptions.filter((option) => typeof option === "object").find((option) => option.value === value)
?.label ?? value,
}
: null,
)
}
/>
);
};
+2
View File
@@ -42,6 +42,7 @@ import * as smartHomeExecuteAutomation from "./smart-home/execute-automation";
import * as stockPrice from "./stocks";
import * as systemDisks from "./system-disks";
import * as systemResources from "./system-resources";
import * as timetable from "./timetable";
import * as tracearr from "./tracearr";
import * as video from "./video";
import * as weather from "./weather";
@@ -82,6 +83,7 @@ export const widgetImports = {
systemResources,
coolify,
systemDisks,
timetable,
"immich-serverStats": immichServerStats,
"immich-albumCarousel": immichAlbumCarousel,
tracearr,
+17
View File
@@ -6,6 +6,7 @@ import type { ZodType } from "zod/v4";
import type { IntegrationKind } from "@homarr/definitions";
import type { DynamicSelectOption } from "./_inputs/widget-dynamic-select-input";
import type { inferSelectOptionValue, SelectOption } from "./_inputs/widget-select-input";
import type { ReleasesRepository } from "./releases/releases-repository";
@@ -47,6 +48,16 @@ interface SelectInput<TOptions extends readonly SelectOption[]> extends CommonIn
searchable?: boolean;
}
interface DynamicSelectInput extends CommonInput<DynamicSelectOption | null> {
useOptions: (
query: string,
integrationIds: string[],
) => {
isPending: boolean;
options: DynamicSelectOption[];
};
}
interface NumberInput extends CommonInput<number> {
validate: z.ZodNumber;
step?: number;
@@ -82,6 +93,12 @@ const optionsFactory = {
searchable: input.searchable ?? false,
withDescription: input.withDescription ?? false,
}),
dynamicSelect: (input: DynamicSelectInput) => ({
type: "dynamicSelect" as const,
defaultValue: input.defaultValue ?? null,
useOptions: input.useOptions,
withDescription: input.withDescription ?? false,
}),
select: <const TOptions extends SelectOption[]>(input: SelectInput<TOptions>) => ({
type: "select" as const,
defaultValue: (input.defaultValue ?? input.options[0]) as inferSelectOptionValue<TOptions[number]>,
@@ -0,0 +1,75 @@
import { Badge, Center, Group, Stack, Text } from "@mantine/core";
import dayjs from "dayjs";
import { clientApi } from "@homarr/api/client";
import { useScopedI18n } from "@homarr/translation/client";
import type { DynamicSelectOption } from "../_inputs/widget-dynamic-select-input";
import type { WidgetComponentProps } from "../definition";
export default function TimetableWidget({ options, integrationIds }: WidgetComponentProps<"timetable">) {
// It will always have at least one integration as otherwise the NoIntegrationSelectedError would be thrown in item-content.tsx
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const integrationId = integrationIds[0]!;
const t = useScopedI18n("widget.timetable");
if (!options.station) {
return <Center h="100%">{t("noStation")}</Center>;
}
return <TimetableWidgetInner station={options.station} integrationId={integrationId} />;
}
interface TimetableWidgetInnerProps {
station: DynamicSelectOption;
integrationId: string;
}
const TimetableWidgetInner = ({ station, integrationId }: TimetableWidgetInnerProps) => {
const [timetable] = clientApi.widget.timetable.getTimetable.useSuspenseQuery({
integrationId,
stationId: station.value,
limit: 10,
});
const t = useScopedI18n("widget.timetable");
return (
<Stack w="100%" gap="xs" p="sm">
<Text fw="bold">{t("title", { station: station.label })}</Text>
{timetable.entries.map((entry) => (
<Group key={`${entry.timestamp.toISOString()}-${entry.location}`} justify="space-between" w="100%">
<Group gap="sm">
{entry.line && (
<Badge
size="sm"
color={entry.line.color ?? undefined}
variant={entry.line.color ? "filled" : "default"}
w={32}
p={0}
radius={0}
>
{entry.line.name}
</Badge>
)}
<Text size="sm">
{dayjs(entry.timestamp).format("HH:mm")}{" "}
{entry.delay >= 1 && (
<Text size="sm" span c="red">
+{entry.delay}&apos;
</Text>
)}
</Text>
<Text size="sm">{entry.location}</Text>
</Group>
{entry.platform && (
<Text size="sm" c={entry.platform.hasChanged ? "red" : undefined}>
{entry.platform.name}
</Text>
)}
</Group>
))}
</Stack>
);
};
+37
View File
@@ -0,0 +1,37 @@
import { IconBusStop } from "@tabler/icons-react";
import { clientApi } from "@homarr/api/client";
import { createWidgetDefinition } from "../definition";
import { optionsBuilder } from "../options";
export const { componentLoader, definition } = createWidgetDefinition("timetable", {
icon: IconBusStop,
supportedIntegrations: ["searchCh"],
createOptions() {
return optionsBuilder.from((factory) => ({
station: factory.dynamicSelect({
useOptions(query, integrationIds) {
const { data: stations, isPending } = clientApi.widget.timetable.searchStations.useQuery(
{
integrationId: integrationIds[0] ?? "",
query,
},
{
enabled: integrationIds.length > 0,
},
);
return {
isPending,
options:
stations?.map((station) => ({
label: station.name,
value: station.id,
})) ?? [],
};
},
}),
}));
},
}).withDynamicImport(() => import("./component"));