-
-
Notifications
You must be signed in to change notification settings - Fork 657
Expand file tree
/
Copy pathAddDataDialog.tsx
More file actions
171 lines (161 loc) · 6.15 KB
/
Copy pathAddDataDialog.tsx
File metadata and controls
171 lines (161 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { useAppStore } from "@geolibre/core";
import type { MapController } from "@geolibre/map";
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@geolibre/ui";
import { Database } from "lucide-react";
import { useCallback, useMemo, useState, type RefObject } from "react";
import { useTranslation } from "react-i18next";
import { AddDataShellProvider } from "./add-data/context";
import { KIND_I18N_KEY } from "./add-data/constants";
import { ArcGISSource } from "./add-data/sources/ArcGISSource";
import { CadSource } from "./add-data/sources/CadSource";
import { DeckVizSource } from "./add-data/sources/DeckVizSource";
import { DelimitedTextSource } from "./add-data/sources/DelimitedTextSource";
import { GdbSource } from "./add-data/sources/GdbSource";
import { GeoRssSource } from "./add-data/sources/GeoRssSource";
import { GpxSource } from "./add-data/sources/GpxSource";
import { MbtilesSource } from "./add-data/sources/MbtilesSource";
import { OgcFeaturesSource } from "./add-data/sources/OgcFeaturesSource";
import { OgcVectorTilesSource } from "./add-data/sources/OgcVectorTilesSource";
import { PhotosSource } from "./add-data/sources/PhotosSource";
import { PostgresSource } from "./add-data/sources/PostgresSource";
import { VideoSource } from "./add-data/sources/VideoSource";
import { WfsSource } from "./add-data/sources/WfsSource";
import { WmsSource } from "./add-data/sources/WmsSource";
import { WmtsSource } from "./add-data/sources/WmtsSource";
import { XyzSource } from "./add-data/sources/XyzSource";
import type { AddDataKind } from "./add-data/types";
import type { OpenAddDataPostgres } from "./add-data/open-add-data";
import { useMartinConnection } from "./add-data/useMartinConnection";
export type { AddDataKind } from "./add-data/types";
interface AddDataDialogProps {
kind: AddDataKind | null;
mapControllerRef: RefObject<MapController | null>;
onOpenChange: (open: boolean) => void;
/**
* Deck.gl Layer kind to pre-select when the dialog opens as `deckgl-viz`
* (e.g. a "3D model" menu entry opens it on the scenegraph layer type).
*/
initialDeckVizKind?: string;
/**
* Connection (and optional table) to pre-select when the dialog opens as
* `postgres` — set when the Browser panel opens a saved connection or a
* clicked PostGIS table.
*/
initialPostgres?: OpenAddDataPostgres;
/** Service URL supplied by a browser-extension deep link. */
initialUrl?: string;
}
/**
* Renders the active data-source subcomponent for the given kind. Each source
* is self-contained: it owns its own form state and submit logic and reads the
* shared services from the dialog shell via context.
*/
function renderSource(
kind: AddDataKind,
initialDeckVizKind: string | undefined,
initialPostgres: OpenAddDataPostgres | undefined,
initialUrl: string | undefined,
) {
switch (kind) {
case "xyz":
return <XyzSource initialUrl={initialUrl} />;
case "wms":
return <WmsSource initialUrl={initialUrl} />;
case "wfs":
return <WfsSource initialUrl={initialUrl} />;
case "wmts":
return <WmtsSource initialUrl={initialUrl} />;
case "ogc-features":
return <OgcFeaturesSource initialUrl={initialUrl} />;
case "ogc-vector-tiles":
return <OgcVectorTilesSource initialUrl={initialUrl} />;
case "gpx":
return <GpxSource />;
case "georss":
return <GeoRssSource />;
case "delimited-text":
return <DelimitedTextSource />;
case "cad":
return <CadSource />;
case "gdb":
return <GdbSource />;
case "photos":
return <PhotosSource />;
case "mbtiles":
return <MbtilesSource />;
case "arcgis":
return <ArcGISSource initialUrl={initialUrl} />;
case "postgres":
return <PostgresSource initialPostgres={initialPostgres} />;
case "video":
return <VideoSource />;
case "deckgl-viz":
return <DeckVizSource initialDeckVizKind={initialDeckVizKind} />;
default:
return null;
}
}
/**
* Shell for the Add Data dialog. Owns the cross-cutting state (submit-in-progress,
* the Martin connection that must survive source remounts) and exposes shared
* services to the per-source subcomponents through context.
*/
export function AddDataDialog({
kind,
mapControllerRef,
onOpenChange,
initialDeckVizKind,
initialPostgres,
initialUrl,
}: AddDataDialogProps) {
const { t } = useTranslation();
const open = kind !== null;
const addLayer = useAppStore((s) => s.addLayer);
const existingLayers = useAppStore((s) => s.layers);
const [isSubmitting, setIsSubmitting] = useState(false);
const martin = useMartinConnection();
const title = kind ? t(`addData.kind.${KIND_I18N_KEY[kind]}.label`) : t("addData.title");
const description = kind ? t(`addData.kind.${KIND_I18N_KEY[kind]}.description`) : "";
const closeDialog = useCallback(() => {
martin.stopTransient();
onOpenChange(false);
}, [martin, onOpenChange]);
const handleOpenChange = (next: boolean) => {
if (!next && isSubmitting) return;
if (!next) martin.stopTransient();
onOpenChange(next);
};
// Memoized so context consumers (the source forms) only re-render when shell
// state actually changes, not on every shell render. Effective because
// `martin` and `closeDialog` are stable across renders (see their hooks).
const contextValue = useMemo(
() => ({
mapControllerRef,
addLayer,
existingLayers,
isSubmitting,
setIsSubmitting,
closeDialog,
martin,
}),
[mapControllerRef, addLayer, existingLayers, isSubmitting, closeDialog, martin],
);
return (
<Dialog open={open} onOpenChange={handleOpenChange}>
<DialogContent className="max-w-xl">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Database className="h-4 w-4" />
{title}
</DialogTitle>
<DialogDescription>{description}</DialogDescription>
</DialogHeader>
{kind ? (
<AddDataShellProvider value={contextValue}>
{renderSource(kind, initialDeckVizKind, initialPostgres, initialUrl)}
</AddDataShellProvider>
) : null}
</DialogContent>
</Dialog>
);
}