Skip to content

Commit 62f2dc4

Browse files
authored
1 parent 31cddc9 commit 62f2dc4

5 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/components/header.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default function Header() {
4040

4141
function ShareButton() {
4242
const { map } = useMap();
43+
const visualization = useStore((store) => store.visualization);
4344
const [copied, setCopied] = useState(false);
4445

4546
useEffect(() => {
@@ -51,6 +52,7 @@ function ShareButton() {
5152
async function copyShareUrl() {
5253
const url = new URL(window.location.href);
5354
if (map) url.searchParams.set("bbox", getPaddedViewportBbox(map).join(","));
55+
if (visualization) url.searchParams.set("viz", visualization);
5456
await navigator.clipboard.writeText(url.toString());
5557
setCopied(true);
5658
}

src/components/url-bootstrap.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export default function UrlBootstrap({
1414
if (initial) useStore.getState().setHref(initial);
1515
const projection = resolveInitialProjection();
1616
if (projection) useStore.getState().setProjection(projection);
17+
const viz = new URLSearchParams(location.search).get("viz");
18+
if (viz) useStore.getState().setVisualization(viz);
1719
return null;
1820
});
1921

src/components/value.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ export default function Value({
9292
<Stack gap={4}>
9393
<Stack gap={4}>
9494
<Stack gap={2}>
95-
<Heading>
96-
<HStack gap={4}>
95+
<Heading lineHeight={"shorter"}>
96+
<HStack gap={4} wrap={"wrap"}>
9797
{getStacTitle(value)}
9898
{version && <Badge variant={"surface"}>{version}</Badge>}
9999
</HStack>

src/components/visualization.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,38 @@ export default function Visualization({
7272
return createListCollection({ items });
7373
}, [cogAssets, tilejsonLink, wmtsLink, itemAssetKeys]);
7474

75-
const [selected, setSelected] = useState<string | undefined>(() => {
75+
const visualization = useStore((store) => store.visualization);
76+
const setVisualization = useStore((store) => store.setVisualization);
77+
78+
const validValues = useMemo(
79+
() => new Set(collection.items.map((item) => item.value)),
80+
[collection]
81+
);
82+
83+
const fallback = useMemo(() => {
7684
const bestItemKey = pickBestKeyForItems(allItems);
7785
if (bestItemKey && cogAssets.length === 0 && !tilejsonLink && !wmtsLink) {
7886
return `items:${bestItemKey}`;
7987
}
8088
return collection.items[0]?.value;
81-
});
89+
}, [allItems, cogAssets, tilejsonLink, wmtsLink, collection]);
90+
91+
const selected =
92+
visualization && validValues.has(visualization) ? visualization : fallback;
93+
94+
useEffect(() => {
95+
if (visualization && validValues.has(visualization)) return;
96+
if (fallback && fallback !== visualization) setVisualization(fallback);
97+
}, [visualization, validValues, fallback, setVisualization]);
98+
8299
const [enabled, setEnabled] = useState(true);
83100

84101
const firstPage = itemPages[0];
85102
const [lastFirstPage, setLastFirstPage] = useState(firstPage);
86103
if (lastFirstPage !== firstPage) {
87104
setLastFirstPage(firstPage);
88105
const bestItemKey = pickBestKeyForItems(allItems);
89-
if (bestItemKey) setSelected(`items:${bestItemKey}`);
106+
if (bestItemKey) setVisualization(`items:${bestItemKey}`);
90107
}
91108

92109
const setLayer = useStore((store) => store.setLayer);
@@ -181,7 +198,7 @@ export default function Visualization({
181198
size={"sm"}
182199
collection={collection}
183200
value={selected ? [selected] : []}
184-
onValueChange={(e) => setSelected(e.value[0])}
201+
onValueChange={(e) => setVisualization(e.value[0] ?? null)}
185202
disabled={!enabled}
186203
>
187204
<Select.HiddenSelect />

src/store.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export interface State {
7777
setAddErrorListener: (addErrorListener: boolean) => void;
7878
hivePartitioning: boolean;
7979
setHivePartitioning: (hivePartitioning: boolean) => void;
80+
visualization: string | null;
81+
setVisualization: (visualization: string | null) => void;
8082
}
8183

8284
/**
@@ -146,6 +148,8 @@ export const useStore = create<State>()(
146148
setAddErrorListener: (addErrorListener) => set({ addErrorListener }),
147149
hivePartitioning: false,
148150
setHivePartitioning: (hivePartitioning) => set({ hivePartitioning }),
151+
visualization: null,
152+
setVisualization: (visualization) => set({ visualization }),
149153
}),
150154
{
151155
name: "stac-map-settings",

0 commit comments

Comments
 (0)