import { DateTime } from "effect" import { useCallback, useEffect, useState } from "react" import type { BotSettings, BotStatus, SimulatorSettings, SimulatorStatus } from "../../domain/automation.ts" import { call } from "./client.ts" import { Button, Card } from "./ui.tsx" /** The presenter's controls: the restock bot and the simulated shoppers. */ export const ControlScreen = () => { const [bot, setBot] = useState() const [simulator, setSimulator] = useState() const [error, setError] = useState() const refresh = useCallback(async () => { const [botOutcome, simulatorOutcome] = await Promise.all([ call((client) => client.shop.bot()), call((client) => client.admin.simulator()) ]) if (botOutcome.ok) setBot(botOutcome.value) if (simulatorOutcome.ok) setSimulator(simulatorOutcome.value) else setError(simulatorOutcome.message) }, []) useEffect(() => { void refresh() const timer = setInterval(() => void refresh(), 1500) return () => clearInterval(timer) }, [refresh]) const configureBot = async (payload: Partial) => { const outcome = await call((client) => client.admin.configureBot({ payload })) if (outcome.ok) setBot(outcome.value) else setError(outcome.message) } const runBot = async () => { const outcome = await call((client) => client.admin.runBot()) if (outcome.ok) setBot(outcome.value) else setError(outcome.message) } const configureSimulator = async (payload: Partial) => { const outcome = await call((client) => client.admin.configureSimulator({ payload })) if (outcome.ok) setSimulator(outcome.value) else setError(outcome.message) } return (
{error !== undefined &&

{error}

} {window.location.hash !== "#sala" && ( Apri la sala di controllo ↗ Presenze, eventi in diretta, metriche, trace e dati personali: da mettere sul proiettore. )} void configureBot({ enabled })} />} > {bot === undefined ?

…

: (
void configureBot({ intervalSeconds })} /> void configureBot({ threshold })} /> void configureBot({ batch })} />
{bot.runs} giri · {bot.restocked} pezzi aggiunti {bot.lastRunAt !== undefined && ` · ultimo alle ${DateTime.formatIso(bot.lastRunAt).slice(11, 19)}`}
)}
void configureSimulator({ enabled })} /> )} > {simulator === undefined ?

…

: (
void configureSimulator({ shoppers })} /> void configureSimulator({ actionsPerMinute })} />

{simulator.actions} azioni finora

    {simulator.recent.map((line, index) =>
  • {line}
  • )}
)}
) } const Toggle = ({ on, onChange }: { readonly on: boolean; readonly onChange: (on: boolean) => void }) => ( ) const Choice = ({ label, onChange, options, unit, value }: { readonly label: string readonly unit: string readonly value: number readonly options: ReadonlyArray readonly onChange: (value: number) => void }) => (
{label} {(options.includes(value) ? options : [...options, value].sort((a, b) => a - b)).map((option) => ( ))} {unit}
)