Files
TREK/client/src/components/Admin/useAdminSettingsActions.ts
T
Maurice a3620f9e05 feat(mobile): the admin services section gets what the desktop tab has
- Place search and routing as one card, as on the desktop: TREK's own
  services, today's allowance with a new identity behind a confirm sheet
  (MAdminServiceQuota), sharing place details (MAdminPlaceContrib), the transit
  backend and the place search provider as TREK selects, and the search log.
- The key card holds Google (with its options and the daily request cap,
  MAdminGoogleDailyLimit), Unsplash and Amap, in the desktop order.
- The logic both views need moves into one place: the transit switch into
  useAdminSettingsActions, the allowance figures into serviceIdentityModel,
  and the share-place-details panel into usePlaceContribPanel.
- MAdminSettingsSection renders each section in a function of its own.
- Tests follow the split admin sections (Save order, 2FA label, field marker).
2026-11-14 11:09:22 +01:00

135 lines
4.6 KiB
TypeScript

import type { TransitKeySource, TransitProvider } from '@trek/shared';
import { adminApi, authApi } from '../../api/client';
import type { TranslationFn } from '../../types';
import { getApiErrorMessage } from '../../types';
type Flag = (value: boolean) => void;
interface OidcFields {
issuer: string;
client_id: string;
client_secret: string;
display_name: string;
discovery_url: string;
}
interface SettingsActionsHost {
toast: { success: (message: string) => void; error: (message: string) => void };
oidcConfig: OidcFields;
setSavingOidc: Flag;
allowedFileTypes: string;
setSavingFileTypes: Flag;
setPlacesPhotosEnabledState: Flag;
setPlacesPhotosEnabled: Flag;
setPlacesAutocompleteEnabledState: Flag;
setPlacesAutocompleteEnabled: Flag;
setPlacesDetailsEnabledState: Flag;
setPlacesDetailsEnabled: Flag;
setPlacesEnrichEnabledState: Flag;
setPlacesEnrichEnabled: Flag;
setPlaceShadowEnabledState: Flag;
setPlaceShadowEnabled: Flag;
transitProvider: TransitProvider;
setTransitProviderState: (provider: TransitProvider) => void;
setTransitGoogleKeySource: (source: TransitKeySource) => void;
}
/**
* The saves behind the admin Settings tab and its phone twin: the OIDC provider, the
* allowed file types and the place switches. The values live in the admin hook; this
* only writes them, so both shells hand in the same admin bag.
*/
export function useAdminSettingsActions(admin: SettingsActionsHost, t: TranslationFn) {
const { toast, oidcConfig, setSavingOidc, allowedFileTypes, setSavingFileTypes } = admin;
const saveOidc = async () => {
setSavingOidc(true);
try {
const payload: Record<string, unknown> = {
issuer: oidcConfig.issuer,
client_id: oidcConfig.client_id,
display_name: oidcConfig.display_name,
discovery_url: oidcConfig.discovery_url,
};
if (oidcConfig.client_secret) payload.client_secret = oidcConfig.client_secret;
await adminApi.updateOidc(payload);
toast.success(t('admin.oidcSaved'));
} catch (err: unknown) {
toast.error(getApiErrorMessage(err, t('common.error')));
} finally {
setSavingOidc(false);
}
};
const saveFileTypes = async () => {
setSavingFileTypes(true);
try {
await authApi.updateAppSettings({ allowed_file_types: allowedFileTypes });
toast.success(t('admin.fileTypesSaved'));
} catch {
toast.error(t('common.error'));
} finally {
setSavingFileTypes(false);
}
};
/**
* A place switch shows the new value at once, in the tab and in the store the rest of
* the app reads, and both flip back quietly when the save fails.
*/
const placeSwitch = (setLocal: Flag, setStore: Flag, save: (enabled: boolean) => Promise<unknown>) => {
return async (next: boolean) => {
setLocal(next);
setStore(next);
try {
await save(next);
} catch {
setLocal(!next);
setStore(!next);
}
};
};
/**
* The transit backend (#1699): shown at once, and back to the previous one with a
* word when the save fails, so a select that springs back does not read as broken.
* A pick of the provider already selected writes nothing: the route is audited.
*/
const changeTransitProvider = async (value: string) => {
if (value === admin.transitProvider) return;
const next: TransitProvider = value === 'google' ? 'google' : 'transitous';
const previous = admin.transitProvider;
admin.setTransitProviderState(next);
try {
const saved = await adminApi.updateTransitProvider(next);
admin.setTransitGoogleKeySource(saved.googleKeySource);
} catch (err: unknown) {
admin.setTransitProviderState(previous);
toast.error(getApiErrorMessage(err, t('common.error')));
}
};
return {
saveOidc,
saveFileTypes,
changeTransitProvider,
togglePlacesPhotos: placeSwitch(admin.setPlacesPhotosEnabledState, admin.setPlacesPhotosEnabled, (enabled) =>
adminApi.updatePlacesPhotos(enabled)
),
togglePlacesAutocomplete: placeSwitch(
admin.setPlacesAutocompleteEnabledState,
admin.setPlacesAutocompleteEnabled,
(enabled) => adminApi.updatePlacesAutocomplete(enabled)
),
togglePlacesDetails: placeSwitch(admin.setPlacesDetailsEnabledState, admin.setPlacesDetailsEnabled, (enabled) =>
adminApi.updatePlacesDetails(enabled)
),
togglePlacesEnrich: placeSwitch(admin.setPlacesEnrichEnabledState, admin.setPlacesEnrichEnabled, (enabled) =>
adminApi.updatePlacesEnrich(enabled)
),
togglePlaceShadow: placeSwitch(admin.setPlaceShadowEnabledState, admin.setPlaceShadowEnabled, (enabled) =>
adminApi.updatePlaceShadow(enabled)
),
};
}