Replace direct adapter/persistence imports with context-based injection (AdapterContext + useAdapters) so tests use in-memory implementations instead of vi.mock. Migrate component tests from context mocking to AllProviders with real hooks. Extract export/import logic from ActionBar into useEncounterExportImport hook. Add bestiary-cache and bestiary-index-adapter test suites. Raise adapter coverage thresholds (68→80 lines, 56→62 branches). 77 test files, 891 tests, all passing. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
108 lines
2.5 KiB
TypeScript
108 lines
2.5 KiB
TypeScript
import type { PlayerCharacterStore } from "@initiative/application";
|
|
import {
|
|
createPlayerCharacterUseCase,
|
|
deletePlayerCharacterUseCase,
|
|
editPlayerCharacterUseCase,
|
|
} from "@initiative/application";
|
|
import type { PlayerCharacter, PlayerCharacterId } from "@initiative/domain";
|
|
import { isDomainError, playerCharacterId } from "@initiative/domain";
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import { useAdapters } from "../contexts/adapter-context.js";
|
|
|
|
let nextPcId = 0;
|
|
|
|
function generatePcId(): PlayerCharacterId {
|
|
return playerCharacterId(`pc-${++nextPcId}`);
|
|
}
|
|
|
|
interface EditFields {
|
|
readonly name?: string;
|
|
readonly ac?: number;
|
|
readonly maxHp?: number;
|
|
readonly color?: string | null;
|
|
readonly icon?: string | null;
|
|
readonly level?: number | null;
|
|
}
|
|
|
|
export function usePlayerCharacters() {
|
|
const { playerCharacterPersistence } = useAdapters();
|
|
const [characters, setCharacters] = useState<PlayerCharacter[]>(() =>
|
|
playerCharacterPersistence.load(),
|
|
);
|
|
const charactersRef = useRef(characters);
|
|
charactersRef.current = characters;
|
|
|
|
useEffect(() => {
|
|
playerCharacterPersistence.save(characters);
|
|
}, [characters, playerCharacterPersistence]);
|
|
|
|
const makeStore = useCallback((): PlayerCharacterStore => {
|
|
return {
|
|
getAll: () => charactersRef.current,
|
|
save: (updated) => {
|
|
charactersRef.current = updated;
|
|
setCharacters(updated);
|
|
},
|
|
};
|
|
}, []);
|
|
|
|
const createCharacter = useCallback(
|
|
(
|
|
name: string,
|
|
ac: number,
|
|
maxHp: number,
|
|
color: string | undefined,
|
|
icon: string | undefined,
|
|
level: number | undefined,
|
|
) => {
|
|
const id = generatePcId();
|
|
const result = createPlayerCharacterUseCase(
|
|
makeStore(),
|
|
id,
|
|
name,
|
|
ac,
|
|
maxHp,
|
|
color,
|
|
icon,
|
|
level,
|
|
);
|
|
if (isDomainError(result)) {
|
|
return result;
|
|
}
|
|
return undefined;
|
|
},
|
|
[makeStore],
|
|
);
|
|
|
|
const editCharacter = useCallback(
|
|
(id: PlayerCharacterId, fields: EditFields) => {
|
|
const result = editPlayerCharacterUseCase(makeStore(), id, fields);
|
|
if (isDomainError(result)) {
|
|
return result;
|
|
}
|
|
return undefined;
|
|
},
|
|
[makeStore],
|
|
);
|
|
|
|
const deleteCharacter = useCallback(
|
|
(id: PlayerCharacterId) => {
|
|
const result = deletePlayerCharacterUseCase(makeStore(), id);
|
|
if (isDomainError(result)) {
|
|
return result;
|
|
}
|
|
return undefined;
|
|
},
|
|
[makeStore],
|
|
);
|
|
|
|
return {
|
|
characters,
|
|
createCharacter,
|
|
editCharacter,
|
|
deleteCharacter,
|
|
replacePlayerCharacters: setCharacters,
|
|
makeStore,
|
|
} as const;
|
|
}
|