|
| 1 | +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { Check } from "lucide-react"; |
| 4 | +import { factorioPathsFn, setFactorioPathsFn, type FactorioPathField } from "../../server/factorio"; |
| 5 | +import { Button } from "#/components/ui/button.tsx"; |
| 6 | +import { Card, CardHeader, CardTitle } from "#/components/ui/card.tsx"; |
| 7 | +import { Input } from "#/components/ui/input.tsx"; |
| 8 | +import { FieldLabel } from "#/components/ui/label.tsx"; |
| 9 | +import { Skeleton } from "#/components/ui/skeleton.tsx"; |
| 10 | +import { InfoHint } from "../info-hint"; |
| 11 | + |
| 12 | +type PathKey = "bin" | "dataDir" | "modsDir"; |
| 13 | + |
| 14 | +const FIELDS: { key: PathKey; label: string; hint: string; env: string }[] = [ |
| 15 | + { |
| 16 | + key: "bin", |
| 17 | + label: "Factorio executable", |
| 18 | + hint: "The game binary, e.g. Factorio/bin/x64/factorio — used by game-data sync and Launch Factorio.", |
| 19 | + env: "FACTORIO_BIN", |
| 20 | + }, |
| 21 | + { |
| 22 | + key: "dataDir", |
| 23 | + label: "Factorio user-data folder", |
| 24 | + hint: "Contains Factorio's configuration, saves, mods, and script-output — not the installation folder.", |
| 25 | + env: "FACTORIO_DATA_DIR", |
| 26 | + }, |
| 27 | + { |
| 28 | + key: "modsDir", |
| 29 | + label: "Factorio mods folder", |
| 30 | + hint: "The folder that directly contains mod-list.json and installed mods. Defaults to <user-data>/mods.", |
| 31 | + env: "FACTORIO_MODS_DIR", |
| 32 | + }, |
| 33 | +]; |
| 34 | + |
| 35 | +/** Where Factorio lives on this machine — stored in app config, editable here. |
| 36 | + * Env vars always win; blank fields fall back to the per-OS platform default. */ |
| 37 | +export function FactorioPathsCard() { |
| 38 | + const qc = useQueryClient(); |
| 39 | + const paths = useQuery({ queryKey: ["factorioPaths"], queryFn: () => factorioPathsFn() }); |
| 40 | + const save = useMutation({ |
| 41 | + mutationFn: (d: { bin?: string; dataDir?: string; modsDir?: string }) => |
| 42 | + setFactorioPathsFn({ data: d }), |
| 43 | + onSuccess: () => { |
| 44 | + setDraft({}); |
| 45 | + // paths feed the sync, launch button, companion installer, and drift checks |
| 46 | + void qc.invalidateQueries({ queryKey: ["factorioPaths"] }); |
| 47 | + void qc.invalidateQueries({ queryKey: ["companionStatus"] }); |
| 48 | + void qc.invalidateQueries({ queryKey: ["dataStatus"] }); |
| 49 | + void qc.invalidateQueries({ queryKey: ["modDrift"] }); |
| 50 | + }, |
| 51 | + }); |
| 52 | + const [draft, setDraft] = useState<Partial<Record<PathKey, string>>>({}); |
| 53 | + |
| 54 | + const d = paths.data; |
| 55 | + if (!d) { |
| 56 | + return ( |
| 57 | + <Card> |
| 58 | + <CardHeader> |
| 59 | + <CardTitle>Factorio paths</CardTitle> |
| 60 | + </CardHeader> |
| 61 | + <div className="space-y-2 px-3 pb-3"> |
| 62 | + {FIELDS.map((f) => ( |
| 63 | + <Skeleton key={f.key} className="h-14 w-full" /> |
| 64 | + ))} |
| 65 | + </div> |
| 66 | + </Card> |
| 67 | + ); |
| 68 | + } |
| 69 | + if (d.hidden) { |
| 70 | + return ( |
| 71 | + <Card> |
| 72 | + <CardHeader> |
| 73 | + <CardTitle>Factorio paths</CardTitle> |
| 74 | + </CardHeader> |
| 75 | + <div className="px-3 pb-3 text-sm text-muted-foreground"> |
| 76 | + Path settings are hidden for this instance. |
| 77 | + </div> |
| 78 | + </Card> |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + const fieldValue = (key: PathKey) => draft[key] ?? d[key].stored; |
| 83 | + const dirty = FIELDS.some((f) => fieldValue(f.key) !== d[f.key].stored); |
| 84 | + const anyStored = FIELDS.some((f) => d[f.key].stored !== ""); |
| 85 | + |
| 86 | + const saveAll = () => { |
| 87 | + const patch: Partial<Record<PathKey, string>> = {}; |
| 88 | + for (const f of FIELDS) { |
| 89 | + if (fieldValue(f.key) !== d[f.key].stored) patch[f.key] = fieldValue(f.key); |
| 90 | + } |
| 91 | + save.mutate(patch); |
| 92 | + }; |
| 93 | + |
| 94 | + return ( |
| 95 | + <Card> |
| 96 | + <CardHeader> |
| 97 | + <CardTitle>Factorio paths</CardTitle> |
| 98 | + <InfoHint content="Where PyOps finds your Factorio install. Blank fields use the platform default; the FACTORIO_BIN / FACTORIO_DATA_DIR / FACTORIO_MODS_DIR env vars take priority when set." /> |
| 99 | + </CardHeader> |
| 100 | + <div className="space-y-3 px-3 pb-3 text-sm"> |
| 101 | + {FIELDS.map((f) => ( |
| 102 | + <PathFieldRow |
| 103 | + key={f.key} |
| 104 | + label={f.label} |
| 105 | + hint={f.hint} |
| 106 | + env={f.env} |
| 107 | + field={d[f.key]} |
| 108 | + value={fieldValue(f.key)} |
| 109 | + onChange={(v) => setDraft((prev) => ({ ...prev, [f.key]: v }))} |
| 110 | + /> |
| 111 | + ))} |
| 112 | + <div className="flex flex-wrap items-center gap-2 pt-1"> |
| 113 | + <Button variant="outline" size="sm" onClick={saveAll} disabled={!dirty || save.isPending}> |
| 114 | + Save paths |
| 115 | + </Button> |
| 116 | + <Button |
| 117 | + variant="ghost" |
| 118 | + size="sm" |
| 119 | + onClick={() => save.mutate({ bin: "", dataDir: "", modsDir: "" })} |
| 120 | + disabled={save.isPending || (!anyStored && !dirty)} |
| 121 | + className="text-muted-foreground" |
| 122 | + > |
| 123 | + Use platform defaults |
| 124 | + </Button> |
| 125 | + </div> |
| 126 | + {save.isError && ( |
| 127 | + <p className="text-sm text-destructive">Save failed: {save.error.message}</p> |
| 128 | + )} |
| 129 | + </div> |
| 130 | + </Card> |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +/** One path field: env-set fields show the winning value read-only; otherwise an |
| 135 | + * input whose placeholder is the platform default in effect when left blank. */ |
| 136 | +function PathFieldRow({ |
| 137 | + label, |
| 138 | + hint, |
| 139 | + env, |
| 140 | + field, |
| 141 | + value, |
| 142 | + onChange, |
| 143 | +}: { |
| 144 | + label: string; |
| 145 | + hint: string; |
| 146 | + env: string; |
| 147 | + field: FactorioPathField; |
| 148 | + value: string; |
| 149 | + onChange: (v: string) => void; |
| 150 | +}) { |
| 151 | + return ( |
| 152 | + <div> |
| 153 | + <FieldLabel>{label}</FieldLabel> |
| 154 | + {field.source === "env" ? ( |
| 155 | + <div className="mt-1 flex items-center gap-1 text-sm text-success"> |
| 156 | + <Check className="size-3.5 shrink-0" /> |
| 157 | + <span className="min-w-0 truncate" title={field.effective}> |
| 158 | + Set via {env} env (wins): {field.effective} |
| 159 | + </span> |
| 160 | + </div> |
| 161 | + ) : ( |
| 162 | + <Input |
| 163 | + value={value} |
| 164 | + placeholder={field.fallback} |
| 165 | + aria-label={label} |
| 166 | + onChange={(e) => onChange(e.target.value)} |
| 167 | + className="mt-1 w-full font-mono" |
| 168 | + /> |
| 169 | + )} |
| 170 | + <p className="mt-0.5 text-sm text-muted-foreground">{hint}</p> |
| 171 | + {!field.exists && ( |
| 172 | + <p className="text-sm break-all text-warning">Not found on disk: {field.effective}</p> |
| 173 | + )} |
| 174 | + </div> |
| 175 | + ); |
| 176 | +} |
0 commit comments