Add support for decimal expiration time and switch to days if interval exceeds 48h (#357)

* Add helper function to check for integer

* Add support for decimal expiration time and switch to days if interval exceeds 48h
This commit is contained in:
Eduard Gert
2024-03-15 15:54:06 +01:00
committed by GitHub
parent 121976d101
commit fe6d8c9bd5
2 changed files with 12 additions and 5 deletions

View File

@@ -15,7 +15,7 @@ import {
} from "@components/Select";
import * as Tabs from "@radix-ui/react-tabs";
import { useApiCall } from "@utils/api";
import { cn } from "@utils/helpers";
import { cn, isInt } from "@utils/helpers";
import { isLocalDev, isNetBirdHosted } from "@utils/netbird";
import { CalendarClock, ShieldIcon, TimerReset, VoteIcon } from "lucide-react";
import React, { useMemo, useState } from "react";
@@ -55,18 +55,21 @@ export default function AuthenticationTab({ account }: Props) {
const [expiresInSeconds] = useState(
account.settings.peer_login_expiration || 86400,
);
const [expiresIn, setExpiresIn] = useState(() => {
if (expiresInSeconds <= 86400) {
return Math.round(expiresInSeconds / 3600).toString();
if (expiresInSeconds <= 172800) {
const hours = expiresInSeconds / 3600;
return isInt(hours) ? hours.toString() : hours.toFixed(2).toString();
}
return Math.round(expiresInSeconds / 86400).toString();
const days = expiresInSeconds / 86400;
return isInt(days) ? days.toString() : days.toFixed(2).toString();
});
/**
* Interval
*/
const initialInterval = useMemo(() => {
if (expiresInSeconds <= 86400) return "hours";
if (expiresInSeconds <= 172800) return "hours";
return "days";
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

View File

@@ -75,3 +75,7 @@ export const validator = {
return semverRegex.test(version);
},
};
export function isInt(n: number) {
return n % 1 === 0;
}