diff --git a/src/components/instance-select.astro b/src/components/instance-select.astro index c455070..96c112f 100644 --- a/src/components/instance-select.astro +++ b/src/components/instance-select.astro @@ -7,7 +7,7 @@ const { prefilledInstance } = Astro.props; --- - + Fediverse instance @@ -17,7 +17,7 @@ const { prefilledInstance } = Astro.props; name="instance" id="instance" placeholder="mastodon.social" - list="instance-list" + list="instances" required aria-describedby="https-label" value={prefilledInstance} @@ -67,6 +67,7 @@ const { prefilledInstance } = Astro.props; - diff --git a/src/stores/popular-instances.ts b/src/stores/popular-instances.ts new file mode 100644 index 0000000..7699790 --- /dev/null +++ b/src/stores/popular-instances.ts @@ -0,0 +1,38 @@ +import { persistentAtom } from "@nanostores/persistent"; +import { onMount, task } from "nanostores"; + +const UPDATE_INTERVAL_MS = 1000 * 60 * 60 * 24; // one day + +export const $popularInstances = persistentAtom( + "popularInstances", + [], + { + encode: JSON.stringify, + decode: JSON.parse, + }, +); + +export const $lastFetched = persistentAtom( + "popularInstancesLastFetched", + new Date(0), + { + encode: (date) => date.toISOString(), + decode: (encoded) => new Date(encoded), + }, +); + +onMount($popularInstances, () => { + task(async () => { + if (Date.now() - $lastFetched.get().getTime() < UPDATE_INTERVAL_MS) { + return; + } + + try { + const response = await fetch("/api/instances"); + $popularInstances.set(await response.json()); + $lastFetched.set(new Date()); + } catch (error) { + console.error("Could not fetch popular instances:", error); + } + }); +});