Skip to content

Commit 2b37c07

Browse files
committed
Merge branch 'main' into groups-page
2 parents 740879a + 3160211 commit 2b37c07

File tree

5 files changed

+176
-30
lines changed

5 files changed

+176
-30
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
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { fetchAPIData } from "~/_utils/fetch-helpers";
2+
3+
export async function data() {
4+
const res = await fetchAPIData(`/defs/strat_name_concepts`, {
5+
page_size: 20,
6+
last_id: 0,
7+
});
8+
return { res };
9+
}

pages/lex/strat-names/+Page.client.ts

Lines changed: 78 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@ import { Card, Switch, Spinner } from "@blueprintjs/core";
66
import { useState, useEffect, useRef } from "react";
77
import { ContentPage } from "~/layouts";
88
import { Loading, SearchBar } from "../../index";
9+
import { useData } from "vike-react/useData";
910

1011
export function Page() {
1112
return StratPage({ show: false });
1213
}
1314

1415
export function StratPage({ show }) {
16+
const { res } = useData();
17+
console.log("res", res);
18+
const startingID = show
19+
? res[res?.length - 1]?.concept_id
20+
: res[res?.length - 1]?.strat_name_id;
21+
1522
const [input, setInput] = useState("");
1623
const [showConcepts, setShowConcepts] = useState(show ?? false);
17-
const [lastID, setLastID] = useState(0);
18-
const [data, setData] = useState([]);
24+
const [lastID, setLastID] = useState(startingID);
25+
const [data, setData] = useState(res);
1926
const pageSize = 20;
2027

2128
const strat_name_vars = {
@@ -38,16 +45,40 @@ export function StratPage({ show }) {
3845

3946
const result = useStratData(lastID, input, pageSize, data_route, like);
4047

48+
console.log(lastID);
49+
console.log("data", data);
50+
51+
const prevInputRef = useRef(input);
52+
const prevShowConceptsRef = useRef(showConcepts);
53+
4154
useEffect(() => {
42-
if (result) {
43-
setData((prevData) => [...prevData, ...result]);
55+
// Only reset if input or showConcepts actually changed from previous render
56+
if (
57+
prevInputRef.current !== input ||
58+
prevShowConceptsRef.current !== showConcepts
59+
) {
60+
// Reset data and lastID to starting ID for current mode
61+
setData([]);
62+
setLastID(0);
63+
64+
prevInputRef.current = input;
65+
prevShowConceptsRef.current = showConcepts;
4466
}
45-
}, [result]);
67+
}, [input, showConcepts]);
4668

4769
useEffect(() => {
48-
setData([]);
49-
setLastID(0);
50-
}, [input, showConcepts]);
70+
if (
71+
result &&
72+
data[data.length - 1]?.[showConcepts ? "concept_id" : "strat_name_id"] !==
73+
result[result.length - 1]?.[
74+
showConcepts ? "concept_id" : "strat_name_id"
75+
]
76+
) {
77+
setData((prevData) => {
78+
return [...prevData, ...result];
79+
});
80+
}
81+
}, [result]);
5182

5283
if (data == null) return h(Loading);
5384

@@ -59,17 +90,43 @@ export function StratPage({ show }) {
5990
h(StickyHeader, { className: "header" }, [
6091
h(PageBreadcrumbs, { title }),
6192
h("div.header-description", [
62-
h("p", [
63-
h("strong", "Strat Names: "),
64-
h("span", "names of rock units, organized hierarchically"),
65-
]),
66-
h("p", [
67-
h("strong", "Strat Concepts: "),
68-
h(
69-
"span",
70-
"capture relationships between differently-named rock units"
71-
),
72-
]),
93+
h(
94+
Card,
95+
{
96+
className: !showConcepts ? "selected" : "unselected",
97+
onClick: () => {
98+
if (showConcepts) {
99+
setShowConcepts(false);
100+
setLastID(0);
101+
setData([]);
102+
}
103+
},
104+
},
105+
[
106+
h("strong", "Strat Names: "),
107+
h("span", "names of rock units, organized hierarchically"),
108+
]
109+
),
110+
h(
111+
Card,
112+
{
113+
className: showConcepts ? "selected" : "unselected",
114+
onClick: () => {
115+
if (!showConcepts) {
116+
setShowConcepts(true);
117+
setLastID(0);
118+
setData([]);
119+
}
120+
},
121+
},
122+
[
123+
h("strong", "Strat Concepts: "),
124+
h(
125+
"span",
126+
"capture relationships between differently-named rock units"
127+
),
128+
]
129+
),
73130
]),
74131
h(Card, { className: "filter" }, [
75132
h(SearchBar, {
@@ -81,6 +138,8 @@ export function StratPage({ show }) {
81138
checked: showConcepts,
82139
onChange: (e) => {
83140
setShowConcepts(e.target.checked);
141+
setLastID(0);
142+
setData([]);
84143
},
85144
}),
86145
]),

pages/lex/strat-names/+data.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { fetchAPIData } from "~/_utils/fetch-helpers";
2+
3+
export async function data() {
4+
const res = await fetchAPIData(`/defs/strat_names`, {
5+
page_size: 20,
6+
last_id: 0,
7+
});
8+
return { res };
9+
}

pages/lex/strat-names/main.module.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@
7979
flex-direction: column;
8080
gap: 1em;
8181

82+
.selected {
83+
color: var(--text-emphasized-color) !important;
84+
background-color: var(--secondary-background) !important;
85+
}
86+
87+
.unselected {
88+
cursor: pointer;
89+
}
90+
8291
p {
8392
display: flex;
8493
align-items: flex-end;

0 commit comments

Comments
 (0)