Skip to content

Commit

Permalink
Add support for decimal expiration time and switch to days if interva…
Browse files Browse the repository at this point in the history
…l exceeds 48h (#357)

* Add helper function to check for integer

* Add support for decimal expiration time and switch to days if interval exceeds 48h
  • Loading branch information
heisbrot authored Mar 15, 2024
1 parent 121976d commit fe6d8c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/modules/settings/AuthenticationTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
}, []);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ export const validator = {
return semverRegex.test(version);
},
};

export function isInt(n: number) {
return n % 1 === 0;
}

0 comments on commit fe6d8c9

Please sign in to comment.