Modified to follow Airbnb's style guide

This commit is contained in:
Andre Jarrell 2023-05-06 10:14:10 -04:00
parent 619241c315
commit 3ad2f5bf4c
2 changed files with 33 additions and 28 deletions

View File

@ -6,7 +6,7 @@ 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 refreshInterval: 1000 * 60,
}); });
if (statsError) { if (statsError) {
@ -23,37 +23,42 @@ export default function Component({ service }) {
); );
} }
const address = statsData.addresses[0] const {
addresses: [address],
keyExpiryDisabled,
lastSeen,
expires,
} = statsData;
const now = new Date() const now = new Date();
const compareDifferenceInTwoDates = (priorDate, futureDate) => { const compareDifferenceInTwoDates = (priorDate, futureDate) => {
const diff = futureDate.getTime() - priorDate.getTime() const diff = futureDate.getTime() - priorDate.getTime();
const diffInYears = Math.ceil(diff / (1000 * 60 * 60 * 24 * 365)) const diffInYears = Math.ceil(diff / (1000 * 60 * 60 * 24 * 365));
if (diffInYears > 1) return `${diffInYears}y` if (diffInYears > 1) return `${diffInYears}y`;
const diffInWeeks = Math.ceil(diff / (1000 * 60 * 60 * 24 * 7)) const diffInWeeks = Math.ceil(diff / (1000 * 60 * 60 * 24 * 7));
if (diffInWeeks > 1) return `${diffInWeeks}w` if (diffInWeeks > 1) return `${diffInWeeks}w`;
const diffInDays = Math.ceil(diff / (1000 * 60 * 60 * 24)) const diffInDays = Math.ceil(diff / (1000 * 60 * 60 * 24));
if (diffInDays > 1) return `${diffInDays}d` if (diffInDays > 1) return `${diffInDays}d`;
const diffInHours = Math.ceil(diff / (1000 * 60 * 60)) const diffInHours = Math.ceil(diff / (1000 * 60 * 60));
if (diffInHours > 1) return `${diffInHours}h` if (diffInHours > 1) return `${diffInHours}h`;
const diffInMinutes = Math.ceil(diff / (1000 * 60)) const diffInMinutes = Math.ceil(diff / (1000 * 60));
if (diffInMinutes > 1) return `${diffInMinutes}m` if (diffInMinutes > 1) return `${diffInMinutes}m`;
const diffInSeconds = Math.ceil(diff / 1000) const diffInSeconds = Math.ceil(diff / 1000);
if (diffInSeconds > 10) return `${diffInSeconds}s` if (diffInSeconds > 10) return `${diffInSeconds}s`;
return 'Now' return "Now";
} };
const getLastSeen = () => { const getLastSeen = () => {
const lastSeen = new Date(statsData.lastSeen) const date = new Date(lastSeen);
const diff = compareDifferenceInTwoDates(lastSeen, now) const diff = compareDifferenceInTwoDates(date, now);
return `${diff === 'Now' ? diff : `${diff} Ago`}` return `${diff === "Now" ? diff : `${diff} Ago`}`;
} };
const getExpiry = () => { const getExpiry = () => {
if (statsData.keyExpiryDisabled) return 'Never' if (keyExpiryDisabled) return "Never";
const expiry = new Date(statsData.expires) const date = new Date(expires);
return compareDifferenceInTwoDates(now, expiry) return compareDifferenceInTwoDates(now, date);
} };
return ( return (
<Container service={service}> <Container service={service}>

View File

@ -6,7 +6,7 @@ const widget = {
mappings: { mappings: {
device: { device: {
endpoint: "device" endpoint: "device",
}, },
}, },
}; };