Skip to content

Commit

Permalink
Bbox saved in url
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarrenechea committed Oct 24, 2023
1 parent 87e8e96 commit 2f1f54d
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 24 deletions.
18 changes: 2 additions & 16 deletions client/src/app/(app)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { PropsWithChildren } from "react";

import MapSettingsManagerPanel from "@/containers/map-settings";
import MapSettingsManager from "@/containers/map-settings/manager";
import Map from "@/containers/map";
import Navigation from "@/containers/navigation";
import Sidebar from "@/containers/sidebar";

import Map from "@/components/map";
import Controls from "@/components/map/controls";
import SettingsControl from "@/components/map/controls/settings";
import ZoomControl from "@/components/map/controls/zoom";

import LayoutProviders from "./layout-providers";

export default async function AppLayout({ children }: PropsWithChildren) {
Expand All @@ -18,15 +12,7 @@ export default async function AppLayout({ children }: PropsWithChildren) {
<main className="flex h-[100svh] w-full justify-between">
<Navigation />
<Sidebar>{children}</Sidebar>
<Map>
<Controls className="absolute right-6 top-4">
<ZoomControl />
<SettingsControl>
<MapSettingsManagerPanel />
</SettingsControl>
</Controls>
<MapSettingsManager />
</Map>
<Map />
</main>
</LayoutProviders>
);
Expand Down
8 changes: 6 additions & 2 deletions client/src/app/url-query-params.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { useQueryState } from "next-usequerystate";
import { parseAsArrayOf, parseAsFloat, useQueryState } from "next-usequerystate";
import { parseAsJson } from "next-usequerystate/parsers";

import { DEFAULT_MAP_SETTINGS } from "@/components/map/constants";
import { DEFAULT_BBOX, DEFAULT_MAP_SETTINGS } from "@/constants/map";

export const useSyncBbox = () => {
return useQueryState("bbox", parseAsArrayOf(parseAsFloat).withDefault(DEFAULT_BBOX));
};

export const useSyncMapSettings = () => {
return useQueryState(
Expand Down
3 changes: 2 additions & 1 deletion client/src/components/map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import env from "@/env.mjs";

import { cn } from "@/lib/classnames";

import { DEFAULT_VIEW_STATE } from "./constants";
import { DEFAULT_VIEW_STATE } from "../../constants/map";

import type { CustomMapProps } from "./types";

export const Map: FC<CustomMapProps> = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export const DEFAULT_VIEW_STATE: Partial<ViewState> = {
longitude: 0,
};

export const DEFAULT_BBOX: [number, number, number, number] = [
-118.3665, 1.1768, -53.9775, 32.7186,
];

export const BASEMAPS = [
{
label: "Light",
Expand Down
2 changes: 1 addition & 1 deletion client/src/containers/map-settings/basemaps/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { BASEMAPS } from "@/components/map/constants";
import { BASEMAPS } from "@/constants/map";

import BasemapItem from "./item";

Expand Down
2 changes: 1 addition & 1 deletion client/src/containers/map-settings/basemaps/item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { cn } from "@/lib/classnames";

import { useSyncMapSettings } from "@/app/url-query-params";

import { BASEMAPS } from "@/components/map/constants";
import { BASEMAPS } from "@/constants/map";

const BasemapItem = ({
label,
Expand Down
3 changes: 2 additions & 1 deletion client/src/containers/map-settings/labels/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { useCallback } from "react";

import { useSyncMapSettings } from "@/app/url-query-params";

import { LABELS } from "@/components/map/constants";
import { LABELS } from "@/constants/map";

import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";

Expand Down
2 changes: 1 addition & 1 deletion client/src/containers/map-settings/manager/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AnyLayer } from "mapbox-gl";

import { useSyncMapSettings } from "@/app/url-query-params";

import { BASEMAPS } from "@/components/map/constants";
import { BASEMAPS } from "@/constants/map";

type AnyLayerWithMetadata = AnyLayer & {
metadata: Record<string, unknown>;
Expand Down
53 changes: 53 additions & 0 deletions client/src/containers/map/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use client";

import { LngLatBoundsLike, useMap } from "react-map-gl";

import { useSyncBbox } from "@/app/url-query-params";

import { DEFAULT_BBOX } from "@/constants/map";

import MapSettingsManagerPanel from "@/containers/map-settings";
import MapSettingsManager from "@/containers/map-settings/manager";

import Map from "@/components/map";
import Controls from "@/components/map/controls";
import SettingsControl from "@/components/map/controls/settings";
import ZoomControl from "@/components/map/controls/zoom";

export default function MapContainer({ id = "default" }: { id?: string }) {
const { [id]: map } = useMap();
const [bbox, setBbox] = useSyncBbox();

const onMapViewStateChange = () => {
if (map) {
const b = map
.getBounds()
.toArray()
.flat()
.map((v: number) => {
return parseFloat(v.toFixed(2));
});
setBbox(b);
// setTmpBbox(undefined);
}
};

return (
<Map
id={id}
initialViewState={{
bounds: bbox as LngLatBoundsLike,
}}
maxBounds={DEFAULT_BBOX}
onMapViewStateChange={onMapViewStateChange}
>
<Controls className="absolute right-6 top-4">
<ZoomControl />
<SettingsControl>
<MapSettingsManagerPanel />
</SettingsControl>
</Controls>
<MapSettingsManager />
</Map>
);
}
2 changes: 1 addition & 1 deletion client/src/containers/popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LuChevronLeft } from "react-icons/lu";
import { cn } from "@/lib/classnames";

const Popup = ({ children }: PropsWithChildren): JSX.Element => {
const [open, setOpen] = useState(true);
const [open, setOpen] = useState(false);

const toggleOpen = () => setOpen((prev) => !prev);

Expand Down

0 comments on commit 2f1f54d

Please sign in to comment.