finished tailscale widget

This commit is contained in:
Andre Jarrell 2023-05-05 04:27:05 -04:00
parent fea59b5177
commit e1610e38f0
3 changed files with 40 additions and 14 deletions

View File

@ -233,8 +233,9 @@
"total": "Total" "total": "Total"
}, },
"tailscale": { "tailscale": {
"private_ip": "Private IP", "address": "Address",
"status": "Status" "expires": "Expires",
"last_seen": "Last Seen"
}, },
"tdarr": { "tdarr": {
"queue": "Queue", "queue": "Queue",

View File

@ -5,7 +5,9 @@ import useWidgetAPI from "utils/proxy/use-widget-api";
export default function Component({ service }) { export default function Component({ service }) {
const { widget } = service; const { widget } = service;
const { data: statsData, error: statsError } = useWidgetAPI(widget, "device"); const { data: statsData, error: statsError } = useWidgetAPI(widget, "device", {
refreshInterval: 1000 * 60
});
if (statsError) { if (statsError) {
return <Container service={service} error={statsError} />; return <Container service={service} error={statsError} />;
@ -14,24 +16,47 @@ export default function Component({ service }) {
if (!statsData) { if (!statsData) {
return ( return (
<Container service={service}> <Container service={service}>
<Block label="tailscale.status" /> <Block label="tailscale.address" />
<Block label="tailscale.private_ip" /> <Block label="tailscale.last_seen" />
<Block label="tailscale.expires" />
</Container> </Container>
); );
} }
const getStatus = () => { const now = new Date()
const { endpoints, latency } = statsData.clientConnectivity const address = statsData.addresses[0]
return (endpoints.length === 0 || Object.keys(latency).length === 0) ? "Offline" : "Online"
const getLastSeen = () => {
const lastSeen = new Date(statsData.lastSeen)
const diff = now.getTime() - lastSeen.getTime()
const diffInYears = Math.ceil(diff / (1000 * 60 * 60 * 24 * 365))
if (diffInYears > 1) return `${diffInYears}y Ago`
const diffInWeeks = Math.ceil(diff / (1000 * 60 * 60 * 24 * 7))
if (diffInWeeks > 1) return `${diffInWeeks}w Ago`
const diffInDays = Math.ceil(diff / (1000 * 60 * 60 * 24))
if (diffInDays > 1) return `${diffInDays}d Ago`
const diffInHours = Math.ceil(diff / (1000 * 60 * 60))
if (diffInHours > 1) return `${diffInHours}h Ago`
const diffInMinutes = Math.ceil(diff / (1000 * 60))
if (diffInMinutes > 1) return `${diffInMinutes}m Ago`
const diffInSeconds = Math.ceil(diff / 1000)
if (diffInSeconds > 10) return `${diffInSeconds}s Ago`
return 'Just Now'
} }
const getExpiry = () => {
const privateIP = statsData.addresses[0] if (statsData.keyExpiryDisabled) return 'Never'
const expiry = new Date(statsData.expires)
const diff = expiry.getTime() - now.getTime()
const days = Math.ceil(diff / (1000 * 60 * 60 * 24))
return `${days} Days`
}
return ( return (
<Container service={service}> <Container service={service}>
<Block label="tailscale.status" value={getStatus()} /> <Block label="tailscale.address" value={address} />
<Block label="tailscale.private_ip" value={privateIP} /> <Block label="tailscale.last_seen" value={getLastSeen()} />
<Block label="tailscale.expires" value={getExpiry()} />
</Container> </Container>
); );
} }

View File

@ -1,7 +1,7 @@
import credentialedProxyHandler from "utils/proxy/handlers/credentialed"; import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
const widget = { const widget = {
api: "https://api.tailscale.com/api/v2/{endpoint}/{deviceid}?fields=all", api: "https://api.tailscale.com/api/v2/{endpoint}/{deviceid}",
proxyHandler: credentialedProxyHandler, proxyHandler: credentialedProxyHandler,
mappings: { mappings: {