Skip to content

Commit fa8c542

Browse files
committed
feat: accept any registry dbname via url param and alert on invaild names; closes #95
1 parent b53a802 commit fa8c542

File tree

1 file changed

+39
-61
lines changed

1 file changed

+39
-61
lines changed

src/pages/SearchPage.tsx

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Drawer,
1313
Tooltip,
1414
IconButton,
15+
Alert,
1516
} from "@mui/material";
1617
import { useTheme } from "@mui/material/styles";
1718
import useMediaQuery from "@mui/material/useMediaQuery";
@@ -54,9 +55,12 @@ const matchesKeyword = (item: RegistryItem, keyword: string) => {
5455
);
5556
};
5657

57-
const getPresetKey = () => {
58-
return new URLSearchParams(window.location.search).get("preset");
59-
};
58+
// const getDbnameKey = () => {
59+
// return new URLSearchParams(window.location.search).get("dbname");
60+
// };
61+
const getDbnameFromURL = () =>
62+
new URLSearchParams(window.location.search).get("dbname")?.trim() || "";
63+
// const [invalidDbNotice, setInvalidDbNotice] = useState<string | null>(null);
6064

6165
const SearchPage: React.FC = () => {
6266
const dispatch = useAppDispatch();
@@ -72,7 +76,7 @@ const SearchPage: React.FC = () => {
7276
const [formData, setFormData] = useState<Record<string, any>>({});
7377
const [showSubjectFilters, setShowSubjectFilters] = useState(false);
7478
const [showDatasetFilters, setShowDatasetFilters] = useState(true); // for dataset-level filters
75-
79+
const [invalidDbNotice, setInvalidDbNotice] = useState<string | null>(null);
7680
const [results, setResults] = useState<
7781
any[] | { status: string; msg: string }
7882
>([]);
@@ -124,18 +128,31 @@ const SearchPage: React.FC = () => {
124128
useEffect(() => {
125129
// If a #query=... already exists, existing effect will handle it.
126130
if (window.location.hash.startsWith("#query=")) return;
127-
128-
const key = getPresetKey(); // "openneuro"
129-
if (key === "openneuro") {
130-
const initial = { database: "openneuro" };
131+
if (!Array.isArray(registry) || registry.length === 0) return; // wait until registry is loaded
132+
// const key = getDbnameKey(); // "openneuro"
133+
const urlDb = getDbnameFromURL(); // e.g., "openneuro", "bfnirs", etc.
134+
if (!urlDb) return;
135+
// case-insensitive match against registry ids
136+
const match = (registry as RegistryItem[]).find(
137+
(r) => String(r.id).toLowerCase() === urlDb.toLowerCase()
138+
);
139+
// if (!match) return; // unknown dbname; do nothing
140+
141+
if (match) {
142+
const initial = { database: match.id };
131143
// set initial form/filter state
132144
setFormData(initial);
133145
setAppliedFilters(initial);
134146
setHasSearched(false); // set it to true if want to auto-run search
135147
setShowSubjectFilters(true); // expand the subject-level section
136148
setShowDatasetFilters(false); // collapse the dataset-level section
149+
} else {
150+
setInvalidDbNotice(
151+
`Database “${urlDb}” isn’t available. Showing all databases instead.`
152+
);
153+
return;
137154
}
138-
}, []);
155+
}, [registry]);
139156

140157
// parse query from url on page load
141158
useEffect(() => {
@@ -474,24 +491,6 @@ const SearchPage: React.FC = () => {
474491
</Box>
475492
)}
476493

477-
{/* before submit box */}
478-
{/* <Box>
479-
{!hasSearched && (
480-
<Typography
481-
variant="subtitle1"
482-
sx={{
483-
flexWrap: "wrap",
484-
fontWeight: 500,
485-
fontSize: "large",
486-
color: Colors.darkPurple,
487-
}}
488-
>
489-
Use the filters and click submit to search for datasets or
490-
subjects based on metadata.
491-
</Typography>
492-
)}
493-
</Box> */}
494-
495494
{/* after submit box */}
496495
<Box
497496
sx={{
@@ -569,47 +568,26 @@ const SearchPage: React.FC = () => {
569568
</Box>
570569
)}
571570

572-
{/* {!hasSearched && (
573-
<Typography
574-
variant="subtitle1"
575-
sx={{
576-
flexWrap: "wrap",
577-
fontWeight: 500,
578-
fontSize: "large",
579-
color: Colors.darkPurple,
580-
mb: 2,
581-
pt: 1,
582-
}}
583-
>
584-
Use the filters and click submit to search for{" "}
585-
<Box component="span" sx={{ color: Colors.darkOrange, fontWeight: 700 }}>
586-
datasets
587-
</Box>{" "}
588-
and{" "}
589-
<Box
590-
component="span"
591-
sx={{ color: Colors.darkOrange, fontWeight: 700 }}
592-
>
593-
subjects
594-
</Box>{" "}
595-
based on metadata.
596-
</Typography>
597-
)} */}
598-
599571
<Box
600572
sx={{
601-
// display: "grid",
602-
// gridTemplateColumns: {
603-
// xs: "1fr",
604-
// md: hasDbMatches ? "1fr 2fr" : "1fr",
605-
// },
606-
// gap: 2,
607-
// alignItems: "baseline",
608573
display: "flex",
609574
flexDirection: "column",
610575
gap: 2,
611576
}}
612577
>
578+
{/* if the dbname in the url is invalid */}
579+
{invalidDbNotice && (
580+
<Box mb={2}>
581+
<Alert
582+
severity="warning"
583+
onClose={() => setInvalidDbNotice(null)}
584+
sx={{ border: `1px solid ${Colors.lightGray}` }}
585+
>
586+
{invalidDbNotice}
587+
</Alert>
588+
</Box>
589+
)}
590+
613591
{/* suggested databases */}
614592
{registryMatches.length > 0 && (
615593
<Box

0 commit comments

Comments
 (0)