added expiry warning

This commit is contained in:
Christopher Bisset 2022-08-13 11:14:39 +10:00
parent 98df17298f
commit d91cda082a

View file

@ -1,27 +1,46 @@
<script lang="ts">
import { getAPIKeys } from '$lib/common/apiFunctions.svelte';
import { APIKey } from '$lib/common/classes';
import { alertStore } from '$lib/common/stores';
import { onMount } from 'svelte';
let keyList = [new APIKey()];
import { APIKey } from '$lib/common/classes';
import { alertStore, APIKeyStore } from '$lib/common/stores';
import { onMount } from 'svelte';
let keyList = [new APIKey()];
let timeLeftWarning = false;
let timeLeftTip = '';
function getAPIKeysAction() {
function getAPIKeysAction(): void {
getAPIKeys()
.then((keys) => {
keyList = keys;
console.log(keyList);
// match up the current apikey to the keylist
keyList.forEach(key => {
if($APIKeyStore.includes(key.prefix)) {
timeLeftTip = timeLeft(new Date(key.expiration));
}
});
})
.catch((error) => {
$alertStore = error;
});
}
onMount(() => {
getAPIKeysAction();
});
// returns time last seen in human readable format
function timeLeft(date: Date) {
let currentTime = new Date();
// gets time difference in seconds
let timeDifferenceDays = Math.round((date.getTime() - currentTime.getTime()) / 1000 / 60 / 60 / 24);
if(timeDifferenceDays < 30) {
$alertStore = `${timeDifferenceDays} days left before expiry, consider rolling your key`
timeLeftWarning = true;
}
return `${timeDifferenceDays} days left before expiry`;
}
onMount(() => {
getAPIKeysAction();
});
</script>
<button type="button" class="tooltip" data-tip="stuff">
<button type="button" class="tooltip" data-tip={timeLeftTip}>
<!-- clock -->
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 inline" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>
<svg xmlns="http://www.w3.org/2000/svg" class:stroke-error="{timeLeftWarning}" class="h-5 w-5 inline" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>
</button>