mirror of
https://github.com/karl0ss/homepage.git
synced 2025-04-29 12:03:41 +01:00
Add Tailscale Widget (#1468)
* Added tailscale widget * finished tailscale widget * Consolidated date comparison to it's own function * Modified to follow Airbnb's style guide * Removed refresh and added translations * fix some tailscale translation strings --------- Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com>
This commit is contained in:
parent
142be300e0
commit
acc19ccca1
@ -232,6 +232,20 @@
|
||||
"stopped": "Stopped",
|
||||
"total": "Total"
|
||||
},
|
||||
"tailscale": {
|
||||
"address": "Address",
|
||||
"expires": "Expires",
|
||||
"never": "Never",
|
||||
"last_seen": "Last Seen",
|
||||
"now": "Now",
|
||||
"years": "{{number}}y",
|
||||
"weeks": "{{number}}w",
|
||||
"days": "{{number}}d",
|
||||
"hours": "{{number}}h",
|
||||
"minutes": "{{number}}m",
|
||||
"seconds": "{{number}}s",
|
||||
"ago": "{{value}} Ago"
|
||||
},
|
||||
"tdarr": {
|
||||
"queue": "Queue",
|
||||
"processed": "Processed",
|
||||
|
@ -32,6 +32,7 @@ export default async function credentialedProxyHandler(req, res, map) {
|
||||
"authentik",
|
||||
"cloudflared",
|
||||
"ghostfolio",
|
||||
"tailscale",
|
||||
"truenas",
|
||||
"pterodactyl",
|
||||
].includes(widget.type))
|
||||
|
@ -71,6 +71,7 @@ const components = {
|
||||
sonarr: dynamic(() => import("./sonarr/component")),
|
||||
speedtest: dynamic(() => import("./speedtest/component")),
|
||||
strelaysrv: dynamic(() => import("./strelaysrv/component")),
|
||||
tailscale: dynamic(() => import("./tailscale/component")),
|
||||
tautulli: dynamic(() => import("./tautulli/component")),
|
||||
tdarr: dynamic(() => import("./tdarr/component")),
|
||||
traefik: dynamic(() => import("./traefik/component")),
|
||||
|
72
src/widgets/tailscale/component.jsx
Normal file
72
src/widgets/tailscale/component.jsx
Normal file
@ -0,0 +1,72 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: statsData, error: statsError } = useWidgetAPI(widget, "device");
|
||||
|
||||
if (statsError) {
|
||||
return <Container service={service} error={statsError} />;
|
||||
}
|
||||
|
||||
if (!statsData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="tailscale.address" />
|
||||
<Block label="tailscale.last_seen" />
|
||||
<Block label="tailscale.expires" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
const {
|
||||
addresses: [address],
|
||||
keyExpiryDisabled,
|
||||
lastSeen,
|
||||
expires,
|
||||
} = statsData;
|
||||
|
||||
const now = new Date();
|
||||
const compareDifferenceInTwoDates = (priorDate, futureDate) => {
|
||||
const diff = futureDate.getTime() - priorDate.getTime();
|
||||
const diffInYears = Math.ceil(diff / (1000 * 60 * 60 * 24 * 365));
|
||||
if (diffInYears > 1) return t("tailscale.years", { number: diffInYears });
|
||||
const diffInWeeks = Math.ceil(diff / (1000 * 60 * 60 * 24 * 7));
|
||||
if (diffInWeeks > 1) return t("tailscale.weeks", { number: diffInWeeks });
|
||||
const diffInDays = Math.ceil(diff / (1000 * 60 * 60 * 24));
|
||||
if (diffInDays > 1) return t("tailscale.days", { number: diffInDays });
|
||||
const diffInHours = Math.ceil(diff / (1000 * 60 * 60));
|
||||
if (diffInHours > 1) return t("tailscale.hours", { number: diffInHours });
|
||||
const diffInMinutes = Math.ceil(diff / (1000 * 60));
|
||||
if (diffInMinutes > 1) return t("tailscale.minutes", { number: diffInMinutes });
|
||||
const diffInSeconds = Math.ceil(diff / 1000);
|
||||
if (diffInSeconds > 10) return t("tailscale.seconds", { number: diffInSeconds });
|
||||
return "Now";
|
||||
};
|
||||
|
||||
const getLastSeen = () => {
|
||||
const date = new Date(lastSeen);
|
||||
const diff = compareDifferenceInTwoDates(date, now);
|
||||
return diff === "Now" ? t("tailscale.now") : t("tailscale.ago", { value: diff });
|
||||
};
|
||||
|
||||
const getExpiry = () => {
|
||||
if (keyExpiryDisabled) return t("tailscale.never");
|
||||
const date = new Date(expires);
|
||||
return compareDifferenceInTwoDates(now, date);
|
||||
};
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="tailscale.address" value={address} />
|
||||
<Block label="tailscale.last_seen" value={getLastSeen()} />
|
||||
<Block label="tailscale.expires" value={getExpiry()} />
|
||||
</Container>
|
||||
);
|
||||
}
|
14
src/widgets/tailscale/widget.js
Normal file
14
src/widgets/tailscale/widget.js
Normal file
@ -0,0 +1,14 @@
|
||||
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
|
||||
|
||||
const widget = {
|
||||
api: "https://api.tailscale.com/api/v2/{endpoint}/{deviceid}",
|
||||
proxyHandler: credentialedProxyHandler,
|
||||
|
||||
mappings: {
|
||||
device: {
|
||||
endpoint: "device",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
@ -65,6 +65,7 @@ import scrutiny from "./scrutiny/widget";
|
||||
import sonarr from "./sonarr/widget";
|
||||
import speedtest from "./speedtest/widget";
|
||||
import strelaysrv from "./strelaysrv/widget";
|
||||
import tailscale from "./tailscale/widget";
|
||||
import tautulli from "./tautulli/widget";
|
||||
import tdarr from "./tdarr/widget";
|
||||
import traefik from "./traefik/widget";
|
||||
@ -147,6 +148,7 @@ const widgets = {
|
||||
sonarr,
|
||||
speedtest,
|
||||
strelaysrv,
|
||||
tailscale,
|
||||
tautulli,
|
||||
tdarr,
|
||||
traefik,
|
||||
|
Loading…
x
Reference in New Issue
Block a user