// Rôle : règle les Expéditions depuis Administration › Guilde : un panneau par groupe de réglages, les destinations, et le suivi du jour.
import { useState } from "react";
import { ConfirmModal } from "../../../components/ToolboxModals.jsx";
import { cancelExpedition, updateExpeditionSettings } from "../cardsApi.js";
import { DATE_FORMATTER } from "./adminFormat.js";
import { ApplyActions } from "./adminSettingForm.jsx";
import { inRange, parse } from "./adminSettings.js";
import { EXPEDITION_SETTING_GROUPS } from "./expeditionSettingFields.js";
const readPath = (source, path) => path.split(".").reduce((value, key) => value?.[key], source);
function writePath(target, path, value) {
const keys = path.split(".");
keys.slice(0, -1).reduce((node, key) => node[key], target)[keys.at(-1)] = value;
}
const toDraft = (value) => (typeof value === "boolean" ? value : Array.isArray(value) ? value.map(String) : String(value));
const fromDraft = (value) => (typeof value === "boolean" ? value : Array.isArray(value) ? value.map(parse) : parse(value));
const validValue = (field, value) => (field.kind === "bool" ? typeof value === "boolean" : (Array.isArray(value) ? value : [value]).every((item) => inRange(item, field.min, field.max)));
// Un panneau : brouillon de ses réglages, vérifiés avec les bornes du serveur ; les contraintes entre réglages sont vérifiées par le serveur.
function SettingsGroup({ group, settings, schema, busy, save }) {
const fields = group.fields.map((field) => ({ ...field, ...schema.find((item) => item.path === field.path) }));
const initial = () => Object.fromEntries(fields.map((field) => [field.path, toDraft(readPath(settings, field.path))]));
const [draft, setDraft] = useState(initial);
const values = Object.fromEntries(fields.map((field) => [field.path, fromDraft(draft[field.path])]));
const invalid = fields.filter((field) => !validValue(field, values[field.path]));
const changed = !invalid.length && fields.some((field) => JSON.stringify(values[field.path]) !== JSON.stringify(readPath(settings, field.path)));
const set = (path, value) => setDraft((current) => ({ ...current, [path]: value }));
return
{group.title}
{group.help}
{fields.map((field) => {
const value = draft[field.path];
if (field.kind === "bool") return
{field.label}
;
if (Array.isArray(value)) return ;
return ;
})}
{invalid.length > 0 &&
{invalid.map((field) => `${field.label} : entier de ${field.min.toLocaleString("fr-FR")} à ${field.max.toLocaleString("fr-FR")}`).join(" · ")}
}
setDraft(initial())}
onApply={() => save((next) => fields.forEach((field) => writePath(next, field.path, values[field.path])))}
confirmTitle={`Appliquer les réglages « ${group.title} » ?`}
confirmMessage="Les nouveaux réglages s’appliquent aux prochains départs. Les expéditions en route gardent ceux de leur départ."
/>
;
}
// Destinations : proposées ou non, et poids dans le tirage du jour. Leurs textes vivent dans le fichier du serveur.
function DestinationSettings({ settings, destinations, busy, save }) {
const initial = () => Object.fromEntries(destinations.map((destination) => [destination.id, { active: settings.destinations[destination.id].active, weight: String(settings.destinations[destination.id].weight) }]));
const [draft, setDraft] = useState(initial);
const invalid = destinations.filter((destination) => !inRange(parse(draft[destination.id].weight), 0, 100));
const changed = !invalid.length && destinations.some((destination) => draft[destination.id].active !== settings.destinations[destination.id].active || parse(draft[destination.id].weight) !== settings.destinations[destination.id].weight);
const set = (id, changes) => setDraft((current) => ({ ...current, [id]: { ...current[id], ...changes } }));
return
Destinations
Une destination retirée n’est plus tirée ; le poids règle sa fréquence parmi les destinations proposées. Les destinations cachées ne sont jamais tirées.
{destinations.map((destination) =>
{destination.name}{destination.hidden && Cachée}
)}
{invalid.length > 0 &&
Poids : entier de 0 à 100.
}
setDraft(initial())}
onApply={() => save((next) => destinations.forEach((destination) => { next.destinations[destination.id] = { active: draft[destination.id].active, weight: parse(draft[destination.id].weight) }; }))}
confirmTitle="Appliquer les destinations ?"
confirmMessage="Le changement vaut pour les prochains tirages : les destinations déjà proposées aujourd’hui restent jusqu’à minuit."
/>
;
}
function ExpeditionsToday({ today, destinations, busy, onCancel }) {
const [cancelling, setCancelling] = useState(null);
const name = (id) => destinations.find((destination) => destination.id === id)?.name || id;
return