Indicate when the next inbox check will be done

This commit is contained in:
nemunaire 2025-10-20 11:15:04 +07:00
commit 0084fd9660
2 changed files with 40 additions and 4 deletions

View file

@ -1,12 +1,22 @@
<script lang="ts"> <script lang="ts">
import { createEventDispatcher } from "svelte";
import type { Test } from "$lib/api/types.gen"; import type { Test } from "$lib/api/types.gen";
import EmailAddressDisplay from "./EmailAddressDisplay.svelte"; import EmailAddressDisplay from "./EmailAddressDisplay.svelte";
const dispatch = createEventDispatcher();
interface Props { interface Props {
test: Test; test: Test;
} }
let { test }: Props = $props(); let { test, nbfetch, nextfetch }: Props = $props();
let lastForcedCheck = $state(Date.now() - 12345);
function forceCheck() {
lastForcedCheck = Date.now();
dispatch("force-inbox-check");
}
</script> </script>
<div class="row justify-content-center"> <div class="row justify-content-center">
@ -32,7 +42,16 @@
<div class="d-flex align-items-center justify-content-center gap-2 text-muted"> <div class="d-flex align-items-center justify-content-center gap-2 text-muted">
<div class="spinner-border spinner-border-sm" role="status"></div> <div class="spinner-border spinner-border-sm" role="status"></div>
{#if nextfetch}
<small>Next inbox check in {nextfetch} second{#if nextfetch > 1}s{/if}...</small>
{#if nbfetch > 0}
<button type="button" class="btn btn-sm btn-link" onclick={forceCheck} disabled={nbfetch + Date.now() < lastForcedCheck + 10000}>
<i class="bi bi-mailbox me-1"></i>Check now
</button>
{/if}
{:else}
<small>Checking for email every 3 seconds...</small> <small>Checking for email every 3 seconds...</small>
{/if}
</div> </div>
</div> </div>
</div> </div>

View file

@ -11,8 +11,15 @@
let loading = $state(true); let loading = $state(true);
let error = $state<string | null>(null); let error = $state<string | null>(null);
let pollInterval: ReturnType<typeof setInterval> | null = null; let pollInterval: ReturnType<typeof setInterval> | null = null;
let nextfetch = $state(23);
let nbfetch = $state(0);
async function fetchTest() { async function fetchTest() {
if (nbfetch > 0) {
nextfetch = Math.max(nextfetch, Math.floor(3 + nbfetch * 0.5));
}
nbfetch += 1;
try { try {
const testResponse = await getTest({ path: { id: testId } }); const testResponse = await getTest({ path: { id: testId } });
if (testResponse.data) { if (testResponse.data) {
@ -35,7 +42,17 @@
} }
function startPolling() { function startPolling() {
pollInterval = setInterval(fetchTest, 3000); pollInterval = setInterval(() => {
nextfetch -= 1;
if (nextfetch <= 0) {
if (!document.hidden) {
fetchTest();
} else {
nextfetch = 1;
}
}
}, 1000);
} }
function stopPolling() { function stopPolling() {
@ -82,7 +99,7 @@
</div> </div>
{:else if test && test.status !== "analyzed"} {:else if test && test.status !== "analyzed"}
<!-- Pending State --> <!-- Pending State -->
<PendingState {test} /> <PendingState {nextfetch} {nbfetch} {test} on:force-inbox-check={() => fetchTest()} />
{:else if report} {:else if report}
<!-- Results State --> <!-- Results State -->
<div class="fade-in"> <div class="fade-in">