Skip to content

Commit 4325611

Browse files
authored
Merge pull request #289 from UW-Macrostrat/legacy-links
Legacy Links
2 parents f5e718a + 8a96011 commit 4325611

File tree

1 file changed

+71
-11
lines changed

1 file changed

+71
-11
lines changed

pages/dev/url-tests/+Page.ts

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import h from "@macrostrat/hyper";
2+
import { useEffect, useState } from "react";
23

34
export function Page() {
45
const siftUrls = [
@@ -78,6 +79,74 @@ export function Page() {
7879
"/-112.1976/36.0962#strat_name_concepts=11016&x=-112.236&y=36.2119&z=15.61km&a=165&e=42",
7980
];
8081

82+
const allUrls = [
83+
...locUrls.map((url) => ({ path: "/map/loc" + url, type: "loc" })),
84+
...siftUrls.flatMap((url) =>
85+
url.startsWith("/")
86+
? [
87+
{ path: "/sift#" + url, type: "sift" },
88+
{ path: "/sift/#" + url, type: "sift" },
89+
{ path: "/sift" + url, type: "sift" },
90+
]
91+
: [{ path: null, label: url }]
92+
),
93+
];
94+
95+
const [statusMap, setStatusMap] = useState({});
96+
97+
useEffect(() => {
98+
async function checkUrls() {
99+
const updates = {};
100+
for (const entry of allUrls) {
101+
if (!entry.path) continue;
102+
try {
103+
const response = await fetch(entry.path, {
104+
method: "HEAD",
105+
redirect: "follow",
106+
});
107+
setStatusMap((prev) => ({
108+
...prev,
109+
[entry.path]: {
110+
ok: response.ok,
111+
redirected: response.redirected,
112+
finalUrl: response.url,
113+
status: response.status,
114+
},
115+
}));
116+
} catch (err) {
117+
setStatusMap((prev) => ({ ...prev, [entry.path]: { ok: false } }));
118+
}
119+
}
120+
}
121+
checkUrls();
122+
}, []);
123+
124+
function renderUrlEntry(entry) {
125+
if (!entry.path) return h("h3", entry.label);
126+
127+
const status = statusMap[entry.path];
128+
let statusColor = "yellow";
129+
130+
if (status) {
131+
statusColor = status.ok ? "green" : "red";
132+
}
133+
134+
return h("div.url-entry", [
135+
h("span.status", {
136+
style: {
137+
backgroundColor: statusColor,
138+
width: "1.5em",
139+
height: "1.5em",
140+
display: "inline-block",
141+
borderRadius: "10%",
142+
marginRight: "0.5em",
143+
},
144+
}),
145+
h("a", { href: entry.path }, entry.path),
146+
status?.finalUrl ? h("span.redirect", ` -> ${status.finalUrl}`) : null,
147+
]);
148+
}
149+
81150
return h(
82151
"div.url-list",
83152
{
@@ -90,18 +159,9 @@ export function Page() {
90159
},
91160
[
92161
h("h1", "Loc URLs"),
93-
...locUrls.map((url) => {
94-
return [h("a", { href: "/map/loc" + url }, "/map/loc" + url)];
95-
}),
96-
162+
...allUrls.slice(0, locUrls.length).map(renderUrlEntry),
97163
h("h1", "Sift URLs"),
98-
...siftUrls.map((url) => {
99-
if (url.slice(0, 1) != "/") return h("h3", url);
100-
return [
101-
h("a", { href: "/sift#" + url }, "/sift#" + url),
102-
h("a", { href: "/sift/#" + url }, "/sift/#" + url),
103-
];
104-
}),
164+
...allUrls.slice(locUrls.length).map(renderUrlEntry),
105165
]
106166
);
107167
}

0 commit comments

Comments
 (0)