forked from opengeos/GeoLibre
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginsMenu.tsx
More file actions
217 lines (207 loc) · 7.92 KB
/
Copy pathPluginsMenu.tsx
File metadata and controls
217 lines (207 loc) · 7.92 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import {
COMPONENTS_PLUGIN_ID,
DECK_VIZ_PLUGIN_ID,
DGGS_PLUGIN_IDS,
DIRECTIONS_PLUGIN_ID,
type GeoLibreMapControlPosition,
GRATICULE_PLUGIN_ID,
CLOUDS_PLUGIN_ID,
PRECIPITATION_PLUGIN_ID,
REVERSE_GEOCODE_PLUGIN_ID,
EFFECTS_PLUGIN_ID,
ROUTE_ANIMATION_PLUGIN_ID,
SUN_PLUGIN_ID,
WEB_SERVICE_PLUGIN_IDS,
} from "@geolibre/plugins";
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from "@geolibre/ui";
import { Puzzle } from "lucide-react";
import { useTranslation } from "react-i18next";
import type { usePluginRegistry } from "../../../hooks/usePlugins";
import { type AppApi, PLUGIN_POSITION_ITEMS, type ToolbarChrome } from "./constants";
type PluginRegistry = ReturnType<typeof usePluginRegistry>;
type RegisteredPlugin = PluginRegistry["plugins"][number];
// Plugins grouped under the "Web Services" submenu of the Plugins menu.
const WEB_SERVICE_PLUGIN_ID_SET = new Set<string>(WEB_SERVICE_PLUGIN_IDS);
// The discrete global grids (H3, S2, A5) grouped under the "DGGS" submenu.
const DGGS_PLUGIN_ID_SET = new Set<string>(DGGS_PLUGIN_IDS);
interface PluginsMenuProps {
chrome: ToolbarChrome;
appApi: AppApi;
plugins: RegisteredPlugin[];
isActive: PluginRegistry["isActive"];
toggle: PluginRegistry["toggle"];
getMapControlPosition: PluginRegistry["getMapControlPosition"];
setMapControlPosition: PluginRegistry["setMapControlPosition"];
/** Plugin ids hidden by the active UI profile (issue #500). */
hiddenPluginIds: Set<string>;
}
/** The Plugins menu: one toggle per registered plugin, with position submenus. */
export function PluginsMenu({
chrome,
appApi,
plugins,
isActive,
toggle,
getMapControlPosition,
setMapControlPosition,
hiddenPluginIds,
}: PluginsMenuProps) {
const { t } = useTranslation();
const renderPluginMenuItem = (p: RegisteredPlugin) => {
const pluginPosition = getMapControlPosition(p.id);
// Translate the plugin's display name when a locale string exists for its id,
// falling back to the registered (English) name — brand/proper-noun plugins
// (Mapillary, NASA Earthdata, …) simply have no key and stay as-is.
const pluginName = t(`toolbar.plugin.${p.id}`, { defaultValue: p.name });
if (!pluginPosition) {
return (
<DropdownMenuItem key={p.id} onClick={() => toggle(p.id, appApi)}>
{pluginName}
{isActive(p.id) ? " ✓" : ""}
</DropdownMenuItem>
);
}
return (
<DropdownMenuSub key={p.id}>
<DropdownMenuSubTrigger>
{pluginName}
{isActive(p.id) ? " ✓" : ""}
</DropdownMenuSubTrigger>
<DropdownMenuSubContent>
<DropdownMenuItem onClick={() => toggle(p.id, appApi)}>
{isActive(p.id) ? t("toolbar.item.deactivate") : t("toolbar.item.activate")}
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuLabel>{t("toolbar.item.position")}</DropdownMenuLabel>
<DropdownMenuRadioGroup
value={pluginPosition}
onValueChange={(position: string) =>
setMapControlPosition(p.id, appApi, position as GeoLibreMapControlPosition)
}
>
{PLUGIN_POSITION_ITEMS.map((position) => (
<DropdownMenuRadioItem
key={position.value}
value={position.value}
onSelect={(event: Event) => event.preventDefault()}
>
{t(position.labelKey)}
</DropdownMenuRadioItem>
))}
</DropdownMenuRadioGroup>
</DropdownMenuSubContent>
</DropdownMenuSub>
);
};
const webServicePlugins = plugins.filter(
(p) => WEB_SERVICE_PLUGIN_ID_SET.has(p.id) && !hiddenPluginIds.has(p.id),
);
// The web service plugins render as one grouped submenu, placed where the
// first of them appears in registration order (just above Historical Imagery).
let webServicesRendered = false;
const dggsPlugins = plugins.filter(
(p) => DGGS_PLUGIN_ID_SET.has(p.id) && !hiddenPluginIds.has(p.id),
);
// The DGGS grid plugins (H3, S2, A5) render as one grouped submenu, placed
// where the first of them appears in registration order.
let dggsRendered = false;
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
className={chrome.buttonClass}
variant="ghost"
size={chrome.buttonSize}
aria-label={t("toolbar.menu.plugins")}
>
<Puzzle className={chrome.iconClassName} />
{chrome.renderLabel(t("toolbar.menu.plugins"))}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start">
<DropdownMenuLabel>{t("toolbar.item.activatePlugin")}</DropdownMenuLabel>
<DropdownMenuSeparator />
{plugins.map((p) => {
// Atmospheric Effects, Directions, Reverse Geocode, Gridlines, and the
// Weather overlays (Clouds, Precipitation) are toggled from the
// Controls menu instead, so they are omitted here to avoid a duplicate
// toggle. The deck.gl viz overlay is an internal renderer driven by the
// Add Data → "Deck.gl Layer" dialog, not a
// user-facing toggle, so it is hidden here too. The Components plugin
// stays registered for in-app use, but its catch-all grid is hidden
// from the menu now that its panels are reachable through dedicated
// plugins.
if (
p.id === EFFECTS_PLUGIN_ID ||
p.id === SUN_PLUGIN_ID ||
p.id === ROUTE_ANIMATION_PLUGIN_ID ||
p.id === DIRECTIONS_PLUGIN_ID ||
p.id === REVERSE_GEOCODE_PLUGIN_ID ||
p.id === GRATICULE_PLUGIN_ID ||
p.id === CLOUDS_PLUGIN_ID ||
p.id === PRECIPITATION_PLUGIN_ID ||
p.id === DECK_VIZ_PLUGIN_ID ||
p.id === COMPONENTS_PLUGIN_ID
) {
return null;
}
// Hidden by the active UI profile (issue #500).
if (hiddenPluginIds.has(p.id)) {
return null;
}
if (DGGS_PLUGIN_ID_SET.has(p.id)) {
// Same one-shot pattern as the Web Services submenu below: the
// submenu renders at the first visible DGGS plugin's position and
// later ones are skipped.
if (dggsRendered) return null;
dggsRendered = true;
return (
<DropdownMenuSub key="dggs">
<DropdownMenuSubTrigger>
{t("toolbar.item.dggs")}
{dggsPlugins.some((plugin) => isActive(plugin.id)) ? " ✓" : ""}
</DropdownMenuSubTrigger>
<DropdownMenuSubContent>
{dggsPlugins.map(renderPluginMenuItem)}
</DropdownMenuSubContent>
</DropdownMenuSub>
);
}
if (!WEB_SERVICE_PLUGIN_ID_SET.has(p.id)) {
return renderPluginMenuItem(p);
}
// Reaching here means `p` is a visible web service plugin, so the
// submenu has at least one entry. When all web service plugins are
// hidden no plugin reaches this branch and the submenu simply never
// renders.
if (webServicesRendered) return null;
webServicesRendered = true;
return (
<DropdownMenuSub key="web-services">
<DropdownMenuSubTrigger>
{t("toolbar.item.webServices")}
{webServicePlugins.some((plugin) => isActive(plugin.id)) ? " ✓" : ""}
</DropdownMenuSubTrigger>
<DropdownMenuSubContent>
{webServicePlugins.map(renderPluginMenuItem)}
</DropdownMenuSubContent>
</DropdownMenuSub>
);
})}
</DropdownMenuContent>
</DropdownMenu>
);
}