Files
initiative/packages/domain/src/toggle-condition.ts
T
Lukas f4fb69dbc7
CI / check (push) Successful in 1m13s
CI / build-image (push) Has been skipped
Add jsinspect-plus structural duplication gate, extract shared helpers
Add jsinspect-plus (AST-based structural duplication detector) to pnpm
check with threshold 50 / min 3 instances. Fix all findings:

- Extract condition icon/color maps to shared condition-styles.ts
- Extract useClickOutside hook (5 components)
- Extract dispatchAction + resolveAndRename in use-encounter
- Extract runEncounterAction in application layer (13 use cases)
- Extract findCombatant helper in domain (9 functions)
- Extract TraitSection in stat-block (4 trait rendering blocks)
- Extract DialogHeader in dialog.tsx (4 dialogs)

Net result: -263 lines across 40 files.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 02:16:54 +01:00

64 lines
1.8 KiB
TypeScript

import type { ConditionId } from "./conditions.js";
import { CONDITION_DEFINITIONS, VALID_CONDITION_IDS } from "./conditions.js";
import type { DomainEvent } from "./events.js";
import {
type CombatantId,
type DomainError,
type Encounter,
findCombatant,
isDomainError,
} from "./types.js";
export interface ToggleConditionSuccess {
readonly encounter: Encounter;
readonly events: DomainEvent[];
}
export function toggleCondition(
encounter: Encounter,
combatantId: CombatantId,
conditionId: ConditionId,
): ToggleConditionSuccess | DomainError {
if (!VALID_CONDITION_IDS.has(conditionId)) {
return {
kind: "domain-error",
code: "unknown-condition",
message: `Unknown condition "${conditionId}"`,
};
}
const found = findCombatant(encounter, combatantId);
if (isDomainError(found)) return found;
const { combatant: target } = found;
const current = target.conditions ?? [];
const isActive = current.includes(conditionId);
let newConditions: readonly ConditionId[] | undefined;
let event: DomainEvent;
if (isActive) {
const filtered = current.filter((c) => c !== conditionId);
newConditions = filtered.length > 0 ? filtered : undefined;
event = { type: "ConditionRemoved", combatantId, condition: conditionId };
} else {
const added = [...current, conditionId];
const order = CONDITION_DEFINITIONS.map((d) => d.id);
added.sort((a, b) => order.indexOf(a) - order.indexOf(b));
newConditions = added;
event = { type: "ConditionAdded", combatantId, condition: conditionId };
}
const updatedCombatants = encounter.combatants.map((c) =>
c.id === combatantId ? { ...c, conditions: newConditions } : c,
);
return {
encounter: {
combatants: updatedCombatants,
activeIndex: encounter.activeIndex,
roundNumber: encounter.roundNumber,
},
events: [event],
};
}