mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
feat(admin): let an operator switch the place index off
trek_places_enabled was read by six call paths — search, autocomplete, the POI pills, booking-import geocoding, the offline area and details resolution — and written by nothing. No admin route, no MCP tool, no switch anywhere in the UI, and the key is not in the settings allow-lists either, so the only way to set it was an INSERT against app_settings by hand on the server. That matters more than the four switches beside it, which trade away quota. This one decides whether every search and every keystroke in the place box leaves the instance at all. The flag reads fail-open, so an install that upgrades without visiting the admin panel starts sending them, which is the right default and exactly why the operator needs somewhere to say no. Adds the pair symmetrically to place-shadow from the same branch: getTrekPlaces /updateTrekPlaces on AddonsService, GET/PUT /api/admin/trek-places with an audit row, and a real switch on the card. The read stays fail-open so it keeps agreeing with MapsService.trekPlacesEnabled(), and a test pins that agreement across every stored value — if those two ever diverge the panel shows "off" while the queries keep leaving, which is worse than either default. The card said this itself: `available` was a display-only prop whose comment read "False until the client is wired to the service; the card then says so instead of offering a switch that would do nothing." It is wired now, so the prop is a switch. Its two badge strings had been shipped to all 23 locales and rendered nowhere; they make way for the switch label and a line naming what answers while the index is off.
This commit is contained in:
@@ -648,6 +648,8 @@ export const adminApi = {
|
||||
updateBagTracking: (enabled: boolean) => apiClient.put('/admin/bag-tracking', { enabled }).then(r => r.data),
|
||||
getPlacesPhotos: () => apiClient.get('/admin/places-photos').then(r => r.data),
|
||||
updatePlacesPhotos: (enabled: boolean) => apiClient.put('/admin/places-photos', { enabled }).then(r => r.data),
|
||||
getTrekPlaces: () => apiClient.get('/admin/trek-places').then(r => r.data),
|
||||
updateTrekPlaces: (enabled: boolean) => apiClient.put('/admin/trek-places', { enabled }).then(r => r.data),
|
||||
getPlaceShadow: () => apiClient.get('/admin/place-shadow').then(r => r.data),
|
||||
updatePlaceShadow: (enabled: boolean) => apiClient.put('/admin/place-shadow', { enabled }).then(r => r.data),
|
||||
getPlacesAutocomplete: () => apiClient.get('/admin/places-autocomplete').then(r => r.data),
|
||||
|
||||
@@ -493,4 +493,51 @@ describe('AdminSettingsTab', () => {
|
||||
|
||||
expect(admin.setShowRotateJwtModal).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
describe('FE-ADMSET-041: the index switch', () => {
|
||||
// The one control on this card about egress rather than about spending:
|
||||
// with the index on, every search and every keystroke in the place box
|
||||
// leaves the instance. The flag was read by six call paths and written by
|
||||
// nothing, so before this there was no way to turn it off short of an
|
||||
// INSERT against app_settings by hand.
|
||||
const switchFor = () =>
|
||||
screen.getByRole('button', { name: /trek place index/i });
|
||||
|
||||
it('turns the index off and tells the server', async () => {
|
||||
let sent: unknown = null;
|
||||
server.use(
|
||||
http.put('/api/admin/trek-places', async ({ request }) => {
|
||||
sent = await request.json();
|
||||
return HttpResponse.json({ enabled: false });
|
||||
}),
|
||||
);
|
||||
const admin = renderTab({ trekPlacesEnabled: true });
|
||||
|
||||
expect(switchFor()).toHaveAttribute('aria-pressed', 'true');
|
||||
fireEvent.click(switchFor());
|
||||
|
||||
expect(admin.setTrekPlacesEnabledState).toHaveBeenCalledWith(false);
|
||||
await waitFor(() => expect(sent).toEqual({ enabled: false }));
|
||||
});
|
||||
|
||||
it('rolls back when the save fails, instead of showing a state the server never took', async () => {
|
||||
server.use(http.put('/api/admin/trek-places', () => HttpResponse.error()));
|
||||
const admin = renderTab({ trekPlacesEnabled: true });
|
||||
|
||||
fireEvent.click(switchFor());
|
||||
|
||||
await waitFor(() => expect(admin.setTrekPlacesEnabledState).toHaveBeenLastCalledWith(true));
|
||||
});
|
||||
|
||||
it('says what answers instead, but only while it is off', () => {
|
||||
renderTab({ trekPlacesEnabled: false });
|
||||
expect(screen.getByText(/place search uses openstreetmap/i)).toBeInTheDocument();
|
||||
expect(switchFor()).toHaveAttribute('aria-pressed', 'false');
|
||||
});
|
||||
|
||||
it('stays quiet about the fallback while the index is on', () => {
|
||||
renderTab({ trekPlacesEnabled: true });
|
||||
expect(screen.queryByText(/place search uses openstreetmap/i)).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -26,6 +26,7 @@ export default function AdminSettingsTab({ admin, t }: AdminSettingsTabProps): R
|
||||
placesEnrichEnabled, setPlacesEnrichEnabledState,
|
||||
transitProvider, setTransitProviderState,
|
||||
transitGoogleKeySource, setTransitGoogleKeySource,
|
||||
trekPlacesEnabled, setTrekPlacesEnabledState,
|
||||
placeShadowEnabled, setPlaceShadowEnabledState,
|
||||
oidcConfig, setOidcConfig, savingOidc, setSavingOidc,
|
||||
passwordLogin, setPasswordLogin, passwordRegistration, setPasswordRegistration,
|
||||
@@ -381,7 +382,15 @@ export default function AdminSettingsTab({ admin, t }: AdminSettingsTabProps): R
|
||||
{/* Three comparable blocks, in the order we would have people choose
|
||||
them. Weather moved out entirely: it needs no key and had nothing
|
||||
to configure, so it had no business in a card about keys. */}
|
||||
<TrekApiCard t={t} />
|
||||
<TrekApiCard
|
||||
t={t}
|
||||
enabled={trekPlacesEnabled}
|
||||
onToggle={async () => {
|
||||
const next = !trekPlacesEnabled
|
||||
setTrekPlacesEnabledState(next)
|
||||
try { await adminApi.updateTrekPlaces(next) } catch { setTrekPlacesEnabledState(!next) }
|
||||
}}
|
||||
/>
|
||||
|
||||
{!managed && (<>
|
||||
<ProviderBlock title={t('admin.mapsKey')}>
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
import React from 'react'
|
||||
import { Check, ChevronRight, Globe2, KeyRound, Library, ShieldOff, WifiOff, X } from 'lucide-react'
|
||||
import TrekMark from '../../components/shared/TrekMark'
|
||||
import ToggleSwitch from '../../components/Settings/ToggleSwitch'
|
||||
import type { TranslationFn } from '../../types'
|
||||
|
||||
interface TrekApiCardProps {
|
||||
t: TranslationFn
|
||||
/**
|
||||
* Whether this instance can actually use it yet. False until the client is
|
||||
* wired to the service; the card then says so instead of offering a switch
|
||||
* that would do nothing.
|
||||
* Whether the index answers on this instance.
|
||||
*
|
||||
* A real switch rather than a badge, and the only one on this card that is
|
||||
* about egress rather than about spending: with it on, every search and every
|
||||
* keystroke in the place box leaves the instance for the service. An operator
|
||||
* who does not want that has to be able to say so here — the flag was read by
|
||||
* six call paths and written by nothing, so the only way to turn it off was an
|
||||
* INSERT against app_settings by hand.
|
||||
*/
|
||||
available?: boolean
|
||||
enabled: boolean
|
||||
onToggle: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,7 +39,7 @@ interface TrekApiCardProps {
|
||||
*/
|
||||
const SOURCES = ['Overture Maps Foundation', 'OpenStreetMap', 'Wikivoyage', 'Wikimedia']
|
||||
|
||||
export default function TrekApiCard({ t, available = false }: TrekApiCardProps): React.ReactElement {
|
||||
export default function TrekApiCard({ t, enabled, onToggle }: TrekApiCardProps): React.ReactElement {
|
||||
// Reuses the words TREK already has for these fields wherever it has them,
|
||||
// so the chip row costs two new strings instead of ten.
|
||||
const fields = [
|
||||
@@ -77,17 +84,22 @@ export default function TrekApiCard({ t, available = false }: TrekApiCardProps):
|
||||
<div className="px-5 pt-6 pb-4">
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<TrekMark className="h-8 w-auto text-content" aria-label="TREK Places API" />
|
||||
{available && (
|
||||
<span className="flex-shrink-0 rounded-full border border-edge-secondary px-2 py-0.5 text-[11px] font-medium text-content-faint">
|
||||
{t('admin.trekApi.badgeActive')}
|
||||
</span>
|
||||
)}
|
||||
<ToggleSwitch on={enabled} onToggle={onToggle} label={t('admin.trekApi.toggleLabel')} />
|
||||
</div>
|
||||
|
||||
<p className="mt-3 text-sm leading-relaxed text-content-secondary">
|
||||
{t('admin.trekApi.tagline')}
|
||||
</p>
|
||||
|
||||
{/* Said only when it is off, and said plainly: this is the state where
|
||||
searches stop using the index, and the reader should know what
|
||||
answers instead rather than being left to guess. */}
|
||||
{!enabled && (
|
||||
<p className="mt-2 text-xs leading-relaxed text-content-faint">
|
||||
{t('admin.trekApi.offNote')}
|
||||
</p>
|
||||
)}
|
||||
|
||||
<ul className="mt-4 grid grid-cols-1 gap-x-6 gap-y-2 sm:grid-cols-2">
|
||||
{facts.map(({ Icon, text }) => (
|
||||
<li key={text} className="flex items-center gap-2 text-xs text-content-secondary">
|
||||
|
||||
@@ -112,6 +112,11 @@ export function useAdmin() {
|
||||
}, [])
|
||||
// Place shadow log — off unless an admin turns it on, so the initial state is
|
||||
// false rather than the true the four switches above start from.
|
||||
// The index switch. Read fail-open like the server does, so the state shown
|
||||
// before the request lands matches what an unset row actually means.
|
||||
const [trekPlacesEnabled, setTrekPlacesEnabledState] = useState<boolean>(true)
|
||||
useEffect(() => { adminApi.getTrekPlaces().then(d => setTrekPlacesEnabledState(d.enabled)).catch(() => {}) }, [])
|
||||
|
||||
const [placeShadowEnabled, setPlaceShadowEnabledState] = useState<boolean>(false)
|
||||
useEffect(() => { adminApi.getPlaceShadow().then(d => setPlaceShadowEnabledState(d.enabled)).catch(() => {}) }, [])
|
||||
|
||||
@@ -448,6 +453,7 @@ export function useAdmin() {
|
||||
placesEnrichEnabled, setPlacesEnrichEnabledState,
|
||||
transitProvider, setTransitProviderState,
|
||||
transitGoogleKeySource, setTransitGoogleKeySource,
|
||||
trekPlacesEnabled, setTrekPlacesEnabledState,
|
||||
placeShadowEnabled, setPlaceShadowEnabledState,
|
||||
collabFeatures, setCollabFeatures,
|
||||
oidcConfig, setOidcConfig, savingOidc, setSavingOidc,
|
||||
|
||||
Reference in New Issue
Block a user