feat: add widgets
This commit is contained in:
1 parent
973baedfcb
commit
b902f93a3c
20 files changed
+323
-19
No files matched your search
@@ -0,0 +1,10 @@
|
||||
.placeItem {
|
||||
padding: 8px 12px;
|
||||
border-radius: var(--mantine-radius-sm);
|
||||
background-color: light-dark(var(--mantine-color-gray-0), var(--mantine-color-dark-6));
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background-color: light-dark(var(--mantine-color-gray-1), var(--mantine-color-dark-5));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
"use client";
|
||||
|
||||
import { Anchor, Group, Stack, Text } from "@mantine/core";
|
||||
import { IconMapPin } from "@tabler/icons-react";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import { useI18n } from "@homarr/translation/client";
|
||||
|
||||
import { WidgetEmptyState } from "../common/empty-state";
|
||||
import type { WidgetComponentProps } from "../definition";
|
||||
import classes from "./component.module.css";
|
||||
|
||||
export default function DawarichPlacesWidget({
|
||||
integrationIds,
|
||||
options,
|
||||
}: WidgetComponentProps<"dawarichPlaces">) {
|
||||
const t = useI18n();
|
||||
const { data: places } = clientApi.widget.dawarich.getPlaces.useQuery(
|
||||
{
|
||||
integrationId: integrationIds[0] ?? "",
|
||||
limit: options.maxPlaces,
|
||||
},
|
||||
{ staleTime: 5 * 60 * 1000 },
|
||||
);
|
||||
|
||||
if (!places || places.length === 0) return <WidgetEmptyState />;
|
||||
|
||||
return (
|
||||
<Stack gap="xs" h="100%" p="md">
|
||||
{places.map((place) => (
|
||||
<Group key={place.id} gap="sm" align="center" className={classes.placeItem}>
|
||||
<IconMapPin size={16} />
|
||||
<Text size="sm" fw={500} style={{ flex: 1 }}>
|
||||
{place.name}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{place.visits_count} {t("widget.dawarichPlaces.visits")}
|
||||
</Text>
|
||||
</Group>
|
||||
))}
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { IconMapPin } from "@tabler/icons-react";
|
||||
import z from "zod";
|
||||
|
||||
import { createWidgetDefinition } from "../definition";
|
||||
import { optionsBuilder } from "../options";
|
||||
|
||||
export const { definition, componentLoader } = createWidgetDefinition("dawarichPlaces", {
|
||||
icon: IconMapPin,
|
||||
supportedIntegrations: ["dawarich"],
|
||||
integrationsRequired: true,
|
||||
createOptions() {
|
||||
return optionsBuilder.from((factory) => ({
|
||||
maxPlaces: factory.number({ defaultValue: 10, validate: z.number().min(1).max(50) }),
|
||||
}));
|
||||
},
|
||||
}).withDynamicImport(() => import("./component"));
|
||||
@@ -0,0 +1,10 @@
|
||||
.statItem {
|
||||
padding: 12px;
|
||||
border-radius: var(--mantine-radius-md);
|
||||
background-color: light-dark(var(--mantine-color-gray-0), var(--mantine-color-dark-6));
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background-color: light-dark(var(--mantine-color-gray-1), var(--mantine-color-dark-5));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
"use client";
|
||||
|
||||
import { Group, Stack, Text } from "@mantine/core";
|
||||
import { IconMap, IconMapPin, IconRoute, IconWorld, IconBuildingSkyscraper } from "@tabler/icons-react";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import { useI18n } from "@homarr/translation/client";
|
||||
|
||||
import { WidgetEmptyState } from "../common/empty-state";
|
||||
import type { WidgetComponentProps } from "../definition";
|
||||
import classes from "./component.module.css";
|
||||
|
||||
export default function DawarichStatisticsWidget({
|
||||
integrationIds,
|
||||
options,
|
||||
}: WidgetComponentProps<"dawarichStatistics">) {
|
||||
const t = useI18n();
|
||||
const { data: stats } = clientApi.widget.dawarich.getStatistics.useQuery(
|
||||
{
|
||||
integrationId: integrationIds[0] ?? "",
|
||||
},
|
||||
{ staleTime: 5 * 60 * 1000 },
|
||||
);
|
||||
|
||||
if (!stats) return <WidgetEmptyState />;
|
||||
|
||||
return (
|
||||
<Stack gap="md" h="100%" p="md">
|
||||
{options.showTotalDistance && (
|
||||
<StatItem
|
||||
icon={<IconRoute size={20} />}
|
||||
label={t("widget.dawarichStatistics.totalDistance")}
|
||||
value={`${stats.totalDistanceKm.toLocaleString(undefined, { maximumFractionDigits: 1 })} km`}
|
||||
/>
|
||||
)}
|
||||
{options.showTotalPoints && (
|
||||
<StatItem
|
||||
icon={<IconMap size={20} />}
|
||||
label={t("widget.dawarichStatistics.totalPoints")}
|
||||
value={stats.totalPointsTracked.toLocaleString()}
|
||||
/>
|
||||
)}
|
||||
{options.showReverseGeocoded && (
|
||||
<StatItem
|
||||
icon={<IconMapPin size={20} />}
|
||||
label={t("widget.dawarichStatistics.reverseGeocoded")}
|
||||
value={stats.totalReverseGeocodedPoints.toLocaleString()}
|
||||
/>
|
||||
)}
|
||||
{options.showCountries && (
|
||||
<StatItem
|
||||
icon={<IconWorld size={20} />}
|
||||
label={t("widget.dawarichStatistics.countries")}
|
||||
value={stats.totalCountriesVisited.toLocaleString()}
|
||||
/>
|
||||
)}
|
||||
{options.showCities && (
|
||||
<StatItem
|
||||
icon={<IconBuildingSkyscraper size={20} />}
|
||||
label={t("widget.dawarichStatistics.cities")}
|
||||
value={stats.totalCitiesVisited.toLocaleString()}
|
||||
/>
|
||||
)}
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
|
||||
interface StatItemProps {
|
||||
icon: React.ReactNode;
|
||||
label: string;
|
||||
value: string | number;
|
||||
}
|
||||
|
||||
function StatItem({ icon, label, value }: StatItemProps) {
|
||||
return (
|
||||
<Group justify="space-between" align="center" className={classes.statItem}>
|
||||
<Group gap="sm" align="center">
|
||||
{icon}
|
||||
<Text size="sm" fw={500}>
|
||||
{label}
|
||||
</Text>
|
||||
</Group>
|
||||
<Text size="sm" fw={700} c="var(--mantine-primary-color)">
|
||||
{value}
|
||||
</Text>
|
||||
</Group>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { IconChartBar } from "@tabler/icons-react";
|
||||
|
||||
import { createWidgetDefinition } from "../definition";
|
||||
import { optionsBuilder } from "../options";
|
||||
|
||||
export const { definition, componentLoader } = createWidgetDefinition("dawarichStatistics", {
|
||||
icon: IconChartBar,
|
||||
supportedIntegrations: ["dawarich"],
|
||||
integrationsRequired: true,
|
||||
createOptions() {
|
||||
return optionsBuilder.from((factory) => ({
|
||||
showTotalDistance: factory.switch({ defaultValue: true }),
|
||||
showTotalPoints: factory.switch({ defaultValue: true }),
|
||||
showReverseGeocoded: factory.switch({ defaultValue: true }),
|
||||
showCountries: factory.switch({ defaultValue: true }),
|
||||
showCities: factory.switch({ defaultValue: true }),
|
||||
}));
|
||||
},
|
||||
}).withDynamicImport(() => import("./component"));
|
||||
@@ -60,6 +60,8 @@ import * as beszelSystemStats from "./beszel-system-stats";
|
||||
import * as video from "./video";
|
||||
import * as weather from "./weather";
|
||||
import * as customApi from "./custom-api";
|
||||
import * as dawarichStatistics from "./dawarich-statistics";
|
||||
import * as dawarichPlaces from "./dawarich-places";
|
||||
|
||||
export type {
|
||||
WidgetDefinition,
|
||||
@@ -121,6 +123,8 @@ export const widgetImports = {
|
||||
beszelAlerts,
|
||||
beszelSystemStats,
|
||||
customApi,
|
||||
dawarichStatistics,
|
||||
dawarichPlaces,
|
||||
} satisfies WidgetImportRecord;
|
||||
|
||||
export type WidgetImports = typeof widgetImports;
|
||||
|
||||
Reference in new issue
Block a user