Compare commits
88 Commits
99d1ba1bcd
...
0.9.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2971898f0c | ||
|
|
43780772f6 | ||
|
|
7b3dbe2069 | ||
|
|
827a3978e9 | ||
|
|
f024562a7d | ||
|
|
dfef2194a5 | ||
|
|
502adca81b | ||
|
|
12e8bf6e69 | ||
|
|
472574ac31 | ||
|
|
f4a7b53393 | ||
|
|
8aec460ee4 | ||
|
|
6e10238fe0 | ||
|
|
b6e882add2 | ||
|
|
7a87d979bf | ||
|
|
02096bcee6 | ||
|
|
c092192b0e | ||
|
|
4d1a7c6420 | ||
|
|
46b444caba | ||
|
|
e68145319f | ||
|
|
d64e1f5e4a | ||
|
|
ef0b755eec | ||
|
|
4be816d10f | ||
|
|
e531d82d1b | ||
|
|
5a262c66cd | ||
|
|
32b69f8df1 | ||
|
|
8efba288f7 | ||
|
|
c94c30e459 | ||
|
|
36768d3aa1 | ||
|
|
473f1eaefe | ||
|
|
971e0ded49 | ||
|
|
36dcfc5076 | ||
|
|
127ed01064 | ||
|
|
179c3658ad | ||
|
|
01f2bb3ff1 | ||
|
|
930301de71 | ||
|
|
aa806d4fb9 | ||
|
|
61bc274715 | ||
|
|
1932e837fb | ||
|
|
cce87318fb | ||
|
|
3ef2370a34 | ||
|
|
c75d148d1e | ||
|
|
63e233bd8d | ||
|
|
8c62ec28f2 | ||
|
|
72195e90f6 | ||
|
|
6ac8e67970 | ||
|
|
a4797d5b15 | ||
|
|
d48e39ced4 | ||
|
|
b7406c4b54 | ||
|
|
07cdd4867a | ||
|
|
85acb5c185 | ||
|
|
f9ef64bb00 | ||
|
|
bd39808000 | ||
|
|
75778884bd | ||
|
|
72d4f30e60 | ||
|
|
96b37d4bdd | ||
|
|
76ca78c169 | ||
|
|
b0c27b8ab9 | ||
|
|
458c277e9f | ||
|
|
91703ddebc | ||
|
|
768e7a390f | ||
|
|
7feaf90eab | ||
|
|
b39e4923e1 | ||
|
|
369feb3cc8 | ||
|
|
51bdb799ae | ||
|
|
1baddad939 | ||
|
|
e701e4dd70 | ||
|
|
e2b0e7d5ee | ||
|
|
635e9c0705 | ||
|
|
582a42e62d | ||
|
|
fc43f440aa | ||
|
|
1cf30b3622 | ||
|
|
2ce0ff50b9 | ||
|
|
96a7b2d00e | ||
|
|
2d8e823eff | ||
|
|
613bb70065 | ||
|
|
b6e052f198 | ||
|
|
460c65bf49 | ||
|
|
95cb2edc23 | ||
|
|
55d322a727 | ||
|
|
0c903bc9a5 | ||
|
|
236c3bf64a | ||
|
|
0747d044f3 | ||
|
|
d101906776 | ||
|
|
69363d4f7d | ||
|
|
47da942b73 | ||
|
|
94d125d9c4 | ||
|
|
c323adc343 | ||
|
|
91120d7c82 |
143
.claude/commands/integrate-issue.md
Normal file
143
.claude/commands/integrate-issue.md
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
---
|
||||||
|
description: Fetch a Gitea issue, identify the affected feature spec(s), and integrate the issue's requirements into the spec. For new features, hands off to /speckit.specify.
|
||||||
|
---
|
||||||
|
|
||||||
|
## User Input
|
||||||
|
|
||||||
|
```text
|
||||||
|
$ARGUMENTS
|
||||||
|
```
|
||||||
|
|
||||||
|
You **MUST** provide an issue number as the argument (e.g. `/integrate-issue 6`). If `$ARGUMENTS` is empty or not a valid number, ask the user for the issue number.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
1. Verify the `GITEA_TOKEN_ISSUES` environment variable is set:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
test -n "$GITEA_TOKEN_ISSUES" && echo "TOKEN_OK" || echo "TOKEN_MISSING"
|
||||||
|
```
|
||||||
|
|
||||||
|
If missing, tell the user to set it:
|
||||||
|
```
|
||||||
|
export GITEA_TOKEN_ISSUES="your-gitea-personal-access-token"
|
||||||
|
```
|
||||||
|
Then abort.
|
||||||
|
|
||||||
|
2. Parse the git remote to extract the Gitea API base URL, owner, and repo:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git config --get remote.origin.url
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected format: `ssh://git@<host>:<port>/<owner>/<repo>.git` or `https://<host>/<owner>/<repo>.git`
|
||||||
|
|
||||||
|
Extract:
|
||||||
|
- `GITEA_HOST` — the hostname
|
||||||
|
- `OWNER` — the repo owner/org
|
||||||
|
- `REPO` — the repo name (strip `.git` suffix)
|
||||||
|
- `API_BASE` — `https://<GITEA_HOST>/api/v1`
|
||||||
|
|
||||||
|
## Execution
|
||||||
|
|
||||||
|
### Step 1 — Fetch the issue
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -sf -H "Authorization: token $GITEA_TOKEN_ISSUES" "$API_BASE/repos/$OWNER/$REPO/issues/<NUMBER>"
|
||||||
|
```
|
||||||
|
|
||||||
|
Extract from the JSON response:
|
||||||
|
- `title` — the issue title
|
||||||
|
- `body` — the issue body (markdown)
|
||||||
|
- `labels` — array of label names (if any)
|
||||||
|
|
||||||
|
If the API call fails or returns no issue, abort with a clear error.
|
||||||
|
|
||||||
|
### Step 2 — Fetch issue comments (if any)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -sf -H "Authorization: token $GITEA_TOKEN_ISSUES" "$API_BASE/repos/$OWNER/$REPO/issues/<NUMBER>/comments"
|
||||||
|
```
|
||||||
|
|
||||||
|
If comments exist, include them as additional context (they may contain clarifications or requirements discussed after the issue was created).
|
||||||
|
|
||||||
|
### Step 3 — Route: new feature or existing feature?
|
||||||
|
|
||||||
|
List the existing feature specs by reading the `specs/` directory:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ls -d specs/*/
|
||||||
|
```
|
||||||
|
|
||||||
|
Present the issue summary and existing specs to the user. Ask:
|
||||||
|
|
||||||
|
**"Does this issue belong to an existing feature, or is it a new feature?"**
|
||||||
|
|
||||||
|
Present options:
|
||||||
|
- Each existing spec as a numbered option (show spec name and one-line description from CLAUDE.md or the spec's overview)
|
||||||
|
- A "New feature" option
|
||||||
|
|
||||||
|
If the user selects **New feature**, compose the feature description from the issue content (title + body + comments) and hand off to `/speckit.specify`. Stop here.
|
||||||
|
|
||||||
|
If the user selects an **existing spec**, continue to Step 4.
|
||||||
|
|
||||||
|
### Step 4 — Read the affected spec
|
||||||
|
|
||||||
|
Load the selected spec file. Identify the sections that the issue's requirements affect:
|
||||||
|
- Which user stories need updating?
|
||||||
|
- Which requirements (FR-NNN) need adding or modifying?
|
||||||
|
- Which acceptance scenarios change?
|
||||||
|
- Are new edge cases introduced?
|
||||||
|
|
||||||
|
Present your analysis to the user:
|
||||||
|
- **Stories affected**: list the story IDs/titles that need changes
|
||||||
|
- **New stories needed**: if the issue introduces behavior not covered by any existing story
|
||||||
|
- **Requirements to add/modify**: list specific FR numbers or new ones needed
|
||||||
|
|
||||||
|
Ask the user to confirm or adjust the scope.
|
||||||
|
|
||||||
|
### Step 5 — Draft spec changes
|
||||||
|
|
||||||
|
For each affected section, draft the specific changes:
|
||||||
|
|
||||||
|
- **Modified stories**: show the before/after for acceptance scenarios
|
||||||
|
- **New stories**: write them in the spec's format (matching the existing story naming convention — e.g., `**Story HP-7**` for combatant-state, `**Story A4**` for combatant-management)
|
||||||
|
- **New/modified requirements**: write them with the next available FR number
|
||||||
|
- **New edge cases**: add to the relevant edge cases section
|
||||||
|
|
||||||
|
For per-topic specs (003-combatant-state, 004-bestiary), place changes in the correct topic section.
|
||||||
|
|
||||||
|
### Step 6 — Preview and confirm
|
||||||
|
|
||||||
|
Show the user a complete preview of all changes:
|
||||||
|
- Which file(s) will be modified
|
||||||
|
- The exact additions/modifications (as diffs or before/after blocks)
|
||||||
|
|
||||||
|
Ask for confirmation before writing.
|
||||||
|
|
||||||
|
### Step 7 — Write changes
|
||||||
|
|
||||||
|
On confirmation:
|
||||||
|
- Write the updated spec file(s)
|
||||||
|
- Report what was changed (sections touched, stories added/modified, requirements added)
|
||||||
|
|
||||||
|
### Step 8 — Suggest next steps
|
||||||
|
|
||||||
|
Report completion and suggest next steps based on scope:
|
||||||
|
|
||||||
|
- **Straightforward change** (1-2 stories, clear acceptance scenarios): "Implement the changes and commit"
|
||||||
|
- **Larger change** (multiple stories, cross-cutting concerns): "Use `rpi-research` to investigate the affected code, then `rpi-plan` to create a phased implementation plan, then `rpi-implement` to execute it"
|
||||||
|
- **Complex or ambiguous change**: "Run `/speckit.clarify` to resolve remaining ambiguities before implementing"
|
||||||
|
- Only if the spec adds substantive new criteria not already captured in the issue: "Run `/sync-issue <number>` to update the Gitea issue with the new acceptance criteria". Skip this if the spec merely reformulates what the issue already says into Given/When/Then format.
|
||||||
|
|
||||||
|
## Behavior Rules
|
||||||
|
|
||||||
|
- Never modify the issue on Gitea — this is a read-only operation on the issue side.
|
||||||
|
- Always preview before writing spec changes — never write without user confirmation.
|
||||||
|
- Include comment authors in the context so requirements can be attributed.
|
||||||
|
- If the issue body is empty, warn the user but still proceed with just the title.
|
||||||
|
- Strip HTML tags from the body/comments if present (Gitea sometimes includes rendered HTML).
|
||||||
|
- Use `curl` for all API calls — do not rely on `gh` CLI.
|
||||||
|
- Match the existing spec's naming conventions for stories, requirements, and structure.
|
||||||
|
- When adding to per-topic specs (003, 004), place content in the correct topic section — do not create new top-level sections unless the change introduces an entirely new topic area.
|
||||||
|
- Increment FR/SC numbers from the highest existing number in the spec.
|
||||||
@@ -75,14 +75,7 @@ You **MUST** consider the user input before proceeding (if not empty).
|
|||||||
- Examples: public APIs for libraries, command schemas for CLI tools, endpoints for web services, grammars for parsers, UI contracts for applications
|
- Examples: public APIs for libraries, command schemas for CLI tools, endpoints for web services, grammars for parsers, UI contracts for applications
|
||||||
- Skip if project is purely internal (build scripts, one-off tools, etc.)
|
- Skip if project is purely internal (build scripts, one-off tools, etc.)
|
||||||
|
|
||||||
3. **Agent context update**:
|
**Output**: data-model.md, /contracts/*, quickstart.md
|
||||||
- Run `.specify/scripts/bash/update-agent-context.sh claude`
|
|
||||||
- These scripts detect which AI agent is in use
|
|
||||||
- Update the appropriate agent-specific context file
|
|
||||||
- Add only new technology from current plan
|
|
||||||
- Preserve manual additions between markers
|
|
||||||
|
|
||||||
**Output**: data-model.md, /contracts/*, quickstart.md, agent-specific file
|
|
||||||
|
|
||||||
## Key rules
|
## Key rules
|
||||||
|
|
||||||
|
|||||||
162
.claude/commands/sync-issue.md
Normal file
162
.claude/commands/sync-issue.md
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
---
|
||||||
|
description: Update a Gitea issue with business-level acceptance criteria extracted from the feature spec's user stories.
|
||||||
|
---
|
||||||
|
|
||||||
|
## User Input
|
||||||
|
|
||||||
|
```text
|
||||||
|
$ARGUMENTS
|
||||||
|
```
|
||||||
|
|
||||||
|
You **MUST** provide an issue number as the argument (e.g. `/sync-issue 42`). If `$ARGUMENTS` is empty or not a valid number, ask the user for the issue number.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
1. Verify the `GITEA_TOKEN_ISSUES` environment variable is set:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
test -n "$GITEA_TOKEN_ISSUES" && echo "TOKEN_OK" || echo "TOKEN_MISSING"
|
||||||
|
```
|
||||||
|
|
||||||
|
If missing, tell the user to set it:
|
||||||
|
```
|
||||||
|
export GITEA_TOKEN_ISSUES="your-gitea-personal-access-token"
|
||||||
|
```
|
||||||
|
Then abort.
|
||||||
|
|
||||||
|
2. Parse the git remote to extract the Gitea API base URL, owner, and repo:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git config --get remote.origin.url
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected format: `ssh://git@<host>:<port>/<owner>/<repo>.git` or `https://<host>/<owner>/<repo>.git`
|
||||||
|
|
||||||
|
Extract:
|
||||||
|
- `GITEA_HOST` — the hostname
|
||||||
|
- `OWNER` — the repo owner/org
|
||||||
|
- `REPO` — the repo name (strip `.git` suffix)
|
||||||
|
- `API_BASE` — `https://<GITEA_HOST>/api/v1`
|
||||||
|
|
||||||
|
3. Locate the spec file. List the available feature specs:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ls specs/*/spec.md
|
||||||
|
```
|
||||||
|
|
||||||
|
Present the specs to the user and ask which one contains the acceptance criteria for this issue. If only one spec exists, use it automatically.
|
||||||
|
|
||||||
|
## Execution
|
||||||
|
|
||||||
|
### Step 1 — Read the spec
|
||||||
|
|
||||||
|
Load the spec file at `FEATURE_SPEC`. Extract user stories and acceptance scenarios using these patterns:
|
||||||
|
|
||||||
|
**Flat specs** (001-combatant-management, 002-turn-tracking):
|
||||||
|
- Look for the `## User Scenarios & Testing` section
|
||||||
|
- Each `### ... Story ...` or `**Story ...** ` block
|
||||||
|
- The **Acceptance Scenarios** numbered list within each story (Given/When/Then format)
|
||||||
|
- The **Edge Cases** section(s)
|
||||||
|
|
||||||
|
**Per-topic specs** (003-combatant-state, 004-bestiary):
|
||||||
|
- Stories are nested inside topic sections (e.g., `## Hit Points` > `### User Stories` > `**Story HP-1 — ...**`)
|
||||||
|
- Scan ALL `##` sections for `**Story ...` or `**US-...` patterns
|
||||||
|
- Extract acceptance scenarios from each story regardless of nesting depth
|
||||||
|
- Collect edge cases from each topic section's `### Edge Cases` subsection
|
||||||
|
|
||||||
|
### Step 2 — Condense into business-level acceptance criteria
|
||||||
|
|
||||||
|
For each user story, extract the acceptance scenarios and rewrite them as concise, business-level checkbox items. Group by user story title.
|
||||||
|
|
||||||
|
**Transformation rules:**
|
||||||
|
- Strip Given/When/Then syntax — write as plain outcomes
|
||||||
|
- Remove implementation details (API names, database references, component names, file paths, config values, tool names)
|
||||||
|
- Focus on what the user **can do** or **can see**
|
||||||
|
- Keep each item to one line
|
||||||
|
- Preserve the grouping by user story for readability
|
||||||
|
|
||||||
|
**Example transformation:**
|
||||||
|
|
||||||
|
Input (from spec):
|
||||||
|
```
|
||||||
|
**Given** no sources are cached, **When** the user clicks the import button in the top bar,
|
||||||
|
**Then** the stat block side panel opens showing a descriptive explanation, an editable
|
||||||
|
pre-filled base URL, and a "Load All" button.
|
||||||
|
```
|
||||||
|
|
||||||
|
Output (for issue):
|
||||||
|
```
|
||||||
|
- [ ] Clicking the import button opens a panel with a description, editable URL, and "Load All" button
|
||||||
|
```
|
||||||
|
|
||||||
|
Also include edge cases as a separate group if they describe user-facing behavior.
|
||||||
|
|
||||||
|
### Step 3 — Fetch the existing issue
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -sf -H "Authorization: token $GITEA_TOKEN_ISSUES" "$API_BASE/repos/$OWNER/$REPO/issues/<NUMBER>"
|
||||||
|
```
|
||||||
|
|
||||||
|
Extract the current `body` from the response.
|
||||||
|
|
||||||
|
### Step 4 — Update the issue body
|
||||||
|
|
||||||
|
Merge the acceptance criteria into the existing issue body:
|
||||||
|
|
||||||
|
- If the body already has an `## Acceptance Criteria` section, **replace** its contents (everything between `## Acceptance Criteria` and the next `##` heading or end of body).
|
||||||
|
- If the body does not have an `## Acceptance Criteria` section, insert it after the `## Summary` section (or at the end if no Summary exists).
|
||||||
|
|
||||||
|
Preserve all other sections of the issue body unchanged.
|
||||||
|
|
||||||
|
The acceptance criteria section should look like:
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
## Acceptance Criteria
|
||||||
|
|
||||||
|
### <User Story 1 Title>
|
||||||
|
- [ ] <criterion from acceptance scenario>
|
||||||
|
- [ ] <criterion from acceptance scenario>
|
||||||
|
|
||||||
|
### <User Story 2 Title>
|
||||||
|
- [ ] <criterion from acceptance scenario>
|
||||||
|
|
||||||
|
### Edge Cases
|
||||||
|
- [ ] <edge case behavior>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 5 — Preview and confirm
|
||||||
|
|
||||||
|
Show the user:
|
||||||
|
- The full updated issue body
|
||||||
|
- A diff summary of what changed (sections added/replaced)
|
||||||
|
|
||||||
|
Ask for confirmation before updating.
|
||||||
|
|
||||||
|
### Step 6 — Push the update
|
||||||
|
|
||||||
|
On confirmation:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -sf -X PATCH \
|
||||||
|
-H "Authorization: token $GITEA_TOKEN_ISSUES" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
"$API_BASE/repos/$OWNER/$REPO/issues/<NUMBER>" \
|
||||||
|
-d @- <<'PAYLOAD'
|
||||||
|
{
|
||||||
|
"body": "<updated body>"
|
||||||
|
}
|
||||||
|
PAYLOAD
|
||||||
|
```
|
||||||
|
|
||||||
|
Report success with the issue URL.
|
||||||
|
|
||||||
|
## Behavior Rules
|
||||||
|
|
||||||
|
- Never modify the issue title, labels, milestone, or assignees — only the body.
|
||||||
|
- Always preview before updating — never push without user confirmation.
|
||||||
|
- If the spec has no user stories or acceptance scenarios, abort with a clear message suggesting the user run `/speckit.specify` first.
|
||||||
|
- Acceptance criteria must be business-level. If you find yourself writing implementation details, rewrite at a higher level of abstraction.
|
||||||
|
- Do NOT sync when the issue's existing acceptance criteria already capture the same requirements as the spec. The spec's Given/When/Then format is not needed in the issue — if the only difference is formatting, skip the sync and tell the user the criteria already align.
|
||||||
|
- Use `curl` for all API calls — do not rely on `gh` CLI.
|
||||||
|
- Always use HEREDOC for the JSON payload to handle special characters in the body.
|
||||||
|
- Escape double quotes and newlines properly in the JSON body.
|
||||||
163
.claude/commands/write-issue.md
Normal file
163
.claude/commands/write-issue.md
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
---
|
||||||
|
description: Interactive interview to create a well-structured Gitea issue with business-level acceptance criteria.
|
||||||
|
---
|
||||||
|
|
||||||
|
## User Input
|
||||||
|
|
||||||
|
```text
|
||||||
|
$ARGUMENTS
|
||||||
|
```
|
||||||
|
|
||||||
|
You **MUST** consider the user input before proceeding (if not empty).
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
1. Verify the `GITEA_TOKEN_ISSUES` environment variable is set:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
test -n "$GITEA_TOKEN_ISSUES" && echo "TOKEN_OK" || echo "TOKEN_MISSING"
|
||||||
|
```
|
||||||
|
|
||||||
|
If missing, tell the user to set it:
|
||||||
|
```
|
||||||
|
export GITEA_TOKEN_ISSUES="your-gitea-personal-access-token"
|
||||||
|
```
|
||||||
|
Then abort.
|
||||||
|
|
||||||
|
2. Parse the git remote to extract the Gitea base URL, owner, and repo:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git config --get remote.origin.url
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected format: `ssh://git@<host>:<port>/<owner>/<repo>.git` or `https://<host>/<owner>/<repo>.git`
|
||||||
|
|
||||||
|
Extract:
|
||||||
|
- `GITEA_HOST` — the hostname (e.g. `git.bahamut.nitrix.one`)
|
||||||
|
- `GITEA_PORT` — the port if present in SSH URL (for API, always use HTTPS on default port)
|
||||||
|
- `OWNER` — the repo owner/org
|
||||||
|
- `REPO` — the repo name (strip `.git` suffix)
|
||||||
|
- `API_BASE` — `https://<GITEA_HOST>/api/v1`
|
||||||
|
|
||||||
|
Verify the remote is reachable:
|
||||||
|
```bash
|
||||||
|
curl -sf -H "Authorization: token $GITEA_TOKEN_ISSUES" "$API_BASE/repos/$OWNER/$REPO" | head -c 200
|
||||||
|
```
|
||||||
|
|
||||||
|
If this fails, abort with an error explaining the API is unreachable.
|
||||||
|
|
||||||
|
## Interview Flow
|
||||||
|
|
||||||
|
### Step 1 — What's the feature?
|
||||||
|
|
||||||
|
If `$ARGUMENTS` is not empty, use it as the initial feature description and skip asking.
|
||||||
|
|
||||||
|
Otherwise, ask the user: **"What feature or change do you want to build?"**
|
||||||
|
|
||||||
|
Accept a free-form description (1-3 sentences is fine).
|
||||||
|
|
||||||
|
### Step 2 — Contextual business questions
|
||||||
|
|
||||||
|
Analyze the feature description and identify what's ambiguous or underspecified **for this specific feature**. Generate 1-3 targeted questions that would materially improve the issue.
|
||||||
|
|
||||||
|
Rules:
|
||||||
|
- Questions MUST be derived from the specific feature description, not from a generic checklist.
|
||||||
|
- Only ask questions whose answers change the scope, behavior, or acceptance criteria.
|
||||||
|
- If the feature is straightforward and well-described, skip this step entirely.
|
||||||
|
- Present one question at a time using the AskUserQuestion tool.
|
||||||
|
- For each question, provide 2-4 concrete options plus the ability to give a custom answer.
|
||||||
|
- After each answer, decide if you need another question or have enough clarity.
|
||||||
|
|
||||||
|
Examples of good contextual questions:
|
||||||
|
- For a "color overhaul" feature: "Are you targeting specific components or a global palette change?"
|
||||||
|
- For a "bulk import" feature: "Should the user see progress during import or just a final result?"
|
||||||
|
- For an "initiative tiebreaker" feature: "Should ties be broken by DEX score, manual choice, or random roll?"
|
||||||
|
|
||||||
|
Examples of bad questions (never ask these):
|
||||||
|
- Generic: "Who is the target user?"
|
||||||
|
- Obvious: "Should errors be handled?"
|
||||||
|
- Implementation: "What database should we use?"
|
||||||
|
|
||||||
|
### Step 3 — Acceptance criteria
|
||||||
|
|
||||||
|
Based on the description and clarifications, draft 3-8 business-level acceptance criteria. These should describe **what the user can do or see**, not how the system implements it.
|
||||||
|
|
||||||
|
Present the draft to the user and ask if they want to add, remove, or change any.
|
||||||
|
|
||||||
|
Good: "User can load all sources with a single click"
|
||||||
|
Bad: "System fires concurrent fetch requests to IndexedDB"
|
||||||
|
|
||||||
|
### Step 4 — Labels and milestone (optional)
|
||||||
|
|
||||||
|
Fetch available labels and milestones from the Gitea API:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -sf -H "Authorization: token $GITEA_TOKEN_ISSUES" "$API_BASE/repos/$OWNER/$REPO/labels"
|
||||||
|
curl -sf -H "Authorization: token $GITEA_TOKEN_ISSUES" "$API_BASE/repos/$OWNER/$REPO/milestones"
|
||||||
|
```
|
||||||
|
|
||||||
|
If labels/milestones exist, present them to the user and ask which (if any) to apply. If none exist or the API returns empty, skip this step.
|
||||||
|
|
||||||
|
### Step 5 — Branch linking
|
||||||
|
|
||||||
|
Check if we're on a feature branch:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git branch --show-current
|
||||||
|
```
|
||||||
|
|
||||||
|
If the current branch is not `main` (and looks like a feature branch, e.g. `NNN-feature-name`), offer to include it in the issue body as a linked branch.
|
||||||
|
|
||||||
|
### Step 6 — Preview and create
|
||||||
|
|
||||||
|
Compose the issue body using this template:
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
<2-3 sentence description synthesized from the interview>
|
||||||
|
|
||||||
|
## Acceptance Criteria
|
||||||
|
|
||||||
|
- [ ] <criterion 1>
|
||||||
|
- [ ] <criterion 2>
|
||||||
|
- [ ] <criterion 3>
|
||||||
|
...
|
||||||
|
|
||||||
|
## Branch
|
||||||
|
|
||||||
|
`<branch-name>` *(if linked in step 5)*
|
||||||
|
```
|
||||||
|
|
||||||
|
Show the full preview (title + body) to the user and ask for confirmation.
|
||||||
|
|
||||||
|
On confirmation, create the issue:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -sf -X POST \
|
||||||
|
-H "Authorization: token $GITEA_TOKEN_ISSUES" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
"$API_BASE/repos/$OWNER/$REPO/issues" \
|
||||||
|
-d @- <<'PAYLOAD'
|
||||||
|
{
|
||||||
|
"title": "<issue title>",
|
||||||
|
"body": "<issue body>",
|
||||||
|
"labels": [<label IDs if selected>],
|
||||||
|
"milestone": <milestone ID if selected or null>
|
||||||
|
}
|
||||||
|
PAYLOAD
|
||||||
|
```
|
||||||
|
|
||||||
|
Parse the response and report:
|
||||||
|
- Issue number and URL (`https://<GITEA_HOST>/<OWNER>/<REPO>/issues/<number>`)
|
||||||
|
- Suggest next step: `/integrate-issue <number>` to integrate into a feature spec
|
||||||
|
|
||||||
|
## Behavior Rules
|
||||||
|
|
||||||
|
- Keep the title short — under 70 characters.
|
||||||
|
- Acceptance criteria must be business-level, not implementation details.
|
||||||
|
- Never create an issue without user confirmation of the preview.
|
||||||
|
- If any API call fails, show the error and suggest the user check their token permissions.
|
||||||
|
- Use `curl` for all API calls — do not rely on `gh` CLI.
|
||||||
|
- Always use HEREDOC for the JSON payload to handle special characters in the body.
|
||||||
|
- Escape double quotes and newlines properly in the JSON body.
|
||||||
82
.claude/skills/rpi-implement/SKILL.md
Normal file
82
.claude/skills/rpi-implement/SKILL.md
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
---
|
||||||
|
name: rpi-implement
|
||||||
|
description: Execute approved implementation plans phase by phase with automated and manual verification. Use when the user explicitly says "implement the plan", "execute the plan", or "start implementing" and has a plan file ready. Do not use for ad-hoc coding tasks without a plan.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Implement Plan
|
||||||
|
|
||||||
|
You are tasked with implementing an approved technical plan. These plans contain phases with specific changes and success criteria.
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
If the user provided a plan path, proceed directly. If no plan path was provided, check `docs/agents/plans/` for recent plans. If none found, ask the user for a path.
|
||||||
|
|
||||||
|
When you have a plan:
|
||||||
|
- Read the plan completely and check for any existing checkmarks (`- [x]`)
|
||||||
|
- Read all files mentioned in the plan
|
||||||
|
- **Read files fully** - never use limit/offset parameters, you need complete context
|
||||||
|
- Think deeply about how the pieces fit together
|
||||||
|
- If you have a todo list, use it to track your progress
|
||||||
|
- Start implementing if you understand what needs to be done
|
||||||
|
|
||||||
|
## Implementation Philosophy
|
||||||
|
|
||||||
|
Plans are carefully designed, but reality can be messy. Your job is to:
|
||||||
|
- Follow the plan's intent while adapting to what you find
|
||||||
|
- Implement each phase fully before moving to the next
|
||||||
|
- Verify your work makes sense in the broader codebase context
|
||||||
|
- Keep plan checkboxes current: `[-]` before starting an item, `[x]` right after it passes verification. Never batch updates.
|
||||||
|
|
||||||
|
When things don't match the plan exactly, think about why and communicate clearly. The plan is your guide, but your judgment matters too.
|
||||||
|
|
||||||
|
If you encounter a mismatch:
|
||||||
|
- STOP and think deeply about why the plan can't be followed
|
||||||
|
- Present the issue clearly:
|
||||||
|
```
|
||||||
|
Issue in Phase [N]:
|
||||||
|
Expected: [what the plan says]
|
||||||
|
Found: [actual situation]
|
||||||
|
Why this matters: [explanation]
|
||||||
|
|
||||||
|
How should I proceed?
|
||||||
|
```
|
||||||
|
|
||||||
|
## Verification Approach
|
||||||
|
|
||||||
|
After implementing a phase:
|
||||||
|
- Run the success criteria checks listed in the plan (test commands, linters, type checkers, etc.)
|
||||||
|
- Fix any issues before proceeding
|
||||||
|
- **Check if manual verification is needed**: Look at the plan's success criteria for the current phase.
|
||||||
|
- If the phase has **manual verification steps**, pause and inform the human:
|
||||||
|
```
|
||||||
|
Phase [N] Complete - Ready for Manual Verification
|
||||||
|
|
||||||
|
Automated verification passed:
|
||||||
|
- [List automated checks that passed]
|
||||||
|
|
||||||
|
Please perform the manual verification steps listed in the plan:
|
||||||
|
- [List manual verification items from the plan]
|
||||||
|
|
||||||
|
Let me know when manual testing is complete so I can proceed to Phase [N+1].
|
||||||
|
```
|
||||||
|
- If the phase has **only automated verification** (no manual steps), continue directly to the next phase without pausing. Just note in passing that the phase is complete and automated checks passed.
|
||||||
|
|
||||||
|
Do not check off items in the manual testing steps until confirmed by the user.
|
||||||
|
|
||||||
|
## If You Get Stuck
|
||||||
|
|
||||||
|
When something isn't working as expected:
|
||||||
|
- First, make sure you've read and understood all the relevant code
|
||||||
|
- Consider if the codebase has evolved since the plan was written
|
||||||
|
- Present the mismatch clearly and ask for guidance
|
||||||
|
|
||||||
|
Use sub-agents sparingly - mainly for targeted debugging or exploring unfamiliar territory.
|
||||||
|
|
||||||
|
## Resuming Work
|
||||||
|
|
||||||
|
If the plan has existing checkmarks:
|
||||||
|
- Trust that completed work is done
|
||||||
|
- Pick up from the first unchecked item
|
||||||
|
- Verify previous work only if something seems off
|
||||||
|
|
||||||
|
Remember: You're implementing a solution, not just checking boxes. Keep the end goal in mind and maintain forward momentum.
|
||||||
349
.claude/skills/rpi-plan/SKILL.md
Normal file
349
.claude/skills/rpi-plan/SKILL.md
Normal file
@@ -0,0 +1,349 @@
|
|||||||
|
---
|
||||||
|
name: rpi-plan
|
||||||
|
description: Create detailed, phased implementation plans through interactive research and iteration. Use when the user explicitly asks to "create a plan", "plan the implementation", or "design an approach" for a feature, refactor, or bug fix. Do not use for quick questions or simple tasks.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Implementation Plan
|
||||||
|
|
||||||
|
You are tasked with creating detailed implementation plans through an interactive, iterative process. You should be skeptical, thorough, and work collaboratively with the user to produce high-quality technical specifications.
|
||||||
|
|
||||||
|
## Initial Setup
|
||||||
|
|
||||||
|
If the user already provided a task description, file path, or topic alongside this command, proceed directly to step 1 below. Only if no context was given, respond with:
|
||||||
|
```
|
||||||
|
I'll help you create a detailed implementation plan. Let me start by understanding what we're building.
|
||||||
|
|
||||||
|
Please provide:
|
||||||
|
1. A description of what you want to build or change
|
||||||
|
2. Any relevant context, constraints, or specific requirements
|
||||||
|
3. Pointers to related files or previous research
|
||||||
|
|
||||||
|
I'll analyze this information and work with you to create a comprehensive plan.
|
||||||
|
```
|
||||||
|
Then wait for the user's input.
|
||||||
|
|
||||||
|
## Process Steps
|
||||||
|
|
||||||
|
### Step 1: Context Gathering & Initial Analysis
|
||||||
|
|
||||||
|
1. **Read all mentioned files immediately and FULLY**:
|
||||||
|
- Any files the user referenced (docs, research, code)
|
||||||
|
- **IMPORTANT**: Use the Read tool WITHOUT limit/offset parameters to read entire files
|
||||||
|
- **CRITICAL**: DO NOT spawn sub-tasks before reading these files yourself in the main context
|
||||||
|
- **NEVER** read files partially - if a file is mentioned, read it completely
|
||||||
|
|
||||||
|
2. **Determine if research already exists**:
|
||||||
|
- If the user provided a research document (e.g. from `docs/agents/research/`), **trust it as the source of truth**. Do NOT re-research topics that the document already covers. Use its findings, file references, and architecture analysis directly as the basis for planning.
|
||||||
|
- **NEVER repeat or re-do research that has already been provided.** The plan phase is about turning existing research into actionable implementation steps, not about gathering information that's already available.
|
||||||
|
- If NO research document was provided, proceed with targeted research as described below.
|
||||||
|
|
||||||
|
3. **Read the most relevant files directly into your main context**:
|
||||||
|
- Based on the research document and/or user input, identify the most relevant source files
|
||||||
|
- **Read these files yourself using the Read tool** — do NOT delegate this to sub-agents. You need these files in your own context to write an accurate plan.
|
||||||
|
- Focus on files that will be modified or that define interfaces/patterns you need to follow
|
||||||
|
|
||||||
|
4. **Only spawn sub-agents for genuinely missing information**:
|
||||||
|
- Do NOT spawn sub-agents to re-discover what the research document already covers
|
||||||
|
- Only use sub-agents if there are specific gaps: e.g. the research doesn't cover test conventions, a specific API surface, or a file that was added after the research was written
|
||||||
|
- Each sub-agent should have a narrow, specific question to answer — not broad exploration
|
||||||
|
|
||||||
|
5. **Analyze and verify understanding**:
|
||||||
|
- Cross-reference the requirements with actual code (and research document if provided)
|
||||||
|
- Identify any discrepancies or misunderstandings
|
||||||
|
- Note assumptions that need verification
|
||||||
|
- Determine true scope based on codebase reality
|
||||||
|
|
||||||
|
6. **Present informed understanding and focused questions**:
|
||||||
|
```
|
||||||
|
Based on the task and my research of the codebase, I understand we need to [accurate summary].
|
||||||
|
|
||||||
|
I've found that:
|
||||||
|
- [Current implementation detail with file:line reference]
|
||||||
|
- [Relevant pattern or constraint discovered]
|
||||||
|
- [Potential complexity or edge case identified]
|
||||||
|
|
||||||
|
Questions that my research couldn't answer:
|
||||||
|
- [Specific technical question that requires human judgment]
|
||||||
|
- [Business logic clarification]
|
||||||
|
- [Design preference that affects implementation]
|
||||||
|
```
|
||||||
|
|
||||||
|
Only ask questions that you genuinely cannot answer through code investigation.
|
||||||
|
|
||||||
|
### Step 2: Targeted Research & Discovery
|
||||||
|
|
||||||
|
After getting initial clarifications:
|
||||||
|
|
||||||
|
1. **If the user corrects any misunderstanding**:
|
||||||
|
- DO NOT just accept the correction
|
||||||
|
- Read the specific files/directories they mention directly into your context
|
||||||
|
- Only proceed once you've verified the facts yourself
|
||||||
|
|
||||||
|
2. If you have a todo list, use it to track exploration progress
|
||||||
|
|
||||||
|
3. **Fill in gaps — do NOT redo existing research**:
|
||||||
|
- If a research document was provided, identify only the specific gaps that need filling
|
||||||
|
- Read additional files directly when possible — only spawn sub-agents for searches where you don't know the file paths
|
||||||
|
- **Ask yourself before any research action: "Is this already covered by the provided research?"** If yes, skip it and use what's there.
|
||||||
|
|
||||||
|
4. **Present findings and design options**:
|
||||||
|
```
|
||||||
|
Based on my research, here's what I found:
|
||||||
|
|
||||||
|
**Current State:**
|
||||||
|
- [Key discovery about existing code]
|
||||||
|
- [Pattern or convention to follow]
|
||||||
|
|
||||||
|
**Design Options:**
|
||||||
|
1. [Option A] - [pros/cons]
|
||||||
|
2. [Option B] - [pros/cons]
|
||||||
|
|
||||||
|
**Open Questions:**
|
||||||
|
- [Technical uncertainty]
|
||||||
|
- [Design decision needed]
|
||||||
|
|
||||||
|
Which approach aligns best with your vision?
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 3: Plan Structure Development
|
||||||
|
|
||||||
|
Once aligned on approach:
|
||||||
|
|
||||||
|
1. **Create initial plan outline**:
|
||||||
|
```
|
||||||
|
Here's my proposed plan structure:
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
[1-2 sentence summary]
|
||||||
|
|
||||||
|
## Implementation Phases:
|
||||||
|
1. [Phase name] - [what it accomplishes]
|
||||||
|
2. [Phase name] - [what it accomplishes]
|
||||||
|
3. [Phase name] - [what it accomplishes]
|
||||||
|
|
||||||
|
Does this phasing make sense? Should I adjust the order or granularity?
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Get feedback on structure** before writing details
|
||||||
|
|
||||||
|
### Step 4: Detailed Plan Writing
|
||||||
|
|
||||||
|
After structure approval:
|
||||||
|
|
||||||
|
1. **Gather metadata**:
|
||||||
|
- Run `python <skill_directory>/scripts/metadata.py` to get date, commit, branch, and repository info
|
||||||
|
- Determine the output filename: `docs/agents/plans/YYYY-MM-DD-description.md`
|
||||||
|
- YYYY-MM-DD is today's date
|
||||||
|
- description is a brief kebab-case description
|
||||||
|
- Example: `2025-01-08-improve-error-handling.md`
|
||||||
|
- The output folder (`docs/agents/plans/`) can be overridden by instructions in the project's `AGENTS.md` or `CLAUDE.md`
|
||||||
|
|
||||||
|
2. **Write the plan** to `docs/agents/plans/YYYY-MM-DD-description.md`
|
||||||
|
- Ensure the `docs/agents/plans/` directory exists (create if needed)
|
||||||
|
- **Every actionable item must have a checkbox** (`- [ ]`) so progress can be tracked during implementation. This includes each change in "Changes Required" and each verification step in "Success Criteria".
|
||||||
|
- Use the template structure below:
|
||||||
|
|
||||||
|
````markdown
|
||||||
|
---
|
||||||
|
date: [ISO date/time from metadata]
|
||||||
|
git_commit: [Current commit hash from metadata]
|
||||||
|
branch: [Current branch name from metadata]
|
||||||
|
topic: "[Feature/Task Name]"
|
||||||
|
tags: [plan, relevant-component-names]
|
||||||
|
status: draft
|
||||||
|
---
|
||||||
|
|
||||||
|
# [Feature/Task Name] Implementation Plan
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
[Brief description of what we're implementing and why]
|
||||||
|
|
||||||
|
## Current State Analysis
|
||||||
|
|
||||||
|
[What exists now, what's missing, key constraints discovered]
|
||||||
|
|
||||||
|
## Desired End State
|
||||||
|
|
||||||
|
[A specification of the desired end state after this plan is complete, and how to verify it]
|
||||||
|
|
||||||
|
### UI Mockups (if applicable)
|
||||||
|
[If the changes involve user-facing interfaces (CLI output, web UI, terminal UI, etc.), include ASCII mockups
|
||||||
|
that visually illustrate the intended result. This helps the reader quickly grasp the change.]
|
||||||
|
|
||||||
|
### Key Discoveries:
|
||||||
|
- [Important finding with file:line reference]
|
||||||
|
- [Pattern to follow]
|
||||||
|
- [Constraint to work within]
|
||||||
|
|
||||||
|
## What We're NOT Doing
|
||||||
|
|
||||||
|
[Explicitly list out-of-scope items to prevent scope creep]
|
||||||
|
|
||||||
|
## Implementation Approach
|
||||||
|
|
||||||
|
[High-level strategy and reasoning]
|
||||||
|
|
||||||
|
## Phase 1: [Descriptive Name]
|
||||||
|
|
||||||
|
### Overview
|
||||||
|
[What this phase accomplishes]
|
||||||
|
|
||||||
|
### Changes Required:
|
||||||
|
|
||||||
|
#### [ ] 1. [Component/File Group]
|
||||||
|
**File**: `path/to/file.ext`
|
||||||
|
**Changes**: [Summary of changes]
|
||||||
|
|
||||||
|
```[language]
|
||||||
|
// Specific code to add/modify
|
||||||
|
```
|
||||||
|
|
||||||
|
### Success Criteria:
|
||||||
|
|
||||||
|
#### Automated Verification:
|
||||||
|
- [ ] Tests pass: `[test command]`
|
||||||
|
- [ ] Type checking passes: `[typecheck command]`
|
||||||
|
- [ ] Linting passes: `[lint command]`
|
||||||
|
|
||||||
|
#### Manual Verification:
|
||||||
|
- [ ] Feature works as expected when tested
|
||||||
|
- [ ] Edge case handling verified
|
||||||
|
- [ ] No regressions in related features
|
||||||
|
|
||||||
|
**Implementation Note**: After completing this phase and all automated verification passes, pause here for manual confirmation from the human before proceeding to the next phase.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Phase 2: [Descriptive Name]
|
||||||
|
|
||||||
|
[Similar structure with both automated and manual success criteria...]
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Testing Strategy
|
||||||
|
|
||||||
|
### Unit Tests:
|
||||||
|
- [What to test]
|
||||||
|
- [Key edge cases]
|
||||||
|
|
||||||
|
### Integration Tests:
|
||||||
|
- [End-to-end scenarios]
|
||||||
|
|
||||||
|
### Manual Testing Steps:
|
||||||
|
1. [Specific step to verify feature]
|
||||||
|
2. [Another verification step]
|
||||||
|
|
||||||
|
## Performance Considerations
|
||||||
|
|
||||||
|
[Any performance implications or optimizations needed]
|
||||||
|
|
||||||
|
## Migration Notes
|
||||||
|
|
||||||
|
[If applicable, how to handle existing data/systems]
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
- [Related research or documentation]
|
||||||
|
- [Similar implementation: file:line]
|
||||||
|
````
|
||||||
|
|
||||||
|
### Step 5: Review & Iterate
|
||||||
|
|
||||||
|
1. **Present the draft plan location**:
|
||||||
|
```
|
||||||
|
I've created the initial implementation plan at:
|
||||||
|
`docs/agents/plans/YYYY-MM-DD-description.md`
|
||||||
|
|
||||||
|
Please review it and let me know:
|
||||||
|
- Are the phases properly scoped?
|
||||||
|
- Are the success criteria specific enough?
|
||||||
|
- Any technical details that need adjustment?
|
||||||
|
- Missing edge cases or considerations?
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Iterate based on feedback** - be ready to:
|
||||||
|
- Add missing phases
|
||||||
|
- Adjust technical approach
|
||||||
|
- Clarify success criteria (both automated and manual)
|
||||||
|
- Add/remove scope items
|
||||||
|
|
||||||
|
3. **Continue refining** until the user is satisfied
|
||||||
|
|
||||||
|
## Important Guidelines
|
||||||
|
|
||||||
|
1. **Be Skeptical**:
|
||||||
|
- Question vague requirements
|
||||||
|
- Identify potential issues early
|
||||||
|
- Ask "why" and "what about"
|
||||||
|
- Don't assume - verify with code
|
||||||
|
|
||||||
|
2. **Be Interactive**:
|
||||||
|
- Don't write the full plan in one shot
|
||||||
|
- Get buy-in at each major step
|
||||||
|
- Allow course corrections
|
||||||
|
- Work collaboratively
|
||||||
|
|
||||||
|
3. **Be Thorough But Not Redundant**:
|
||||||
|
- Read all context files COMPLETELY before planning
|
||||||
|
- Use provided research as-is — do not re-investigate what's already documented
|
||||||
|
- Read key source files directly into your context rather than delegating to sub-agents
|
||||||
|
- Only spawn sub-agents for narrow, specific questions that aren't answered by existing research
|
||||||
|
- Include specific file paths and line numbers
|
||||||
|
- Write measurable success criteria with clear automated vs manual distinction
|
||||||
|
|
||||||
|
4. **Be Visual**:
|
||||||
|
- If the change involves any user-facing interface (web UI, CLI output, terminal UI, forms, dashboards, etc.), include ASCII mockups in the plan
|
||||||
|
- Mockups make the intended result immediately understandable and help catch misunderstandings early
|
||||||
|
- Show both the current state and the proposed state when the change modifies an existing UI
|
||||||
|
- Keep mockups simple but accurate enough to convey layout, key elements, and interactions
|
||||||
|
|
||||||
|
5. **Be Practical**:
|
||||||
|
- Focus on incremental, testable changes
|
||||||
|
- Consider migration and rollback
|
||||||
|
- Think about edge cases
|
||||||
|
- Include "what we're NOT doing"
|
||||||
|
|
||||||
|
6. **No Open Questions in Final Plan**:
|
||||||
|
- If you encounter open questions during planning, STOP
|
||||||
|
- Research or ask for clarification immediately
|
||||||
|
- Do NOT write the plan with unresolved questions
|
||||||
|
- The implementation plan must be complete and actionable
|
||||||
|
- Every decision must be made before finalizing the plan
|
||||||
|
|
||||||
|
## Success Criteria Guidelines
|
||||||
|
|
||||||
|
**Always separate success criteria into two categories:**
|
||||||
|
|
||||||
|
1. **Automated Verification** (can be run by agents):
|
||||||
|
- Commands that can be run: test suites, linters, type checkers
|
||||||
|
- Specific files that should exist
|
||||||
|
- Code compilation/type checking
|
||||||
|
|
||||||
|
2. **Manual Verification** (requires human testing):
|
||||||
|
- UI/UX functionality
|
||||||
|
- Performance under real conditions
|
||||||
|
- Edge cases that are hard to automate
|
||||||
|
- User acceptance criteria
|
||||||
|
|
||||||
|
## Common Patterns
|
||||||
|
|
||||||
|
### For Database Changes:
|
||||||
|
- Start with schema/migration
|
||||||
|
- Add store methods
|
||||||
|
- Update business logic
|
||||||
|
- Expose via API
|
||||||
|
- Update clients
|
||||||
|
|
||||||
|
### For New Features:
|
||||||
|
- Research existing patterns first
|
||||||
|
- Start with data model
|
||||||
|
- Build backend logic
|
||||||
|
- Add API endpoints
|
||||||
|
- Implement UI last
|
||||||
|
|
||||||
|
### For Refactoring:
|
||||||
|
- Document current behavior
|
||||||
|
- Plan incremental changes
|
||||||
|
- Maintain backwards compatibility
|
||||||
|
- Include migration strategy
|
||||||
37
.claude/skills/rpi-plan/scripts/metadata.py
Executable file
37
.claude/skills/rpi-plan/scripts/metadata.py
Executable file
@@ -0,0 +1,37 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Get git metadata for plan documents."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def run(cmd: list[str]) -> str:
|
||||||
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||||
|
return result.stdout.strip() if result.returncode == 0 else ""
|
||||||
|
|
||||||
|
|
||||||
|
def get_repo_name() -> str:
|
||||||
|
remote = run(["git", "remote", "get-url", "origin"])
|
||||||
|
if remote:
|
||||||
|
name = remote.rstrip("/").rsplit("/", 1)[-1]
|
||||||
|
return name.removesuffix(".git")
|
||||||
|
root = run(["git", "rev-parse", "--show-toplevel"])
|
||||||
|
return Path(root).name if root else Path.cwd().name
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
metadata = {
|
||||||
|
"date": datetime.now(timezone.utc).isoformat(),
|
||||||
|
"commit": run(["git", "rev-parse", "HEAD"]),
|
||||||
|
"branch": run(["git", "branch", "--show-current"]),
|
||||||
|
"repository": get_repo_name(),
|
||||||
|
}
|
||||||
|
json.dump(metadata, sys.stdout, indent=2)
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
146
.claude/skills/rpi-research/SKILL.md
Normal file
146
.claude/skills/rpi-research/SKILL.md
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
---
|
||||||
|
name: rpi-research
|
||||||
|
description: Conduct deep codebase research and produce a written report. Use when the user explicitly requests research like "start a research for", "deeply investigate", or "fully understand how X works". Do not use for quick questions or simple code lookups.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Research Codebase
|
||||||
|
|
||||||
|
You are tasked with conducting comprehensive research across the codebase to answer user questions by spawning parallel sub-agents and synthesizing their findings.
|
||||||
|
|
||||||
|
## CRITICAL: YOUR ONLY JOB IS TO DOCUMENT AND EXPLAIN THE CODEBASE AS IT EXISTS TODAY
|
||||||
|
- DO NOT suggest improvements or changes unless the user explicitly asks for them
|
||||||
|
- DO NOT perform root cause analysis unless the user explicitly asks for them
|
||||||
|
- DO NOT propose future enhancements unless the user explicitly asks for them
|
||||||
|
- DO NOT critique the implementation or identify problems
|
||||||
|
- DO NOT recommend refactoring, optimization, or architectural changes
|
||||||
|
- ONLY describe what exists, where it exists, how it works, and how components interact
|
||||||
|
- You are creating a technical map/documentation of the existing system
|
||||||
|
|
||||||
|
## Initial Setup
|
||||||
|
|
||||||
|
If the user already provided a research question or topic alongside this command, proceed directly to step 1 below. Only if no query was given, respond with:
|
||||||
|
```
|
||||||
|
I'm ready to research the codebase. Please provide your research question or area of interest, and I'll analyze it thoroughly by exploring relevant components and connections.
|
||||||
|
```
|
||||||
|
Then wait for the user's research query.
|
||||||
|
|
||||||
|
## Steps to follow after receiving the research query:
|
||||||
|
|
||||||
|
1. **Read any directly mentioned files first:**
|
||||||
|
- If the user mentions specific files or docs, read them FULLY first
|
||||||
|
- **IMPORTANT**: Use the Read tool WITHOUT limit/offset parameters to read entire files
|
||||||
|
- **CRITICAL**: Read these files yourself in the main context before spawning any sub-tasks
|
||||||
|
- This ensures you have full context before decomposing the research
|
||||||
|
|
||||||
|
2. **Analyze and decompose the research question:**
|
||||||
|
- Break down the user's query into composable research areas
|
||||||
|
- Take time to think deeply about the underlying patterns, connections, and architectural implications the user might be seeking
|
||||||
|
- Identify specific components, patterns, or concepts to investigate
|
||||||
|
- If you have a todo list, use it to track progress
|
||||||
|
- Consider which directories, files, or architectural patterns are relevant
|
||||||
|
|
||||||
|
3. **Spawn parallel sub-agents to identify relevant files and map the landscape:**
|
||||||
|
- Create multiple Task agents to search for files and identify what's relevant
|
||||||
|
- Each sub-agent should focus on locating files and reporting back paths and brief summaries — NOT on deeply analyzing code
|
||||||
|
- The key is to use these agents for discovery:
|
||||||
|
- Search for files related to each research area
|
||||||
|
- Identify entry points, key types, and important functions
|
||||||
|
- Report back file paths, line numbers, and short descriptions of what each file contains
|
||||||
|
- Run multiple agents in parallel when they're searching for different things
|
||||||
|
- Remind agents they are documenting, not evaluating or improving
|
||||||
|
- **If the user explicitly asks for web research**, spawn additional agents with WebSearch/WebFetch tools and instruct them to return links with their findings
|
||||||
|
|
||||||
|
4. **Read the most relevant files yourself in the main context:**
|
||||||
|
- After sub-agents report back, identify the most important files for answering the research question
|
||||||
|
- **Read these files yourself using the Read tool** — you need them in your own context to write an accurate, detailed research document
|
||||||
|
- Do NOT rely solely on sub-agent summaries for the core findings — sub-agent summaries may miss nuances, connections, or important details
|
||||||
|
- Prioritize files that are central to the research question; skip peripheral files that sub-agents already summarized adequately
|
||||||
|
- This is the step where you build deep understanding — the previous step was just finding what to read
|
||||||
|
|
||||||
|
5. **Synthesize findings into a complete picture:**
|
||||||
|
- Combine your own reading with sub-agent discoveries
|
||||||
|
- Connect findings across different components
|
||||||
|
- Include specific file paths and line numbers for reference
|
||||||
|
- Highlight patterns, connections, and architectural decisions
|
||||||
|
- Answer the user's specific questions with concrete evidence
|
||||||
|
|
||||||
|
6. **Gather metadata for the research document:**
|
||||||
|
- Run `python <skill_directory>/scripts/metadata.py` to get date, commit, branch, and repository info
|
||||||
|
- Determine the output filename: `docs/agents/research/YYYY-MM-DD-description.md`
|
||||||
|
- YYYY-MM-DD is today's date
|
||||||
|
- description is a brief kebab-case description of the research topic
|
||||||
|
- Example: `2025-01-08-authentication-flow.md`
|
||||||
|
- The output folder (`docs/agents/research/`) can be overridden by instructions in the project's `AGENTS.md` or `CLAUDE.md`
|
||||||
|
|
||||||
|
7. **Generate research document:**
|
||||||
|
- Use the metadata gathered in step 5
|
||||||
|
- Ensure the `docs/agents/research/` directory exists (create if needed)
|
||||||
|
- Structure the document with YAML frontmatter followed by content:
|
||||||
|
```markdown
|
||||||
|
---
|
||||||
|
date: [ISO date/time from metadata]
|
||||||
|
git_commit: [Current commit hash from metadata]
|
||||||
|
branch: [Current branch name from metadata]
|
||||||
|
topic: "[User's Question/Topic]"
|
||||||
|
tags: [research, codebase, relevant-component-names]
|
||||||
|
status: complete
|
||||||
|
---
|
||||||
|
|
||||||
|
# Research: [User's Question/Topic]
|
||||||
|
|
||||||
|
## Research Question
|
||||||
|
[Original user query]
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
[High-level documentation of what was found, answering the user's question by describing what exists]
|
||||||
|
|
||||||
|
## Detailed Findings
|
||||||
|
|
||||||
|
### [Component/Area 1]
|
||||||
|
- Description of what exists (file.ext:line)
|
||||||
|
- How it connects to other components
|
||||||
|
- Current implementation details (without evaluation)
|
||||||
|
|
||||||
|
### [Component/Area 2]
|
||||||
|
...
|
||||||
|
|
||||||
|
## Code References
|
||||||
|
- `path/to/file.py:123` - Description of what's there
|
||||||
|
- `another/file.ts:45-67` - Description of the code block
|
||||||
|
|
||||||
|
## Architecture Documentation
|
||||||
|
[Current patterns, conventions, and design implementations found in the codebase]
|
||||||
|
|
||||||
|
## Open Questions
|
||||||
|
[Any areas that need further investigation]
|
||||||
|
```
|
||||||
|
|
||||||
|
8. **Present findings to the user:**
|
||||||
|
- Present a concise summary of findings
|
||||||
|
- Include key file references for easy navigation
|
||||||
|
- Ask if they have follow-up questions or need clarification
|
||||||
|
|
||||||
|
9. **Handle follow-up questions:**
|
||||||
|
- If the user has follow-up questions, append to the same research document
|
||||||
|
- Add a new section: `## Follow-up Research [timestamp]`
|
||||||
|
- Spawn new sub-agents as needed for additional investigation
|
||||||
|
- Continue updating the document
|
||||||
|
|
||||||
|
## Important notes:
|
||||||
|
- Use parallel sub-agents for file discovery and landscape mapping, but **read the most important files yourself** in the main context
|
||||||
|
- Sub-agents are scouts that find relevant files — the main agent must read key files to build deep understanding
|
||||||
|
- Do NOT rely solely on sub-agent summaries for your core findings; they may miss nuances and connections
|
||||||
|
- Focus on finding concrete file paths and line numbers for developer reference
|
||||||
|
- Research documents should be self-contained with all necessary context
|
||||||
|
- Each sub-agent prompt should be specific and focused on locating files and reporting back paths
|
||||||
|
- Document cross-component connections and how systems interact
|
||||||
|
- **CRITICAL**: You and all sub-agents are documentarians, not evaluators
|
||||||
|
- **REMEMBER**: Document what IS, not what SHOULD BE
|
||||||
|
- **NO RECOMMENDATIONS**: Only describe the current state of the codebase
|
||||||
|
- **File reading**: Always read mentioned files FULLY (no limit/offset) before spawning sub-tasks
|
||||||
|
- **Critical ordering**: Follow the numbered steps exactly
|
||||||
|
- ALWAYS read mentioned files first before spawning sub-tasks (step 1)
|
||||||
|
- ALWAYS read key files yourself after sub-agents report back (step 4)
|
||||||
|
- ALWAYS wait for your own reading to complete before synthesizing (step 5)
|
||||||
|
- ALWAYS gather metadata before writing the document (step 6 before step 7)
|
||||||
|
- NEVER write the research document with placeholder values
|
||||||
39
.claude/skills/rpi-research/scripts/metadata.py
Executable file
39
.claude/skills/rpi-research/scripts/metadata.py
Executable file
@@ -0,0 +1,39 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Get git metadata for research documents."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def run(cmd: list[str]) -> str:
|
||||||
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||||
|
return result.stdout.strip() if result.returncode == 0 else ""
|
||||||
|
|
||||||
|
|
||||||
|
def get_repo_name() -> str:
|
||||||
|
remote = run(["git", "remote", "get-url", "origin"])
|
||||||
|
if remote:
|
||||||
|
# Handle both HTTPS and SSH URLs
|
||||||
|
name = remote.rstrip("/").rsplit("/", 1)[-1]
|
||||||
|
return name.removesuffix(".git")
|
||||||
|
# Fall back to directory name
|
||||||
|
root = run(["git", "rev-parse", "--show-toplevel"])
|
||||||
|
return Path(root).name if root else Path.cwd().name
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
metadata = {
|
||||||
|
"date": datetime.now(timezone.utc).isoformat(),
|
||||||
|
"commit": run(["git", "rev-parse", "HEAD"]),
|
||||||
|
"branch": run(["git", "branch", "--show-current"]),
|
||||||
|
"repository": get_repo_name(),
|
||||||
|
}
|
||||||
|
json.dump(metadata, sys.stdout, indent=2)
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
9
.dockerignore
Normal file
9
.dockerignore
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
node_modules
|
||||||
|
.pnpm-store
|
||||||
|
dist
|
||||||
|
coverage
|
||||||
|
.git
|
||||||
|
.claude
|
||||||
|
.specify
|
||||||
|
specs
|
||||||
|
docs
|
||||||
49
.gitea/workflows/ci.yml
Normal file
49
.gitea/workflows/ci.yml
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
tags: ["*"]
|
||||||
|
pull_request:
|
||||||
|
branches: [main]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
check:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- uses: pnpm/action-setup@v4
|
||||||
|
|
||||||
|
- uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: 22
|
||||||
|
|
||||||
|
- run: pnpm install --frozen-lockfile
|
||||||
|
- run: pnpm check
|
||||||
|
|
||||||
|
build-image:
|
||||||
|
if: startsWith(github.ref, 'refs/tags/')
|
||||||
|
needs: check
|
||||||
|
runs-on: host
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Log in to Gitea registry
|
||||||
|
run: echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login git.bahamut.nitrix.one -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin
|
||||||
|
|
||||||
|
- name: Build and push
|
||||||
|
run: |
|
||||||
|
IMAGE=git.bahamut.nitrix.one/dostulata/initiative
|
||||||
|
TAG=${GITHUB_REF#refs/tags/}
|
||||||
|
docker build -t $IMAGE:$TAG -t $IMAGE:latest .
|
||||||
|
docker push $IMAGE:$TAG
|
||||||
|
docker push $IMAGE:latest
|
||||||
|
|
||||||
|
- name: Deploy
|
||||||
|
run: |
|
||||||
|
IMAGE=git.bahamut.nitrix.one/dostulata/initiative
|
||||||
|
TAG=${GITHUB_REF#refs/tags/}
|
||||||
|
docker stop initiative || true
|
||||||
|
docker rm initiative || true
|
||||||
|
docker run -d --name initiative --restart unless-stopped -p 8080:80 $IMAGE:$TAG
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -11,3 +11,4 @@ Thumbs.db
|
|||||||
.idea/
|
.idea/
|
||||||
coverage/
|
coverage/
|
||||||
*.tsbuildinfo
|
*.tsbuildinfo
|
||||||
|
docs/agents/plans/
|
||||||
|
|||||||
27
.oxlintrc.json
Normal file
27
.oxlintrc.json
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://raw.githubusercontent.com/nicolo-ribaudo/tc39-proposal-type-annotations/refs/heads/main/packages/oxlint/configuration_file_schema.json",
|
||||||
|
"plugins": ["typescript", "unicorn", "jest"],
|
||||||
|
"categories": {},
|
||||||
|
"rules": {
|
||||||
|
"typescript/no-unnecessary-type-assertion": "error",
|
||||||
|
"typescript/no-deprecated": "warn",
|
||||||
|
"typescript/prefer-regexp-exec": "error",
|
||||||
|
"unicorn/prefer-string-replace-all": "error",
|
||||||
|
"unicorn/prefer-string-raw": "error",
|
||||||
|
"jest/expect-expect": [
|
||||||
|
"error",
|
||||||
|
{
|
||||||
|
"assertFunctionNames": ["expect", "expectDomainError"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"ignorePatterns": [
|
||||||
|
"dist",
|
||||||
|
"coverage",
|
||||||
|
".claude",
|
||||||
|
".specify",
|
||||||
|
"specs",
|
||||||
|
".pnpm-store",
|
||||||
|
"scripts"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -1,14 +1,10 @@
|
|||||||
<!--
|
<!--
|
||||||
Sync Impact Report
|
Sync Impact Report
|
||||||
───────────────────
|
───────────────────
|
||||||
Version change: 1.0.2 → 1.0.3 (PATCH — add merge-gate rule)
|
Version change: 2.2.1 → 3.0.0 (MAJOR — specs describe features not changes, proportional workflow)
|
||||||
Modified sections:
|
Modified sections:
|
||||||
- Development Workflow: added automated-checks merge gate
|
- Development Workflow: specs are living feature documents; full pipeline for new features only
|
||||||
Templates requiring updates:
|
Templates requiring updates: none
|
||||||
- .specify/templates/plan-template.md ✅ no update needed
|
|
||||||
- .specify/templates/spec-template.md ✅ no update needed
|
|
||||||
- .specify/templates/tasks-template.md ✅ no update needed
|
|
||||||
Follow-up TODOs: none
|
|
||||||
-->
|
-->
|
||||||
# Encounter Console Constitution
|
# Encounter Console Constitution
|
||||||
|
|
||||||
@@ -29,7 +25,7 @@ be injected at the boundary, never sourced inside the domain layer.
|
|||||||
|
|
||||||
### II. Layered Architecture
|
### II. Layered Architecture
|
||||||
|
|
||||||
The codebase MUST be organized into four layers with strict
|
The codebase MUST be organized into three layers with strict
|
||||||
dependency direction:
|
dependency direction:
|
||||||
|
|
||||||
1. **Domain** — pure types, state transitions, validation rules.
|
1. **Domain** — pure types, state transitions, validation rules.
|
||||||
@@ -39,34 +35,21 @@ dependency direction:
|
|||||||
interfaces that Adapters implement. May import Domain only.
|
interfaces that Adapters implement. May import Domain only.
|
||||||
3. **Adapters** — I/O, persistence, UI rendering, external APIs.
|
3. **Adapters** — I/O, persistence, UI rendering, external APIs.
|
||||||
May import Application and Domain.
|
May import Application and Domain.
|
||||||
4. **Agent** — AI-assisted features (suggestions, analysis).
|
|
||||||
May import Application and Domain as read-only consumers.
|
|
||||||
|
|
||||||
A module in an inner layer MUST NOT import from an outer layer.
|
A module in an inner layer MUST NOT import from an outer layer.
|
||||||
|
|
||||||
### III. Agent Boundary
|
### III. Clarification-First
|
||||||
|
|
||||||
The agent layer MAY read domain events and current state. The agent
|
|
||||||
MAY produce suggestions, annotations, or recommendations. The agent
|
|
||||||
MUST NOT mutate domain state directly. All agent-originated changes
|
|
||||||
MUST flow through the Application layer as explicit user-confirmed
|
|
||||||
commands.
|
|
||||||
|
|
||||||
- Agent output MUST be clearly labeled as suggestions.
|
|
||||||
- No silent or automatic application of agent recommendations.
|
|
||||||
|
|
||||||
### IV. Clarification-First
|
|
||||||
|
|
||||||
Before making any non-trivial assumption during specification,
|
Before making any non-trivial assumption during specification,
|
||||||
planning, or implementation, the agent MUST surface a clarification
|
planning, or implementation, Claude Code MUST surface a clarification
|
||||||
question to the user. "Non-trivial" means any decision that would
|
question to the user. "Non-trivial" means any decision that would
|
||||||
alter observable behavior, data model shape, or public API surface.
|
alter observable behavior, data model shape, or public API surface.
|
||||||
The agent MUST also ask when multiple valid interpretations exist,
|
Claude Code MUST also ask when multiple valid interpretations exist,
|
||||||
when a choice would affect architectural layering, or when scope
|
when a choice would affect architectural layering, or when scope
|
||||||
would expand beyond the current spec. The agent MUST NOT silently
|
would expand beyond the current spec. Claude Code MUST NOT silently
|
||||||
choose among valid alternatives.
|
choose among valid alternatives.
|
||||||
|
|
||||||
### V. Escalation Gates
|
### IV. Escalation Gates
|
||||||
|
|
||||||
Any feature, requirement, or scope change not present in the current
|
Any feature, requirement, or scope change not present in the current
|
||||||
spec MUST be rejected at implementation time until the spec is
|
spec MUST be rejected at implementation time until the spec is
|
||||||
@@ -77,7 +60,7 @@ explicitly updated. The workflow is:
|
|||||||
3. Update the spec via `/speckit.specify` or `/speckit.clarify`.
|
3. Update the spec via `/speckit.specify` or `/speckit.clarify`.
|
||||||
4. Only then proceed with implementation.
|
4. Only then proceed with implementation.
|
||||||
|
|
||||||
### VI. MVP Baseline Language
|
### V. MVP Baseline Language
|
||||||
|
|
||||||
Constraints in this constitution and in specs MUST use MVP baseline
|
Constraints in this constitution and in specs MUST use MVP baseline
|
||||||
language ("MVP baseline does not include X") rather than permanent
|
language ("MVP baseline does not include X") rather than permanent
|
||||||
@@ -86,7 +69,7 @@ add capabilities in future iterations without constitutional
|
|||||||
amendment. The current MVP baseline is local-first and single-user;
|
amendment. The current MVP baseline is local-first and single-user;
|
||||||
this is a starting scope, not a permanent restriction.
|
this is a starting scope, not a permanent restriction.
|
||||||
|
|
||||||
### VII. No Gameplay Rules in Constitution
|
### VI. No Gameplay Rules in Constitution
|
||||||
|
|
||||||
This constitution MUST NOT contain concrete gameplay mechanics,
|
This constitution MUST NOT contain concrete gameplay mechanics,
|
||||||
rule-system specifics, or encounter resolution logic. Such details
|
rule-system specifics, or encounter resolution logic. Such details
|
||||||
@@ -96,9 +79,9 @@ architecture, and quality — not product behavior.
|
|||||||
## Scope Constraints
|
## Scope Constraints
|
||||||
|
|
||||||
- The Encounter Console's primary focus is initiative tracking and
|
- The Encounter Console's primary focus is initiative tracking and
|
||||||
encounter state management. Adjacent capabilities (e.g., richer
|
encounter state management. Adjacent capabilities (e.g., bestiary
|
||||||
game-engine features) are not in the MVP baseline but may be
|
integration, richer game-engine features) may be added via spec
|
||||||
added via spec updates in future iterations.
|
updates.
|
||||||
- Technology choices, UI framework, and storage mechanism are
|
- Technology choices, UI framework, and storage mechanism are
|
||||||
spec-level decisions, not constitutional mandates.
|
spec-level decisions, not constitutional mandates.
|
||||||
- Testing strategy (unit, integration, contract) is determined per
|
- Testing strategy (unit, integration, contract) is determined per
|
||||||
@@ -109,16 +92,31 @@ architecture, and quality — not product behavior.
|
|||||||
|
|
||||||
- No change may be merged unless all automated checks (tests and
|
- No change may be merged unless all automated checks (tests and
|
||||||
static analysis as defined by the project) pass.
|
static analysis as defined by the project) pass.
|
||||||
- Every feature begins with a spec (`/speckit.specify`).
|
- Specs describe **features**, not individual changes. Each spec is
|
||||||
- Implementation follows the plan → tasks → implement pipeline.
|
a living document. New features begin with `/speckit.specify`
|
||||||
|
(which creates a feature branch for the full speckit pipeline);
|
||||||
|
changes to existing features update the existing spec via
|
||||||
|
`/integrate-issue`.
|
||||||
|
- The full pipeline (spec → plan → tasks → implement) applies to new
|
||||||
|
features and significant additions. Bug fixes, tooling changes,
|
||||||
|
and trivial UI adjustments do not require specs.
|
||||||
- Domain logic MUST be testable without mocks for external systems.
|
- Domain logic MUST be testable without mocks for external systems.
|
||||||
- Long-running or multi-step state transitions SHOULD be verifiable
|
- Long-running or multi-step state transitions SHOULD be verifiable
|
||||||
through reproducible event logs or snapshot-style tests.
|
through reproducible event logs or snapshot-style tests.
|
||||||
- Commits SHOULD be atomic and map to individual tasks where
|
- Commits SHOULD be atomic and map to individual tasks where
|
||||||
practical.
|
practical.
|
||||||
- Layer boundary compliance MUST be verified by automated import
|
- Layer boundary compliance MUST be verified by automated import
|
||||||
rules or architectural tests. Agent-assisted or manual review MAY
|
rules or architectural tests.
|
||||||
supplement but not replace automated checks.
|
- All automated quality gates MUST run at the earliest feasible
|
||||||
|
enforcement point (currently pre-commit via Lefthook). No gate
|
||||||
|
may exist only as a CI step or manual process.
|
||||||
|
- When a feature adds, removes, or changes user-facing capabilities
|
||||||
|
described in README.md, the README MUST be updated in the same
|
||||||
|
change. Features that materially alter what the product does or
|
||||||
|
how it is set up SHOULD also be reflected in the README.
|
||||||
|
- When a feature changes the tech stack, project structure, or
|
||||||
|
architectural patterns documented in CLAUDE.md, the CLAUDE.md
|
||||||
|
MUST be updated in the same change.
|
||||||
|
|
||||||
## Governance
|
## Governance
|
||||||
|
|
||||||
@@ -142,4 +140,4 @@ MUST comply with its principles.
|
|||||||
**Compliance review**: Every spec and plan MUST include a
|
**Compliance review**: Every spec and plan MUST include a
|
||||||
Constitution Check section validating adherence to all principles.
|
Constitution Check section validating adherence to all principles.
|
||||||
|
|
||||||
**Version**: 1.0.3 | **Ratified**: 2026-03-03 | **Last Amended**: 2026-03-03
|
**Version**: 3.0.0 | **Ratified**: 2026-03-03 | **Last Amended**: 2026-03-11
|
||||||
|
|||||||
117
CLAUDE.md
117
CLAUDE.md
@@ -5,7 +5,8 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
|
|||||||
## Commands
|
## Commands
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pnpm check # Merge gate — must pass before every commit (knip + format + lint + typecheck + test)
|
pnpm check # Merge gate — must pass before every commit (audit + knip + biome + oxlint + typecheck + test/coverage + jscpd)
|
||||||
|
pnpm oxlint # Type-aware linting (oxlint — complements Biome)
|
||||||
pnpm knip # Unused code detection (Knip)
|
pnpm knip # Unused code detection (Knip)
|
||||||
pnpm test # Run all tests (Vitest)
|
pnpm test # Run all tests (Vitest)
|
||||||
pnpm test:watch # Tests in watch mode
|
pnpm test:watch # Tests in watch mode
|
||||||
@@ -27,19 +28,90 @@ apps/web (React 19 + Vite) → packages/application (use cases) → packages
|
|||||||
```
|
```
|
||||||
|
|
||||||
- **Domain** — Pure functions, no I/O, no framework imports. All state transitions are deterministic. Errors returned as values (`DomainError`), never thrown. Adapters may throw only for programmer errors.
|
- **Domain** — Pure functions, no I/O, no framework imports. All state transitions are deterministic. Errors returned as values (`DomainError`), never thrown. Adapters may throw only for programmer errors.
|
||||||
- **Application** — Orchestrates domain calls via port interfaces (e.g., `EncounterStore`). No business logic here.
|
- **Application** — Orchestrates domain calls via port interfaces (`EncounterStore`, `BestiarySourceCache`). No business logic here.
|
||||||
- **Web** — React adapter. Implements ports using hooks/state.
|
- **Web** — React adapter. Implements ports using hooks/state. All UI components, routing, and user interaction live here.
|
||||||
|
|
||||||
Layer boundaries are enforced by `scripts/check-layer-boundaries.mjs`, which runs as a Vitest test. Domain and application must never import from React, Vite, or upper layers.
|
Layer boundaries are enforced by `scripts/check-layer-boundaries.mjs`, which runs as a Vitest test. Domain and application must never import from React, Vite, or upper layers.
|
||||||
|
|
||||||
|
### Data & Storage
|
||||||
|
|
||||||
|
- **localStorage** — encounter persistence (adapter layer, JSON serialization)
|
||||||
|
- **IndexedDB** — bestiary source cache (`apps/web/src/adapters/bestiary-cache.ts`, via `idb` wrapper)
|
||||||
|
- **`data/bestiary/index.json`** — pre-built search index for creature lookup, generated by `scripts/generate-bestiary-index.mjs`
|
||||||
|
|
||||||
|
### Project Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
apps/web/ React app — components, hooks, adapters
|
||||||
|
packages/domain/src/ Pure state transitions, types, validation
|
||||||
|
packages/application/src/ Use cases, port interfaces
|
||||||
|
data/bestiary/ Bestiary search index
|
||||||
|
scripts/ Build tooling (layer checks, index generation)
|
||||||
|
specs/NNN-feature-name/ Feature specs (spec.md, plan.md, tasks.md)
|
||||||
|
.specify/ Speckit config (templates, scripts, constitution)
|
||||||
|
docs/agents/ RPI skill artifacts (research reports, plans)
|
||||||
|
.claude/skills/ Agent skills (rpi-research, rpi-plan, rpi-implement)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Tech Stack
|
||||||
|
|
||||||
|
- TypeScript 5.8 (strict mode, `verbatimModuleSyntax`)
|
||||||
|
- React 19, Vite 6, Tailwind CSS v4
|
||||||
|
- Lucide React (icons)
|
||||||
|
- `idb` (IndexedDB wrapper for bestiary cache)
|
||||||
|
- Biome 2.4 (formatting + linting), oxlint (type-aware linting), Knip (unused code), jscpd (copy-paste detection)
|
||||||
|
- Vitest (testing, v8 coverage), Lefthook (pre-commit hooks)
|
||||||
|
|
||||||
## Conventions
|
## Conventions
|
||||||
|
|
||||||
- **Biome 2.0** for formatting and linting (no Prettier, no ESLint). Tab indentation, 80-char lines. Imports are auto-organized alphabetically.
|
- **Biome 2.4** for formatting and linting (no Prettier, no ESLint). Tab indentation, 80-char lines. Imports are auto-organized alphabetically.
|
||||||
|
- **oxlint** for type-aware linting that Biome can't do (unnecessary type assertions, deprecated APIs, `replaceAll` preference, `String.raw`). Configured in `.oxlintrc.json`.
|
||||||
- **TypeScript strict mode** with `verbatimModuleSyntax`. Use `.js` extensions in relative imports when required by the repo's ESM settings (e.g., `./types.js`).
|
- **TypeScript strict mode** with `verbatimModuleSyntax`. Use `.js` extensions in relative imports when required by the repo's ESM settings (e.g., `./types.js`).
|
||||||
- **Branded types** for identity values (e.g., `CombatantId`). Prefer immutability/`readonly` where practical.
|
- **Branded types** for identity values (e.g., `CombatantId`). Prefer immutability/`readonly` where practical.
|
||||||
- **Domain events** are plain data objects with a `type` discriminant — no classes.
|
- **Domain events** are plain data objects with a `type` discriminant — no classes.
|
||||||
- **Tests** live in `packages/*/src/__tests__/*.test.ts`. Test pure functions directly; map acceptance scenarios and invariants from specs to individual `it()` blocks.
|
- **Tests** live in `packages/*/src/__tests__/*.test.ts`. Test pure functions directly; map acceptance scenarios and invariants from specs to individual `it()` blocks.
|
||||||
- **Feature specs** live in `specs/<feature>/` with spec.md, plan.md, tasks.md. The project constitution is at `.specify/memory/constitution.md`.
|
- **Feature specs** live in `specs/NNN-feature-name/` with spec.md (and optionally plan.md, tasks.md for new work). Specs describe features, not individual changes. The project constitution is at `.specify/memory/constitution.md`.
|
||||||
|
- **Quality gates** are enforced at pre-commit via Lefthook's `pnpm check` — the project's single earliest enforcement point. No gate may exist only as a CI step or manual process.
|
||||||
|
|
||||||
|
## Self-Review Checklist
|
||||||
|
|
||||||
|
Before finishing a change, consider:
|
||||||
|
- Is this the simplest approach that solves the current problem?
|
||||||
|
- Is there duplication that hurts readability? (But don't abstract prematurely.)
|
||||||
|
- Are errors handled correctly and communicated sensibly to the user?
|
||||||
|
- Does the UI follow modern patterns and feel intuitive to interact with?
|
||||||
|
|
||||||
|
## Speckit Workflow
|
||||||
|
|
||||||
|
Speckit (`/speckit.*` skills) manages the spec-driven development pipeline. Specs are **living documents** that describe features, not individual changes.
|
||||||
|
|
||||||
|
### Issue-driven workflow
|
||||||
|
- `/write-issue` — create a well-structured Gitea issue via interactive interview
|
||||||
|
- `/integrate-issue <number>` — fetch an issue, route it to the right spec, and update the spec with the new/changed requirements. Then implement directly.
|
||||||
|
- `/sync-issue <number>` — push acceptance criteria from the spec back to the Gitea issue
|
||||||
|
|
||||||
|
### RPI skills (Research → Plan → Implement)
|
||||||
|
- `rpi-research` — deep codebase research producing a written report in `docs/agents/research/`
|
||||||
|
- `rpi-plan` — interactive phased implementation plan in `docs/agents/plans/`
|
||||||
|
- `rpi-implement` — execute a plan file phase by phase with automated + manual verification
|
||||||
|
|
||||||
|
### Choosing the right workflow by scope
|
||||||
|
|
||||||
|
| Scope | Workflow |
|
||||||
|
|---|---|
|
||||||
|
| Bug fix / CSS tweak | Just fix it, commit |
|
||||||
|
| Small change to existing feature | `/integrate-issue` → implement → commit |
|
||||||
|
| Larger addition to existing feature | `/integrate-issue` → `rpi-research` → `rpi-plan` → `rpi-implement` |
|
||||||
|
| New feature | `/speckit.specify` → `/speckit.clarify` → `/speckit.plan` → `/speckit.tasks` → `/speckit.implement` |
|
||||||
|
|
||||||
|
Speckit manages **what** to build (specs as living documents). RPI manages **how** to build it (research, planning, execution). The full speckit pipeline is for new features. For changes to existing features, update the spec via `/integrate-issue`, then use RPI skills if the change is non-trivial.
|
||||||
|
|
||||||
|
### Current feature specs
|
||||||
|
- `specs/001-combatant-management/` — CRUD, persistence, clear, batch add, confirm buttons
|
||||||
|
- `specs/002-turn-tracking/` — rounds, turn order, advance/retreat, top bar
|
||||||
|
- `specs/003-combatant-state/` — HP, AC, conditions, concentration, initiative
|
||||||
|
- `specs/004-bestiary/` — search index, stat blocks, source management, panel UX
|
||||||
|
- `specs/005-player-characters/` — persistent player character templates (CRUD), search & add to encounters, color/icon visual distinction, `PlayerCharacterStore` port
|
||||||
|
|
||||||
## Constitution (key principles)
|
## Constitution (key principles)
|
||||||
|
|
||||||
@@ -49,38 +121,11 @@ The constitution (`.specify/memory/constitution.md`) governs all feature work:
|
|||||||
2. **Layered Architecture** — Domain → Application → Adapters. Never skip layers or reverse dependencies.
|
2. **Layered Architecture** — Domain → Application → Adapters. Never skip layers or reverse dependencies.
|
||||||
3. **Clarification-First** — Ask before making non-trivial assumptions.
|
3. **Clarification-First** — Ask before making non-trivial assumptions.
|
||||||
4. **MVP Baseline** — Say "MVP baseline does not include X", never permanent bans.
|
4. **MVP Baseline** — Say "MVP baseline does not include X", never permanent bans.
|
||||||
5. **Every feature begins with a spec** — Spec → Plan → Tasks → Implementation.
|
5. **Spec-driven features** — Features are described in living specs; evolve existing specs via `/integrate-issue`, create new ones via `/speckit.specify`. Bug fixes and tooling changes do not require specs.
|
||||||
|
|
||||||
## Active Technologies
|
## Active Technologies
|
||||||
- TypeScript 5.x (strict mode, verbatimModuleSyntax) + React 19, Vite (003-remove-combatant)
|
- TypeScript 5.8 (strict mode, `verbatimModuleSyntax`) + React 19, Vite 6, Tailwind CSS v4, Lucide Reac (005-player-characters)
|
||||||
- In-memory React state (local-first, single-user MVP) (003-remove-combatant)
|
- localStorage (new key `"initiative:player-characters"`) (005-player-characters)
|
||||||
- TypeScript 5.x (project), Go binary via npm (Lefthook) + `lefthook` (npm devDependency) (006-pre-commit-gate)
|
|
||||||
- TypeScript 5.x (strict mode, verbatimModuleSyntax) + Knip v5 (new), Biome 2.0, Vitest, Vite 6, React 19 (007-add-knip)
|
|
||||||
- TypeScript 5.x (strict mode, verbatimModuleSyntax) + React 19, Vite 6, Biome 2.0, existing domain/application packages (008-persist-encounter)
|
|
||||||
- Browser localStorage (adapter layer only) (008-persist-encounter)
|
|
||||||
- TypeScript 5.x (strict mode, verbatimModuleSyntax) + React 19, Vite 6, Biome 2.0, Vites (009-combatant-hp)
|
|
||||||
- TypeScript 5.8 (strict mode, verbatimModuleSyntax) + React 19, Vite 6, Tailwind CSS v4, shadcn/ui, Lucide React (icons) (010-ui-baseline)
|
|
||||||
- N/A (no storage changes — localStorage persistence unchanged) (010-ui-baseline)
|
|
||||||
- TypeScript 5.8 (strict mode, verbatimModuleSyntax) + React 19, Vite 6, Tailwind CSS v4, shadcn/ui-style components, Lucide React (icons) (011-quick-hp-input)
|
|
||||||
- N/A (no storage changes -- existing localStorage persistence handles HP via `adjustHpUseCase`) (011-quick-hp-input)
|
|
||||||
- N/A (no storage changes -- existing localStorage persistence unchanged) (012-turn-navigation)
|
|
||||||
- N/A (no storage changes -- purely derived state, existing localStorage persistence unchanged) (013-hp-status-indicators)
|
|
||||||
- TypeScript 5.8 (strict mode, verbatimModuleSyntax) + jscpd (new dev dependency), Lefthook (existing), Biome 2.0 (existing), Knip (existing) (015-add-jscpd-gate)
|
|
||||||
- N/A (no storage changes) (015-add-jscpd-gate)
|
|
||||||
- Browser localStorage (existing adapter, transparent JSON serialization) (016-combatant-ac)
|
|
||||||
- TypeScript 5.8 (strict mode, verbatimModuleSyntax) + React 19, Vite 6, Tailwind CSS v4, Lucide React (icons) (017-combat-conditions)
|
|
||||||
- N/A (no storage changes — existing localStorage persistence unchanged) (019-combatant-row-declutter)
|
|
||||||
- TypeScript 5.8 (strict mode, verbatimModuleSyntax) + React 19, Tailwind CSS v4, Lucide React (icons) (020-fix-zero-hp-opacity)
|
|
||||||
- Browser localStorage (existing adapter, extended for creatureId) (021-bestiary-statblock)
|
|
||||||
- TypeScript 5.8 (strict mode, verbatimModuleSyntax) + React 19, Tailwind CSS v4 + React 19, Tailwind CSS v4, Vite 6 (022-fixed-layout-bars)
|
|
||||||
- N/A (no storage changes -- purely presentational) (022-fixed-layout-bars)
|
|
||||||
- Browser localStorage (existing adapter, updated to handle empty encounters) (023-clear-encounter)
|
|
||||||
- N/A (no storage changes — purely presentational fix) (024-fix-hp-popover-overflow)
|
|
||||||
- N/A (no storage changes — purely derived from existing bestiary data) (025-display-initiative)
|
|
||||||
- TypeScript 5.8 (strict mode, verbatimModuleSyntax) + React 19, Tailwind CSS v4, Lucide React (icons), Vite 6 (026-roll-initiative)
|
|
||||||
- N/A (no storage changes — existing localStorage persistence handles initiative via `setInitiativeUseCase`) (026-roll-initiative)
|
|
||||||
- N/A (no storage changes — purely presentational) (027-ui-polish)
|
|
||||||
- TypeScript 5.8 (strict mode, verbatimModuleSyntax) + React 19, Tailwind CSS v4, Vite 6 + Tailwind CSS v4 (CSS-native `@theme` theming), Lucide React (icons) (028-semantic-hover-tokens)
|
|
||||||
|
|
||||||
## Recent Changes
|
## Recent Changes
|
||||||
- 003-remove-combatant: Added TypeScript 5.x (strict mode, verbatimModuleSyntax) + React 19, Vite
|
- 005-player-characters: Added TypeScript 5.8 (strict mode, `verbatimModuleSyntax`) + React 19, Vite 6, Tailwind CSS v4, Lucide Reac
|
||||||
|
|||||||
19
Dockerfile
Normal file
19
Dockerfile
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
FROM node:22-slim AS build
|
||||||
|
ENV PNPM_HOME="/pnpm"
|
||||||
|
ENV PATH="$PNPM_HOME:$PATH"
|
||||||
|
RUN corepack enable
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
|
||||||
|
COPY packages/domain/package.json packages/domain/
|
||||||
|
COPY packages/application/package.json packages/application/
|
||||||
|
COPY apps/web/package.json apps/web/
|
||||||
|
RUN pnpm install --frozen-lockfile --ignore-scripts
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN pnpm --filter web build
|
||||||
|
|
||||||
|
FROM nginx:alpine
|
||||||
|
COPY --from=build /app/apps/web/dist /usr/share/nginx/html
|
||||||
|
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||||
|
EXPOSE 80
|
||||||
44
README.md
44
README.md
@@ -1,10 +1,18 @@
|
|||||||
# Initiative Tracker
|
# Encounter Console
|
||||||
|
|
||||||
A turn-based initiative tracker for tabletop RPG encounters. Click "Next Turn" to cycle through combatants and advance rounds.
|
A local-first initiative tracker and encounter manager for tabletop RPGs (D&D 5e / 2024). Runs entirely in the browser — no server, no account, no data leaves your machine.
|
||||||
|
|
||||||
|
## What it does
|
||||||
|
|
||||||
|
- **Initiative tracking** — add combatants (batch-add from bestiary, custom creatures with optional stats), roll initiative (manual or d20), cycle turns and rounds
|
||||||
|
- **Encounter state** — HP, AC, conditions, concentration tracking with visual status indicators
|
||||||
|
- **Bestiary integration** — import bestiary JSON sources, search creatures, and view full stat blocks
|
||||||
|
- **Player characters** — create reusable player character templates with name, AC, HP, color, and icon; search and add them to encounters with pre-filled stats; manage (edit/delete) from a dedicated panel
|
||||||
|
- **Persistent** — encounters survive page reloads via localStorage; bestiary data cached in IndexedDB; player characters stored independently
|
||||||
|
|
||||||
## Prerequisites
|
## Prerequisites
|
||||||
|
|
||||||
- Node.js 22
|
- Node.js 22+
|
||||||
- pnpm 10.6+
|
- pnpm 10.6+
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
@@ -14,9 +22,7 @@ pnpm install
|
|||||||
pnpm --filter web dev
|
pnpm --filter web dev
|
||||||
```
|
```
|
||||||
|
|
||||||
Open the URL printed in your terminal (typically `http://localhost:5173`).
|
Open `http://localhost:5173`.
|
||||||
|
|
||||||
The app starts with a 3-combatant demo encounter (Aria, Brak, Cael). Click **Next Turn** to advance through the initiative order. When the last combatant finishes their turn, the round number increments and the cycle restarts.
|
|
||||||
|
|
||||||
## Scripts
|
## Scripts
|
||||||
|
|
||||||
@@ -24,5 +30,27 @@ The app starts with a 3-combatant demo encounter (Aria, Brak, Cael). Click **Nex
|
|||||||
|---------|-------------|
|
|---------|-------------|
|
||||||
| `pnpm --filter web dev` | Start the dev server |
|
| `pnpm --filter web dev` | Start the dev server |
|
||||||
| `pnpm --filter web build` | Production build |
|
| `pnpm --filter web build` | Production build |
|
||||||
| `pnpm test` | Run all tests |
|
| `pnpm test` | Run all tests (Vitest) |
|
||||||
| `pnpm check` | Full merge gate (format, lint, typecheck, test) |
|
| `pnpm check` | Full merge gate (knip, biome, typecheck, test, jscpd) |
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
apps/web/ React 19 + Vite — UI components, hooks, adapters
|
||||||
|
packages/domain/ Pure functions — state transitions, types, validation
|
||||||
|
packages/app/ Use cases — orchestrates domain via port interfaces
|
||||||
|
data/bestiary/ Bestiary index for creature search
|
||||||
|
scripts/ Build tooling (layer boundary checks, index generation)
|
||||||
|
specs/ Feature specifications (spec → plan → tasks)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
Strict layered architecture with enforced dependency direction:
|
||||||
|
|
||||||
|
```
|
||||||
|
apps/web (adapters) → packages/application (use cases) → packages/domain (pure logic)
|
||||||
|
```
|
||||||
|
|
||||||
|
Domain is pure — no I/O, no randomness, no framework imports. Layer boundaries are enforced by automated import checks that run as part of the test suite. See [CLAUDE.md](./CLAUDE.md) for full conventions.
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
"@initiative/domain": "workspace:*",
|
"@initiative/domain": "workspace:*",
|
||||||
"class-variance-authority": "^0.7.1",
|
"class-variance-authority": "^0.7.1",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
|
"idb": "^8.0.3",
|
||||||
"lucide-react": "^0.577.0",
|
"lucide-react": "^0.577.0",
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
"react-dom": "^19.0.0",
|
"react-dom": "^19.0.0",
|
||||||
@@ -20,9 +21,13 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@tailwindcss/vite": "^4.2.1",
|
"@tailwindcss/vite": "^4.2.1",
|
||||||
|
"@testing-library/jest-dom": "^6.9.1",
|
||||||
|
"@testing-library/react": "^16.3.2",
|
||||||
|
"@testing-library/user-event": "^14.6.1",
|
||||||
"@types/react": "^19.0.0",
|
"@types/react": "^19.0.0",
|
||||||
"@types/react-dom": "^19.0.0",
|
"@types/react-dom": "^19.0.0",
|
||||||
"@vitejs/plugin-react": "^4.3.0",
|
"@vitejs/plugin-react": "^4.3.0",
|
||||||
|
"jsdom": "^28.1.0",
|
||||||
"tailwindcss": "^4.2.1",
|
"tailwindcss": "^4.2.1",
|
||||||
"vite": "^6.2.0"
|
"vite": "^6.2.0"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,22 +2,84 @@ import {
|
|||||||
rollAllInitiativeUseCase,
|
rollAllInitiativeUseCase,
|
||||||
rollInitiativeUseCase,
|
rollInitiativeUseCase,
|
||||||
} from "@initiative/application";
|
} from "@initiative/application";
|
||||||
import type { CombatantId, Creature, CreatureId } from "@initiative/domain";
|
import {
|
||||||
import { useCallback, useEffect, useRef, useState } from "react";
|
type CombatantId,
|
||||||
|
type Creature,
|
||||||
|
type CreatureId,
|
||||||
|
isDomainError,
|
||||||
|
} from "@initiative/domain";
|
||||||
|
import {
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useLayoutEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from "react";
|
||||||
import { ActionBar } from "./components/action-bar";
|
import { ActionBar } from "./components/action-bar";
|
||||||
|
import { BulkImportToasts } from "./components/bulk-import-toasts";
|
||||||
import { CombatantRow } from "./components/combatant-row";
|
import { CombatantRow } from "./components/combatant-row";
|
||||||
|
import {
|
||||||
|
PlayerCharacterSection,
|
||||||
|
type PlayerCharacterSectionHandle,
|
||||||
|
} from "./components/player-character-section";
|
||||||
import { StatBlockPanel } from "./components/stat-block-panel";
|
import { StatBlockPanel } from "./components/stat-block-panel";
|
||||||
|
import { Toast } from "./components/toast";
|
||||||
import { TurnNavigation } from "./components/turn-navigation";
|
import { TurnNavigation } from "./components/turn-navigation";
|
||||||
import { useBestiary } from "./hooks/use-bestiary";
|
import { type SearchResult, useBestiary } from "./hooks/use-bestiary";
|
||||||
|
import { useBulkImport } from "./hooks/use-bulk-import";
|
||||||
import { useEncounter } from "./hooks/use-encounter";
|
import { useEncounter } from "./hooks/use-encounter";
|
||||||
|
import { usePlayerCharacters } from "./hooks/use-player-characters";
|
||||||
|
import { useSidePanelState } from "./hooks/use-side-panel-state";
|
||||||
|
import { useTheme } from "./hooks/use-theme";
|
||||||
|
import { cn } from "./lib/utils";
|
||||||
|
|
||||||
function rollDice(): number {
|
function rollDice(): number {
|
||||||
return Math.floor(Math.random() * 20) + 1;
|
return Math.floor(Math.random() * 20) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function useActionBarAnimation(combatantCount: number) {
|
||||||
|
const wasEmptyRef = useRef(combatantCount === 0);
|
||||||
|
const [settling, setSettling] = useState(false);
|
||||||
|
const [rising, setRising] = useState(false);
|
||||||
|
const [topBarExiting, setTopBarExiting] = useState(false);
|
||||||
|
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
const nowEmpty = combatantCount === 0;
|
||||||
|
if (wasEmptyRef.current && !nowEmpty) {
|
||||||
|
setSettling(true);
|
||||||
|
} else if (!wasEmptyRef.current && nowEmpty) {
|
||||||
|
setRising(true);
|
||||||
|
setTopBarExiting(true);
|
||||||
|
}
|
||||||
|
wasEmptyRef.current = nowEmpty;
|
||||||
|
}, [combatantCount]);
|
||||||
|
|
||||||
|
const empty = combatantCount === 0;
|
||||||
|
const risingClass = rising ? "animate-rise-to-center" : "";
|
||||||
|
const settlingClass = settling ? "animate-settle-to-bottom" : "";
|
||||||
|
const exitingClass = topBarExiting
|
||||||
|
? "absolute inset-x-0 top-0 z-10 px-4 animate-slide-up-out"
|
||||||
|
: "";
|
||||||
|
const topBarClass = settling ? "animate-slide-down-in" : exitingClass;
|
||||||
|
const showTopBar = !empty || topBarExiting;
|
||||||
|
|
||||||
|
return {
|
||||||
|
risingClass,
|
||||||
|
settlingClass,
|
||||||
|
topBarClass,
|
||||||
|
showTopBar,
|
||||||
|
onSettleEnd: () => setSettling(false),
|
||||||
|
onRiseEnd: () => setRising(false),
|
||||||
|
onTopBarExitEnd: () => setTopBarExiting(false),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function App() {
|
export function App() {
|
||||||
const {
|
const {
|
||||||
encounter,
|
encounter,
|
||||||
|
isEmpty,
|
||||||
|
hasCreatureCombatants,
|
||||||
|
canRollAllInitiative,
|
||||||
advanceTurn,
|
advanceTurn,
|
||||||
retreatTurn,
|
retreatTurn,
|
||||||
addCombatant,
|
addCombatant,
|
||||||
@@ -31,58 +93,127 @@ export function App() {
|
|||||||
toggleCondition,
|
toggleCondition,
|
||||||
toggleConcentration,
|
toggleConcentration,
|
||||||
addFromBestiary,
|
addFromBestiary,
|
||||||
|
addFromPlayerCharacter,
|
||||||
makeStore,
|
makeStore,
|
||||||
} = useEncounter();
|
} = useEncounter();
|
||||||
|
|
||||||
const { search, getCreature, isLoaded } = useBestiary();
|
const {
|
||||||
|
characters: playerCharacters,
|
||||||
|
createCharacter: createPlayerCharacter,
|
||||||
|
editCharacter: editPlayerCharacter,
|
||||||
|
deleteCharacter: deletePlayerCharacter,
|
||||||
|
} = usePlayerCharacters();
|
||||||
|
|
||||||
const [selectedCreature, setSelectedCreature] = useState<Creature | null>(
|
const {
|
||||||
null,
|
search,
|
||||||
);
|
getCreature,
|
||||||
const [suggestions, setSuggestions] = useState<Creature[]>([]);
|
isLoaded,
|
||||||
|
isSourceCached,
|
||||||
|
fetchAndCacheSource,
|
||||||
|
uploadAndCacheSource,
|
||||||
|
refreshCache,
|
||||||
|
} = useBestiary();
|
||||||
|
|
||||||
|
const bulkImport = useBulkImport();
|
||||||
|
const sidePanel = useSidePanelState();
|
||||||
|
const { preference: themePreference, cycleTheme } = useTheme();
|
||||||
|
|
||||||
|
const [rollSkippedCount, setRollSkippedCount] = useState(0);
|
||||||
|
const [rollSingleSkipped, setRollSingleSkipped] = useState(false);
|
||||||
|
|
||||||
|
const selectedCreature: Creature | null = sidePanel.selectedCreatureId
|
||||||
|
? (getCreature(sidePanel.selectedCreatureId) ?? null)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
const pinnedCreature: Creature | null = sidePanel.pinnedCreatureId
|
||||||
|
? (getCreature(sidePanel.pinnedCreatureId) ?? null)
|
||||||
|
: null;
|
||||||
|
|
||||||
const handleAddFromBestiary = useCallback(
|
const handleAddFromBestiary = useCallback(
|
||||||
(creature: Creature) => {
|
(result: SearchResult) => {
|
||||||
addFromBestiary(creature);
|
const creatureId = addFromBestiary(result);
|
||||||
setSelectedCreature(creature);
|
if (creatureId && sidePanel.panelView.mode === "closed") {
|
||||||
|
sidePanel.showCreature(creatureId);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[addFromBestiary],
|
[addFromBestiary, sidePanel.panelView.mode, sidePanel.showCreature],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleShowStatBlock = useCallback((creature: Creature) => {
|
|
||||||
setSelectedCreature(creature);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handleCombatantStatBlock = useCallback(
|
const handleCombatantStatBlock = useCallback(
|
||||||
(creatureId: string) => {
|
(creatureId: string) => {
|
||||||
const creature = getCreature(creatureId as CreatureId);
|
sidePanel.showCreature(creatureId as CreatureId);
|
||||||
if (creature) setSelectedCreature(creature);
|
|
||||||
},
|
},
|
||||||
[getCreature],
|
[sidePanel.showCreature],
|
||||||
);
|
|
||||||
|
|
||||||
const handleSearchChange = useCallback(
|
|
||||||
(query: string) => {
|
|
||||||
if (!isLoaded || query.length < 2) {
|
|
||||||
setSuggestions([]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setSuggestions(search(query));
|
|
||||||
},
|
|
||||||
[isLoaded, search],
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleRollInitiative = useCallback(
|
const handleRollInitiative = useCallback(
|
||||||
(id: CombatantId) => {
|
(id: CombatantId) => {
|
||||||
rollInitiativeUseCase(makeStore(), id, rollDice(), getCreature);
|
const result = rollInitiativeUseCase(
|
||||||
|
makeStore(),
|
||||||
|
id,
|
||||||
|
rollDice(),
|
||||||
|
getCreature,
|
||||||
|
);
|
||||||
|
if (isDomainError(result)) {
|
||||||
|
setRollSingleSkipped(true);
|
||||||
|
const combatant = encounter.combatants.find((c) => c.id === id);
|
||||||
|
if (combatant?.creatureId) {
|
||||||
|
sidePanel.showCreature(combatant.creatureId);
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[makeStore, getCreature],
|
[makeStore, getCreature, encounter.combatants, sidePanel.showCreature],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleRollAllInitiative = useCallback(() => {
|
const handleRollAllInitiative = useCallback(() => {
|
||||||
rollAllInitiativeUseCase(makeStore(), rollDice, getCreature);
|
const result = rollAllInitiativeUseCase(makeStore(), rollDice, getCreature);
|
||||||
|
if (!isDomainError(result) && result.skippedNoSource > 0) {
|
||||||
|
setRollSkippedCount(result.skippedNoSource);
|
||||||
|
}
|
||||||
}, [makeStore, getCreature]);
|
}, [makeStore, getCreature]);
|
||||||
|
|
||||||
|
const handleViewStatBlock = useCallback(
|
||||||
|
(result: SearchResult) => {
|
||||||
|
const slug = result.name
|
||||||
|
.toLowerCase()
|
||||||
|
.replaceAll(/[^a-z0-9]+/g, "-")
|
||||||
|
.replaceAll(/(^-|-$)/g, "");
|
||||||
|
const cId = `${result.source.toLowerCase()}:${slug}` as CreatureId;
|
||||||
|
sidePanel.showCreature(cId);
|
||||||
|
},
|
||||||
|
[sidePanel.showCreature],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleStartBulkImport = useCallback(
|
||||||
|
(baseUrl: string) => {
|
||||||
|
bulkImport.startImport(
|
||||||
|
baseUrl,
|
||||||
|
fetchAndCacheSource,
|
||||||
|
isSourceCached,
|
||||||
|
refreshCache,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
[bulkImport.startImport, fetchAndCacheSource, isSourceCached, refreshCache],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleBulkImportDone = useCallback(() => {
|
||||||
|
sidePanel.dismissPanel();
|
||||||
|
bulkImport.reset();
|
||||||
|
}, [sidePanel.dismissPanel, bulkImport.reset]);
|
||||||
|
|
||||||
|
const actionBarInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
const playerCharacterRef = useRef<PlayerCharacterSectionHandle>(null);
|
||||||
|
const actionBarAnim = useActionBarAnimation(encounter.combatants.length);
|
||||||
|
|
||||||
|
// Auto-update stat block panel when the active combatant changes
|
||||||
|
const activeCreatureId =
|
||||||
|
encounter.combatants[encounter.activeIndex]?.creatureId;
|
||||||
|
useEffect(() => {
|
||||||
|
if (activeCreatureId && sidePanel.panelView.mode === "creature") {
|
||||||
|
sidePanel.showCreature(activeCreatureId);
|
||||||
|
}
|
||||||
|
}, [activeCreatureId, sidePanel.panelView.mode, sidePanel.showCreature]);
|
||||||
|
|
||||||
// Auto-scroll to the active combatant when the turn changes
|
// Auto-scroll to the active combatant when the turn changes
|
||||||
const activeRowRef = useRef<HTMLDivElement>(null);
|
const activeRowRef = useRef<HTMLDivElement>(null);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -90,45 +221,62 @@ export function App() {
|
|||||||
block: "nearest",
|
block: "nearest",
|
||||||
behavior: "smooth",
|
behavior: "smooth",
|
||||||
});
|
});
|
||||||
}, [encounter.activeIndex]);
|
}, []);
|
||||||
|
|
||||||
// Auto-show stat block for the active combatant when turn changes,
|
|
||||||
// but only when the viewport is wide enough to show it alongside the tracker.
|
|
||||||
// Only react to activeIndex changes — not combatant reordering (e.g. Roll All).
|
|
||||||
const prevActiveIndexRef = useRef(encounter.activeIndex);
|
|
||||||
useEffect(() => {
|
|
||||||
if (prevActiveIndexRef.current === encounter.activeIndex) return;
|
|
||||||
prevActiveIndexRef.current = encounter.activeIndex;
|
|
||||||
if (!window.matchMedia("(min-width: 1024px)").matches) return;
|
|
||||||
const active = encounter.combatants[encounter.activeIndex];
|
|
||||||
if (!active?.creatureId || !isLoaded) return;
|
|
||||||
const creature = getCreature(active.creatureId as CreatureId);
|
|
||||||
if (creature) setSelectedCreature(creature);
|
|
||||||
}, [encounter.activeIndex, encounter.combatants, getCreature, isLoaded]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex h-screen flex-col">
|
<div className="flex h-dvh flex-col">
|
||||||
<div className="mx-auto flex w-full max-w-2xl flex-1 flex-col gap-3 px-4 min-h-0">
|
<div className="relative mx-auto flex min-h-0 w-full max-w-2xl flex-1 flex-col gap-3 px-4">
|
||||||
{/* Turn Navigation — fixed at top */}
|
{!!actionBarAnim.showTopBar && (
|
||||||
<div className="shrink-0 pt-8">
|
<div
|
||||||
|
className={cn("shrink-0 pt-8", actionBarAnim.topBarClass)}
|
||||||
|
onAnimationEnd={actionBarAnim.onTopBarExitEnd}
|
||||||
|
>
|
||||||
<TurnNavigation
|
<TurnNavigation
|
||||||
encounter={encounter}
|
encounter={encounter}
|
||||||
onAdvanceTurn={advanceTurn}
|
onAdvanceTurn={advanceTurn}
|
||||||
onRetreatTurn={retreatTurn}
|
onRetreatTurn={retreatTurn}
|
||||||
onClearEncounter={clearEncounter}
|
onClearEncounter={clearEncounter}
|
||||||
onRollAllInitiative={handleRollAllInitiative}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Scrollable area — combatant list */}
|
{isEmpty ? (
|
||||||
<div className="flex-1 overflow-y-auto min-h-0">
|
/* Empty state — ActionBar centered */
|
||||||
<div className="flex flex-col pb-2">
|
<div className="flex min-h-0 flex-1 items-center justify-center pt-8 pb-[15%]">
|
||||||
{encounter.combatants.length === 0 ? (
|
<div
|
||||||
<p className="py-12 text-center text-sm text-muted-foreground">
|
className={cn("w-full", actionBarAnim.risingClass)}
|
||||||
No combatants yet — add one to get started
|
onAnimationEnd={actionBarAnim.onRiseEnd}
|
||||||
</p>
|
>
|
||||||
|
<ActionBar
|
||||||
|
onAddCombatant={addCombatant}
|
||||||
|
onAddFromBestiary={handleAddFromBestiary}
|
||||||
|
bestiarySearch={search}
|
||||||
|
bestiaryLoaded={isLoaded}
|
||||||
|
onViewStatBlock={handleViewStatBlock}
|
||||||
|
onBulkImport={sidePanel.showBulkImport}
|
||||||
|
bulkImportDisabled={bulkImport.state.status === "loading"}
|
||||||
|
inputRef={actionBarInputRef}
|
||||||
|
playerCharacters={playerCharacters}
|
||||||
|
onAddFromPlayerCharacter={addFromPlayerCharacter}
|
||||||
|
onManagePlayers={() =>
|
||||||
|
playerCharacterRef.current?.openManagement()
|
||||||
|
}
|
||||||
|
onRollAllInitiative={handleRollAllInitiative}
|
||||||
|
showRollAllInitiative={hasCreatureCombatants}
|
||||||
|
rollAllInitiativeDisabled={!canRollAllInitiative}
|
||||||
|
onOpenSourceManager={sidePanel.showSourceManager}
|
||||||
|
themePreference={themePreference}
|
||||||
|
onCycleTheme={cycleTheme}
|
||||||
|
autoFocus
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
) : (
|
) : (
|
||||||
encounter.combatants.map((c, i) => (
|
<>
|
||||||
|
{/* Scrollable area — combatant list */}
|
||||||
|
<div className="min-h-0 flex-1 overflow-y-auto">
|
||||||
|
<div className="flex flex-col px-2 py-2">
|
||||||
|
{encounter.combatants.map((c, i) => (
|
||||||
<CombatantRow
|
<CombatantRow
|
||||||
key={c.id}
|
key={c.id}
|
||||||
ref={i === encounter.activeIndex ? activeRowRef : null}
|
ref={i === encounter.activeIndex ? activeRowRef : null}
|
||||||
@@ -147,33 +295,119 @@ export function App() {
|
|||||||
? () => handleCombatantStatBlock(c.creatureId as string)
|
? () => handleCombatantStatBlock(c.creatureId as string)
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
|
isStatBlockOpen={
|
||||||
|
c.creatureId === sidePanel.selectedCreatureId
|
||||||
|
}
|
||||||
onRollInitiative={
|
onRollInitiative={
|
||||||
c.creatureId ? handleRollInitiative : undefined
|
c.creatureId ? handleRollInitiative : undefined
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
))
|
))}
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Action Bar — fixed at bottom */}
|
{/* Action Bar — fixed at bottom */}
|
||||||
<div className="shrink-0 pb-8">
|
<div
|
||||||
|
className={cn("shrink-0 pb-8", actionBarAnim.settlingClass)}
|
||||||
|
onAnimationEnd={actionBarAnim.onSettleEnd}
|
||||||
|
>
|
||||||
<ActionBar
|
<ActionBar
|
||||||
onAddCombatant={addCombatant}
|
onAddCombatant={addCombatant}
|
||||||
onAddFromBestiary={handleAddFromBestiary}
|
onAddFromBestiary={handleAddFromBestiary}
|
||||||
bestiarySearch={search}
|
bestiarySearch={search}
|
||||||
bestiaryLoaded={isLoaded}
|
bestiaryLoaded={isLoaded}
|
||||||
suggestions={suggestions}
|
onViewStatBlock={handleViewStatBlock}
|
||||||
onSearchChange={handleSearchChange}
|
onBulkImport={sidePanel.showBulkImport}
|
||||||
onShowStatBlock={handleShowStatBlock}
|
bulkImportDisabled={bulkImport.state.status === "loading"}
|
||||||
|
inputRef={actionBarInputRef}
|
||||||
|
playerCharacters={playerCharacters}
|
||||||
|
onAddFromPlayerCharacter={addFromPlayerCharacter}
|
||||||
|
onManagePlayers={() =>
|
||||||
|
playerCharacterRef.current?.openManagement()
|
||||||
|
}
|
||||||
|
onRollAllInitiative={handleRollAllInitiative}
|
||||||
|
showRollAllInitiative={hasCreatureCombatants}
|
||||||
|
rollAllInitiativeDisabled={!canRollAllInitiative}
|
||||||
|
onOpenSourceManager={sidePanel.showSourceManager}
|
||||||
|
themePreference={themePreference}
|
||||||
|
onCycleTheme={cycleTheme}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Stat Block Panel */}
|
{/* Pinned Stat Block Panel (left) */}
|
||||||
|
{!!sidePanel.pinnedCreatureId && sidePanel.isWideDesktop && (
|
||||||
<StatBlockPanel
|
<StatBlockPanel
|
||||||
|
creatureId={sidePanel.pinnedCreatureId}
|
||||||
|
creature={pinnedCreature}
|
||||||
|
isSourceCached={isSourceCached}
|
||||||
|
fetchAndCacheSource={fetchAndCacheSource}
|
||||||
|
uploadAndCacheSource={uploadAndCacheSource}
|
||||||
|
refreshCache={refreshCache}
|
||||||
|
panelRole="pinned"
|
||||||
|
isCollapsed={false}
|
||||||
|
onToggleCollapse={() => {}}
|
||||||
|
onPin={() => {}}
|
||||||
|
onUnpin={sidePanel.unpin}
|
||||||
|
showPinButton={false}
|
||||||
|
side="left"
|
||||||
|
onDismiss={() => {}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Browse Stat Block Panel (right) */}
|
||||||
|
<StatBlockPanel
|
||||||
|
creatureId={sidePanel.selectedCreatureId}
|
||||||
creature={selectedCreature}
|
creature={selectedCreature}
|
||||||
onClose={() => setSelectedCreature(null)}
|
isSourceCached={isSourceCached}
|
||||||
|
fetchAndCacheSource={fetchAndCacheSource}
|
||||||
|
uploadAndCacheSource={uploadAndCacheSource}
|
||||||
|
refreshCache={refreshCache}
|
||||||
|
panelRole="browse"
|
||||||
|
isCollapsed={sidePanel.isRightPanelCollapsed}
|
||||||
|
onToggleCollapse={sidePanel.toggleCollapse}
|
||||||
|
onPin={sidePanel.togglePin}
|
||||||
|
onUnpin={() => {}}
|
||||||
|
showPinButton={sidePanel.isWideDesktop && !!selectedCreature}
|
||||||
|
side="right"
|
||||||
|
onDismiss={sidePanel.dismissPanel}
|
||||||
|
bulkImportMode={sidePanel.bulkImportMode}
|
||||||
|
bulkImportState={bulkImport.state}
|
||||||
|
onStartBulkImport={handleStartBulkImport}
|
||||||
|
onBulkImportDone={handleBulkImportDone}
|
||||||
|
sourceManagerMode={sidePanel.sourceManagerMode}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<BulkImportToasts
|
||||||
|
state={bulkImport.state}
|
||||||
|
visible={!sidePanel.bulkImportMode || sidePanel.isRightPanelCollapsed}
|
||||||
|
onReset={bulkImport.reset}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{rollSkippedCount > 0 && (
|
||||||
|
<Toast
|
||||||
|
message={`${rollSkippedCount} skipped — bestiary source not loaded`}
|
||||||
|
onDismiss={() => setRollSkippedCount(0)}
|
||||||
|
autoDismissMs={4000}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!!rollSingleSkipped && (
|
||||||
|
<Toast
|
||||||
|
message="Can't roll — bestiary source not loaded"
|
||||||
|
onDismiss={() => setRollSingleSkipped(false)}
|
||||||
|
autoDismissMs={4000}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<PlayerCharacterSection
|
||||||
|
ref={playerCharacterRef}
|
||||||
|
characters={playerCharacters}
|
||||||
|
onCreateCharacter={createPlayerCharacter}
|
||||||
|
onEditCharacter={editPlayerCharacter}
|
||||||
|
onDeleteCharacter={deletePlayerCharacter}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
163
apps/web/src/__tests__/app-integration.test.tsx
Normal file
163
apps/web/src/__tests__/app-integration.test.tsx
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import "@testing-library/jest-dom/vitest";
|
||||||
|
|
||||||
|
import { cleanup, render, screen } from "@testing-library/react";
|
||||||
|
import userEvent from "@testing-library/user-event";
|
||||||
|
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
||||||
|
import { App } from "../App";
|
||||||
|
|
||||||
|
// Mock persistence — no localStorage interaction
|
||||||
|
vi.mock("../persistence/encounter-storage.js", () => ({
|
||||||
|
loadEncounter: () => null,
|
||||||
|
saveEncounter: () => {},
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../persistence/player-character-storage.js", () => ({
|
||||||
|
loadPlayerCharacters: () => [],
|
||||||
|
savePlayerCharacters: () => {},
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Mock bestiary — no IndexedDB or JSON index
|
||||||
|
vi.mock("../adapters/bestiary-cache.js", () => ({
|
||||||
|
loadAllCachedCreatures: () => Promise.resolve(new Map()),
|
||||||
|
isSourceCached: () => Promise.resolve(false),
|
||||||
|
cacheSource: () => Promise.resolve(),
|
||||||
|
getCachedSources: () => Promise.resolve([]),
|
||||||
|
clearSource: () => Promise.resolve(),
|
||||||
|
clearAll: () => Promise.resolve(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../adapters/bestiary-index-adapter.js", () => ({
|
||||||
|
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }),
|
||||||
|
getAllSourceCodes: () => [],
|
||||||
|
getDefaultFetchUrl: () => "",
|
||||||
|
getSourceDisplayName: (code: string) => code,
|
||||||
|
}));
|
||||||
|
|
||||||
|
// DOM API stubs — jsdom doesn't implement these
|
||||||
|
beforeAll(() => {
|
||||||
|
Object.defineProperty(globalThis, "matchMedia", {
|
||||||
|
writable: true,
|
||||||
|
value: vi.fn().mockImplementation((query: string) => ({
|
||||||
|
matches: false,
|
||||||
|
media: query,
|
||||||
|
onchange: null,
|
||||||
|
addListener: vi.fn(),
|
||||||
|
removeListener: vi.fn(),
|
||||||
|
addEventListener: vi.fn(),
|
||||||
|
removeEventListener: vi.fn(),
|
||||||
|
dispatchEvent: vi.fn(),
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
Element.prototype.scrollIntoView = vi.fn();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
async function addCombatant(
|
||||||
|
user: ReturnType<typeof userEvent.setup>,
|
||||||
|
name: string,
|
||||||
|
opts?: { maxHp?: string },
|
||||||
|
) {
|
||||||
|
const inputs = screen.getAllByPlaceholderText("+ Add combatants");
|
||||||
|
// biome-ignore lint/style/noNonNullAssertion: getAllBy always returns at least one
|
||||||
|
const input = inputs.at(-1)!;
|
||||||
|
await user.type(input, name);
|
||||||
|
|
||||||
|
if (opts?.maxHp) {
|
||||||
|
const maxHpInput = screen.getByPlaceholderText("MaxHP");
|
||||||
|
await user.type(maxHpInput, opts.maxHp);
|
||||||
|
}
|
||||||
|
|
||||||
|
const addButton = screen.getByRole("button", { name: "Add" });
|
||||||
|
await user.click(addButton);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("App integration", () => {
|
||||||
|
it("adds a combatant and removes it, returning to empty state", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
render(<App />);
|
||||||
|
|
||||||
|
// Empty state: centered input visible, no TurnNavigation
|
||||||
|
expect(screen.getByPlaceholderText("+ Add combatants")).toBeInTheDocument();
|
||||||
|
expect(screen.queryByText("R1")).not.toBeInTheDocument();
|
||||||
|
|
||||||
|
// Add a combatant
|
||||||
|
await addCombatant(user, "Goblin");
|
||||||
|
|
||||||
|
// Verify combatant appears and TurnNavigation shows
|
||||||
|
expect(screen.getByRole("button", { name: "Goblin" })).toBeInTheDocument();
|
||||||
|
expect(screen.getByText("R1")).toBeInTheDocument();
|
||||||
|
expect(screen.getAllByText("Goblin").length).toBeGreaterThanOrEqual(2);
|
||||||
|
|
||||||
|
// Remove combatant via ConfirmButton (two clicks)
|
||||||
|
const removeBtn = screen.getByRole("button", {
|
||||||
|
name: "Remove combatant",
|
||||||
|
});
|
||||||
|
await user.click(removeBtn);
|
||||||
|
const confirmBtn = screen.getByRole("button", {
|
||||||
|
name: "Confirm remove combatant",
|
||||||
|
});
|
||||||
|
await user.click(confirmBtn);
|
||||||
|
|
||||||
|
// Back to empty state (R1 badge may linger due to exit animation in jsdom)
|
||||||
|
expect(
|
||||||
|
screen.queryByRole("button", { name: "Goblin" }),
|
||||||
|
).not.toBeInTheDocument();
|
||||||
|
expect(screen.getByPlaceholderText("+ Add combatants")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("advances and retreats turns across two combatants", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
render(<App />);
|
||||||
|
|
||||||
|
await addCombatant(user, "Fighter");
|
||||||
|
await addCombatant(user, "Wizard");
|
||||||
|
|
||||||
|
// Initial state — R1, Fighter active (Previous turn disabled)
|
||||||
|
expect(screen.getByText("R1")).toBeInTheDocument();
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Previous turn" }),
|
||||||
|
).toBeDisabled();
|
||||||
|
|
||||||
|
// Advance turn — Wizard becomes active
|
||||||
|
await user.click(screen.getByRole("button", { name: "Next turn" }));
|
||||||
|
expect(screen.getByText("R1")).toBeInTheDocument();
|
||||||
|
expect(screen.getByRole("button", { name: "Previous turn" })).toBeEnabled();
|
||||||
|
|
||||||
|
// Advance again — wraps to R2, Fighter active
|
||||||
|
await user.click(screen.getByRole("button", { name: "Next turn" }));
|
||||||
|
expect(screen.getByText("R2")).toBeInTheDocument();
|
||||||
|
expect(screen.getByRole("button", { name: "Previous turn" })).toBeEnabled();
|
||||||
|
|
||||||
|
// Retreat — back to R1, Wizard active
|
||||||
|
await user.click(screen.getByRole("button", { name: "Previous turn" }));
|
||||||
|
expect(screen.getByText("R1")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("adds a combatant with HP, applies damage, and shows unconscious state", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
render(<App />);
|
||||||
|
|
||||||
|
await addCombatant(user, "Ogre", { maxHp: "59" });
|
||||||
|
|
||||||
|
// Verify HP displays — currentHp and maxHp both show "59"
|
||||||
|
expect(screen.getByText("/")).toBeInTheDocument();
|
||||||
|
const hpButton = screen.getByRole("button", {
|
||||||
|
name: "Current HP: 59 (healthy)",
|
||||||
|
});
|
||||||
|
expect(hpButton).toBeInTheDocument();
|
||||||
|
|
||||||
|
// Click currentHp to open HpAdjustPopover, apply full damage
|
||||||
|
await user.click(hpButton);
|
||||||
|
const hpInput = screen.getByPlaceholderText("HP");
|
||||||
|
expect(hpInput).toBeInTheDocument();
|
||||||
|
await user.type(hpInput, "59");
|
||||||
|
await user.click(screen.getByRole("button", { name: "Apply damage" }));
|
||||||
|
|
||||||
|
// Verify HP decreased to 0 and unconscious state
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Current HP: 0 (unconscious)" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
40
apps/web/src/__tests__/bestiary-index-helpers.test.ts
Normal file
40
apps/web/src/__tests__/bestiary-index-helpers.test.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
getAllSourceCodes,
|
||||||
|
getDefaultFetchUrl,
|
||||||
|
} from "../adapters/bestiary-index-adapter.js";
|
||||||
|
|
||||||
|
describe("getAllSourceCodes", () => {
|
||||||
|
it("returns all keys from the index sources object", () => {
|
||||||
|
const codes = getAllSourceCodes();
|
||||||
|
expect(codes.length).toBeGreaterThan(0);
|
||||||
|
expect(Array.isArray(codes)).toBe(true);
|
||||||
|
for (const code of codes) {
|
||||||
|
expect(typeof code).toBe("string");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("getDefaultFetchUrl", () => {
|
||||||
|
it("returns the default URL when no baseUrl is provided", () => {
|
||||||
|
const url = getDefaultFetchUrl("XMM");
|
||||||
|
expect(url).toBe(
|
||||||
|
"https://raw.githubusercontent.com/5etools-mirror-3/5etools-src/main/data/bestiary/bestiary-xmm.json",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("constructs URL from baseUrl with trailing slash", () => {
|
||||||
|
const url = getDefaultFetchUrl("PHB", "https://example.com/data/");
|
||||||
|
expect(url).toBe("https://example.com/data/bestiary-phb.json");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("normalizes baseUrl without trailing slash", () => {
|
||||||
|
const url = getDefaultFetchUrl("PHB", "https://example.com/data");
|
||||||
|
expect(url).toBe("https://example.com/data/bestiary-phb.json");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lowercases the source code in the filename", () => {
|
||||||
|
const url = getDefaultFetchUrl("MM", "https://example.com/");
|
||||||
|
expect(url).toBe("https://example.com/bestiary-mm.json");
|
||||||
|
});
|
||||||
|
});
|
||||||
219
apps/web/src/__tests__/confirm-button.test.tsx
Normal file
219
apps/web/src/__tests__/confirm-button.test.tsx
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import {
|
||||||
|
act,
|
||||||
|
cleanup,
|
||||||
|
fireEvent,
|
||||||
|
render,
|
||||||
|
screen,
|
||||||
|
} from "@testing-library/react";
|
||||||
|
import "@testing-library/jest-dom/vitest";
|
||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { ConfirmButton } from "../components/ui/confirm-button";
|
||||||
|
|
||||||
|
function XIcon() {
|
||||||
|
return <span data-testid="x-icon">X</span>;
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderButton(
|
||||||
|
props: Partial<Parameters<typeof ConfirmButton>[0]> = {},
|
||||||
|
) {
|
||||||
|
const onConfirm = props.onConfirm ?? vi.fn();
|
||||||
|
render(
|
||||||
|
<ConfirmButton
|
||||||
|
icon={<XIcon />}
|
||||||
|
label="Remove combatant"
|
||||||
|
onConfirm={onConfirm}
|
||||||
|
{...props}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
return { onConfirm };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("ConfirmButton", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.useRealTimers();
|
||||||
|
cleanup();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders the default icon in idle state", () => {
|
||||||
|
renderButton();
|
||||||
|
expect(screen.getByTestId("x-icon")).toBeTruthy();
|
||||||
|
expect(screen.getByRole("button")).toHaveAttribute(
|
||||||
|
"aria-label",
|
||||||
|
"Remove combatant",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("transitions to confirm state with Check icon on first click", () => {
|
||||||
|
renderButton();
|
||||||
|
fireEvent.click(screen.getByRole("button"));
|
||||||
|
|
||||||
|
expect(screen.queryByTestId("x-icon")).toBeNull();
|
||||||
|
expect(screen.getByRole("button")).toHaveAttribute(
|
||||||
|
"aria-label",
|
||||||
|
"Confirm remove combatant",
|
||||||
|
);
|
||||||
|
expect(screen.getByRole("button").className).toContain("bg-destructive");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calls onConfirm on second click in confirm state", () => {
|
||||||
|
const { onConfirm } = renderButton();
|
||||||
|
const button = screen.getByRole("button");
|
||||||
|
|
||||||
|
fireEvent.click(button);
|
||||||
|
fireEvent.click(button);
|
||||||
|
|
||||||
|
expect(onConfirm).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("auto-reverts after 5 seconds", () => {
|
||||||
|
renderButton();
|
||||||
|
fireEvent.click(screen.getByRole("button"));
|
||||||
|
|
||||||
|
expect(screen.queryByTestId("x-icon")).toBeNull();
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
vi.advanceTimersByTime(5000);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(screen.getByTestId("x-icon")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reverts on Escape key", () => {
|
||||||
|
renderButton();
|
||||||
|
fireEvent.click(screen.getByRole("button"));
|
||||||
|
|
||||||
|
expect(screen.queryByTestId("x-icon")).toBeNull();
|
||||||
|
|
||||||
|
fireEvent.keyDown(document, { key: "Escape" });
|
||||||
|
|
||||||
|
expect(screen.getByTestId("x-icon")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reverts on click outside", () => {
|
||||||
|
renderButton();
|
||||||
|
fireEvent.click(screen.getByRole("button"));
|
||||||
|
|
||||||
|
expect(screen.queryByTestId("x-icon")).toBeNull();
|
||||||
|
|
||||||
|
fireEvent.mouseDown(document.body);
|
||||||
|
|
||||||
|
expect(screen.getByTestId("x-icon")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not enter confirm state when disabled", () => {
|
||||||
|
renderButton({ disabled: true });
|
||||||
|
fireEvent.click(screen.getByRole("button"));
|
||||||
|
|
||||||
|
expect(screen.getByTestId("x-icon")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("cleans up timer on unmount", () => {
|
||||||
|
const clearTimeoutSpy = vi.spyOn(globalThis, "clearTimeout");
|
||||||
|
renderButton();
|
||||||
|
fireEvent.click(screen.getByRole("button"));
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
|
||||||
|
expect(clearTimeoutSpy).toHaveBeenCalled();
|
||||||
|
clearTimeoutSpy.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("manages independent instances separately", () => {
|
||||||
|
const onConfirm1 = vi.fn();
|
||||||
|
const onConfirm2 = vi.fn();
|
||||||
|
|
||||||
|
render(
|
||||||
|
<>
|
||||||
|
<ConfirmButton
|
||||||
|
icon={<span data-testid="icon-1">1</span>}
|
||||||
|
label="Remove first"
|
||||||
|
onConfirm={onConfirm1}
|
||||||
|
/>
|
||||||
|
<ConfirmButton
|
||||||
|
icon={<span data-testid="icon-2">2</span>}
|
||||||
|
label="Remove second"
|
||||||
|
onConfirm={onConfirm2}
|
||||||
|
/>
|
||||||
|
</>,
|
||||||
|
);
|
||||||
|
|
||||||
|
const buttons = screen.getAllByRole("button");
|
||||||
|
fireEvent.click(buttons[0]);
|
||||||
|
|
||||||
|
// First is confirming, second is still idle
|
||||||
|
expect(screen.queryByTestId("icon-1")).toBeNull();
|
||||||
|
expect(screen.getByTestId("icon-2")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
// T008: Keyboard-specific tests
|
||||||
|
it("Enter key triggers confirm state", () => {
|
||||||
|
renderButton();
|
||||||
|
const button = screen.getByRole("button");
|
||||||
|
|
||||||
|
// Native button handles Enter via click event
|
||||||
|
fireEvent.click(button);
|
||||||
|
|
||||||
|
expect(screen.queryByTestId("x-icon")).toBeNull();
|
||||||
|
expect(button).toHaveAttribute("aria-label", "Confirm remove combatant");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Enter in confirm state calls onConfirm", () => {
|
||||||
|
const { onConfirm } = renderButton();
|
||||||
|
const button = screen.getByRole("button");
|
||||||
|
|
||||||
|
fireEvent.click(button); // enter confirm state
|
||||||
|
fireEvent.click(button); // confirm
|
||||||
|
|
||||||
|
expect(onConfirm).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Escape in confirm state reverts", () => {
|
||||||
|
renderButton();
|
||||||
|
fireEvent.click(screen.getByRole("button"));
|
||||||
|
|
||||||
|
fireEvent.keyDown(document, { key: "Escape" });
|
||||||
|
|
||||||
|
expect(screen.getByTestId("x-icon")).toBeTruthy();
|
||||||
|
expect(screen.getByRole("button")).toHaveAttribute(
|
||||||
|
"aria-label",
|
||||||
|
"Remove combatant",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("blur event reverts confirm state", () => {
|
||||||
|
renderButton();
|
||||||
|
const button = screen.getByRole("button");
|
||||||
|
|
||||||
|
fireEvent.click(button);
|
||||||
|
expect(screen.queryByTestId("x-icon")).toBeNull();
|
||||||
|
|
||||||
|
fireEvent.blur(button);
|
||||||
|
expect(screen.getByTestId("x-icon")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Enter/Space keydown stops propagation to prevent parent handlers", () => {
|
||||||
|
const parentHandler = vi.fn();
|
||||||
|
render(
|
||||||
|
// biome-ignore lint/a11y/noStaticElementInteractions: test wrapper
|
||||||
|
// biome-ignore lint/a11y/noNoninteractiveElementInteractions: test wrapper
|
||||||
|
<div onKeyDown={parentHandler}>
|
||||||
|
<ConfirmButton
|
||||||
|
icon={<XIcon />}
|
||||||
|
label="Remove combatant"
|
||||||
|
onConfirm={vi.fn()}
|
||||||
|
/>
|
||||||
|
</div>,
|
||||||
|
);
|
||||||
|
const button = screen.getByRole("button");
|
||||||
|
|
||||||
|
fireEvent.keyDown(button, { key: "Enter" });
|
||||||
|
fireEvent.keyDown(button, { key: " " });
|
||||||
|
|
||||||
|
expect(parentHandler).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
266
apps/web/src/__tests__/stat-block-collapse-pin.test.tsx
Normal file
266
apps/web/src/__tests__/stat-block-collapse-pin.test.tsx
Normal file
@@ -0,0 +1,266 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import "@testing-library/jest-dom/vitest";
|
||||||
|
|
||||||
|
import type { Creature, CreatureId } from "@initiative/domain";
|
||||||
|
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { StatBlockPanel } from "../components/stat-block-panel";
|
||||||
|
|
||||||
|
const CLOSE_REGEX = /close/i;
|
||||||
|
const COLLAPSE_REGEX = /collapse/i;
|
||||||
|
|
||||||
|
const CREATURE_ID = "srd:goblin" as CreatureId;
|
||||||
|
const CREATURE: Creature = {
|
||||||
|
id: CREATURE_ID,
|
||||||
|
name: "Goblin",
|
||||||
|
source: "SRD",
|
||||||
|
sourceDisplayName: "SRD",
|
||||||
|
size: "Small",
|
||||||
|
type: "humanoid",
|
||||||
|
alignment: "neutral evil",
|
||||||
|
ac: 15,
|
||||||
|
hp: { average: 7, formula: "2d6" },
|
||||||
|
speed: "30 ft.",
|
||||||
|
abilities: { str: 8, dex: 14, con: 10, int: 10, wis: 8, cha: 8 },
|
||||||
|
cr: "1/4",
|
||||||
|
initiativeProficiency: 0,
|
||||||
|
proficiencyBonus: 2,
|
||||||
|
passive: 9,
|
||||||
|
};
|
||||||
|
|
||||||
|
function mockMatchMedia(matches: boolean) {
|
||||||
|
Object.defineProperty(globalThis, "matchMedia", {
|
||||||
|
writable: true,
|
||||||
|
value: vi.fn().mockImplementation((query: string) => ({
|
||||||
|
matches,
|
||||||
|
media: query,
|
||||||
|
onchange: null,
|
||||||
|
addListener: vi.fn(),
|
||||||
|
removeListener: vi.fn(),
|
||||||
|
addEventListener: vi.fn(),
|
||||||
|
removeEventListener: vi.fn(),
|
||||||
|
dispatchEvent: vi.fn(),
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PanelProps {
|
||||||
|
creatureId?: CreatureId | null;
|
||||||
|
creature?: Creature | null;
|
||||||
|
panelRole?: "browse" | "pinned";
|
||||||
|
isCollapsed?: boolean;
|
||||||
|
onToggleCollapse?: () => void;
|
||||||
|
onPin?: () => void;
|
||||||
|
onUnpin?: () => void;
|
||||||
|
showPinButton?: boolean;
|
||||||
|
side?: "left" | "right";
|
||||||
|
onDismiss?: () => void;
|
||||||
|
bulkImportMode?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderPanel(overrides: PanelProps = {}) {
|
||||||
|
const props = {
|
||||||
|
creatureId: CREATURE_ID,
|
||||||
|
creature: CREATURE,
|
||||||
|
isSourceCached: vi.fn().mockResolvedValue(true),
|
||||||
|
fetchAndCacheSource: vi.fn(),
|
||||||
|
uploadAndCacheSource: vi.fn(),
|
||||||
|
refreshCache: vi.fn(),
|
||||||
|
panelRole: "browse" as const,
|
||||||
|
isCollapsed: false,
|
||||||
|
onToggleCollapse: vi.fn(),
|
||||||
|
onPin: vi.fn(),
|
||||||
|
onUnpin: vi.fn(),
|
||||||
|
showPinButton: false,
|
||||||
|
side: "right" as const,
|
||||||
|
onDismiss: vi.fn(),
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
|
||||||
|
render(<StatBlockPanel {...props} />);
|
||||||
|
return props;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("Stat Block Panel Collapse/Expand and Pin", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockMatchMedia(true); // desktop by default
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
describe("US1: Collapse and Expand", () => {
|
||||||
|
it("shows collapse button instead of close button on desktop", () => {
|
||||||
|
renderPanel();
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Collapse stat block panel" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
expect(
|
||||||
|
screen.queryByRole("button", { name: CLOSE_REGEX }),
|
||||||
|
).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not show 'Stat Block' heading", () => {
|
||||||
|
renderPanel();
|
||||||
|
expect(screen.queryByText("Stat Block")).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders collapsed tab with creature name when isCollapsed is true", () => {
|
||||||
|
renderPanel({ isCollapsed: true });
|
||||||
|
expect(screen.getByText("Goblin")).toBeInTheDocument();
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Expand stat block panel" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calls onToggleCollapse when collapse button is clicked", () => {
|
||||||
|
const props = renderPanel();
|
||||||
|
fireEvent.click(
|
||||||
|
screen.getByRole("button", { name: "Collapse stat block panel" }),
|
||||||
|
);
|
||||||
|
expect(props.onToggleCollapse).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calls onToggleCollapse when collapsed tab is clicked", () => {
|
||||||
|
const props = renderPanel({ isCollapsed: true });
|
||||||
|
fireEvent.click(
|
||||||
|
screen.getByRole("button", { name: "Expand stat block panel" }),
|
||||||
|
);
|
||||||
|
expect(props.onToggleCollapse).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("applies translate-x class when collapsed (right side)", () => {
|
||||||
|
renderPanel({ isCollapsed: true, side: "right" });
|
||||||
|
const panel = screen
|
||||||
|
.getByRole("button", { name: "Expand stat block panel" })
|
||||||
|
.closest("div");
|
||||||
|
expect(panel?.className).toContain("translate-x-[calc(100%-40px)]");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("applies translate-x-0 when expanded", () => {
|
||||||
|
renderPanel({ isCollapsed: false });
|
||||||
|
const foldBtn = screen.getByRole("button", {
|
||||||
|
name: "Collapse stat block panel",
|
||||||
|
});
|
||||||
|
const panel = foldBtn.closest("div.fixed") as HTMLElement;
|
||||||
|
expect(panel?.className).toContain("translate-x-0");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("US1: Mobile behavior", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockMatchMedia(false); // mobile
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows collapse button instead of X close button on mobile drawer", () => {
|
||||||
|
renderPanel();
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Collapse stat block panel" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
// No X close icon button — only backdrop dismiss and collapse toggle
|
||||||
|
const buttons = screen.getAllByRole("button");
|
||||||
|
const buttonLabels = buttons.map((b) => b.getAttribute("aria-label"));
|
||||||
|
expect(buttonLabels).not.toContain("Close");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calls onDismiss when backdrop is clicked on mobile", () => {
|
||||||
|
const props = renderPanel();
|
||||||
|
fireEvent.click(screen.getByRole("button", { name: "Close stat block" }));
|
||||||
|
expect(props.onDismiss).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not render pinned panel on mobile", () => {
|
||||||
|
const { container } = render(
|
||||||
|
<StatBlockPanel
|
||||||
|
creatureId={CREATURE_ID}
|
||||||
|
creature={CREATURE}
|
||||||
|
isSourceCached={vi.fn().mockResolvedValue(true)}
|
||||||
|
fetchAndCacheSource={vi.fn()}
|
||||||
|
uploadAndCacheSource={vi.fn()}
|
||||||
|
refreshCache={vi.fn()}
|
||||||
|
panelRole="pinned"
|
||||||
|
isCollapsed={false}
|
||||||
|
onToggleCollapse={vi.fn()}
|
||||||
|
onPin={vi.fn()}
|
||||||
|
onUnpin={vi.fn()}
|
||||||
|
showPinButton={false}
|
||||||
|
side="left"
|
||||||
|
onDismiss={vi.fn()}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
expect(container.innerHTML).toBe("");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("US2: Pin and Unpin", () => {
|
||||||
|
it("shows pin button when showPinButton is true on desktop", () => {
|
||||||
|
renderPanel({ showPinButton: true });
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Pin creature" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("hides pin button when showPinButton is false", () => {
|
||||||
|
renderPanel({ showPinButton: false });
|
||||||
|
expect(
|
||||||
|
screen.queryByRole("button", { name: "Pin creature" }),
|
||||||
|
).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calls onPin when pin button is clicked", () => {
|
||||||
|
const props = renderPanel({ showPinButton: true });
|
||||||
|
fireEvent.click(screen.getByRole("button", { name: "Pin creature" }));
|
||||||
|
expect(props.onPin).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows unpin button for pinned role", () => {
|
||||||
|
renderPanel({ panelRole: "pinned", side: "left" });
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Unpin creature" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calls onUnpin when unpin button is clicked", () => {
|
||||||
|
const props = renderPanel({ panelRole: "pinned", side: "left" });
|
||||||
|
fireEvent.click(screen.getByRole("button", { name: "Unpin creature" }));
|
||||||
|
expect(props.onUnpin).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("positions pinned panel on the left side", () => {
|
||||||
|
renderPanel({ panelRole: "pinned", side: "left" });
|
||||||
|
const unpinBtn = screen.getByRole("button", {
|
||||||
|
name: "Unpin creature",
|
||||||
|
});
|
||||||
|
const panel = unpinBtn.closest("div.fixed") as HTMLElement;
|
||||||
|
expect(panel?.className).toContain("left-0");
|
||||||
|
expect(panel?.className).toContain("border-r");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("positions browse panel on the right side", () => {
|
||||||
|
renderPanel({ panelRole: "browse", side: "right" });
|
||||||
|
const foldBtn = screen.getByRole("button", {
|
||||||
|
name: "Collapse stat block panel",
|
||||||
|
});
|
||||||
|
const panel = foldBtn.closest("div.fixed") as HTMLElement;
|
||||||
|
expect(panel?.className).toContain("right-0");
|
||||||
|
expect(panel?.className).toContain("border-l");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("US3: Collapse independence with pinned panel", () => {
|
||||||
|
it("pinned panel has no collapse button", () => {
|
||||||
|
renderPanel({ panelRole: "pinned", side: "left" });
|
||||||
|
expect(
|
||||||
|
screen.queryByRole("button", { name: COLLAPSE_REGEX }),
|
||||||
|
).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("pinned panel is always expanded (no translate offset)", () => {
|
||||||
|
renderPanel({ panelRole: "pinned", side: "left", isCollapsed: false });
|
||||||
|
const unpinBtn = screen.getByRole("button", {
|
||||||
|
name: "Unpin creature",
|
||||||
|
});
|
||||||
|
const panel = unpinBtn.closest("div.fixed") as HTMLElement;
|
||||||
|
expect(panel?.className).toContain("translate-x-0");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
235
apps/web/src/__tests__/use-bulk-import.test.ts
Normal file
235
apps/web/src/__tests__/use-bulk-import.test.ts
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import * as indexAdapter from "../adapters/bestiary-index-adapter.js";
|
||||||
|
|
||||||
|
// We test the bulk import logic by extracting and exercising the async flow.
|
||||||
|
// Since useBulkImport is a thin React wrapper around async logic,
|
||||||
|
// we test the core behavior via a direct simulation.
|
||||||
|
|
||||||
|
vi.mock("../adapters/bestiary-index-adapter.js", async () => {
|
||||||
|
const actual = await vi.importActual<
|
||||||
|
typeof import("../adapters/bestiary-index-adapter.js")
|
||||||
|
>("../adapters/bestiary-index-adapter.js");
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
getAllSourceCodes: vi.fn(),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const mockGetAllSourceCodes = vi.mocked(indexAdapter.getAllSourceCodes);
|
||||||
|
|
||||||
|
interface BulkImportState {
|
||||||
|
status: "idle" | "loading" | "complete" | "partial-failure";
|
||||||
|
total: number;
|
||||||
|
completed: number;
|
||||||
|
failed: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Simulate the core bulk import logic extracted from the hook */
|
||||||
|
async function runBulkImport(
|
||||||
|
baseUrl: string,
|
||||||
|
fetchAndCacheSource: (sourceCode: string, url: string) => Promise<void>,
|
||||||
|
isSourceCached: (sourceCode: string) => Promise<boolean>,
|
||||||
|
refreshCache: () => Promise<void>,
|
||||||
|
): Promise<BulkImportState> {
|
||||||
|
const allCodes = indexAdapter.getAllSourceCodes();
|
||||||
|
const total = allCodes.length;
|
||||||
|
|
||||||
|
const cacheChecks = await Promise.all(
|
||||||
|
allCodes.map(async (code) => ({
|
||||||
|
code,
|
||||||
|
cached: await isSourceCached(code),
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
|
const alreadyCached = cacheChecks.filter((c) => c.cached).length;
|
||||||
|
const uncached = cacheChecks.filter((c) => !c.cached);
|
||||||
|
|
||||||
|
if (uncached.length === 0) {
|
||||||
|
return { status: "complete", total, completed: total, failed: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
let completed = alreadyCached;
|
||||||
|
let failed = 0;
|
||||||
|
|
||||||
|
await Promise.allSettled(
|
||||||
|
uncached.map(async ({ code }) => {
|
||||||
|
const url = indexAdapter.getDefaultFetchUrl(code, baseUrl);
|
||||||
|
try {
|
||||||
|
await fetchAndCacheSource(code, url);
|
||||||
|
completed++;
|
||||||
|
} catch {
|
||||||
|
failed++;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
await refreshCache();
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: failed > 0 ? "partial-failure" : "complete",
|
||||||
|
total,
|
||||||
|
completed,
|
||||||
|
failed,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("bulk import logic", () => {
|
||||||
|
it("skips already-cached sources and counts them into completed", async () => {
|
||||||
|
mockGetAllSourceCodes.mockReturnValue(["SRC1", "SRC2", "SRC3"]);
|
||||||
|
|
||||||
|
const fetchAndCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
const isSourceCached = vi
|
||||||
|
.fn()
|
||||||
|
.mockImplementation((code: string) =>
|
||||||
|
Promise.resolve(code === "SRC1" || code === "SRC3"),
|
||||||
|
);
|
||||||
|
const refreshCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const result = await runBulkImport(
|
||||||
|
"https://example.com/",
|
||||||
|
fetchAndCache,
|
||||||
|
isSourceCached,
|
||||||
|
refreshCache,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(fetchAndCache).toHaveBeenCalledTimes(1);
|
||||||
|
expect(fetchAndCache).toHaveBeenCalledWith(
|
||||||
|
"SRC2",
|
||||||
|
"https://example.com/bestiary-src2.json",
|
||||||
|
);
|
||||||
|
expect(result.completed).toBe(3);
|
||||||
|
expect(result.status).toBe("complete");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("increments completed on successful fetch", async () => {
|
||||||
|
mockGetAllSourceCodes.mockReturnValue(["SRC1"]);
|
||||||
|
|
||||||
|
const fetchAndCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
const isSourceCached = vi.fn().mockResolvedValue(false);
|
||||||
|
const refreshCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const result = await runBulkImport(
|
||||||
|
"https://example.com/",
|
||||||
|
fetchAndCache,
|
||||||
|
isSourceCached,
|
||||||
|
refreshCache,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.completed).toBe(1);
|
||||||
|
expect(result.failed).toBe(0);
|
||||||
|
expect(result.status).toBe("complete");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("increments failed on rejected fetch", async () => {
|
||||||
|
mockGetAllSourceCodes.mockReturnValue(["SRC1"]);
|
||||||
|
|
||||||
|
const fetchAndCache = vi.fn().mockRejectedValue(new Error("fail"));
|
||||||
|
const isSourceCached = vi.fn().mockResolvedValue(false);
|
||||||
|
const refreshCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const result = await runBulkImport(
|
||||||
|
"https://example.com/",
|
||||||
|
fetchAndCache,
|
||||||
|
isSourceCached,
|
||||||
|
refreshCache,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.failed).toBe(1);
|
||||||
|
expect(result.completed).toBe(0);
|
||||||
|
expect(result.status).toBe("partial-failure");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("transitions to complete when all succeed", async () => {
|
||||||
|
mockGetAllSourceCodes.mockReturnValue(["A", "B"]);
|
||||||
|
|
||||||
|
const fetchAndCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
const isSourceCached = vi.fn().mockResolvedValue(false);
|
||||||
|
const refreshCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const result = await runBulkImport(
|
||||||
|
"https://example.com/",
|
||||||
|
fetchAndCache,
|
||||||
|
isSourceCached,
|
||||||
|
refreshCache,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.status).toBe("complete");
|
||||||
|
expect(result.completed).toBe(2);
|
||||||
|
expect(result.failed).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("transitions to partial-failure when any fetch fails", async () => {
|
||||||
|
mockGetAllSourceCodes.mockReturnValue(["A", "B"]);
|
||||||
|
|
||||||
|
const fetchAndCache = vi
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValueOnce(undefined)
|
||||||
|
.mockRejectedValueOnce(new Error("fail"));
|
||||||
|
const isSourceCached = vi.fn().mockResolvedValue(false);
|
||||||
|
const refreshCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const result = await runBulkImport(
|
||||||
|
"https://example.com/",
|
||||||
|
fetchAndCache,
|
||||||
|
isSourceCached,
|
||||||
|
refreshCache,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.status).toBe("partial-failure");
|
||||||
|
expect(result.failed).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("immediately transitions to complete when all sources are cached", async () => {
|
||||||
|
mockGetAllSourceCodes.mockReturnValue(["A", "B", "C"]);
|
||||||
|
|
||||||
|
const fetchAndCache = vi.fn();
|
||||||
|
const isSourceCached = vi.fn().mockResolvedValue(true);
|
||||||
|
const refreshCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const result = await runBulkImport(
|
||||||
|
"https://example.com/",
|
||||||
|
fetchAndCache,
|
||||||
|
isSourceCached,
|
||||||
|
refreshCache,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.status).toBe("complete");
|
||||||
|
expect(result.total).toBe(3);
|
||||||
|
expect(result.completed).toBe(3);
|
||||||
|
expect(fetchAndCache).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calls refreshCache exactly once when all settle", async () => {
|
||||||
|
mockGetAllSourceCodes.mockReturnValue(["A", "B"]);
|
||||||
|
|
||||||
|
const fetchAndCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
const isSourceCached = vi.fn().mockResolvedValue(false);
|
||||||
|
const refreshCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
await runBulkImport(
|
||||||
|
"https://example.com/",
|
||||||
|
fetchAndCache,
|
||||||
|
isSourceCached,
|
||||||
|
refreshCache,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(refreshCache).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not call refreshCache when all sources are already cached", async () => {
|
||||||
|
mockGetAllSourceCodes.mockReturnValue(["A"]);
|
||||||
|
|
||||||
|
const fetchAndCache = vi.fn();
|
||||||
|
const isSourceCached = vi.fn().mockResolvedValue(true);
|
||||||
|
const refreshCache = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
await runBulkImport(
|
||||||
|
"https://example.com/",
|
||||||
|
fetchAndCache,
|
||||||
|
isSourceCached,
|
||||||
|
refreshCache,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(refreshCache).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,5 +1,12 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { beforeAll, describe, expect, it } from "vitest";
|
||||||
import { normalizeBestiary } from "../bestiary-adapter.js";
|
import {
|
||||||
|
normalizeBestiary,
|
||||||
|
setSourceDisplayNames,
|
||||||
|
} from "../bestiary-adapter.js";
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
setSourceDisplayNames({ XMM: "MM 2024" });
|
||||||
|
});
|
||||||
|
|
||||||
describe("normalizeBestiary", () => {
|
describe("normalizeBestiary", () => {
|
||||||
it("normalizes a simple creature", () => {
|
it("normalizes a simple creature", () => {
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
import { expect, it } from "vitest";
|
|
||||||
import rawData from "../../../../../data/bestiary/xmm.json";
|
|
||||||
import { normalizeBestiary } from "../bestiary-adapter.js";
|
|
||||||
|
|
||||||
it("normalizes all 503 monsters without error", () => {
|
|
||||||
const creatures = normalizeBestiary(
|
|
||||||
rawData as unknown as Parameters<typeof normalizeBestiary>[0],
|
|
||||||
);
|
|
||||||
expect(creatures.length).toBe(503);
|
|
||||||
for (const c of creatures) {
|
|
||||||
expect(c.name).toBeTruthy();
|
|
||||||
expect(c.id).toBeTruthy();
|
|
||||||
expect(c.ac).toBeGreaterThanOrEqual(0);
|
|
||||||
expect(c.hp.average).toBeGreaterThan(0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
@@ -30,11 +30,11 @@ describe("stripTags", () => {
|
|||||||
expect(stripTags("{@hit 5}")).toBe("+5");
|
expect(stripTags("{@hit 5}")).toBe("+5");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("strips {@h} to Hit: ", () => {
|
it("strips {@h} to Hit:", () => {
|
||||||
expect(stripTags("{@h}")).toBe("Hit: ");
|
expect(stripTags("{@h}")).toBe("Hit: ");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("strips {@hom} to Hit or Miss: ", () => {
|
it("strips {@hom} to Hit or Miss:", () => {
|
||||||
expect(stripTags("{@hom}")).toBe("Hit or Miss: ");
|
expect(stripTags("{@hom}")).toBe("Hit or Miss: ");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import type {
|
|||||||
import { creatureId, proficiencyBonus } from "@initiative/domain";
|
import { creatureId, proficiencyBonus } from "@initiative/domain";
|
||||||
import { stripTags } from "./strip-tags.js";
|
import { stripTags } from "./strip-tags.js";
|
||||||
|
|
||||||
|
const LEADING_DIGITS_REGEX = /^(\d+)/;
|
||||||
|
|
||||||
// --- Raw 5etools types (minimal, for parsing) ---
|
// --- Raw 5etools types (minimal, for parsing) ---
|
||||||
|
|
||||||
interface RawMonster {
|
interface RawMonster {
|
||||||
@@ -17,8 +19,8 @@ interface RawMonster {
|
|||||||
size: string[];
|
size: string[];
|
||||||
type: string | { type: string; tags?: string[]; swarmSize?: string };
|
type: string | { type: string; tags?: string[]; swarmSize?: string };
|
||||||
alignment?: string[];
|
alignment?: string[];
|
||||||
ac: (number | { ac: number; from?: string[] })[];
|
ac: (number | { ac: number; from?: string[] } | { special: string })[];
|
||||||
hp: { average: number; formula: string };
|
hp: { average?: number; formula?: string; special?: string };
|
||||||
speed: Record<
|
speed: Record<
|
||||||
string,
|
string,
|
||||||
number | { number: number; condition?: string } | boolean
|
number | { number: number; condition?: string } | boolean
|
||||||
@@ -38,7 +40,7 @@ interface RawMonster {
|
|||||||
vulnerable?: (string | { special: string })[];
|
vulnerable?: (string | { special: string })[];
|
||||||
conditionImmune?: string[];
|
conditionImmune?: string[];
|
||||||
languages?: string[];
|
languages?: string[];
|
||||||
cr: string | { cr: string };
|
cr?: string | { cr: string };
|
||||||
trait?: RawEntry[];
|
trait?: RawEntry[];
|
||||||
action?: RawEntry[];
|
action?: RawEntry[];
|
||||||
bonus?: RawEntry[];
|
bonus?: RawEntry[];
|
||||||
@@ -49,6 +51,7 @@ interface RawMonster {
|
|||||||
legendaryHeader?: string[];
|
legendaryHeader?: string[];
|
||||||
spellcasting?: RawSpellcasting[];
|
spellcasting?: RawSpellcasting[];
|
||||||
initiative?: { proficiency?: number };
|
initiative?: { proficiency?: number };
|
||||||
|
_copy?: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface RawEntry {
|
interface RawEntry {
|
||||||
@@ -81,9 +84,11 @@ interface RawSpellcasting {
|
|||||||
|
|
||||||
// --- Source mapping ---
|
// --- Source mapping ---
|
||||||
|
|
||||||
const SOURCE_DISPLAY_NAMES: Record<string, string> = {
|
let sourceDisplayNames: Record<string, string> = {};
|
||||||
XMM: "MM 2024",
|
|
||||||
};
|
export function setSourceDisplayNames(names: Record<string, string>): void {
|
||||||
|
sourceDisplayNames = names;
|
||||||
|
}
|
||||||
|
|
||||||
// --- Size mapping ---
|
// --- Size mapping ---
|
||||||
|
|
||||||
@@ -138,7 +143,12 @@ function formatType(
|
|||||||
|
|
||||||
let result = baseType;
|
let result = baseType;
|
||||||
if (type.tags && type.tags.length > 0) {
|
if (type.tags && type.tags.length > 0) {
|
||||||
result += ` (${type.tags.map(capitalize).join(", ")})`;
|
const tagStrs = type.tags
|
||||||
|
.filter((t): t is string => typeof t === "string")
|
||||||
|
.map(capitalize);
|
||||||
|
if (tagStrs.length > 0) {
|
||||||
|
result += ` (${tagStrs.join(", ")})`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (type.swarmSize) {
|
if (type.swarmSize) {
|
||||||
const swarmSizeLabel = SIZE_MAP[type.swarmSize] ?? type.swarmSize;
|
const swarmSizeLabel = SIZE_MAP[type.swarmSize] ?? type.swarmSize;
|
||||||
@@ -159,6 +169,14 @@ function extractAc(ac: RawMonster["ac"]): {
|
|||||||
if (typeof first === "number") {
|
if (typeof first === "number") {
|
||||||
return { value: first };
|
return { value: first };
|
||||||
}
|
}
|
||||||
|
if ("special" in first) {
|
||||||
|
// Variable AC (e.g. spell summons) — parse leading number if possible
|
||||||
|
const match = LEADING_DIGITS_REGEX.exec(first.special);
|
||||||
|
return {
|
||||||
|
value: match ? Number(match[1]) : 0,
|
||||||
|
source: first.special,
|
||||||
|
};
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
value: first.ac,
|
value: first.ac,
|
||||||
source: first.from ? stripTags(first.from.join(", ")) : undefined,
|
source: first.from ? stripTags(first.from.join(", ")) : undefined,
|
||||||
@@ -239,26 +257,37 @@ function formatConditionImmunities(
|
|||||||
.join(", ");
|
.join(", ");
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderEntries(entries: (string | RawEntryObject)[]): string {
|
function renderListItem(item: string | RawEntryObject): string | undefined {
|
||||||
const parts: string[] = [];
|
|
||||||
for (const entry of entries) {
|
|
||||||
if (typeof entry === "string") {
|
|
||||||
parts.push(stripTags(entry));
|
|
||||||
} else if (entry.type === "list") {
|
|
||||||
for (const item of entry.items ?? []) {
|
|
||||||
if (typeof item === "string") {
|
if (typeof item === "string") {
|
||||||
parts.push(`• ${stripTags(item)}`);
|
return `• ${stripTags(item)}`;
|
||||||
} else if (item.name && item.entries) {
|
|
||||||
parts.push(
|
|
||||||
`• ${stripTags(item.name)}: ${renderEntries(item.entries)}`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
if (item.name && item.entries) {
|
||||||
|
return `• ${stripTags(item.name)}: ${renderEntries(item.entries)}`;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderEntryObject(entry: RawEntryObject, parts: string[]): void {
|
||||||
|
if (entry.type === "list") {
|
||||||
|
for (const item of entry.items ?? []) {
|
||||||
|
const rendered = renderListItem(item);
|
||||||
|
if (rendered) parts.push(rendered);
|
||||||
}
|
}
|
||||||
} else if (entry.type === "item" && entry.name && entry.entries) {
|
} else if (entry.type === "item" && entry.name && entry.entries) {
|
||||||
parts.push(`${stripTags(entry.name)}: ${renderEntries(entry.entries)}`);
|
parts.push(`${stripTags(entry.name)}: ${renderEntries(entry.entries)}`);
|
||||||
} else if (entry.entries) {
|
} else if (entry.entries) {
|
||||||
parts.push(renderEntries(entry.entries));
|
parts.push(renderEntries(entry.entries));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderEntries(entries: (string | RawEntryObject)[]): string {
|
||||||
|
const parts: string[] = [];
|
||||||
|
for (const entry of entries) {
|
||||||
|
if (typeof entry === "string") {
|
||||||
|
parts.push(stripTags(entry));
|
||||||
|
} else {
|
||||||
|
renderEntryObject(entry, parts);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return parts.join(" ");
|
return parts.join(" ");
|
||||||
}
|
}
|
||||||
@@ -337,15 +366,16 @@ function normalizeLegendary(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractCr(cr: string | { cr: string }): string {
|
function extractCr(cr: string | { cr: string } | undefined): string {
|
||||||
|
if (cr === undefined) return "—";
|
||||||
return typeof cr === "string" ? cr : cr.cr;
|
return typeof cr === "string" ? cr : cr.cr;
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeCreatureId(source: string, name: string): CreatureId {
|
function makeCreatureId(source: string, name: string): CreatureId {
|
||||||
const slug = name
|
const slug = name
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.replace(/[^a-z0-9]+/g, "-")
|
.replaceAll(/[^a-z0-9]+/g, "-")
|
||||||
.replace(/(^-|-$)/g, "");
|
.replaceAll(/(^-|-$)/g, "");
|
||||||
return creatureId(`${source.toLowerCase()}:${slug}`);
|
return creatureId(`${source.toLowerCase()}:${slug}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,7 +383,31 @@ function makeCreatureId(source: string, name: string): CreatureId {
|
|||||||
* Normalizes raw 5etools bestiary JSON into domain Creature[].
|
* Normalizes raw 5etools bestiary JSON into domain Creature[].
|
||||||
*/
|
*/
|
||||||
export function normalizeBestiary(raw: { monster: RawMonster[] }): Creature[] {
|
export function normalizeBestiary(raw: { monster: RawMonster[] }): Creature[] {
|
||||||
return raw.monster.map((m) => {
|
// Filter out _copy entries (reference another source's monster) and
|
||||||
|
// monsters missing required fields (ac, hp, size, type)
|
||||||
|
const monsters = raw.monster.filter((m) => {
|
||||||
|
if (m._copy) return false;
|
||||||
|
return (
|
||||||
|
Array.isArray(m.ac) &&
|
||||||
|
m.ac.length > 0 &&
|
||||||
|
m.hp !== undefined &&
|
||||||
|
Array.isArray(m.size) &&
|
||||||
|
m.size.length > 0 &&
|
||||||
|
m.type !== undefined
|
||||||
|
);
|
||||||
|
});
|
||||||
|
const creatures: Creature[] = [];
|
||||||
|
for (const m of monsters) {
|
||||||
|
try {
|
||||||
|
creatures.push(normalizeMonster(m));
|
||||||
|
} catch {
|
||||||
|
// Skip monsters with unexpected data shapes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return creatures;
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeMonster(m: RawMonster): Creature {
|
||||||
const crStr = extractCr(m.cr);
|
const crStr = extractCr(m.cr);
|
||||||
const ac = extractAc(m.ac);
|
const ac = extractAc(m.ac);
|
||||||
|
|
||||||
@@ -361,13 +415,16 @@ export function normalizeBestiary(raw: { monster: RawMonster[] }): Creature[] {
|
|||||||
id: makeCreatureId(m.source, m.name),
|
id: makeCreatureId(m.source, m.name),
|
||||||
name: m.name,
|
name: m.name,
|
||||||
source: m.source,
|
source: m.source,
|
||||||
sourceDisplayName: SOURCE_DISPLAY_NAMES[m.source] ?? m.source,
|
sourceDisplayName: sourceDisplayNames[m.source] ?? m.source,
|
||||||
size: formatSize(m.size),
|
size: formatSize(m.size),
|
||||||
type: formatType(m.type),
|
type: formatType(m.type),
|
||||||
alignment: formatAlignment(m.alignment),
|
alignment: formatAlignment(m.alignment),
|
||||||
ac: ac.value,
|
ac: ac.value,
|
||||||
acSource: ac.source,
|
acSource: ac.source,
|
||||||
hp: { average: m.hp.average, formula: m.hp.formula },
|
hp: {
|
||||||
|
average: m.hp.average ?? 0,
|
||||||
|
formula: m.hp.formula ?? m.hp.special ?? "",
|
||||||
|
},
|
||||||
speed: formatSpeed(m.speed),
|
speed: formatSpeed(m.speed),
|
||||||
abilities: {
|
abilities: {
|
||||||
str: m.str,
|
str: m.str,
|
||||||
@@ -402,5 +459,4 @@ export function normalizeBestiary(raw: { monster: RawMonster[] }): Creature[] {
|
|||||||
legendaryActions: normalizeLegendary(m.legendary, m),
|
legendaryActions: normalizeLegendary(m.legendary, m),
|
||||||
spellcasting: normalizeSpellcasting(m.spellcasting),
|
spellcasting: normalizeSpellcasting(m.spellcasting),
|
||||||
};
|
};
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
139
apps/web/src/adapters/bestiary-cache.ts
Normal file
139
apps/web/src/adapters/bestiary-cache.ts
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
import type { Creature, CreatureId } from "@initiative/domain";
|
||||||
|
import { type IDBPDatabase, openDB } from "idb";
|
||||||
|
|
||||||
|
const DB_NAME = "initiative-bestiary";
|
||||||
|
const STORE_NAME = "sources";
|
||||||
|
const DB_VERSION = 1;
|
||||||
|
|
||||||
|
export interface CachedSourceInfo {
|
||||||
|
readonly sourceCode: string;
|
||||||
|
readonly displayName: string;
|
||||||
|
readonly creatureCount: number;
|
||||||
|
readonly cachedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CachedSourceRecord {
|
||||||
|
sourceCode: string;
|
||||||
|
displayName: string;
|
||||||
|
creatures: Creature[];
|
||||||
|
cachedAt: number;
|
||||||
|
creatureCount: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
let db: IDBPDatabase | null = null;
|
||||||
|
let dbFailed = false;
|
||||||
|
|
||||||
|
// In-memory fallback when IndexedDB is unavailable
|
||||||
|
const memoryStore = new Map<string, CachedSourceRecord>();
|
||||||
|
|
||||||
|
async function getDb(): Promise<IDBPDatabase | null> {
|
||||||
|
if (db) return db;
|
||||||
|
if (dbFailed) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
db = await openDB(DB_NAME, DB_VERSION, {
|
||||||
|
upgrade(database) {
|
||||||
|
if (!database.objectStoreNames.contains(STORE_NAME)) {
|
||||||
|
database.createObjectStore(STORE_NAME, {
|
||||||
|
keyPath: "sourceCode",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return db;
|
||||||
|
} catch {
|
||||||
|
dbFailed = true;
|
||||||
|
console.warn(
|
||||||
|
"IndexedDB unavailable — bestiary cache will not persist across sessions.",
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function cacheSource(
|
||||||
|
sourceCode: string,
|
||||||
|
displayName: string,
|
||||||
|
creatures: Creature[],
|
||||||
|
): Promise<void> {
|
||||||
|
const record: CachedSourceRecord = {
|
||||||
|
sourceCode,
|
||||||
|
displayName,
|
||||||
|
creatures,
|
||||||
|
cachedAt: Date.now(),
|
||||||
|
creatureCount: creatures.length,
|
||||||
|
};
|
||||||
|
|
||||||
|
const database = await getDb();
|
||||||
|
if (database) {
|
||||||
|
await database.put(STORE_NAME, record);
|
||||||
|
} else {
|
||||||
|
memoryStore.set(sourceCode, record);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function isSourceCached(sourceCode: string): Promise<boolean> {
|
||||||
|
const database = await getDb();
|
||||||
|
if (database) {
|
||||||
|
const record = await database.get(STORE_NAME, sourceCode);
|
||||||
|
return record !== undefined;
|
||||||
|
}
|
||||||
|
return memoryStore.has(sourceCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getCachedSources(): Promise<CachedSourceInfo[]> {
|
||||||
|
const database = await getDb();
|
||||||
|
if (database) {
|
||||||
|
const all: CachedSourceRecord[] = await database.getAll(STORE_NAME);
|
||||||
|
return all.map((r) => ({
|
||||||
|
sourceCode: r.sourceCode,
|
||||||
|
displayName: r.displayName,
|
||||||
|
creatureCount: r.creatureCount,
|
||||||
|
cachedAt: r.cachedAt,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
return [...memoryStore.values()].map((r) => ({
|
||||||
|
sourceCode: r.sourceCode,
|
||||||
|
displayName: r.displayName,
|
||||||
|
creatureCount: r.creatureCount,
|
||||||
|
cachedAt: r.cachedAt,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function clearSource(sourceCode: string): Promise<void> {
|
||||||
|
const database = await getDb();
|
||||||
|
if (database) {
|
||||||
|
await database.delete(STORE_NAME, sourceCode);
|
||||||
|
} else {
|
||||||
|
memoryStore.delete(sourceCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function clearAll(): Promise<void> {
|
||||||
|
const database = await getDb();
|
||||||
|
if (database) {
|
||||||
|
await database.clear(STORE_NAME);
|
||||||
|
} else {
|
||||||
|
memoryStore.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function loadAllCachedCreatures(): Promise<
|
||||||
|
Map<CreatureId, Creature>
|
||||||
|
> {
|
||||||
|
const map = new Map<CreatureId, Creature>();
|
||||||
|
const database = await getDb();
|
||||||
|
|
||||||
|
let records: CachedSourceRecord[];
|
||||||
|
if (database) {
|
||||||
|
records = await database.getAll(STORE_NAME);
|
||||||
|
} else {
|
||||||
|
records = [...memoryStore.values()];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const record of records) {
|
||||||
|
for (const creature of record.creatures) {
|
||||||
|
map.set(creature.id, creature);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
96
apps/web/src/adapters/bestiary-index-adapter.ts
Normal file
96
apps/web/src/adapters/bestiary-index-adapter.ts
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
import type { BestiaryIndex, BestiaryIndexEntry } from "@initiative/domain";
|
||||||
|
|
||||||
|
import rawIndex from "../../../../data/bestiary/index.json";
|
||||||
|
|
||||||
|
interface CompactCreature {
|
||||||
|
readonly n: string;
|
||||||
|
readonly s: string;
|
||||||
|
readonly ac: number;
|
||||||
|
readonly hp: number;
|
||||||
|
readonly dx: number;
|
||||||
|
readonly cr: string;
|
||||||
|
readonly ip: number;
|
||||||
|
readonly sz: string;
|
||||||
|
readonly tp: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CompactIndex {
|
||||||
|
readonly sources: Record<string, string>;
|
||||||
|
readonly creatures: readonly CompactCreature[];
|
||||||
|
}
|
||||||
|
|
||||||
|
function mapCreature(c: CompactCreature): BestiaryIndexEntry {
|
||||||
|
return {
|
||||||
|
name: c.n,
|
||||||
|
source: c.s,
|
||||||
|
ac: c.ac,
|
||||||
|
hp: c.hp,
|
||||||
|
dex: c.dx,
|
||||||
|
cr: c.cr,
|
||||||
|
initiativeProficiency: c.ip,
|
||||||
|
size: c.sz,
|
||||||
|
type: c.tp,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Source codes whose filename on the remote differs from a simple lowercase.
|
||||||
|
// Plane Shift sources use a hyphen: PSA -> ps-a, etc.
|
||||||
|
const FILENAME_OVERRIDES: Record<string, string> = {
|
||||||
|
PSA: "ps-a",
|
||||||
|
PSD: "ps-d",
|
||||||
|
PSI: "ps-i",
|
||||||
|
PSK: "ps-k",
|
||||||
|
PSX: "ps-x",
|
||||||
|
PSZ: "ps-z",
|
||||||
|
};
|
||||||
|
|
||||||
|
// Source codes with no corresponding remote bestiary file.
|
||||||
|
// Excluded from the index entirely so creatures aren't searchable
|
||||||
|
// without a fetchable source.
|
||||||
|
const EXCLUDED_SOURCES = new Set<string>([]);
|
||||||
|
|
||||||
|
let cachedIndex: BestiaryIndex | undefined;
|
||||||
|
|
||||||
|
export function loadBestiaryIndex(): BestiaryIndex {
|
||||||
|
if (cachedIndex) return cachedIndex;
|
||||||
|
|
||||||
|
const compact = rawIndex as unknown as CompactIndex;
|
||||||
|
const sources = Object.fromEntries(
|
||||||
|
Object.entries(compact.sources).filter(
|
||||||
|
([code]) => !EXCLUDED_SOURCES.has(code),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
cachedIndex = {
|
||||||
|
sources,
|
||||||
|
creatures: compact.creatures
|
||||||
|
.filter((c) => !EXCLUDED_SOURCES.has(c.s))
|
||||||
|
.map(mapCreature),
|
||||||
|
};
|
||||||
|
return cachedIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAllSourceCodes(): string[] {
|
||||||
|
const index = loadBestiaryIndex();
|
||||||
|
return Object.keys(index.sources).filter((c) => !EXCLUDED_SOURCES.has(c));
|
||||||
|
}
|
||||||
|
|
||||||
|
function sourceCodeToFilename(sourceCode: string): string {
|
||||||
|
return FILENAME_OVERRIDES[sourceCode] ?? sourceCode.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getDefaultFetchUrl(
|
||||||
|
sourceCode: string,
|
||||||
|
baseUrl?: string,
|
||||||
|
): string {
|
||||||
|
const filename = `bestiary-${sourceCodeToFilename(sourceCode)}.json`;
|
||||||
|
if (baseUrl !== undefined) {
|
||||||
|
const normalized = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
|
||||||
|
return `${normalized}${filename}`;
|
||||||
|
}
|
||||||
|
return `https://raw.githubusercontent.com/5etools-mirror-3/5etools-src/main/data/bestiary/${filename}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getSourceDisplayName(sourceCode: string): string {
|
||||||
|
const index = loadBestiaryIndex();
|
||||||
|
return index.sources[sourceCode] ?? sourceCode;
|
||||||
|
}
|
||||||
@@ -20,59 +20,63 @@ const ATKR_MAP: Record<string, string> = {
|
|||||||
* Handles 15+ tag types per research.md R-002 tag resolution rules.
|
* Handles 15+ tag types per research.md R-002 tag resolution rules.
|
||||||
*/
|
*/
|
||||||
export function stripTags(text: string): string {
|
export function stripTags(text: string): string {
|
||||||
|
if (typeof text !== "string") return String(text);
|
||||||
// Process special tags with specific output formats first
|
// Process special tags with specific output formats first
|
||||||
let result = text;
|
let result = text;
|
||||||
|
|
||||||
// {@h} → "Hit: "
|
// {@h} → "Hit: "
|
||||||
result = result.replace(/\{@h\}/g, "Hit: ");
|
result = result.replaceAll("{@h}", "Hit: ");
|
||||||
|
|
||||||
// {@hom} → "Hit or Miss: "
|
// {@hom} → "Hit or Miss: "
|
||||||
result = result.replace(/\{@hom\}/g, "Hit or Miss: ");
|
result = result.replaceAll("{@hom}", "Hit or Miss: ");
|
||||||
|
|
||||||
// {@actTrigger} → "Trigger:"
|
// {@actTrigger} → "Trigger:"
|
||||||
result = result.replace(/\{@actTrigger\}/g, "Trigger:");
|
result = result.replaceAll("{@actTrigger}", "Trigger:");
|
||||||
|
|
||||||
// {@actResponse} → "Response:"
|
// {@actResponse} → "Response:"
|
||||||
result = result.replace(/\{@actResponse\}/g, "Response:");
|
result = result.replaceAll("{@actResponse}", "Response:");
|
||||||
|
|
||||||
// {@actSaveSuccess} → "Success:"
|
// {@actSaveSuccess} → "Success:"
|
||||||
result = result.replace(/\{@actSaveSuccess\}/g, "Success:");
|
result = result.replaceAll("{@actSaveSuccess}", "Success:");
|
||||||
|
|
||||||
// {@actSaveSuccessOrFail} → handled below as parameterized
|
// {@actSaveSuccessOrFail} → handled below as parameterized
|
||||||
|
|
||||||
// {@recharge 5} → "(Recharge 5-6)", {@recharge} → "(Recharge 6)"
|
// {@recharge 5} → "(Recharge 5-6)", {@recharge} → "(Recharge 6)"
|
||||||
result = result.replace(/\{@recharge\s+(\d)\}/g, "(Recharge $1-6)");
|
result = result.replaceAll(/\{@recharge\s+(\d)\}/g, "(Recharge $1-6)");
|
||||||
result = result.replace(/\{@recharge\}/g, "(Recharge 6)");
|
result = result.replaceAll("{@recharge}", "(Recharge 6)");
|
||||||
|
|
||||||
// {@dc N} → "DC N"
|
// {@dc N} → "DC N"
|
||||||
result = result.replace(/\{@dc\s+(\d+)\}/g, "DC $1");
|
result = result.replaceAll(/\{@dc\s+(\d+)\}/g, "DC $1");
|
||||||
|
|
||||||
// {@hit N} → "+N"
|
// {@hit N} → "+N"
|
||||||
result = result.replace(/\{@hit\s+(\d+)\}/g, "+$1");
|
result = result.replaceAll(/\{@hit\s+(\d+)\}/g, "+$1");
|
||||||
|
|
||||||
// {@atkr type} → mapped attack roll text
|
// {@atkr type} → mapped attack roll text
|
||||||
result = result.replace(/\{@atkr\s+([^}]+)\}/g, (_, type: string) => {
|
result = result.replaceAll(/\{@atkr\s+([^}]+)\}/g, (_, type: string) => {
|
||||||
return ATKR_MAP[type.trim()] ?? `Attack Roll:`;
|
return ATKR_MAP[type.trim()] ?? "Attack Roll:";
|
||||||
});
|
});
|
||||||
|
|
||||||
// {@actSave ability} → "Ability saving throw"
|
// {@actSave ability} → "Ability saving throw"
|
||||||
result = result.replace(/\{@actSave\s+([^}]+)\}/g, (_, ability: string) => {
|
result = result.replaceAll(
|
||||||
|
/\{@actSave\s+([^}]+)\}/g,
|
||||||
|
(_, ability: string) => {
|
||||||
const name = ABILITY_MAP[ability.trim().toLowerCase()];
|
const name = ABILITY_MAP[ability.trim().toLowerCase()];
|
||||||
return name ? `${name} saving throw` : `${ability} saving throw`;
|
return name ? `${name} saving throw` : `${ability} saving throw`;
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// {@actSaveFail} → "Failure:" or {@actSaveFail N} → "Failure by N or More:"
|
// {@actSaveFail} → "Failure:" or {@actSaveFail N} → "Failure by N or More:"
|
||||||
result = result.replace(
|
result = result.replaceAll(
|
||||||
/\{@actSaveFail\s+(\d+)\}/g,
|
/\{@actSaveFail\s+(\d+)\}/g,
|
||||||
"Failure by $1 or More:",
|
"Failure by $1 or More:",
|
||||||
);
|
);
|
||||||
result = result.replace(/\{@actSaveFail\}/g, "Failure:");
|
result = result.replaceAll("{@actSaveFail}", "Failure:");
|
||||||
|
|
||||||
// {@actSaveSuccessOrFail} → keep as-is label
|
// {@actSaveSuccessOrFail} → keep as-is label
|
||||||
result = result.replace(/\{@actSaveSuccessOrFail\}/g, "Success or Failure:");
|
result = result.replaceAll("{@actSaveSuccessOrFail}", "Success or Failure:");
|
||||||
|
|
||||||
// {@actSaveFailBy N} → "Failure by N or More:"
|
// {@actSaveFailBy N} → "Failure by N or More:"
|
||||||
result = result.replace(
|
result = result.replaceAll(
|
||||||
/\{@actSaveFailBy\s+(\d+)\}/g,
|
/\{@actSaveFailBy\s+(\d+)\}/g,
|
||||||
"Failure by $1 or More:",
|
"Failure by $1 or More:",
|
||||||
);
|
);
|
||||||
@@ -80,7 +84,7 @@ export function stripTags(text: string): string {
|
|||||||
// Generic tags: {@tag Display|Source|...} → Display (first segment before |)
|
// Generic tags: {@tag Display|Source|...} → Display (first segment before |)
|
||||||
// Covers: spell, condition, damage, dice, variantrule, action, skill,
|
// Covers: spell, condition, damage, dice, variantrule, action, skill,
|
||||||
// creature, hazard, status, plus any unknown tags
|
// creature, hazard, status, plus any unknown tags
|
||||||
result = result.replace(
|
result = result.replaceAll(
|
||||||
/\{@(\w+)\s+([^}]+)\}/g,
|
/\{@(\w+)\s+([^}]+)\}/g,
|
||||||
(_, tag: string, content: string) => {
|
(_, tag: string, content: string) => {
|
||||||
// For tags with Display|Source format, extract first segment
|
// For tags with Display|Source format, extract first segment
|
||||||
|
|||||||
88
apps/web/src/components/__tests__/action-bar.test.tsx
Normal file
88
apps/web/src/components/__tests__/action-bar.test.tsx
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import "@testing-library/jest-dom/vitest";
|
||||||
|
|
||||||
|
import { cleanup, render, screen } from "@testing-library/react";
|
||||||
|
import userEvent from "@testing-library/user-event";
|
||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { ActionBar } from "../action-bar";
|
||||||
|
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
const defaultProps = {
|
||||||
|
onAddCombatant: vi.fn(),
|
||||||
|
onAddFromBestiary: vi.fn(),
|
||||||
|
bestiarySearch: () => [],
|
||||||
|
bestiaryLoaded: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
function renderBar(overrides: Partial<Parameters<typeof ActionBar>[0]> = {}) {
|
||||||
|
const props = { ...defaultProps, ...overrides };
|
||||||
|
return render(<ActionBar {...props} />);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("ActionBar", () => {
|
||||||
|
it("renders input with placeholder '+ Add combatants'", () => {
|
||||||
|
renderBar();
|
||||||
|
expect(screen.getByPlaceholderText("+ Add combatants")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("submitting with a name calls onAddCombatant", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
const onAddCombatant = vi.fn();
|
||||||
|
renderBar({ onAddCombatant });
|
||||||
|
const input = screen.getByPlaceholderText("+ Add combatants");
|
||||||
|
await user.type(input, "Goblin");
|
||||||
|
// The Add button appears when name >= 2 chars and no suggestions
|
||||||
|
const addButton = screen.getByRole("button", { name: "Add" });
|
||||||
|
await user.click(addButton);
|
||||||
|
expect(onAddCombatant).toHaveBeenCalledWith("Goblin", undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("submitting with empty name does nothing", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
const onAddCombatant = vi.fn();
|
||||||
|
renderBar({ onAddCombatant });
|
||||||
|
// Submit the form directly (Enter on empty input)
|
||||||
|
const input = screen.getByPlaceholderText("+ Add combatants");
|
||||||
|
await user.type(input, "{Enter}");
|
||||||
|
expect(onAddCombatant).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows custom fields (Init, AC, MaxHP) when name >= 2 chars and no bestiary suggestions", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
renderBar();
|
||||||
|
const input = screen.getByPlaceholderText("+ Add combatants");
|
||||||
|
await user.type(input, "Go");
|
||||||
|
expect(screen.getByPlaceholderText("Init")).toBeInTheDocument();
|
||||||
|
expect(screen.getByPlaceholderText("AC")).toBeInTheDocument();
|
||||||
|
expect(screen.getByPlaceholderText("MaxHP")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows Add button when name >= 2 chars and no suggestions", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
renderBar();
|
||||||
|
const input = screen.getByPlaceholderText("+ Add combatants");
|
||||||
|
await user.type(input, "Go");
|
||||||
|
expect(screen.getByRole("button", { name: "Add" })).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows roll all initiative button when showRollAllInitiative is true", () => {
|
||||||
|
const onRollAllInitiative = vi.fn();
|
||||||
|
renderBar({ showRollAllInitiative: true, onRollAllInitiative });
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Roll all initiative" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("roll all initiative button is disabled when rollAllInitiativeDisabled is true", () => {
|
||||||
|
const onRollAllInitiative = vi.fn();
|
||||||
|
renderBar({
|
||||||
|
showRollAllInitiative: true,
|
||||||
|
onRollAllInitiative,
|
||||||
|
rollAllInitiativeDisabled: true,
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Roll all initiative" }),
|
||||||
|
).toBeDisabled();
|
||||||
|
});
|
||||||
|
});
|
||||||
164
apps/web/src/components/__tests__/combatant-row.test.tsx
Normal file
164
apps/web/src/components/__tests__/combatant-row.test.tsx
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import "@testing-library/jest-dom/vitest";
|
||||||
|
|
||||||
|
import { combatantId } from "@initiative/domain";
|
||||||
|
import { cleanup, render, screen } from "@testing-library/react";
|
||||||
|
import userEvent from "@testing-library/user-event";
|
||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { CombatantRow } from "../combatant-row";
|
||||||
|
import { PLAYER_COLOR_HEX } from "../player-icon-map";
|
||||||
|
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
const defaultProps = {
|
||||||
|
onRename: vi.fn(),
|
||||||
|
onSetInitiative: vi.fn(),
|
||||||
|
onRemove: vi.fn(),
|
||||||
|
onSetHp: vi.fn(),
|
||||||
|
onAdjustHp: vi.fn(),
|
||||||
|
onSetAc: vi.fn(),
|
||||||
|
onToggleCondition: vi.fn(),
|
||||||
|
onToggleConcentration: vi.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
function renderRow(
|
||||||
|
overrides: Partial<{
|
||||||
|
combatant: Parameters<typeof CombatantRow>[0]["combatant"];
|
||||||
|
isActive: boolean;
|
||||||
|
onRollInitiative: (id: ReturnType<typeof combatantId>) => void;
|
||||||
|
onRemove: (id: ReturnType<typeof combatantId>) => void;
|
||||||
|
onShowStatBlock: () => void;
|
||||||
|
}> = {},
|
||||||
|
) {
|
||||||
|
const combatant = overrides.combatant ?? {
|
||||||
|
id: combatantId("1"),
|
||||||
|
name: "Goblin",
|
||||||
|
initiative: 15,
|
||||||
|
maxHp: 10,
|
||||||
|
currentHp: 10,
|
||||||
|
ac: 13,
|
||||||
|
};
|
||||||
|
const props = {
|
||||||
|
...defaultProps,
|
||||||
|
combatant,
|
||||||
|
isActive: overrides.isActive ?? false,
|
||||||
|
onRollInitiative: overrides.onRollInitiative,
|
||||||
|
onShowStatBlock: overrides.onShowStatBlock,
|
||||||
|
onRemove: overrides.onRemove ?? defaultProps.onRemove,
|
||||||
|
};
|
||||||
|
return render(<CombatantRow {...props} />);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("CombatantRow", () => {
|
||||||
|
it("renders combatant name", () => {
|
||||||
|
renderRow();
|
||||||
|
expect(screen.getByText("Goblin")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders initiative value", () => {
|
||||||
|
renderRow();
|
||||||
|
expect(screen.getByText("15")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders current HP", () => {
|
||||||
|
renderRow({
|
||||||
|
combatant: {
|
||||||
|
id: combatantId("1"),
|
||||||
|
name: "Goblin",
|
||||||
|
maxHp: 10,
|
||||||
|
currentHp: 7,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(screen.getByText("7")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("active combatant gets active border styling", () => {
|
||||||
|
const { container } = renderRow({ isActive: true });
|
||||||
|
const row = container.firstElementChild;
|
||||||
|
expect(row?.className).toContain("border-active-row-border");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("unconscious combatant (currentHp === 0) gets dimmed styling", () => {
|
||||||
|
renderRow({
|
||||||
|
combatant: {
|
||||||
|
id: combatantId("1"),
|
||||||
|
name: "Goblin",
|
||||||
|
maxHp: 10,
|
||||||
|
currentHp: 0,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// The name area should have opacity-50
|
||||||
|
const nameEl = screen.getByText("Goblin");
|
||||||
|
const nameContainer = nameEl.closest(".opacity-50");
|
||||||
|
expect(nameContainer).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows '--' for current HP when no maxHp is set", () => {
|
||||||
|
renderRow({
|
||||||
|
combatant: {
|
||||||
|
id: combatantId("1"),
|
||||||
|
name: "Goblin",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(screen.getByLabelText("No HP set")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows concentration icon when isConcentrating is true", () => {
|
||||||
|
renderRow({
|
||||||
|
combatant: {
|
||||||
|
id: combatantId("1"),
|
||||||
|
name: "Goblin",
|
||||||
|
isConcentrating: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const concButton = screen.getByRole("button", {
|
||||||
|
name: "Toggle concentration",
|
||||||
|
});
|
||||||
|
expect(concButton.className).toContain("text-purple-400");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows player character icon and color when set", () => {
|
||||||
|
const { container } = renderRow({
|
||||||
|
combatant: {
|
||||||
|
id: combatantId("1"),
|
||||||
|
name: "Aragorn",
|
||||||
|
color: "red",
|
||||||
|
icon: "sword",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// The icon should be rendered with the player color
|
||||||
|
const svgIcon = container.querySelector("svg[style]");
|
||||||
|
expect(svgIcon).not.toBeNull();
|
||||||
|
expect(svgIcon).toHaveStyle({ color: PLAYER_COLOR_HEX.red });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("remove button calls onRemove after confirmation", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
const onRemove = vi.fn();
|
||||||
|
renderRow({ onRemove });
|
||||||
|
const removeBtn = screen.getByRole("button", {
|
||||||
|
name: "Remove combatant",
|
||||||
|
});
|
||||||
|
// First click enters confirm state
|
||||||
|
await user.click(removeBtn);
|
||||||
|
// Second click confirms
|
||||||
|
const confirmBtn = screen.getByRole("button", {
|
||||||
|
name: "Confirm remove combatant",
|
||||||
|
});
|
||||||
|
await user.click(confirmBtn);
|
||||||
|
expect(onRemove).toHaveBeenCalledWith(combatantId("1"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows d20 roll button when initiative is undefined and onRollInitiative is provided", () => {
|
||||||
|
renderRow({
|
||||||
|
combatant: {
|
||||||
|
id: combatantId("1"),
|
||||||
|
name: "Goblin",
|
||||||
|
},
|
||||||
|
onRollInitiative: vi.fn(),
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Roll initiative" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
63
apps/web/src/components/__tests__/condition-picker.test.tsx
Normal file
63
apps/web/src/components/__tests__/condition-picker.test.tsx
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import "@testing-library/jest-dom/vitest";
|
||||||
|
|
||||||
|
import { CONDITION_DEFINITIONS, type ConditionId } from "@initiative/domain";
|
||||||
|
import { cleanup, render, screen } from "@testing-library/react";
|
||||||
|
import userEvent from "@testing-library/user-event";
|
||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { ConditionPicker } from "../condition-picker";
|
||||||
|
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
function renderPicker(
|
||||||
|
overrides: Partial<{
|
||||||
|
activeConditions: readonly ConditionId[];
|
||||||
|
onToggle: (conditionId: ConditionId) => void;
|
||||||
|
onClose: () => void;
|
||||||
|
}> = {},
|
||||||
|
) {
|
||||||
|
const onToggle = overrides.onToggle ?? vi.fn();
|
||||||
|
const onClose = overrides.onClose ?? vi.fn();
|
||||||
|
const result = render(
|
||||||
|
<ConditionPicker
|
||||||
|
activeConditions={overrides.activeConditions ?? []}
|
||||||
|
onToggle={onToggle}
|
||||||
|
onClose={onClose}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
return { ...result, onToggle, onClose };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("ConditionPicker", () => {
|
||||||
|
it("renders all condition definitions from domain", () => {
|
||||||
|
renderPicker();
|
||||||
|
for (const def of CONDITION_DEFINITIONS) {
|
||||||
|
expect(screen.getByText(def.label)).toBeInTheDocument();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("active conditions are visually distinguished", () => {
|
||||||
|
renderPicker({ activeConditions: ["blinded"] });
|
||||||
|
const blindedButton = screen.getByText("Blinded").closest("button");
|
||||||
|
expect(blindedButton?.className).toContain("bg-card/50");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clicking a condition calls onToggle with that condition's ID", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
const { onToggle } = renderPicker();
|
||||||
|
await user.click(screen.getByText("Poisoned"));
|
||||||
|
expect(onToggle).toHaveBeenCalledWith("poisoned");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("non-active conditions render with muted styling", () => {
|
||||||
|
renderPicker({ activeConditions: [] });
|
||||||
|
const label = screen.getByText("Charmed");
|
||||||
|
expect(label.className).toContain("text-muted-foreground");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("active condition labels use foreground color", () => {
|
||||||
|
renderPicker({ activeConditions: ["charmed"] });
|
||||||
|
const label = screen.getByText("Charmed");
|
||||||
|
expect(label.className).toContain("text-foreground");
|
||||||
|
});
|
||||||
|
});
|
||||||
115
apps/web/src/components/__tests__/hp-adjust-popover.test.tsx
Normal file
115
apps/web/src/components/__tests__/hp-adjust-popover.test.tsx
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import "@testing-library/jest-dom/vitest";
|
||||||
|
|
||||||
|
import { cleanup, render, screen } from "@testing-library/react";
|
||||||
|
import userEvent from "@testing-library/user-event";
|
||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { HpAdjustPopover } from "../hp-adjust-popover";
|
||||||
|
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
function renderPopover(
|
||||||
|
overrides: Partial<{
|
||||||
|
onAdjust: (delta: number) => void;
|
||||||
|
onClose: () => void;
|
||||||
|
}> = {},
|
||||||
|
) {
|
||||||
|
const onAdjust = overrides.onAdjust ?? vi.fn();
|
||||||
|
const onClose = overrides.onClose ?? vi.fn();
|
||||||
|
const result = render(
|
||||||
|
<HpAdjustPopover onAdjust={onAdjust} onClose={onClose} />,
|
||||||
|
);
|
||||||
|
return { ...result, onAdjust, onClose };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("HpAdjustPopover", () => {
|
||||||
|
it("renders input with placeholder 'HP'", () => {
|
||||||
|
renderPopover();
|
||||||
|
expect(screen.getByPlaceholderText("HP")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("damage and heal buttons are disabled when input is empty", () => {
|
||||||
|
renderPopover();
|
||||||
|
expect(screen.getByRole("button", { name: "Apply damage" })).toBeDisabled();
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Apply healing" }),
|
||||||
|
).toBeDisabled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("damage and heal buttons are disabled when input is '0'", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
renderPopover();
|
||||||
|
await user.type(screen.getByPlaceholderText("HP"), "0");
|
||||||
|
expect(screen.getByRole("button", { name: "Apply damage" })).toBeDisabled();
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Apply healing" }),
|
||||||
|
).toBeDisabled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("typing a valid number enables both buttons", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
renderPopover();
|
||||||
|
await user.type(screen.getByPlaceholderText("HP"), "5");
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Apply damage" }),
|
||||||
|
).not.toBeDisabled();
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Apply healing" }),
|
||||||
|
).not.toBeDisabled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clicking damage button calls onAdjust with negative value and onClose", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
const { onAdjust, onClose } = renderPopover();
|
||||||
|
await user.type(screen.getByPlaceholderText("HP"), "7");
|
||||||
|
await user.click(screen.getByRole("button", { name: "Apply damage" }));
|
||||||
|
expect(onAdjust).toHaveBeenCalledWith(-7);
|
||||||
|
expect(onClose).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clicking heal button calls onAdjust with positive value and onClose", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
const { onAdjust, onClose } = renderPopover();
|
||||||
|
await user.type(screen.getByPlaceholderText("HP"), "3");
|
||||||
|
await user.click(screen.getByRole("button", { name: "Apply healing" }));
|
||||||
|
expect(onAdjust).toHaveBeenCalledWith(3);
|
||||||
|
expect(onClose).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Enter key applies damage (negative)", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
const { onAdjust, onClose } = renderPopover();
|
||||||
|
const input = screen.getByPlaceholderText("HP");
|
||||||
|
await user.type(input, "4");
|
||||||
|
await user.keyboard("{Enter}");
|
||||||
|
expect(onAdjust).toHaveBeenCalledWith(-4);
|
||||||
|
expect(onClose).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Shift+Enter applies healing (positive)", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
const { onAdjust, onClose } = renderPopover();
|
||||||
|
const input = screen.getByPlaceholderText("HP");
|
||||||
|
await user.type(input, "6");
|
||||||
|
await user.keyboard("{Shift>}{Enter}{/Shift}");
|
||||||
|
expect(onAdjust).toHaveBeenCalledWith(6);
|
||||||
|
expect(onClose).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Escape key calls onClose", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
const { onClose } = renderPopover();
|
||||||
|
const input = screen.getByPlaceholderText("HP");
|
||||||
|
await user.type(input, "2");
|
||||||
|
await user.keyboard("{Escape}");
|
||||||
|
expect(onClose).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("only accepts digit characters in input", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
renderPopover();
|
||||||
|
const input = screen.getByPlaceholderText("HP");
|
||||||
|
await user.type(input, "12abc34");
|
||||||
|
expect(input).toHaveValue("1234");
|
||||||
|
});
|
||||||
|
});
|
||||||
127
apps/web/src/components/__tests__/source-manager.test.tsx
Normal file
127
apps/web/src/components/__tests__/source-manager.test.tsx
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import "@testing-library/jest-dom/vitest";
|
||||||
|
|
||||||
|
import { cleanup, render, screen, waitFor } from "@testing-library/react";
|
||||||
|
import userEvent from "@testing-library/user-event";
|
||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
vi.mock("../../adapters/bestiary-cache.js", () => ({
|
||||||
|
getCachedSources: vi.fn(),
|
||||||
|
clearSource: vi.fn(),
|
||||||
|
clearAll: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
import * as bestiaryCache from "../../adapters/bestiary-cache.js";
|
||||||
|
import { SourceManager } from "../source-manager";
|
||||||
|
|
||||||
|
const mockGetCachedSources = vi.mocked(bestiaryCache.getCachedSources);
|
||||||
|
const mockClearSource = vi.mocked(bestiaryCache.clearSource);
|
||||||
|
const mockClearAll = vi.mocked(bestiaryCache.clearAll);
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
cleanup();
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("SourceManager", () => {
|
||||||
|
it("shows 'No cached sources' empty state when no sources", async () => {
|
||||||
|
mockGetCachedSources.mockResolvedValue([]);
|
||||||
|
render(<SourceManager onCacheCleared={vi.fn()} />);
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getByText("No cached sources")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lists cached sources with display name and creature count", async () => {
|
||||||
|
mockGetCachedSources.mockResolvedValue([
|
||||||
|
{
|
||||||
|
sourceCode: "mm",
|
||||||
|
displayName: "Monster Manual",
|
||||||
|
creatureCount: 300,
|
||||||
|
cachedAt: Date.now(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sourceCode: "vgm",
|
||||||
|
displayName: "Volo's Guide",
|
||||||
|
creatureCount: 100,
|
||||||
|
cachedAt: Date.now(),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
render(<SourceManager onCacheCleared={vi.fn()} />);
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getByText("Monster Manual")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
expect(screen.getByText("300 creatures")).toBeInTheDocument();
|
||||||
|
expect(screen.getByText("Volo's Guide")).toBeInTheDocument();
|
||||||
|
expect(screen.getByText("100 creatures")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Clear All button calls cache clear and onCacheCleared", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
const onCacheCleared = vi.fn();
|
||||||
|
mockGetCachedSources
|
||||||
|
.mockResolvedValueOnce([
|
||||||
|
{
|
||||||
|
sourceCode: "mm",
|
||||||
|
displayName: "Monster Manual",
|
||||||
|
creatureCount: 300,
|
||||||
|
cachedAt: Date.now(),
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.mockResolvedValue([]);
|
||||||
|
mockClearAll.mockResolvedValue(undefined);
|
||||||
|
render(<SourceManager onCacheCleared={onCacheCleared} />);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getByText("Monster Manual")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
await user.click(screen.getByRole("button", { name: "Clear All" }));
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(mockClearAll).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
expect(onCacheCleared).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("individual source delete button calls clear for that source", async () => {
|
||||||
|
const user = userEvent.setup();
|
||||||
|
const onCacheCleared = vi.fn();
|
||||||
|
mockGetCachedSources
|
||||||
|
.mockResolvedValueOnce([
|
||||||
|
{
|
||||||
|
sourceCode: "mm",
|
||||||
|
displayName: "Monster Manual",
|
||||||
|
creatureCount: 300,
|
||||||
|
cachedAt: Date.now(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sourceCode: "vgm",
|
||||||
|
displayName: "Volo's Guide",
|
||||||
|
creatureCount: 100,
|
||||||
|
cachedAt: Date.now(),
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.mockResolvedValue([
|
||||||
|
{
|
||||||
|
sourceCode: "vgm",
|
||||||
|
displayName: "Volo's Guide",
|
||||||
|
creatureCount: 100,
|
||||||
|
cachedAt: Date.now(),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
mockClearSource.mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
render(<SourceManager onCacheCleared={onCacheCleared} />);
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getByText("Monster Manual")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
await user.click(
|
||||||
|
screen.getByRole("button", { name: "Remove Monster Manual" }),
|
||||||
|
);
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(mockClearSource).toHaveBeenCalledWith("mm");
|
||||||
|
});
|
||||||
|
expect(onCacheCleared).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
199
apps/web/src/components/__tests__/turn-navigation.test.tsx
Normal file
199
apps/web/src/components/__tests__/turn-navigation.test.tsx
Normal file
@@ -0,0 +1,199 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import "@testing-library/jest-dom/vitest";
|
||||||
|
|
||||||
|
import type { Encounter } from "@initiative/domain";
|
||||||
|
import { combatantId } from "@initiative/domain";
|
||||||
|
import { cleanup, render, screen } from "@testing-library/react";
|
||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { TurnNavigation } from "../turn-navigation";
|
||||||
|
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
function renderNav(overrides: Partial<Encounter> = {}) {
|
||||||
|
const encounter: Encounter = {
|
||||||
|
combatants: [
|
||||||
|
{ id: combatantId("1"), name: "Goblin" },
|
||||||
|
{ id: combatantId("2"), name: "Conjurer" },
|
||||||
|
],
|
||||||
|
activeIndex: 0,
|
||||||
|
roundNumber: 1,
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
|
||||||
|
return render(
|
||||||
|
<TurnNavigation
|
||||||
|
encounter={encounter}
|
||||||
|
onAdvanceTurn={vi.fn()}
|
||||||
|
onRetreatTurn={vi.fn()}
|
||||||
|
onClearEncounter={vi.fn()}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("TurnNavigation", () => {
|
||||||
|
describe("US1: Round badge and combatant name", () => {
|
||||||
|
it("renders the round badge with correct round number", () => {
|
||||||
|
renderNav({ roundNumber: 3 });
|
||||||
|
expect(screen.getByText("R3")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders the combatant name separately from the round badge", () => {
|
||||||
|
renderNav();
|
||||||
|
const badge = screen.getByText("R1");
|
||||||
|
const name = screen.getByText("Goblin");
|
||||||
|
expect(badge).toBeInTheDocument();
|
||||||
|
expect(name).toBeInTheDocument();
|
||||||
|
expect(badge).not.toBe(name);
|
||||||
|
expect(badge.closest("[class]")).not.toBe(name.closest("[class]"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not render an em dash between round and name", () => {
|
||||||
|
const { container } = renderNav();
|
||||||
|
expect(container.textContent).not.toContain("—");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("round badge and combatant name are siblings in the center area", () => {
|
||||||
|
renderNav();
|
||||||
|
const badge = screen.getByText("R1");
|
||||||
|
const name = screen.getByText("Goblin");
|
||||||
|
expect(badge.parentElement).toBe(name.parentElement);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("updates the round badge when round changes", () => {
|
||||||
|
const { rerender } = render(
|
||||||
|
<TurnNavigation
|
||||||
|
encounter={{
|
||||||
|
combatants: [{ id: combatantId("1"), name: "Goblin" }],
|
||||||
|
activeIndex: 0,
|
||||||
|
roundNumber: 2,
|
||||||
|
}}
|
||||||
|
onAdvanceTurn={vi.fn()}
|
||||||
|
onRetreatTurn={vi.fn()}
|
||||||
|
onClearEncounter={vi.fn()}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
expect(screen.getByText("R2")).toBeInTheDocument();
|
||||||
|
|
||||||
|
rerender(
|
||||||
|
<TurnNavigation
|
||||||
|
encounter={{
|
||||||
|
combatants: [{ id: combatantId("1"), name: "Goblin" }],
|
||||||
|
activeIndex: 0,
|
||||||
|
roundNumber: 3,
|
||||||
|
}}
|
||||||
|
onAdvanceTurn={vi.fn()}
|
||||||
|
onRetreatTurn={vi.fn()}
|
||||||
|
onClearEncounter={vi.fn()}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
expect(screen.getByText("R3")).toBeInTheDocument();
|
||||||
|
expect(screen.queryByText("R2")).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders the next combatant name when turn advances", () => {
|
||||||
|
const { rerender } = render(
|
||||||
|
<TurnNavigation
|
||||||
|
encounter={{
|
||||||
|
combatants: [
|
||||||
|
{ id: combatantId("1"), name: "Goblin" },
|
||||||
|
{ id: combatantId("2"), name: "Conjurer" },
|
||||||
|
],
|
||||||
|
activeIndex: 0,
|
||||||
|
roundNumber: 1,
|
||||||
|
}}
|
||||||
|
onAdvanceTurn={vi.fn()}
|
||||||
|
onRetreatTurn={vi.fn()}
|
||||||
|
onClearEncounter={vi.fn()}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
expect(screen.getByText("Goblin")).toBeInTheDocument();
|
||||||
|
|
||||||
|
rerender(
|
||||||
|
<TurnNavigation
|
||||||
|
encounter={{
|
||||||
|
combatants: [
|
||||||
|
{ id: combatantId("1"), name: "Goblin" },
|
||||||
|
{ id: combatantId("2"), name: "Conjurer" },
|
||||||
|
],
|
||||||
|
activeIndex: 1,
|
||||||
|
roundNumber: 1,
|
||||||
|
}}
|
||||||
|
onAdvanceTurn={vi.fn()}
|
||||||
|
onRetreatTurn={vi.fn()}
|
||||||
|
onClearEncounter={vi.fn()}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
expect(screen.getByText("Conjurer")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("US2: Layout robustness", () => {
|
||||||
|
it("applies truncation styles to long combatant names", () => {
|
||||||
|
const longName =
|
||||||
|
"Ancient Red Dragon Wyrm of the Northern Wastes and Beyond";
|
||||||
|
renderNav({
|
||||||
|
combatants: [{ id: combatantId("1"), name: longName }],
|
||||||
|
});
|
||||||
|
const nameEl = screen.getByText(longName);
|
||||||
|
expect(nameEl.className).toContain("truncate");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders three-zone layout with a single-character name", () => {
|
||||||
|
renderNav({
|
||||||
|
combatants: [{ id: combatantId("1"), name: "O" }],
|
||||||
|
});
|
||||||
|
expect(screen.getByText("R1")).toBeInTheDocument();
|
||||||
|
expect(screen.getByText("O")).toBeInTheDocument();
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Previous turn" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Next turn" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps all action buttons accessible regardless of name length", () => {
|
||||||
|
const longName = "A".repeat(60);
|
||||||
|
renderNav({
|
||||||
|
combatants: [{ id: combatantId("1"), name: longName }],
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Previous turn" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Next turn" }),
|
||||||
|
).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders a 40-character name without truncation class issues", () => {
|
||||||
|
const name40 = "A".repeat(40);
|
||||||
|
renderNav({
|
||||||
|
combatants: [{ id: combatantId("1"), name: name40 }],
|
||||||
|
});
|
||||||
|
const nameEl = screen.getByText(name40);
|
||||||
|
expect(nameEl).toBeInTheDocument();
|
||||||
|
// The truncate class is applied but CSS only visually truncates if content overflows
|
||||||
|
expect(nameEl.className).toContain("truncate");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("US3: No combatants state", () => {
|
||||||
|
it("shows the round badge when there are no combatants", () => {
|
||||||
|
renderNav({ combatants: [], roundNumber: 1 });
|
||||||
|
expect(screen.getByText("R1")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows 'No combatants' placeholder text", () => {
|
||||||
|
renderNav({ combatants: [] });
|
||||||
|
expect(screen.getByText("No combatants")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("disables navigation buttons when there are no combatants", () => {
|
||||||
|
renderNav({ combatants: [] });
|
||||||
|
expect(
|
||||||
|
screen.getByRole("button", { name: "Previous turn" }),
|
||||||
|
).toBeDisabled();
|
||||||
|
expect(screen.getByRole("button", { name: "Next turn" })).toBeDisabled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -12,7 +12,7 @@ export function AcShield({ value, onClick, className }: AcShieldProps) {
|
|||||||
type="button"
|
type="button"
|
||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
className={cn(
|
className={cn(
|
||||||
"relative inline-flex items-center justify-center text-sm tabular-nums text-muted-foreground transition-colors hover:text-hover-neutral",
|
"relative inline-flex items-center justify-center text-muted-foreground text-sm tabular-nums transition-colors hover:text-hover-neutral",
|
||||||
className,
|
className,
|
||||||
)}
|
)}
|
||||||
style={{ width: 28, height: 32 }}
|
style={{ width: 28, height: 32 }}
|
||||||
@@ -29,8 +29,8 @@ export function AcShield({ value, onClick, className }: AcShieldProps) {
|
|||||||
>
|
>
|
||||||
<path d="M14 1.5 L2.5 6.5 L2.5 15 Q2.5 25 14 30.5 Q25.5 25 25.5 15 L25.5 6.5 Z" />
|
<path d="M14 1.5 L2.5 6.5 L2.5 15 Q2.5 25 14 30.5 Q25.5 25 25.5 15 L25.5 6.5 Z" />
|
||||||
</svg>
|
</svg>
|
||||||
<span className="relative text-xs font-medium leading-none">
|
<span className="relative font-medium text-xs leading-none">
|
||||||
{value !== undefined ? value : "\u2014"}
|
{value == null ? "\u2014" : String(value)}
|
||||||
</span>
|
</span>
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,18 +1,280 @@
|
|||||||
import type { Creature } from "@initiative/domain";
|
import type { PlayerCharacter } from "@initiative/domain";
|
||||||
import { Search } from "lucide-react";
|
import {
|
||||||
import { type FormEvent, useState } from "react";
|
Check,
|
||||||
import { BestiarySearch } from "./bestiary-search.js";
|
Eye,
|
||||||
|
EyeOff,
|
||||||
|
Import,
|
||||||
|
Library,
|
||||||
|
Minus,
|
||||||
|
Monitor,
|
||||||
|
Moon,
|
||||||
|
Plus,
|
||||||
|
Sun,
|
||||||
|
Users,
|
||||||
|
} from "lucide-react";
|
||||||
|
import React, { type RefObject, useDeferredValue, useState } from "react";
|
||||||
|
import type { SearchResult } from "../hooks/use-bestiary.js";
|
||||||
|
import { cn } from "../lib/utils.js";
|
||||||
|
import { D20Icon } from "./d20-icon.js";
|
||||||
|
import { PLAYER_COLOR_HEX, PLAYER_ICON_MAP } from "./player-icon-map";
|
||||||
import { Button } from "./ui/button.js";
|
import { Button } from "./ui/button.js";
|
||||||
import { Input } from "./ui/input.js";
|
import { Input } from "./ui/input.js";
|
||||||
|
import { OverflowMenu, type OverflowMenuItem } from "./ui/overflow-menu.js";
|
||||||
|
|
||||||
|
interface QueuedCreature {
|
||||||
|
result: SearchResult;
|
||||||
|
count: number;
|
||||||
|
}
|
||||||
|
|
||||||
interface ActionBarProps {
|
interface ActionBarProps {
|
||||||
onAddCombatant: (name: string) => void;
|
onAddCombatant: (
|
||||||
onAddFromBestiary: (creature: Creature) => void;
|
name: string,
|
||||||
bestiarySearch: (query: string) => Creature[];
|
opts?: { initiative?: number; ac?: number; maxHp?: number },
|
||||||
|
) => void;
|
||||||
|
onAddFromBestiary: (result: SearchResult) => void;
|
||||||
|
bestiarySearch: (query: string) => SearchResult[];
|
||||||
bestiaryLoaded: boolean;
|
bestiaryLoaded: boolean;
|
||||||
suggestions: Creature[];
|
onViewStatBlock?: (result: SearchResult) => void;
|
||||||
onSearchChange: (query: string) => void;
|
onBulkImport?: () => void;
|
||||||
onShowStatBlock?: (creature: Creature) => void;
|
bulkImportDisabled?: boolean;
|
||||||
|
inputRef?: RefObject<HTMLInputElement | null>;
|
||||||
|
playerCharacters?: readonly PlayerCharacter[];
|
||||||
|
onAddFromPlayerCharacter?: (pc: PlayerCharacter) => void;
|
||||||
|
onManagePlayers?: () => void;
|
||||||
|
onRollAllInitiative?: () => void;
|
||||||
|
showRollAllInitiative?: boolean;
|
||||||
|
rollAllInitiativeDisabled?: boolean;
|
||||||
|
onOpenSourceManager?: () => void;
|
||||||
|
autoFocus?: boolean;
|
||||||
|
themePreference?: "system" | "light" | "dark";
|
||||||
|
onCycleTheme?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
function creatureKey(r: SearchResult): string {
|
||||||
|
return `${r.source}:${r.name}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function AddModeSuggestions({
|
||||||
|
nameInput,
|
||||||
|
suggestions,
|
||||||
|
pcMatches,
|
||||||
|
suggestionIndex,
|
||||||
|
queued,
|
||||||
|
onDismiss,
|
||||||
|
onClickSuggestion,
|
||||||
|
onSetSuggestionIndex,
|
||||||
|
onSetQueued,
|
||||||
|
onConfirmQueued,
|
||||||
|
onAddFromPlayerCharacter,
|
||||||
|
onClear,
|
||||||
|
}: Readonly<{
|
||||||
|
nameInput: string;
|
||||||
|
suggestions: SearchResult[];
|
||||||
|
pcMatches: PlayerCharacter[];
|
||||||
|
suggestionIndex: number;
|
||||||
|
queued: QueuedCreature | null;
|
||||||
|
onDismiss: () => void;
|
||||||
|
onClear: () => void;
|
||||||
|
onClickSuggestion: (result: SearchResult) => void;
|
||||||
|
onSetSuggestionIndex: (i: number) => void;
|
||||||
|
onSetQueued: (q: QueuedCreature | null) => void;
|
||||||
|
onConfirmQueued: () => void;
|
||||||
|
onAddFromPlayerCharacter?: (pc: PlayerCharacter) => void;
|
||||||
|
}>) {
|
||||||
|
return (
|
||||||
|
<div className="card-glow absolute bottom-full z-50 mb-1 w-full max-w-xs rounded-lg border border-border bg-card">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="flex w-full items-center gap-1.5 border-border border-b px-3 py-2 text-left text-accent text-sm hover:bg-accent/20"
|
||||||
|
onMouseDown={(e) => e.preventDefault()}
|
||||||
|
onClick={onDismiss}
|
||||||
|
>
|
||||||
|
<Plus className="h-3.5 w-3.5" />
|
||||||
|
<span className="flex-1">Add "{nameInput}" as custom</span>
|
||||||
|
<kbd className="rounded border border-border px-1.5 py-0.5 text-muted-foreground text-xs">
|
||||||
|
Esc
|
||||||
|
</kbd>
|
||||||
|
</button>
|
||||||
|
<div className="max-h-48 overflow-y-auto py-1">
|
||||||
|
{pcMatches.length > 0 && (
|
||||||
|
<>
|
||||||
|
<div className="px-3 py-1 font-medium text-muted-foreground text-xs">
|
||||||
|
Players
|
||||||
|
</div>
|
||||||
|
<ul>
|
||||||
|
{pcMatches.map((pc) => {
|
||||||
|
const PcIcon = pc.icon ? PLAYER_ICON_MAP[pc.icon] : undefined;
|
||||||
|
const pcColor = pc.color
|
||||||
|
? PLAYER_COLOR_HEX[pc.color]
|
||||||
|
: undefined;
|
||||||
|
return (
|
||||||
|
<li key={pc.id}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="flex w-full items-center gap-2 px-3 py-1.5 text-left text-foreground text-sm hover:bg-hover-neutral-bg"
|
||||||
|
onMouseDown={(e) => e.preventDefault()}
|
||||||
|
onClick={() => {
|
||||||
|
onAddFromPlayerCharacter?.(pc);
|
||||||
|
onClear();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{!!PcIcon && (
|
||||||
|
<PcIcon size={14} style={{ color: pcColor }} />
|
||||||
|
)}
|
||||||
|
<span className="flex-1 truncate">{pc.name}</span>
|
||||||
|
<span className="text-muted-foreground text-xs">
|
||||||
|
Player
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</ul>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{suggestions.length > 0 && (
|
||||||
|
<ul>
|
||||||
|
{suggestions.map((result, i) => {
|
||||||
|
const key = creatureKey(result);
|
||||||
|
const isQueued =
|
||||||
|
queued !== null && creatureKey(queued.result) === key;
|
||||||
|
return (
|
||||||
|
<li key={key}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={cn(
|
||||||
|
"flex w-full items-center justify-between px-3 py-1.5 text-left text-foreground text-sm",
|
||||||
|
isQueued && "bg-accent/30",
|
||||||
|
!isQueued && i === suggestionIndex && "bg-accent/20",
|
||||||
|
!isQueued &&
|
||||||
|
i !== suggestionIndex &&
|
||||||
|
"hover:bg-hover-neutral-bg",
|
||||||
|
)}
|
||||||
|
onMouseDown={(e) => e.preventDefault()}
|
||||||
|
onClick={() => onClickSuggestion(result)}
|
||||||
|
onMouseEnter={() => onSetSuggestionIndex(i)}
|
||||||
|
>
|
||||||
|
<span>{result.name}</span>
|
||||||
|
<span className="flex items-center gap-1 text-muted-foreground text-xs">
|
||||||
|
{isQueued ? (
|
||||||
|
<>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="rounded p-0.5 text-foreground hover:bg-accent/40"
|
||||||
|
onMouseDown={(e) => e.preventDefault()}
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
if (queued.count <= 1) {
|
||||||
|
onSetQueued(null);
|
||||||
|
} else {
|
||||||
|
onSetQueued({
|
||||||
|
...queued,
|
||||||
|
count: queued.count - 1,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Minus className="h-3 w-3" />
|
||||||
|
</button>
|
||||||
|
<span className="min-w-5 rounded-full bg-accent px-1.5 py-0.5 text-center font-medium text-primary-foreground">
|
||||||
|
{queued.count}
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="rounded p-0.5 text-foreground hover:bg-accent/40"
|
||||||
|
onMouseDown={(e) => e.preventDefault()}
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
onSetQueued({
|
||||||
|
...queued,
|
||||||
|
count: queued.count + 1,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Plus className="h-3 w-3" />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="ml-0.5 rounded p-0.5 text-foreground hover:bg-accent/40"
|
||||||
|
onMouseDown={(e) => e.preventDefault()}
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
onConfirmQueued();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Check className="h-3.5 w-3.5" />
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
result.sourceDisplayName
|
||||||
|
)}
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</ul>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const THEME_ICONS = {
|
||||||
|
system: Monitor,
|
||||||
|
light: Sun,
|
||||||
|
dark: Moon,
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
const THEME_LABELS = {
|
||||||
|
system: "Theme: System",
|
||||||
|
light: "Theme: Light",
|
||||||
|
dark: "Theme: Dark",
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
function buildOverflowItems(opts: {
|
||||||
|
onManagePlayers?: () => void;
|
||||||
|
onOpenSourceManager?: () => void;
|
||||||
|
bestiaryLoaded: boolean;
|
||||||
|
onBulkImport?: () => void;
|
||||||
|
bulkImportDisabled?: boolean;
|
||||||
|
themePreference?: "system" | "light" | "dark";
|
||||||
|
onCycleTheme?: () => void;
|
||||||
|
}): OverflowMenuItem[] {
|
||||||
|
const items: OverflowMenuItem[] = [];
|
||||||
|
if (opts.onManagePlayers) {
|
||||||
|
items.push({
|
||||||
|
icon: <Users className="h-4 w-4" />,
|
||||||
|
label: "Player Characters",
|
||||||
|
onClick: opts.onManagePlayers,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (opts.onOpenSourceManager) {
|
||||||
|
items.push({
|
||||||
|
icon: <Library className="h-4 w-4" />,
|
||||||
|
label: "Manage Sources",
|
||||||
|
onClick: opts.onOpenSourceManager,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (opts.bestiaryLoaded && opts.onBulkImport) {
|
||||||
|
items.push({
|
||||||
|
icon: <Import className="h-4 w-4" />,
|
||||||
|
label: "Import All Sources",
|
||||||
|
onClick: opts.onBulkImport,
|
||||||
|
disabled: opts.bulkImportDisabled,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (opts.onCycleTheme) {
|
||||||
|
const pref = opts.themePreference ?? "system";
|
||||||
|
const ThemeIcon = THEME_ICONS[pref];
|
||||||
|
items.push({
|
||||||
|
icon: <ThemeIcon className="h-4 w-4" />,
|
||||||
|
label: THEME_LABELS[pref],
|
||||||
|
onClick: opts.onCycleTheme,
|
||||||
|
keepOpen: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ActionBar({
|
export function ActionBar({
|
||||||
@@ -20,46 +282,177 @@ export function ActionBar({
|
|||||||
onAddFromBestiary,
|
onAddFromBestiary,
|
||||||
bestiarySearch,
|
bestiarySearch,
|
||||||
bestiaryLoaded,
|
bestiaryLoaded,
|
||||||
suggestions,
|
onViewStatBlock,
|
||||||
onSearchChange,
|
onBulkImport,
|
||||||
onShowStatBlock,
|
bulkImportDisabled,
|
||||||
}: ActionBarProps) {
|
inputRef,
|
||||||
|
playerCharacters,
|
||||||
|
onAddFromPlayerCharacter,
|
||||||
|
onManagePlayers,
|
||||||
|
onRollAllInitiative,
|
||||||
|
showRollAllInitiative,
|
||||||
|
rollAllInitiativeDisabled,
|
||||||
|
onOpenSourceManager,
|
||||||
|
autoFocus,
|
||||||
|
themePreference,
|
||||||
|
onCycleTheme,
|
||||||
|
}: Readonly<ActionBarProps>) {
|
||||||
const [nameInput, setNameInput] = useState("");
|
const [nameInput, setNameInput] = useState("");
|
||||||
const [searchOpen, setSearchOpen] = useState(false);
|
const [suggestions, setSuggestions] = useState<SearchResult[]>([]);
|
||||||
|
const [pcMatches, setPcMatches] = useState<PlayerCharacter[]>([]);
|
||||||
|
const deferredSuggestions = useDeferredValue(suggestions);
|
||||||
|
const deferredPcMatches = useDeferredValue(pcMatches);
|
||||||
const [suggestionIndex, setSuggestionIndex] = useState(-1);
|
const [suggestionIndex, setSuggestionIndex] = useState(-1);
|
||||||
|
const [queued, setQueued] = useState<QueuedCreature | null>(null);
|
||||||
|
const [customInit, setCustomInit] = useState("");
|
||||||
|
const [customAc, setCustomAc] = useState("");
|
||||||
|
const [customMaxHp, setCustomMaxHp] = useState("");
|
||||||
|
const [browseMode, setBrowseMode] = useState(false);
|
||||||
|
|
||||||
const handleAdd = (e: FormEvent) => {
|
const clearCustomFields = () => {
|
||||||
e.preventDefault();
|
setCustomInit("");
|
||||||
if (nameInput.trim() === "") return;
|
setCustomAc("");
|
||||||
onAddCombatant(nameInput);
|
setCustomMaxHp("");
|
||||||
|
};
|
||||||
|
|
||||||
|
const clearInput = () => {
|
||||||
setNameInput("");
|
setNameInput("");
|
||||||
onSearchChange("");
|
setSuggestions([]);
|
||||||
|
setPcMatches([]);
|
||||||
|
setQueued(null);
|
||||||
|
setSuggestionIndex(-1);
|
||||||
|
};
|
||||||
|
|
||||||
|
const dismissSuggestions = () => {
|
||||||
|
setSuggestions([]);
|
||||||
|
setPcMatches([]);
|
||||||
|
setQueued(null);
|
||||||
|
setSuggestionIndex(-1);
|
||||||
|
};
|
||||||
|
|
||||||
|
const confirmQueued = () => {
|
||||||
|
if (!queued) return;
|
||||||
|
for (let i = 0; i < queued.count; i++) {
|
||||||
|
onAddFromBestiary(queued.result);
|
||||||
|
}
|
||||||
|
clearInput();
|
||||||
|
};
|
||||||
|
|
||||||
|
const parseNum = (v: string): number | undefined => {
|
||||||
|
if (v.trim() === "") return undefined;
|
||||||
|
const n = Number(v);
|
||||||
|
return Number.isNaN(n) ? undefined : n;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleAdd = (e: React.SubmitEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (browseMode) return;
|
||||||
|
if (queued) {
|
||||||
|
confirmQueued();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (nameInput.trim() === "") return;
|
||||||
|
const opts: { initiative?: number; ac?: number; maxHp?: number } = {};
|
||||||
|
const init = parseNum(customInit);
|
||||||
|
const ac = parseNum(customAc);
|
||||||
|
const maxHp = parseNum(customMaxHp);
|
||||||
|
if (init !== undefined) opts.initiative = init;
|
||||||
|
if (ac !== undefined) opts.ac = ac;
|
||||||
|
if (maxHp !== undefined) opts.maxHp = maxHp;
|
||||||
|
onAddCombatant(nameInput, Object.keys(opts).length > 0 ? opts : undefined);
|
||||||
|
setNameInput("");
|
||||||
|
setSuggestions([]);
|
||||||
|
setPcMatches([]);
|
||||||
|
clearCustomFields();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleBrowseSearch = (value: string) => {
|
||||||
|
setSuggestions(value.length >= 2 ? bestiarySearch(value) : []);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleAddSearch = (value: string) => {
|
||||||
|
let newSuggestions: SearchResult[] = [];
|
||||||
|
let newPcMatches: PlayerCharacter[] = [];
|
||||||
|
if (value.length >= 2) {
|
||||||
|
newSuggestions = bestiarySearch(value);
|
||||||
|
setSuggestions(newSuggestions);
|
||||||
|
if (playerCharacters && playerCharacters.length > 0) {
|
||||||
|
const lower = value.toLowerCase();
|
||||||
|
newPcMatches = playerCharacters.filter((pc) =>
|
||||||
|
pc.name.toLowerCase().includes(lower),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
setPcMatches(newPcMatches);
|
||||||
|
} else {
|
||||||
|
setSuggestions([]);
|
||||||
|
setPcMatches([]);
|
||||||
|
}
|
||||||
|
if (newSuggestions.length > 0 || newPcMatches.length > 0) {
|
||||||
|
clearCustomFields();
|
||||||
|
}
|
||||||
|
if (queued) {
|
||||||
|
const qKey = creatureKey(queued.result);
|
||||||
|
const stillVisible = newSuggestions.some((s) => creatureKey(s) === qKey);
|
||||||
|
if (!stillVisible) {
|
||||||
|
setQueued(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleNameChange = (value: string) => {
|
const handleNameChange = (value: string) => {
|
||||||
setNameInput(value);
|
setNameInput(value);
|
||||||
setSuggestionIndex(-1);
|
setSuggestionIndex(-1);
|
||||||
onSearchChange(value);
|
if (browseMode) {
|
||||||
|
handleBrowseSearch(value);
|
||||||
|
} else {
|
||||||
|
handleAddSearch(value);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSelectCreature = (creature: Creature) => {
|
const handleClickSuggestion = (result: SearchResult) => {
|
||||||
onAddFromBestiary(creature);
|
const key = creatureKey(result);
|
||||||
setSearchOpen(false);
|
if (queued && creatureKey(queued.result) === key) {
|
||||||
setNameInput("");
|
setQueued({ ...queued, count: queued.count + 1 });
|
||||||
onSearchChange("");
|
} else {
|
||||||
onShowStatBlock?.(creature);
|
setQueued({ result, count: 1 });
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSelectSuggestion = (creature: Creature) => {
|
const handleEnter = () => {
|
||||||
onAddFromBestiary(creature);
|
if (queued) {
|
||||||
setNameInput("");
|
confirmQueued();
|
||||||
onSearchChange("");
|
} else if (suggestionIndex >= 0) {
|
||||||
onShowStatBlock?.(creature);
|
handleClickSuggestion(suggestions[suggestionIndex]);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const hasSuggestions =
|
||||||
|
deferredSuggestions.length > 0 || deferredPcMatches.length > 0;
|
||||||
|
|
||||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||||
if (suggestions.length === 0) return;
|
if (!hasSuggestions) return;
|
||||||
|
|
||||||
|
if (e.key === "ArrowDown") {
|
||||||
|
e.preventDefault();
|
||||||
|
setSuggestionIndex((i) => (i < suggestions.length - 1 ? i + 1 : 0));
|
||||||
|
} else if (e.key === "ArrowUp") {
|
||||||
|
e.preventDefault();
|
||||||
|
setSuggestionIndex((i) => (i > 0 ? i - 1 : suggestions.length - 1));
|
||||||
|
} else if (e.key === "Enter") {
|
||||||
|
e.preventDefault();
|
||||||
|
handleEnter();
|
||||||
|
} else if (e.key === "Escape") {
|
||||||
|
dismissSuggestions();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleBrowseKeyDown = (e: React.KeyboardEvent) => {
|
||||||
|
if (e.key === "Escape") {
|
||||||
|
setBrowseMode(false);
|
||||||
|
clearInput();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (suggestions.length === 0) return;
|
||||||
if (e.key === "ArrowDown") {
|
if (e.key === "ArrowDown") {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setSuggestionIndex((i) => (i < suggestions.length - 1 ? i + 1 : 0));
|
setSuggestionIndex((i) => (i < suggestions.length - 1 ? i + 1 : 0));
|
||||||
@@ -68,53 +461,95 @@ export function ActionBar({
|
|||||||
setSuggestionIndex((i) => (i > 0 ? i - 1 : suggestions.length - 1));
|
setSuggestionIndex((i) => (i > 0 ? i - 1 : suggestions.length - 1));
|
||||||
} else if (e.key === "Enter" && suggestionIndex >= 0) {
|
} else if (e.key === "Enter" && suggestionIndex >= 0) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
handleSelectSuggestion(suggestions[suggestionIndex]);
|
onViewStatBlock?.(suggestions[suggestionIndex]);
|
||||||
} else if (e.key === "Escape") {
|
setBrowseMode(false);
|
||||||
setSuggestionIndex(-1);
|
clearInput();
|
||||||
onSearchChange("");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleBrowseSelect = (result: SearchResult) => {
|
||||||
|
onViewStatBlock?.(result);
|
||||||
|
setBrowseMode(false);
|
||||||
|
clearInput();
|
||||||
|
};
|
||||||
|
|
||||||
|
const toggleBrowseMode = () => {
|
||||||
|
setBrowseMode((m) => !m);
|
||||||
|
clearInput();
|
||||||
|
clearCustomFields();
|
||||||
|
};
|
||||||
|
|
||||||
|
const overflowItems = buildOverflowItems({
|
||||||
|
onManagePlayers,
|
||||||
|
onOpenSourceManager,
|
||||||
|
bestiaryLoaded,
|
||||||
|
onBulkImport,
|
||||||
|
bulkImportDisabled,
|
||||||
|
themePreference,
|
||||||
|
onCycleTheme,
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex items-center gap-3 rounded-md border border-border bg-card px-4 py-3">
|
<div className="card-glow flex items-center gap-3 rounded-lg border border-border bg-card px-4 py-3">
|
||||||
{searchOpen ? (
|
|
||||||
<BestiarySearch
|
|
||||||
onSelectCreature={handleSelectCreature}
|
|
||||||
onClose={() => setSearchOpen(false)}
|
|
||||||
searchFn={bestiarySearch}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<form
|
<form
|
||||||
onSubmit={handleAdd}
|
onSubmit={handleAdd}
|
||||||
className="relative flex flex-1 items-center gap-2"
|
className="relative flex flex-1 items-center gap-2"
|
||||||
>
|
>
|
||||||
<div className="relative flex-1">
|
<div className="flex-1">
|
||||||
|
<div className="relative max-w-xs">
|
||||||
<Input
|
<Input
|
||||||
|
ref={inputRef}
|
||||||
type="text"
|
type="text"
|
||||||
value={nameInput}
|
value={nameInput}
|
||||||
onChange={(e) => handleNameChange(e.target.value)}
|
onChange={(e) => handleNameChange(e.target.value)}
|
||||||
onKeyDown={handleKeyDown}
|
onKeyDown={browseMode ? handleBrowseKeyDown : handleKeyDown}
|
||||||
placeholder="Combatant name"
|
placeholder={
|
||||||
className="max-w-xs"
|
browseMode ? "Search stat blocks..." : "+ Add combatants"
|
||||||
|
}
|
||||||
|
className="pr-8"
|
||||||
|
autoFocus={autoFocus}
|
||||||
/>
|
/>
|
||||||
{suggestions.length > 0 && (
|
{bestiaryLoaded && !!onViewStatBlock && (
|
||||||
<div className="absolute bottom-full z-50 mb-1 w-full max-w-xs rounded-md border border-border bg-card shadow-lg">
|
|
||||||
<ul className="max-h-48 overflow-y-auto py-1">
|
|
||||||
{suggestions.map((creature, i) => (
|
|
||||||
<li key={creature.id}>
|
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className={`flex w-full items-center justify-between px-3 py-1.5 text-left text-sm ${
|
tabIndex={-1}
|
||||||
|
className={cn(
|
||||||
|
"absolute top-1/2 right-2 -translate-y-1/2 text-muted-foreground hover:text-hover-neutral",
|
||||||
|
browseMode && "text-accent",
|
||||||
|
)}
|
||||||
|
onClick={toggleBrowseMode}
|
||||||
|
title={browseMode ? "Switch to add mode" : "Browse stat blocks"}
|
||||||
|
aria-label={
|
||||||
|
browseMode ? "Switch to add mode" : "Browse stat blocks"
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{browseMode ? (
|
||||||
|
<EyeOff className="h-4 w-4" />
|
||||||
|
) : (
|
||||||
|
<Eye className="h-4 w-4" />
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
{browseMode && deferredSuggestions.length > 0 && (
|
||||||
|
<div className="card-glow absolute bottom-full z-50 mb-1 w-full rounded-lg border border-border bg-card">
|
||||||
|
<ul className="max-h-48 overflow-y-auto py-1">
|
||||||
|
{deferredSuggestions.map((result, i) => (
|
||||||
|
<li key={creatureKey(result)}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={cn(
|
||||||
|
"flex w-full items-center justify-between px-3 py-1.5 text-left text-sm",
|
||||||
i === suggestionIndex
|
i === suggestionIndex
|
||||||
? "bg-accent/20 text-foreground"
|
? "bg-accent/20 text-foreground"
|
||||||
: "text-foreground hover:bg-hover-neutral-bg"
|
: "text-foreground hover:bg-hover-neutral-bg",
|
||||||
}`}
|
)}
|
||||||
onClick={() => handleSelectSuggestion(creature)}
|
onMouseDown={(e) => e.preventDefault()}
|
||||||
|
onClick={() => handleBrowseSelect(result)}
|
||||||
onMouseEnter={() => setSuggestionIndex(i)}
|
onMouseEnter={() => setSuggestionIndex(i)}
|
||||||
>
|
>
|
||||||
<span>{creature.name}</span>
|
<span>{result.name}</span>
|
||||||
<span className="text-xs text-muted-foreground">
|
<span className="text-muted-foreground text-xs">
|
||||||
{creature.sourceDisplayName}
|
{result.sourceDisplayName}
|
||||||
</span>
|
</span>
|
||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
@@ -122,22 +557,71 @@ export function ActionBar({
|
|||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{!browseMode && hasSuggestions && (
|
||||||
|
<AddModeSuggestions
|
||||||
|
nameInput={nameInput}
|
||||||
|
suggestions={deferredSuggestions}
|
||||||
|
pcMatches={deferredPcMatches}
|
||||||
|
suggestionIndex={suggestionIndex}
|
||||||
|
queued={queued}
|
||||||
|
onDismiss={dismissSuggestions}
|
||||||
|
onClear={clearInput}
|
||||||
|
onClickSuggestion={handleClickSuggestion}
|
||||||
|
onSetSuggestionIndex={setSuggestionIndex}
|
||||||
|
onSetQueued={setQueued}
|
||||||
|
onConfirmQueued={confirmQueued}
|
||||||
|
onAddFromPlayerCharacter={onAddFromPlayerCharacter}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<Button type="submit" size="sm">
|
</div>
|
||||||
Add
|
{!browseMode && nameInput.length >= 2 && !hasSuggestions && (
|
||||||
</Button>
|
<div className="flex items-center gap-2">
|
||||||
{bestiaryLoaded && (
|
<Input
|
||||||
|
type="text"
|
||||||
|
inputMode="numeric"
|
||||||
|
value={customInit}
|
||||||
|
onChange={(e) => setCustomInit(e.target.value)}
|
||||||
|
placeholder="Init"
|
||||||
|
className="w-16 text-center"
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
inputMode="numeric"
|
||||||
|
value={customAc}
|
||||||
|
onChange={(e) => setCustomAc(e.target.value)}
|
||||||
|
placeholder="AC"
|
||||||
|
className="w-16 text-center"
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
inputMode="numeric"
|
||||||
|
value={customMaxHp}
|
||||||
|
onChange={(e) => setCustomMaxHp(e.target.value)}
|
||||||
|
placeholder="MaxHP"
|
||||||
|
className="w-18 text-center"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{!browseMode && nameInput.length >= 2 && !hasSuggestions && (
|
||||||
|
<Button type="submit">Add</Button>
|
||||||
|
)}
|
||||||
|
{showRollAllInitiative && !!onRollAllInitiative && (
|
||||||
<Button
|
<Button
|
||||||
type="button"
|
type="button"
|
||||||
size="sm"
|
size="icon"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
onClick={() => setSearchOpen(true)}
|
className="text-muted-foreground hover:text-hover-action"
|
||||||
|
onClick={onRollAllInitiative}
|
||||||
|
disabled={rollAllInitiativeDisabled}
|
||||||
|
title="Roll all initiative"
|
||||||
|
aria-label="Roll all initiative"
|
||||||
>
|
>
|
||||||
<Search className="h-4 w-4" />
|
<D20Icon className="h-6 w-6" />
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
|
{overflowItems.length > 0 && <OverflowMenu items={overflowItems} />}
|
||||||
</form>
|
</form>
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,129 +0,0 @@
|
|||||||
import type { Creature } from "@initiative/domain";
|
|
||||||
import { Search, X } from "lucide-react";
|
|
||||||
import {
|
|
||||||
type KeyboardEvent,
|
|
||||||
useCallback,
|
|
||||||
useEffect,
|
|
||||||
useRef,
|
|
||||||
useState,
|
|
||||||
} from "react";
|
|
||||||
import { Input } from "./ui/input.js";
|
|
||||||
|
|
||||||
interface BestiarySearchProps {
|
|
||||||
onSelectCreature: (creature: Creature) => void;
|
|
||||||
onClose: () => void;
|
|
||||||
searchFn: (query: string) => Creature[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function BestiarySearch({
|
|
||||||
onSelectCreature,
|
|
||||||
onClose,
|
|
||||||
searchFn,
|
|
||||||
}: BestiarySearchProps) {
|
|
||||||
const [query, setQuery] = useState("");
|
|
||||||
const [highlightIndex, setHighlightIndex] = useState(-1);
|
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
|
||||||
const results = query.length >= 2 ? searchFn(query) : [];
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
inputRef.current?.focus();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setHighlightIndex(-1);
|
|
||||||
}, [query]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
function handleClickOutside(e: MouseEvent) {
|
|
||||||
if (
|
|
||||||
containerRef.current &&
|
|
||||||
!containerRef.current.contains(e.target as Node)
|
|
||||||
) {
|
|
||||||
onClose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
document.addEventListener("mousedown", handleClickOutside);
|
|
||||||
return () => document.removeEventListener("mousedown", handleClickOutside);
|
|
||||||
}, [onClose]);
|
|
||||||
|
|
||||||
const handleKeyDown = useCallback(
|
|
||||||
(e: KeyboardEvent) => {
|
|
||||||
if (e.key === "Escape") {
|
|
||||||
onClose();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (e.key === "ArrowDown") {
|
|
||||||
e.preventDefault();
|
|
||||||
setHighlightIndex((i) => (i < results.length - 1 ? i + 1 : 0));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (e.key === "ArrowUp") {
|
|
||||||
e.preventDefault();
|
|
||||||
setHighlightIndex((i) => (i > 0 ? i - 1 : results.length - 1));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (e.key === "Enter" && highlightIndex >= 0) {
|
|
||||||
e.preventDefault();
|
|
||||||
onSelectCreature(results[highlightIndex]);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[results, highlightIndex, onClose, onSelectCreature],
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div ref={containerRef} className="relative w-full max-w-sm">
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<Search className="h-4 w-4 text-muted-foreground" />
|
|
||||||
<Input
|
|
||||||
ref={inputRef}
|
|
||||||
type="text"
|
|
||||||
value={query}
|
|
||||||
onChange={(e) => setQuery(e.target.value)}
|
|
||||||
onKeyDown={handleKeyDown}
|
|
||||||
placeholder="Search bestiary..."
|
|
||||||
className="flex-1"
|
|
||||||
/>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={onClose}
|
|
||||||
className="text-muted-foreground hover:text-hover-neutral"
|
|
||||||
>
|
|
||||||
<X className="h-4 w-4" />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{query.length >= 2 && (
|
|
||||||
<div className="absolute bottom-full z-50 mb-1 w-full rounded-md border border-border bg-card shadow-lg">
|
|
||||||
{results.length === 0 ? (
|
|
||||||
<div className="px-3 py-2 text-sm text-muted-foreground">
|
|
||||||
No creatures found
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<ul className="max-h-60 overflow-y-auto py-1">
|
|
||||||
{results.map((creature, i) => (
|
|
||||||
<li key={creature.id}>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
className={`flex w-full items-center justify-between px-3 py-1.5 text-left text-sm ${
|
|
||||||
i === highlightIndex
|
|
||||||
? "bg-accent/20 text-foreground"
|
|
||||||
: "text-foreground hover:bg-hover-neutral-bg"
|
|
||||||
}`}
|
|
||||||
onClick={() => onSelectCreature(creature)}
|
|
||||||
onMouseEnter={() => setHighlightIndex(i)}
|
|
||||||
>
|
|
||||||
<span>{creature.name}</span>
|
|
||||||
<span className="text-xs text-muted-foreground">
|
|
||||||
{creature.sourceDisplayName}
|
|
||||||
</span>
|
|
||||||
</button>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
104
apps/web/src/components/bulk-import-prompt.tsx
Normal file
104
apps/web/src/components/bulk-import-prompt.tsx
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
import { Loader2 } from "lucide-react";
|
||||||
|
import { useId, useState } from "react";
|
||||||
|
import { getAllSourceCodes } from "../adapters/bestiary-index-adapter.js";
|
||||||
|
import type { BulkImportState } from "../hooks/use-bulk-import.js";
|
||||||
|
import { Button } from "./ui/button.js";
|
||||||
|
import { Input } from "./ui/input.js";
|
||||||
|
|
||||||
|
const DEFAULT_BASE_URL =
|
||||||
|
"https://raw.githubusercontent.com/5etools-mirror-3/5etools-src/main/data/bestiary/";
|
||||||
|
|
||||||
|
interface BulkImportPromptProps {
|
||||||
|
importState: BulkImportState;
|
||||||
|
onStartImport: (baseUrl: string) => void;
|
||||||
|
onDone: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function BulkImportPrompt({
|
||||||
|
importState,
|
||||||
|
onStartImport,
|
||||||
|
onDone,
|
||||||
|
}: Readonly<BulkImportPromptProps>) {
|
||||||
|
const [baseUrl, setBaseUrl] = useState(DEFAULT_BASE_URL);
|
||||||
|
const baseUrlId = useId();
|
||||||
|
const totalSources = getAllSourceCodes().length;
|
||||||
|
|
||||||
|
if (importState.status === "complete") {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div className="rounded-md border border-green-500/50 bg-green-500/10 px-3 py-2 text-green-400 text-sm">
|
||||||
|
All sources loaded
|
||||||
|
</div>
|
||||||
|
<Button onClick={onDone}>Done</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (importState.status === "partial-failure") {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div className="rounded-md border border-yellow-500/50 bg-yellow-500/10 px-3 py-2 text-sm text-yellow-400">
|
||||||
|
Loaded {importState.completed}/{importState.total} sources (
|
||||||
|
{importState.failed} failed)
|
||||||
|
</div>
|
||||||
|
<Button onClick={onDone}>Done</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (importState.status === "loading") {
|
||||||
|
const processed = importState.completed + importState.failed;
|
||||||
|
const pct =
|
||||||
|
importState.total > 0
|
||||||
|
? Math.round((processed / importState.total) * 100)
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div className="flex items-center gap-2 text-muted-foreground text-sm">
|
||||||
|
<Loader2 className="h-4 w-4 animate-spin" />
|
||||||
|
Loading sources... {processed}/{importState.total}
|
||||||
|
</div>
|
||||||
|
<div className="h-2 overflow-hidden rounded-full bg-muted">
|
||||||
|
<div
|
||||||
|
className="h-full rounded-full bg-primary transition-all"
|
||||||
|
style={{ width: `${pct}%` }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// idle state
|
||||||
|
const isDisabled = !baseUrl.trim() || importState.status !== "idle";
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div>
|
||||||
|
<h3 className="font-semibold text-foreground text-sm">
|
||||||
|
Import All Sources
|
||||||
|
</h3>
|
||||||
|
<p className="mt-1 text-muted-foreground text-xs">
|
||||||
|
Load stat block data for all {totalSources} sources at once.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
<label htmlFor={baseUrlId} className="text-muted-foreground text-xs">
|
||||||
|
Base URL
|
||||||
|
</label>
|
||||||
|
<Input
|
||||||
|
id={baseUrlId}
|
||||||
|
type="url"
|
||||||
|
value={baseUrl}
|
||||||
|
onChange={(e) => setBaseUrl(e.target.value)}
|
||||||
|
className="text-xs"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Button onClick={() => onStartImport(baseUrl)} disabled={isDisabled}>
|
||||||
|
Load All
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
49
apps/web/src/components/bulk-import-toasts.tsx
Normal file
49
apps/web/src/components/bulk-import-toasts.tsx
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import type { BulkImportState } from "../hooks/use-bulk-import.js";
|
||||||
|
import { Toast } from "./toast.js";
|
||||||
|
|
||||||
|
interface BulkImportToastsProps {
|
||||||
|
state: BulkImportState;
|
||||||
|
visible: boolean;
|
||||||
|
onReset: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function BulkImportToasts({
|
||||||
|
state,
|
||||||
|
visible,
|
||||||
|
onReset,
|
||||||
|
}: Readonly<BulkImportToastsProps>) {
|
||||||
|
if (!visible) return null;
|
||||||
|
|
||||||
|
if (state.status === "loading") {
|
||||||
|
return (
|
||||||
|
<Toast
|
||||||
|
message={`Loading sources... ${state.completed + state.failed}/${state.total}`}
|
||||||
|
progress={
|
||||||
|
state.total > 0 ? (state.completed + state.failed) / state.total : 0
|
||||||
|
}
|
||||||
|
onDismiss={() => {}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.status === "complete") {
|
||||||
|
return (
|
||||||
|
<Toast
|
||||||
|
message="All sources loaded"
|
||||||
|
onDismiss={onReset}
|
||||||
|
autoDismissMs={3000}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.status === "partial-failure") {
|
||||||
|
return (
|
||||||
|
<Toast
|
||||||
|
message={`Loaded ${state.completed}/${state.total} sources (${state.failed} failed)`}
|
||||||
|
onDismiss={onReset}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
36
apps/web/src/components/color-palette.tsx
Normal file
36
apps/web/src/components/color-palette.tsx
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { VALID_PLAYER_COLORS } from "@initiative/domain";
|
||||||
|
import { cn } from "../lib/utils";
|
||||||
|
import { PLAYER_COLOR_HEX } from "./player-icon-map";
|
||||||
|
|
||||||
|
interface ColorPaletteProps {
|
||||||
|
value: string;
|
||||||
|
onChange: (color: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const COLORS = [...VALID_PLAYER_COLORS] as string[];
|
||||||
|
|
||||||
|
export function ColorPalette({ value, onChange }: Readonly<ColorPaletteProps>) {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-wrap gap-2">
|
||||||
|
{COLORS.map((color) => (
|
||||||
|
<button
|
||||||
|
key={color}
|
||||||
|
type="button"
|
||||||
|
onClick={() => onChange(value === color ? "" : color)}
|
||||||
|
className={cn(
|
||||||
|
"h-8 w-8 rounded-full transition-all",
|
||||||
|
value === color
|
||||||
|
? "scale-110 ring-2 ring-foreground ring-offset-2 ring-offset-background"
|
||||||
|
: "hover:scale-110",
|
||||||
|
)}
|
||||||
|
style={{
|
||||||
|
backgroundColor:
|
||||||
|
PLAYER_COLOR_HEX[color as keyof typeof PLAYER_COLOR_HEX],
|
||||||
|
}}
|
||||||
|
aria-label={color}
|
||||||
|
title={color}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -2,8 +2,9 @@ import {
|
|||||||
type CombatantId,
|
type CombatantId,
|
||||||
type ConditionId,
|
type ConditionId,
|
||||||
deriveHpStatus,
|
deriveHpStatus,
|
||||||
|
type PlayerIcon,
|
||||||
} from "@initiative/domain";
|
} from "@initiative/domain";
|
||||||
import { Brain, X } from "lucide-react";
|
import { Book, BookOpen, Brain, X } from "lucide-react";
|
||||||
import { type Ref, useCallback, useEffect, useRef, useState } from "react";
|
import { type Ref, useCallback, useEffect, useRef, useState } from "react";
|
||||||
import { cn } from "../lib/utils";
|
import { cn } from "../lib/utils";
|
||||||
import { AcShield } from "./ac-shield";
|
import { AcShield } from "./ac-shield";
|
||||||
@@ -11,7 +12,8 @@ import { ConditionPicker } from "./condition-picker";
|
|||||||
import { ConditionTags } from "./condition-tags";
|
import { ConditionTags } from "./condition-tags";
|
||||||
import { D20Icon } from "./d20-icon";
|
import { D20Icon } from "./d20-icon";
|
||||||
import { HpAdjustPopover } from "./hp-adjust-popover";
|
import { HpAdjustPopover } from "./hp-adjust-popover";
|
||||||
import { Button } from "./ui/button";
|
import { PLAYER_COLOR_HEX, PLAYER_ICON_MAP } from "./player-icon-map";
|
||||||
|
import { ConfirmButton } from "./ui/confirm-button";
|
||||||
import { Input } from "./ui/input";
|
import { Input } from "./ui/input";
|
||||||
|
|
||||||
interface Combatant {
|
interface Combatant {
|
||||||
@@ -23,6 +25,8 @@ interface Combatant {
|
|||||||
readonly ac?: number;
|
readonly ac?: number;
|
||||||
readonly conditions?: readonly ConditionId[];
|
readonly conditions?: readonly ConditionId[];
|
||||||
readonly isConcentrating?: boolean;
|
readonly isConcentrating?: boolean;
|
||||||
|
readonly color?: string;
|
||||||
|
readonly icon?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CombatantRowProps {
|
interface CombatantRowProps {
|
||||||
@@ -37,6 +41,7 @@ interface CombatantRowProps {
|
|||||||
onToggleCondition: (id: CombatantId, conditionId: ConditionId) => void;
|
onToggleCondition: (id: CombatantId, conditionId: ConditionId) => void;
|
||||||
onToggleConcentration: (id: CombatantId) => void;
|
onToggleConcentration: (id: CombatantId) => void;
|
||||||
onShowStatBlock?: () => void;
|
onShowStatBlock?: () => void;
|
||||||
|
isStatBlockOpen?: boolean;
|
||||||
onRollInitiative?: (id: CombatantId) => void;
|
onRollInitiative?: (id: CombatantId) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,11 +49,13 @@ function EditableName({
|
|||||||
name,
|
name,
|
||||||
combatantId,
|
combatantId,
|
||||||
onRename,
|
onRename,
|
||||||
}: {
|
color,
|
||||||
|
}: Readonly<{
|
||||||
name: string;
|
name: string;
|
||||||
combatantId: CombatantId;
|
combatantId: CombatantId;
|
||||||
onRename: (id: CombatantId, newName: string) => void;
|
onRename: (id: CombatantId, newName: string) => void;
|
||||||
}) {
|
color?: string;
|
||||||
|
}>) {
|
||||||
const [editing, setEditing] = useState(false);
|
const [editing, setEditing] = useState(false);
|
||||||
const [draft, setDraft] = useState(name);
|
const [draft, setDraft] = useState(name);
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
@@ -73,7 +80,7 @@ function EditableName({
|
|||||||
ref={inputRef}
|
ref={inputRef}
|
||||||
type="text"
|
type="text"
|
||||||
value={draft}
|
value={draft}
|
||||||
className="h-7 text-sm"
|
className="h-7 max-w-48 text-sm"
|
||||||
onChange={(e) => setDraft(e.target.value)}
|
onChange={(e) => setDraft(e.target.value)}
|
||||||
onBlur={commit}
|
onBlur={commit}
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
@@ -87,11 +94,9 @@ function EditableName({
|
|||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={(e) => {
|
onClick={startEditing}
|
||||||
e.stopPropagation();
|
className="cursor-text truncate text-left text-foreground text-sm transition-colors hover:text-hover-neutral"
|
||||||
startEditing();
|
style={color ? { color } : undefined}
|
||||||
}}
|
|
||||||
className="truncate text-left text-sm text-foreground hover:text-hover-neutral transition-colors"
|
|
||||||
>
|
>
|
||||||
{name}
|
{name}
|
||||||
</button>
|
</button>
|
||||||
@@ -101,10 +106,10 @@ function EditableName({
|
|||||||
function MaxHpDisplay({
|
function MaxHpDisplay({
|
||||||
maxHp,
|
maxHp,
|
||||||
onCommit,
|
onCommit,
|
||||||
}: {
|
}: Readonly<{
|
||||||
maxHp: number | undefined;
|
maxHp: number | undefined;
|
||||||
onCommit: (value: number | undefined) => void;
|
onCommit: (value: number | undefined) => void;
|
||||||
}) {
|
}>) {
|
||||||
const [editing, setEditing] = useState(false);
|
const [editing, setEditing] = useState(false);
|
||||||
const [draft, setDraft] = useState(maxHp?.toString() ?? "");
|
const [draft, setDraft] = useState(maxHp?.toString() ?? "");
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
@@ -150,7 +155,7 @@ function MaxHpDisplay({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={startEditing}
|
onClick={startEditing}
|
||||||
className="inline-block h-7 min-w-[3ch] text-center text-sm leading-7 tabular-nums text-muted-foreground transition-colors hover:text-hover-neutral"
|
className="inline-block h-7 min-w-[3ch] text-center text-muted-foreground text-sm tabular-nums leading-7 transition-colors hover:text-hover-neutral"
|
||||||
>
|
>
|
||||||
{maxHp ?? "Max"}
|
{maxHp ?? "Max"}
|
||||||
</button>
|
</button>
|
||||||
@@ -162,12 +167,12 @@ function ClickableHp({
|
|||||||
maxHp,
|
maxHp,
|
||||||
onAdjust,
|
onAdjust,
|
||||||
dimmed,
|
dimmed,
|
||||||
}: {
|
}: Readonly<{
|
||||||
currentHp: number | undefined;
|
currentHp: number | undefined;
|
||||||
maxHp: number | undefined;
|
maxHp: number | undefined;
|
||||||
onAdjust: (delta: number) => void;
|
onAdjust: (delta: number) => void;
|
||||||
dimmed?: boolean;
|
dimmed?: boolean;
|
||||||
}) {
|
}>) {
|
||||||
const [popoverOpen, setPopoverOpen] = useState(false);
|
const [popoverOpen, setPopoverOpen] = useState(false);
|
||||||
const status = deriveHpStatus(currentHp, maxHp);
|
const status = deriveHpStatus(currentHp, maxHp);
|
||||||
|
|
||||||
@@ -175,9 +180,11 @@ function ClickableHp({
|
|||||||
return (
|
return (
|
||||||
<span
|
<span
|
||||||
className={cn(
|
className={cn(
|
||||||
"inline-block h-7 w-[4ch] text-center text-sm leading-7 tabular-nums text-muted-foreground",
|
"inline-block h-7 w-[4ch] text-center text-muted-foreground text-sm tabular-nums leading-7",
|
||||||
dimmed && "opacity-50",
|
dimmed && "opacity-50",
|
||||||
)}
|
)}
|
||||||
|
role="status"
|
||||||
|
aria-label="No HP set"
|
||||||
>
|
>
|
||||||
--
|
--
|
||||||
</span>
|
</span>
|
||||||
@@ -189,8 +196,9 @@ function ClickableHp({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => setPopoverOpen(true)}
|
onClick={() => setPopoverOpen(true)}
|
||||||
|
aria-label={`Current HP: ${currentHp} (${status})`}
|
||||||
className={cn(
|
className={cn(
|
||||||
"inline-block h-7 min-w-[3ch] text-center text-sm font-medium leading-7 tabular-nums transition-colors hover:text-hover-neutral",
|
"inline-block h-7 min-w-[3ch] text-center font-medium text-sm tabular-nums leading-7 transition-colors hover:text-hover-neutral",
|
||||||
status === "bloodied" && "text-amber-400",
|
status === "bloodied" && "text-amber-400",
|
||||||
status === "unconscious" && "text-red-400",
|
status === "unconscious" && "text-red-400",
|
||||||
status === "healthy" && "text-foreground",
|
status === "healthy" && "text-foreground",
|
||||||
@@ -199,7 +207,7 @@ function ClickableHp({
|
|||||||
>
|
>
|
||||||
{currentHp}
|
{currentHp}
|
||||||
</button>
|
</button>
|
||||||
{popoverOpen && (
|
{!!popoverOpen && (
|
||||||
<HpAdjustPopover
|
<HpAdjustPopover
|
||||||
onAdjust={onAdjust}
|
onAdjust={onAdjust}
|
||||||
onClose={() => setPopoverOpen(false)}
|
onClose={() => setPopoverOpen(false)}
|
||||||
@@ -212,10 +220,10 @@ function ClickableHp({
|
|||||||
function AcDisplay({
|
function AcDisplay({
|
||||||
ac,
|
ac,
|
||||||
onCommit,
|
onCommit,
|
||||||
}: {
|
}: Readonly<{
|
||||||
ac: number | undefined;
|
ac: number | undefined;
|
||||||
onCommit: (value: number | undefined) => void;
|
onCommit: (value: number | undefined) => void;
|
||||||
}) {
|
}>) {
|
||||||
const [editing, setEditing] = useState(false);
|
const [editing, setEditing] = useState(false);
|
||||||
const [draft, setDraft] = useState(ac?.toString() ?? "");
|
const [draft, setDraft] = useState(ac?.toString() ?? "");
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
@@ -266,13 +274,13 @@ function InitiativeDisplay({
|
|||||||
dimmed,
|
dimmed,
|
||||||
onSetInitiative,
|
onSetInitiative,
|
||||||
onRollInitiative,
|
onRollInitiative,
|
||||||
}: {
|
}: Readonly<{
|
||||||
initiative: number | undefined;
|
initiative: number | undefined;
|
||||||
combatantId: CombatantId;
|
combatantId: CombatantId;
|
||||||
dimmed: boolean;
|
dimmed: boolean;
|
||||||
onSetInitiative: (id: CombatantId, value: number | undefined) => void;
|
onSetInitiative: (id: CombatantId, value: number | undefined) => void;
|
||||||
onRollInitiative?: (id: CombatantId) => void;
|
onRollInitiative?: (id: CombatantId) => void;
|
||||||
}) {
|
}>) {
|
||||||
const [editing, setEditing] = useState(false);
|
const [editing, setEditing] = useState(false);
|
||||||
const [draft, setDraft] = useState(initiative?.toString() ?? "");
|
const [draft, setDraft] = useState(initiative?.toString() ?? "");
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
@@ -342,10 +350,10 @@ function InitiativeDisplay({
|
|||||||
type="button"
|
type="button"
|
||||||
onClick={startEditing}
|
onClick={startEditing}
|
||||||
className={cn(
|
className={cn(
|
||||||
"h-7 w-full text-center text-sm leading-7 tabular-nums transition-colors",
|
"h-7 w-full text-center text-sm tabular-nums leading-7 transition-colors",
|
||||||
initiative !== undefined
|
initiative === undefined
|
||||||
? "font-medium text-foreground hover:text-hover-neutral"
|
? "text-muted-foreground hover:text-hover-neutral"
|
||||||
: "text-muted-foreground hover:text-hover-neutral",
|
: "font-medium text-foreground hover:text-hover-neutral",
|
||||||
dimmed && "opacity-50",
|
dimmed && "opacity-50",
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
@@ -354,6 +362,28 @@ function InitiativeDisplay({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function rowBorderClass(
|
||||||
|
isActive: boolean,
|
||||||
|
isConcentrating: boolean | undefined,
|
||||||
|
): string {
|
||||||
|
if (isActive && isConcentrating)
|
||||||
|
return "border border-l-2 border-active-row-border border-l-purple-400 bg-active-row-bg card-glow";
|
||||||
|
if (isActive)
|
||||||
|
return "border border-l-2 border-active-row-border bg-active-row-bg card-glow";
|
||||||
|
if (isConcentrating)
|
||||||
|
return "border border-l-2 border-transparent border-l-purple-400";
|
||||||
|
return "border border-l-2 border-transparent";
|
||||||
|
}
|
||||||
|
|
||||||
|
function concentrationIconClass(
|
||||||
|
isConcentrating: boolean | undefined,
|
||||||
|
dimmed: boolean,
|
||||||
|
): string {
|
||||||
|
if (!isConcentrating)
|
||||||
|
return "opacity-0 group-hover:opacity-50 text-muted-foreground";
|
||||||
|
return dimmed ? "opacity-50 text-purple-400" : "opacity-100 text-purple-400";
|
||||||
|
}
|
||||||
|
|
||||||
export function CombatantRow({
|
export function CombatantRow({
|
||||||
ref,
|
ref,
|
||||||
combatant,
|
combatant,
|
||||||
@@ -367,6 +397,7 @@ export function CombatantRow({
|
|||||||
onToggleCondition,
|
onToggleCondition,
|
||||||
onToggleConcentration,
|
onToggleConcentration,
|
||||||
onShowStatBlock,
|
onShowStatBlock,
|
||||||
|
isStatBlockOpen,
|
||||||
onRollInitiative,
|
onRollInitiative,
|
||||||
}: CombatantRowProps & { ref?: Ref<HTMLDivElement> }) {
|
}: CombatantRowProps & { ref?: Ref<HTMLDivElement> }) {
|
||||||
const { id, name, initiative, maxHp, currentHp } = combatant;
|
const { id, name, initiative, maxHp, currentHp } = combatant;
|
||||||
@@ -401,49 +432,35 @@ export function CombatantRow({
|
|||||||
}
|
}
|
||||||
}, [combatant.isConcentrating]);
|
}, [combatant.isConcentrating]);
|
||||||
|
|
||||||
|
const pcColor = combatant.color
|
||||||
|
? PLAYER_COLOR_HEX[combatant.color as keyof typeof PLAYER_COLOR_HEX]
|
||||||
|
: undefined;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
/* biome-ignore lint/a11y/useKeyWithClickEvents: row click opens stat block */
|
|
||||||
/* biome-ignore lint/a11y/noStaticElementInteractions: row click opens stat block */
|
|
||||||
<div
|
<div
|
||||||
ref={ref}
|
ref={ref}
|
||||||
className={cn(
|
className={cn(
|
||||||
"group rounded-md pr-3 transition-colors",
|
"group rounded-lg pr-3 transition-colors",
|
||||||
isActive
|
rowBorderClass(isActive, combatant.isConcentrating),
|
||||||
? "border-l-2 border-l-accent bg-accent/10"
|
|
||||||
: combatant.isConcentrating
|
|
||||||
? "border-l-2 border-l-purple-400"
|
|
||||||
: "border-l-2 border-l-transparent",
|
|
||||||
isPulsing && "animate-concentration-pulse",
|
isPulsing && "animate-concentration-pulse",
|
||||||
onShowStatBlock && "cursor-pointer",
|
|
||||||
)}
|
)}
|
||||||
onClick={onShowStatBlock}
|
|
||||||
>
|
>
|
||||||
<div className="grid grid-cols-[2rem_3rem_1fr_auto_auto_2rem] items-center gap-3 py-2">
|
<div className="grid grid-cols-[2rem_3rem_1fr_auto_auto_2rem] items-center gap-3 py-2">
|
||||||
{/* Concentration */}
|
{/* Concentration */}
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={(e) => {
|
onClick={() => onToggleConcentration(id)}
|
||||||
e.stopPropagation();
|
|
||||||
onToggleConcentration(id);
|
|
||||||
}}
|
|
||||||
title="Concentrating"
|
title="Concentrating"
|
||||||
aria-label="Toggle concentration"
|
aria-label="Toggle concentration"
|
||||||
className={cn(
|
className={cn(
|
||||||
"flex w-full items-center justify-center self-stretch -my-2 -ml-[2px] pl-[14px] transition-opacity hover:text-hover-neutral hover:opacity-100",
|
"-my-2 -ml-[2px] flex w-full items-center justify-center self-stretch pl-[14px] transition-opacity hover:text-hover-neutral hover:opacity-100",
|
||||||
combatant.isConcentrating
|
concentrationIconClass(combatant.isConcentrating, dimmed),
|
||||||
? dimmed
|
|
||||||
? "opacity-50 text-purple-400"
|
|
||||||
: "opacity-100 text-purple-400"
|
|
||||||
: "opacity-0 group-hover:opacity-50 text-muted-foreground",
|
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<Brain size={16} />
|
<Brain size={16} />
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
{/* Initiative */}
|
{/* Initiative */}
|
||||||
{/* biome-ignore lint/a11y/useKeyWithClickEvents: stopPropagation wrapper */}
|
|
||||||
{/* biome-ignore lint/a11y/noStaticElementInteractions: stopPropagation wrapper */}
|
|
||||||
<div onClick={(e) => e.stopPropagation()}>
|
|
||||||
<InitiativeDisplay
|
<InitiativeDisplay
|
||||||
initiative={initiative}
|
initiative={initiative}
|
||||||
combatantId={id}
|
combatantId={id}
|
||||||
@@ -451,24 +468,53 @@ export function CombatantRow({
|
|||||||
onSetInitiative={onSetInitiative}
|
onSetInitiative={onSetInitiative}
|
||||||
onRollInitiative={onRollInitiative}
|
onRollInitiative={onRollInitiative}
|
||||||
/>
|
/>
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Name + Conditions */}
|
{/* Name + Conditions */}
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
"relative flex flex-wrap items-center gap-1 min-w-0",
|
"relative flex min-w-0 flex-wrap items-center gap-1",
|
||||||
dimmed && "opacity-50",
|
dimmed && "opacity-50",
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<span className="min-w-0 truncate">
|
{!!onShowStatBlock && (
|
||||||
<EditableName name={name} combatantId={id} onRename={onRename} />
|
<button
|
||||||
</span>
|
type="button"
|
||||||
|
onClick={onShowStatBlock}
|
||||||
|
title="View stat block"
|
||||||
|
aria-label="View stat block"
|
||||||
|
className="shrink-0 text-foreground transition-colors hover:text-hover-neutral"
|
||||||
|
>
|
||||||
|
{isStatBlockOpen ? <BookOpen size={16} /> : <Book size={16} />}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
{!!combatant.icon &&
|
||||||
|
!!combatant.color &&
|
||||||
|
(() => {
|
||||||
|
const PcIcon = PLAYER_ICON_MAP[combatant.icon as PlayerIcon];
|
||||||
|
const iconColor =
|
||||||
|
PLAYER_COLOR_HEX[
|
||||||
|
combatant.color as keyof typeof PLAYER_COLOR_HEX
|
||||||
|
];
|
||||||
|
return PcIcon ? (
|
||||||
|
<PcIcon
|
||||||
|
size={16}
|
||||||
|
style={{ color: iconColor }}
|
||||||
|
className="shrink-0"
|
||||||
|
/>
|
||||||
|
) : null;
|
||||||
|
})()}
|
||||||
|
<EditableName
|
||||||
|
name={name}
|
||||||
|
combatantId={id}
|
||||||
|
onRename={onRename}
|
||||||
|
color={pcColor}
|
||||||
|
/>
|
||||||
<ConditionTags
|
<ConditionTags
|
||||||
conditions={combatant.conditions}
|
conditions={combatant.conditions}
|
||||||
onRemove={(conditionId) => onToggleCondition(id, conditionId)}
|
onRemove={(conditionId) => onToggleCondition(id, conditionId)}
|
||||||
onOpenPicker={() => setPickerOpen((prev) => !prev)}
|
onOpenPicker={() => setPickerOpen((prev) => !prev)}
|
||||||
/>
|
/>
|
||||||
{pickerOpen && (
|
{!!pickerOpen && (
|
||||||
<ConditionPicker
|
<ConditionPicker
|
||||||
activeConditions={combatant.conditions}
|
activeConditions={combatant.conditions}
|
||||||
onToggle={(conditionId) => onToggleCondition(id, conditionId)}
|
onToggle={(conditionId) => onToggleCondition(id, conditionId)}
|
||||||
@@ -478,22 +524,12 @@ export function CombatantRow({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* AC */}
|
{/* AC */}
|
||||||
{/* biome-ignore lint/a11y/useKeyWithClickEvents: stopPropagation wrapper */}
|
<div className={cn(dimmed && "opacity-50")}>
|
||||||
{/* biome-ignore lint/a11y/noStaticElementInteractions: stopPropagation wrapper */}
|
|
||||||
<div
|
|
||||||
className={cn(dimmed && "opacity-50")}
|
|
||||||
onClick={(e) => e.stopPropagation()}
|
|
||||||
>
|
|
||||||
<AcDisplay ac={combatant.ac} onCommit={(v) => onSetAc(id, v)} />
|
<AcDisplay ac={combatant.ac} onCommit={(v) => onSetAc(id, v)} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* HP */}
|
{/* HP */}
|
||||||
{/* biome-ignore lint/a11y/useKeyWithClickEvents: stopPropagation wrapper */}
|
<div className="flex items-center gap-1">
|
||||||
{/* biome-ignore lint/a11y/noStaticElementInteractions: stopPropagation wrapper */}
|
|
||||||
<div
|
|
||||||
className="flex items-center gap-1"
|
|
||||||
onClick={(e) => e.stopPropagation()}
|
|
||||||
>
|
|
||||||
<ClickableHp
|
<ClickableHp
|
||||||
currentHp={currentHp}
|
currentHp={currentHp}
|
||||||
maxHp={maxHp}
|
maxHp={maxHp}
|
||||||
@@ -503,7 +539,7 @@ export function CombatantRow({
|
|||||||
{maxHp !== undefined && (
|
{maxHp !== undefined && (
|
||||||
<span
|
<span
|
||||||
className={cn(
|
className={cn(
|
||||||
"text-sm tabular-nums text-muted-foreground",
|
"text-muted-foreground text-sm tabular-nums",
|
||||||
dimmed && "opacity-50",
|
dimmed && "opacity-50",
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
@@ -516,19 +552,12 @@ export function CombatantRow({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Actions */}
|
{/* Actions */}
|
||||||
<Button
|
<ConfirmButton
|
||||||
variant="ghost"
|
icon={<X size={16} />}
|
||||||
size="icon"
|
label="Remove combatant"
|
||||||
className="h-7 w-7 text-muted-foreground hover:text-hover-destructive opacity-0 pointer-events-none group-hover:opacity-100 group-hover:pointer-events-auto focus:opacity-100 focus:pointer-events-auto transition-opacity"
|
onConfirm={() => onRemove(id)}
|
||||||
onClick={(e) => {
|
className="pointer-events-none pointer-coarse:pointer-events-auto h-7 w-7 text-muted-foreground opacity-0 pointer-coarse:opacity-100 transition-opacity focus:pointer-events-auto focus:opacity-100 group-hover:pointer-events-auto group-hover:opacity-100"
|
||||||
e.stopPropagation();
|
/>
|
||||||
onRemove(id);
|
|
||||||
}}
|
|
||||||
title="Remove combatant"
|
|
||||||
aria-label="Remove combatant"
|
|
||||||
>
|
|
||||||
<X size={16} />
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -61,15 +61,24 @@ export function ConditionPicker({
|
|||||||
activeConditions,
|
activeConditions,
|
||||||
onToggle,
|
onToggle,
|
||||||
onClose,
|
onClose,
|
||||||
}: ConditionPickerProps) {
|
}: Readonly<ConditionPickerProps>) {
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
const [flipped, setFlipped] = useState(false);
|
const [flipped, setFlipped] = useState(false);
|
||||||
|
const [maxHeight, setMaxHeight] = useState<number | undefined>(undefined);
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
const el = ref.current;
|
const el = ref.current;
|
||||||
if (!el) return;
|
if (!el) return;
|
||||||
const rect = el.getBoundingClientRect();
|
const rect = el.getBoundingClientRect();
|
||||||
setFlipped(rect.bottom > window.innerHeight);
|
const spaceBelow = window.innerHeight - rect.top;
|
||||||
|
const spaceAbove = rect.bottom;
|
||||||
|
const shouldFlip =
|
||||||
|
rect.bottom > window.innerHeight && spaceAbove > spaceBelow;
|
||||||
|
setFlipped(shouldFlip);
|
||||||
|
const available = shouldFlip ? spaceAbove : spaceBelow;
|
||||||
|
if (rect.height > available) {
|
||||||
|
setMaxHeight(available - 16);
|
||||||
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -88,9 +97,10 @@ export function ConditionPicker({
|
|||||||
<div
|
<div
|
||||||
ref={ref}
|
ref={ref}
|
||||||
className={cn(
|
className={cn(
|
||||||
"absolute z-10 w-fit rounded-md border border-border bg-background p-1 shadow-lg",
|
"card-glow absolute left-0 z-10 w-fit overflow-y-auto rounded-lg border border-border bg-background p-1",
|
||||||
flipped ? "bottom-full mb-1" : "mt-1",
|
flipped ? "bottom-full mb-1" : "top-full mt-1",
|
||||||
)}
|
)}
|
||||||
|
style={maxHeight ? { maxHeight } : undefined}
|
||||||
>
|
>
|
||||||
{CONDITION_DEFINITIONS.map((def) => {
|
{CONDITION_DEFINITIONS.map((def) => {
|
||||||
const Icon = ICON_MAP[def.iconName];
|
const Icon = ICON_MAP[def.iconName];
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import {
|
|||||||
Sparkles,
|
Sparkles,
|
||||||
ZapOff,
|
ZapOff,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
|
import { cn } from "../lib/utils.js";
|
||||||
|
|
||||||
const ICON_MAP: Record<string, LucideIcon> = {
|
const ICON_MAP: Record<string, LucideIcon> = {
|
||||||
EyeOff,
|
EyeOff,
|
||||||
@@ -60,7 +61,7 @@ export function ConditionTags({
|
|||||||
conditions,
|
conditions,
|
||||||
onRemove,
|
onRemove,
|
||||||
onOpenPicker,
|
onOpenPicker,
|
||||||
}: ConditionTagsProps) {
|
}: Readonly<ConditionTagsProps>) {
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-wrap items-center gap-0.5">
|
<div className="flex flex-wrap items-center gap-0.5">
|
||||||
{conditions?.map((condId) => {
|
{conditions?.map((condId) => {
|
||||||
@@ -75,7 +76,10 @@ export function ConditionTags({
|
|||||||
type="button"
|
type="button"
|
||||||
title={def.label}
|
title={def.label}
|
||||||
aria-label={`Remove ${def.label}`}
|
aria-label={`Remove ${def.label}`}
|
||||||
className={`inline-flex items-center rounded p-0.5 hover:bg-hover-neutral-bg transition-colors ${colorClass}`}
|
className={cn(
|
||||||
|
"inline-flex items-center rounded p-0.5 transition-colors hover:bg-hover-neutral-bg",
|
||||||
|
colorClass,
|
||||||
|
)}
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
onRemove(condId);
|
onRemove(condId);
|
||||||
@@ -89,7 +93,7 @@ export function ConditionTags({
|
|||||||
type="button"
|
type="button"
|
||||||
title="Add condition"
|
title="Add condition"
|
||||||
aria-label="Add condition"
|
aria-label="Add condition"
|
||||||
className="inline-flex items-center rounded p-0.5 text-muted-foreground hover:text-hover-neutral hover:bg-hover-neutral-bg transition-colors opacity-0 group-hover:opacity-100 focus:opacity-100 transition-opacity"
|
className="inline-flex items-center rounded p-0.5 text-muted-foreground opacity-0 pointer-coarse:opacity-100 transition-colors transition-opacity hover:bg-hover-neutral-bg hover:text-hover-neutral focus:opacity-100 group-hover:opacity-100"
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
onOpenPicker();
|
onOpenPicker();
|
||||||
|
|||||||
192
apps/web/src/components/create-player-modal.tsx
Normal file
192
apps/web/src/components/create-player-modal.tsx
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
import type { PlayerCharacter } from "@initiative/domain";
|
||||||
|
import { X } from "lucide-react";
|
||||||
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
import { ColorPalette } from "./color-palette";
|
||||||
|
import { IconGrid } from "./icon-grid";
|
||||||
|
import { Button } from "./ui/button";
|
||||||
|
import { Input } from "./ui/input";
|
||||||
|
|
||||||
|
interface CreatePlayerModalProps {
|
||||||
|
open: boolean;
|
||||||
|
onClose: () => void;
|
||||||
|
onSave: (
|
||||||
|
name: string,
|
||||||
|
ac: number,
|
||||||
|
maxHp: number,
|
||||||
|
color: string | undefined,
|
||||||
|
icon: string | undefined,
|
||||||
|
) => void;
|
||||||
|
playerCharacter?: PlayerCharacter;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function CreatePlayerModal({
|
||||||
|
open,
|
||||||
|
onClose,
|
||||||
|
onSave,
|
||||||
|
playerCharacter,
|
||||||
|
}: Readonly<CreatePlayerModalProps>) {
|
||||||
|
const dialogRef = useRef<HTMLDialogElement>(null);
|
||||||
|
const [name, setName] = useState("");
|
||||||
|
const [ac, setAc] = useState("10");
|
||||||
|
const [maxHp, setMaxHp] = useState("10");
|
||||||
|
const [color, setColor] = useState("blue");
|
||||||
|
const [icon, setIcon] = useState("sword");
|
||||||
|
const [error, setError] = useState("");
|
||||||
|
|
||||||
|
const isEdit = !!playerCharacter;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (open) {
|
||||||
|
if (playerCharacter) {
|
||||||
|
setName(playerCharacter.name);
|
||||||
|
setAc(String(playerCharacter.ac));
|
||||||
|
setMaxHp(String(playerCharacter.maxHp));
|
||||||
|
setColor(playerCharacter.color ?? "");
|
||||||
|
setIcon(playerCharacter.icon ?? "");
|
||||||
|
} else {
|
||||||
|
setName("");
|
||||||
|
setAc("10");
|
||||||
|
setMaxHp("10");
|
||||||
|
setColor("");
|
||||||
|
setIcon("");
|
||||||
|
}
|
||||||
|
setError("");
|
||||||
|
}
|
||||||
|
}, [open, playerCharacter]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const dialog = dialogRef.current;
|
||||||
|
if (!dialog) return;
|
||||||
|
if (open && !dialog.open) {
|
||||||
|
dialog.showModal();
|
||||||
|
} else if (!open && dialog.open) {
|
||||||
|
dialog.close();
|
||||||
|
}
|
||||||
|
}, [open]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const dialog = dialogRef.current;
|
||||||
|
if (!dialog) return;
|
||||||
|
function handleCancel(e: Event) {
|
||||||
|
e.preventDefault();
|
||||||
|
onClose();
|
||||||
|
}
|
||||||
|
function handleBackdropClick(e: MouseEvent) {
|
||||||
|
if (e.target === dialog) onClose();
|
||||||
|
}
|
||||||
|
dialog.addEventListener("cancel", handleCancel);
|
||||||
|
dialog.addEventListener("mousedown", handleBackdropClick);
|
||||||
|
return () => {
|
||||||
|
dialog.removeEventListener("cancel", handleCancel);
|
||||||
|
dialog.removeEventListener("mousedown", handleBackdropClick);
|
||||||
|
};
|
||||||
|
}, [onClose]);
|
||||||
|
|
||||||
|
const handleSubmit = (e: React.SubmitEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const trimmed = name.trim();
|
||||||
|
if (trimmed === "") {
|
||||||
|
setError("Name is required");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const acNum = Number.parseInt(ac, 10);
|
||||||
|
if (Number.isNaN(acNum) || acNum < 0) {
|
||||||
|
setError("AC must be a non-negative number");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const hpNum = Number.parseInt(maxHp, 10);
|
||||||
|
if (Number.isNaN(hpNum) || hpNum < 1) {
|
||||||
|
setError("Max HP must be at least 1");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onSave(trimmed, acNum, hpNum, color || undefined, icon || undefined);
|
||||||
|
onClose();
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<dialog
|
||||||
|
ref={dialogRef}
|
||||||
|
className="card-glow m-auto w-full max-w-md rounded-lg border border-border bg-card p-6 backdrop:bg-black/50"
|
||||||
|
>
|
||||||
|
<div className="mb-4 flex items-center justify-between">
|
||||||
|
<h2 className="font-semibold text-foreground text-lg">
|
||||||
|
{isEdit ? "Edit Player" : "Create Player"}
|
||||||
|
</h2>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
onClick={onClose}
|
||||||
|
className="text-muted-foreground"
|
||||||
|
>
|
||||||
|
<X size={20} />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form onSubmit={handleSubmit} className="flex flex-col gap-4">
|
||||||
|
<div>
|
||||||
|
<span className="mb-1 block text-muted-foreground text-sm">Name</span>
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
value={name}
|
||||||
|
onChange={(e) => {
|
||||||
|
setName(e.target.value);
|
||||||
|
setError("");
|
||||||
|
}}
|
||||||
|
placeholder="Character name"
|
||||||
|
aria-label="Name"
|
||||||
|
autoFocus
|
||||||
|
/>
|
||||||
|
{!!error && <p className="mt-1 text-destructive text-sm">{error}</p>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex gap-3">
|
||||||
|
<div className="flex-1">
|
||||||
|
<span className="mb-1 block text-muted-foreground text-sm">AC</span>
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
inputMode="numeric"
|
||||||
|
value={ac}
|
||||||
|
onChange={(e) => setAc(e.target.value)}
|
||||||
|
placeholder="AC"
|
||||||
|
aria-label="AC"
|
||||||
|
className="text-center"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="flex-1">
|
||||||
|
<span className="mb-1 block text-muted-foreground text-sm">
|
||||||
|
Max HP
|
||||||
|
</span>
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
inputMode="numeric"
|
||||||
|
value={maxHp}
|
||||||
|
onChange={(e) => setMaxHp(e.target.value)}
|
||||||
|
placeholder="Max HP"
|
||||||
|
aria-label="Max HP"
|
||||||
|
className="text-center"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<span className="mb-2 block text-muted-foreground text-sm">
|
||||||
|
Color
|
||||||
|
</span>
|
||||||
|
<ColorPalette value={color} onChange={setColor} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<span className="mb-2 block text-muted-foreground text-sm">Icon</span>
|
||||||
|
<IconGrid value={icon} onChange={setIcon} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex justify-end gap-2 pt-2">
|
||||||
|
<Button type="button" variant="ghost" onClick={onClose}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button type="submit">{isEdit ? "Save" : "Create"}</Button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -6,9 +6,10 @@ import {
|
|||||||
useRef,
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { Button } from "./ui/button";
|
|
||||||
import { Input } from "./ui/input";
|
import { Input } from "./ui/input";
|
||||||
|
|
||||||
|
const DIGITS_ONLY_REGEX = /^\d+$/;
|
||||||
|
|
||||||
interface HpAdjustPopoverProps {
|
interface HpAdjustPopoverProps {
|
||||||
readonly onAdjust: (delta: number) => void;
|
readonly onAdjust: (delta: number) => void;
|
||||||
readonly onClose: () => void;
|
readonly onClose: () => void;
|
||||||
@@ -86,7 +87,7 @@ export function HpAdjustPopover({ onAdjust, onClose }: HpAdjustPopoverProps) {
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
ref={ref}
|
ref={ref}
|
||||||
className="fixed z-10 rounded-md border border-border bg-background p-2 shadow-lg"
|
className="card-glow fixed z-10 rounded-lg border border-border bg-background p-2"
|
||||||
style={
|
style={
|
||||||
pos
|
pos
|
||||||
? { top: pos.top, left: pos.left }
|
? { top: pos.top, left: pos.left }
|
||||||
@@ -103,36 +104,32 @@ export function HpAdjustPopover({ onAdjust, onClose }: HpAdjustPopoverProps) {
|
|||||||
className="h-7 w-[7ch] text-center text-sm tabular-nums"
|
className="h-7 w-[7ch] text-center text-sm tabular-nums"
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const v = e.target.value;
|
const v = e.target.value;
|
||||||
if (v === "" || /^\d+$/.test(v)) {
|
if (v === "" || DIGITS_ONLY_REGEX.test(v)) {
|
||||||
setInputValue(v);
|
setInputValue(v);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onKeyDown={handleKeyDown}
|
onKeyDown={handleKeyDown}
|
||||||
/>
|
/>
|
||||||
<Button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
variant="ghost"
|
|
||||||
size="icon"
|
|
||||||
disabled={!isValid}
|
disabled={!isValid}
|
||||||
className="h-7 w-7 shrink-0 text-red-400 hover:bg-red-950 hover:text-red-300"
|
className="inline-flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-red-400 transition-colors hover:bg-hp-damage-hover-bg hover:text-red-300 disabled:pointer-events-none disabled:opacity-50"
|
||||||
onClick={() => applyDelta(-1)}
|
onClick={() => applyDelta(-1)}
|
||||||
title="Apply damage"
|
title="Apply damage"
|
||||||
aria-label="Apply damage"
|
aria-label="Apply damage"
|
||||||
>
|
>
|
||||||
<Sword size={14} />
|
<Sword size={14} />
|
||||||
</Button>
|
</button>
|
||||||
<Button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
variant="ghost"
|
|
||||||
size="icon"
|
|
||||||
disabled={!isValid}
|
disabled={!isValid}
|
||||||
className="h-7 w-7 shrink-0 text-emerald-400 hover:bg-emerald-950 hover:text-emerald-300"
|
className="inline-flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-emerald-400 transition-colors hover:bg-hp-heal-hover-bg hover:text-emerald-300 disabled:pointer-events-none disabled:opacity-50"
|
||||||
onClick={() => applyDelta(1)}
|
onClick={() => applyDelta(1)}
|
||||||
title="Apply healing"
|
title="Apply healing"
|
||||||
aria-label="Apply healing"
|
aria-label="Apply healing"
|
||||||
>
|
>
|
||||||
<Heart size={14} />
|
<Heart size={14} />
|
||||||
</Button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
38
apps/web/src/components/icon-grid.tsx
Normal file
38
apps/web/src/components/icon-grid.tsx
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import type { PlayerIcon } from "@initiative/domain";
|
||||||
|
import { VALID_PLAYER_ICONS } from "@initiative/domain";
|
||||||
|
import { cn } from "../lib/utils";
|
||||||
|
import { PLAYER_ICON_MAP } from "./player-icon-map";
|
||||||
|
|
||||||
|
interface IconGridProps {
|
||||||
|
value: string;
|
||||||
|
onChange: (icon: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ICONS = [...VALID_PLAYER_ICONS] as PlayerIcon[];
|
||||||
|
|
||||||
|
export function IconGrid({ value, onChange }: Readonly<IconGridProps>) {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-wrap gap-2">
|
||||||
|
{ICONS.map((iconId) => {
|
||||||
|
const Icon = PLAYER_ICON_MAP[iconId];
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={iconId}
|
||||||
|
type="button"
|
||||||
|
onClick={() => onChange(value === iconId ? "" : iconId)}
|
||||||
|
className={cn(
|
||||||
|
"flex h-9 w-9 items-center justify-center rounded-md transition-all",
|
||||||
|
value === iconId
|
||||||
|
? "bg-primary/20 text-foreground ring-2 ring-primary"
|
||||||
|
: "text-muted-foreground hover:bg-card hover:text-foreground",
|
||||||
|
)}
|
||||||
|
aria-label={iconId}
|
||||||
|
title={iconId}
|
||||||
|
>
|
||||||
|
<Icon size={20} />
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
93
apps/web/src/components/player-character-section.tsx
Normal file
93
apps/web/src/components/player-character-section.tsx
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
import type { PlayerCharacter, PlayerCharacterId } from "@initiative/domain";
|
||||||
|
import { type RefObject, useImperativeHandle, useState } from "react";
|
||||||
|
import { CreatePlayerModal } from "./create-player-modal.js";
|
||||||
|
import { PlayerManagement } from "./player-management.js";
|
||||||
|
|
||||||
|
export interface PlayerCharacterSectionHandle {
|
||||||
|
openManagement: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PlayerCharacterSectionProps {
|
||||||
|
characters: readonly PlayerCharacter[];
|
||||||
|
onCreateCharacter: (
|
||||||
|
name: string,
|
||||||
|
ac: number,
|
||||||
|
maxHp: number,
|
||||||
|
color: string | undefined,
|
||||||
|
icon: string | undefined,
|
||||||
|
) => void;
|
||||||
|
onEditCharacter: (
|
||||||
|
id: PlayerCharacterId,
|
||||||
|
fields: {
|
||||||
|
name?: string;
|
||||||
|
ac?: number;
|
||||||
|
maxHp?: number;
|
||||||
|
color?: string | null;
|
||||||
|
icon?: string | null;
|
||||||
|
},
|
||||||
|
) => void;
|
||||||
|
onDeleteCharacter: (id: PlayerCharacterId) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const PlayerCharacterSection = function PlayerCharacterSectionInner({
|
||||||
|
characters,
|
||||||
|
onCreateCharacter,
|
||||||
|
onEditCharacter,
|
||||||
|
onDeleteCharacter,
|
||||||
|
ref,
|
||||||
|
}: PlayerCharacterSectionProps & {
|
||||||
|
ref?: RefObject<PlayerCharacterSectionHandle | null>;
|
||||||
|
}) {
|
||||||
|
const [managementOpen, setManagementOpen] = useState(false);
|
||||||
|
const [createOpen, setCreateOpen] = useState(false);
|
||||||
|
const [editingPlayer, setEditingPlayer] = useState<
|
||||||
|
PlayerCharacter | undefined
|
||||||
|
>();
|
||||||
|
|
||||||
|
useImperativeHandle(ref, () => ({
|
||||||
|
openManagement: () => setManagementOpen(true),
|
||||||
|
}));
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<CreatePlayerModal
|
||||||
|
open={createOpen}
|
||||||
|
onClose={() => {
|
||||||
|
setCreateOpen(false);
|
||||||
|
setEditingPlayer(undefined);
|
||||||
|
setManagementOpen(true);
|
||||||
|
}}
|
||||||
|
onSave={(name, ac, maxHp, color, icon) => {
|
||||||
|
if (editingPlayer) {
|
||||||
|
onEditCharacter(editingPlayer.id, {
|
||||||
|
name,
|
||||||
|
ac,
|
||||||
|
maxHp,
|
||||||
|
color: color ?? null,
|
||||||
|
icon: icon ?? null,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
onCreateCharacter(name, ac, maxHp, color, icon);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
playerCharacter={editingPlayer}
|
||||||
|
/>
|
||||||
|
<PlayerManagement
|
||||||
|
open={managementOpen}
|
||||||
|
onClose={() => setManagementOpen(false)}
|
||||||
|
characters={characters}
|
||||||
|
onEdit={(pc) => {
|
||||||
|
setEditingPlayer(pc);
|
||||||
|
setCreateOpen(true);
|
||||||
|
setManagementOpen(false);
|
||||||
|
}}
|
||||||
|
onDelete={(id) => onDeleteCharacter(id)}
|
||||||
|
onCreate={() => {
|
||||||
|
setEditingPlayer(undefined);
|
||||||
|
setCreateOpen(true);
|
||||||
|
setManagementOpen(false);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
50
apps/web/src/components/player-icon-map.ts
Normal file
50
apps/web/src/components/player-icon-map.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import type { PlayerColor, PlayerIcon } from "@initiative/domain";
|
||||||
|
import type { LucideIcon } from "lucide-react";
|
||||||
|
import {
|
||||||
|
Axe,
|
||||||
|
Crosshair,
|
||||||
|
Crown,
|
||||||
|
Eye,
|
||||||
|
Feather,
|
||||||
|
Flame,
|
||||||
|
Heart,
|
||||||
|
Moon,
|
||||||
|
Shield,
|
||||||
|
Skull,
|
||||||
|
Star,
|
||||||
|
Sun,
|
||||||
|
Sword,
|
||||||
|
Wand,
|
||||||
|
Zap,
|
||||||
|
} from "lucide-react";
|
||||||
|
|
||||||
|
export const PLAYER_ICON_MAP: Record<PlayerIcon, LucideIcon> = {
|
||||||
|
sword: Sword,
|
||||||
|
shield: Shield,
|
||||||
|
skull: Skull,
|
||||||
|
heart: Heart,
|
||||||
|
wand: Wand,
|
||||||
|
flame: Flame,
|
||||||
|
crown: Crown,
|
||||||
|
star: Star,
|
||||||
|
moon: Moon,
|
||||||
|
sun: Sun,
|
||||||
|
axe: Axe,
|
||||||
|
crosshair: Crosshair,
|
||||||
|
eye: Eye,
|
||||||
|
feather: Feather,
|
||||||
|
zap: Zap,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const PLAYER_COLOR_HEX: Record<PlayerColor, string> = {
|
||||||
|
red: "#ef4444",
|
||||||
|
blue: "#3b82f6",
|
||||||
|
green: "#22c55e",
|
||||||
|
purple: "#a855f7",
|
||||||
|
orange: "#f97316",
|
||||||
|
pink: "#ec4899",
|
||||||
|
cyan: "#06b6d4",
|
||||||
|
yellow: "#eab308",
|
||||||
|
emerald: "#10b981",
|
||||||
|
indigo: "#6366f1",
|
||||||
|
};
|
||||||
133
apps/web/src/components/player-management.tsx
Normal file
133
apps/web/src/components/player-management.tsx
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
import type { PlayerCharacter, PlayerCharacterId } from "@initiative/domain";
|
||||||
|
import { Pencil, Plus, Trash2, X } from "lucide-react";
|
||||||
|
import { useEffect, useRef } from "react";
|
||||||
|
import { PLAYER_COLOR_HEX, PLAYER_ICON_MAP } from "./player-icon-map";
|
||||||
|
import { Button } from "./ui/button";
|
||||||
|
import { ConfirmButton } from "./ui/confirm-button";
|
||||||
|
|
||||||
|
interface PlayerManagementProps {
|
||||||
|
open: boolean;
|
||||||
|
onClose: () => void;
|
||||||
|
characters: readonly PlayerCharacter[];
|
||||||
|
onEdit: (pc: PlayerCharacter) => void;
|
||||||
|
onDelete: (id: PlayerCharacterId) => void;
|
||||||
|
onCreate: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PlayerManagement({
|
||||||
|
open,
|
||||||
|
onClose,
|
||||||
|
characters,
|
||||||
|
onEdit,
|
||||||
|
onDelete,
|
||||||
|
onCreate,
|
||||||
|
}: Readonly<PlayerManagementProps>) {
|
||||||
|
const dialogRef = useRef<HTMLDialogElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const dialog = dialogRef.current;
|
||||||
|
if (!dialog) return;
|
||||||
|
if (open && !dialog.open) {
|
||||||
|
dialog.showModal();
|
||||||
|
} else if (!open && dialog.open) {
|
||||||
|
dialog.close();
|
||||||
|
}
|
||||||
|
}, [open]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const dialog = dialogRef.current;
|
||||||
|
if (!dialog) return;
|
||||||
|
function handleCancel(e: Event) {
|
||||||
|
e.preventDefault();
|
||||||
|
onClose();
|
||||||
|
}
|
||||||
|
function handleBackdropClick(e: MouseEvent) {
|
||||||
|
if (e.target === dialog) onClose();
|
||||||
|
}
|
||||||
|
dialog.addEventListener("cancel", handleCancel);
|
||||||
|
dialog.addEventListener("mousedown", handleBackdropClick);
|
||||||
|
return () => {
|
||||||
|
dialog.removeEventListener("cancel", handleCancel);
|
||||||
|
dialog.removeEventListener("mousedown", handleBackdropClick);
|
||||||
|
};
|
||||||
|
}, [onClose]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<dialog
|
||||||
|
ref={dialogRef}
|
||||||
|
className="card-glow m-auto w-full max-w-md rounded-lg border border-border bg-card p-6 backdrop:bg-black/50"
|
||||||
|
>
|
||||||
|
<div className="mb-4 flex items-center justify-between">
|
||||||
|
<h2 className="font-semibold text-foreground text-lg">
|
||||||
|
Player Characters
|
||||||
|
</h2>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
onClick={onClose}
|
||||||
|
className="text-muted-foreground"
|
||||||
|
>
|
||||||
|
<X size={20} />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{characters.length === 0 ? (
|
||||||
|
<div className="flex flex-col items-center gap-3 py-8 text-center">
|
||||||
|
<p className="text-muted-foreground">No player characters yet</p>
|
||||||
|
<Button onClick={onCreate}>
|
||||||
|
<Plus size={16} />
|
||||||
|
Create your first player character
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="flex flex-col gap-1">
|
||||||
|
{characters.map((pc) => {
|
||||||
|
const Icon = pc.icon ? PLAYER_ICON_MAP[pc.icon] : undefined;
|
||||||
|
const color = pc.color ? PLAYER_COLOR_HEX[pc.color] : undefined;
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={pc.id}
|
||||||
|
className="group flex items-center gap-3 rounded-md px-3 py-2 hover:bg-hover-neutral-bg"
|
||||||
|
>
|
||||||
|
{!!Icon && (
|
||||||
|
<Icon size={18} style={{ color }} className="shrink-0" />
|
||||||
|
)}
|
||||||
|
<span className="flex-1 truncate text-foreground text-sm">
|
||||||
|
{pc.name}
|
||||||
|
</span>
|
||||||
|
<span className="text-muted-foreground text-xs tabular-nums">
|
||||||
|
AC {pc.ac}
|
||||||
|
</span>
|
||||||
|
<span className="text-muted-foreground text-xs tabular-nums">
|
||||||
|
HP {pc.maxHp}
|
||||||
|
</span>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon-sm"
|
||||||
|
onClick={() => onEdit(pc)}
|
||||||
|
className="text-muted-foreground"
|
||||||
|
title="Edit"
|
||||||
|
>
|
||||||
|
<Pencil size={14} />
|
||||||
|
</Button>
|
||||||
|
<ConfirmButton
|
||||||
|
icon={<Trash2 size={14} />}
|
||||||
|
label="Delete player character"
|
||||||
|
onConfirm={() => onDelete(pc.id)}
|
||||||
|
size="icon-sm"
|
||||||
|
className="text-muted-foreground"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
<div className="mt-2 flex justify-end">
|
||||||
|
<Button onClick={onCreate} variant="ghost">
|
||||||
|
<Plus size={16} />
|
||||||
|
Add
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
127
apps/web/src/components/source-fetch-prompt.tsx
Normal file
127
apps/web/src/components/source-fetch-prompt.tsx
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
import { Download, Loader2, Upload } from "lucide-react";
|
||||||
|
import { useId, useRef, useState } from "react";
|
||||||
|
import { getDefaultFetchUrl } from "../adapters/bestiary-index-adapter.js";
|
||||||
|
import { Button } from "./ui/button.js";
|
||||||
|
import { Input } from "./ui/input.js";
|
||||||
|
|
||||||
|
interface SourceFetchPromptProps {
|
||||||
|
sourceCode: string;
|
||||||
|
sourceDisplayName: string;
|
||||||
|
fetchAndCacheSource: (sourceCode: string, url: string) => Promise<void>;
|
||||||
|
onSourceLoaded: () => void;
|
||||||
|
onUploadSource: (sourceCode: string, jsonData: unknown) => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SourceFetchPrompt({
|
||||||
|
sourceCode,
|
||||||
|
sourceDisplayName,
|
||||||
|
fetchAndCacheSource,
|
||||||
|
onSourceLoaded,
|
||||||
|
onUploadSource,
|
||||||
|
}: Readonly<SourceFetchPromptProps>) {
|
||||||
|
const [url, setUrl] = useState(() => getDefaultFetchUrl(sourceCode));
|
||||||
|
const [status, setStatus] = useState<"idle" | "fetching" | "error">("idle");
|
||||||
|
const [error, setError] = useState<string>("");
|
||||||
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
const sourceUrlId = useId();
|
||||||
|
|
||||||
|
const handleFetch = async () => {
|
||||||
|
setStatus("fetching");
|
||||||
|
setError("");
|
||||||
|
try {
|
||||||
|
await fetchAndCacheSource(sourceCode, url);
|
||||||
|
onSourceLoaded();
|
||||||
|
} catch (e) {
|
||||||
|
setStatus("error");
|
||||||
|
setError(e instanceof Error ? e.message : "Failed to fetch source data");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleFileUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
const file = e.target.files?.[0];
|
||||||
|
if (!file) return;
|
||||||
|
|
||||||
|
setStatus("fetching");
|
||||||
|
setError("");
|
||||||
|
|
||||||
|
try {
|
||||||
|
const text = await file.text();
|
||||||
|
const json = JSON.parse(text);
|
||||||
|
await onUploadSource(sourceCode, json);
|
||||||
|
onSourceLoaded();
|
||||||
|
} catch (err) {
|
||||||
|
setStatus("error");
|
||||||
|
setError(
|
||||||
|
err instanceof Error ? err.message : "Failed to process uploaded file",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset file input
|
||||||
|
if (fileInputRef.current) {
|
||||||
|
fileInputRef.current.value = "";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div>
|
||||||
|
<h3 className="font-semibold text-foreground text-sm">
|
||||||
|
Load {sourceDisplayName}
|
||||||
|
</h3>
|
||||||
|
<p className="mt-1 text-muted-foreground text-xs">
|
||||||
|
Stat block data for this source needs to be loaded. Enter a URL or
|
||||||
|
upload a JSON file.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
<label htmlFor={sourceUrlId} className="text-muted-foreground text-xs">
|
||||||
|
Source URL
|
||||||
|
</label>
|
||||||
|
<Input
|
||||||
|
id={sourceUrlId}
|
||||||
|
type="url"
|
||||||
|
value={url}
|
||||||
|
onChange={(e) => setUrl(e.target.value)}
|
||||||
|
disabled={status === "fetching"}
|
||||||
|
className="text-xs"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Button onClick={handleFetch} disabled={status === "fetching" || !url}>
|
||||||
|
{status === "fetching" ? (
|
||||||
|
<Loader2 className="mr-1 h-3 w-3 animate-spin" />
|
||||||
|
) : (
|
||||||
|
<Download className="mr-1 h-3 w-3" />
|
||||||
|
)}
|
||||||
|
{status === "fetching" ? "Loading..." : "Load"}
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<span className="text-muted-foreground text-xs">or</span>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => fileInputRef.current?.click()}
|
||||||
|
disabled={status === "fetching"}
|
||||||
|
>
|
||||||
|
<Upload className="mr-1 h-3 w-3" />
|
||||||
|
Upload file
|
||||||
|
</Button>
|
||||||
|
<input
|
||||||
|
ref={fileInputRef}
|
||||||
|
type="file"
|
||||||
|
accept=".json"
|
||||||
|
className="hidden"
|
||||||
|
onChange={handleFileUpload}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{status === "error" && (
|
||||||
|
<div className="rounded-md border border-destructive/50 bg-destructive/10 px-3 py-2 text-destructive text-xs">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
126
apps/web/src/components/source-manager.tsx
Normal file
126
apps/web/src/components/source-manager.tsx
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
import { Database, Search, Trash2 } from "lucide-react";
|
||||||
|
import {
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useMemo,
|
||||||
|
useOptimistic,
|
||||||
|
useState,
|
||||||
|
} from "react";
|
||||||
|
import type { CachedSourceInfo } from "../adapters/bestiary-cache.js";
|
||||||
|
import * as bestiaryCache from "../adapters/bestiary-cache.js";
|
||||||
|
import { Button } from "./ui/button.js";
|
||||||
|
import { Input } from "./ui/input.js";
|
||||||
|
|
||||||
|
interface SourceManagerProps {
|
||||||
|
onCacheCleared: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SourceManager({
|
||||||
|
onCacheCleared,
|
||||||
|
}: Readonly<SourceManagerProps>) {
|
||||||
|
const [sources, setSources] = useState<CachedSourceInfo[]>([]);
|
||||||
|
const [filter, setFilter] = useState("");
|
||||||
|
const [optimisticSources, applyOptimistic] = useOptimistic(
|
||||||
|
sources,
|
||||||
|
(
|
||||||
|
state,
|
||||||
|
action: { type: "remove"; sourceCode: string } | { type: "clear" },
|
||||||
|
) =>
|
||||||
|
action.type === "clear"
|
||||||
|
? []
|
||||||
|
: state.filter((s) => s.sourceCode !== action.sourceCode),
|
||||||
|
);
|
||||||
|
|
||||||
|
const loadSources = useCallback(async () => {
|
||||||
|
const cached = await bestiaryCache.getCachedSources();
|
||||||
|
setSources(cached);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
void loadSources();
|
||||||
|
}, [loadSources]);
|
||||||
|
|
||||||
|
const handleClearSource = async (sourceCode: string) => {
|
||||||
|
applyOptimistic({ type: "remove", sourceCode });
|
||||||
|
await bestiaryCache.clearSource(sourceCode);
|
||||||
|
await loadSources();
|
||||||
|
onCacheCleared();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleClearAll = async () => {
|
||||||
|
applyOptimistic({ type: "clear" });
|
||||||
|
await bestiaryCache.clearAll();
|
||||||
|
await loadSources();
|
||||||
|
onCacheCleared();
|
||||||
|
};
|
||||||
|
|
||||||
|
const filteredSources = useMemo(() => {
|
||||||
|
const term = filter.toLowerCase();
|
||||||
|
return term
|
||||||
|
? optimisticSources.filter((s) =>
|
||||||
|
s.displayName.toLowerCase().includes(term),
|
||||||
|
)
|
||||||
|
: optimisticSources;
|
||||||
|
}, [optimisticSources, filter]);
|
||||||
|
|
||||||
|
if (optimisticSources.length === 0) {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col items-center gap-2 py-8 text-center">
|
||||||
|
<Database className="h-8 w-8 text-muted-foreground" />
|
||||||
|
<p className="text-muted-foreground text-sm">No cached sources</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-3">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span className="font-semibold text-foreground text-sm">
|
||||||
|
Cached Sources
|
||||||
|
</span>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="hover:border-hover-destructive hover:text-hover-destructive"
|
||||||
|
onClick={handleClearAll}
|
||||||
|
>
|
||||||
|
<Trash2 className="mr-1 h-3 w-3" />
|
||||||
|
Clear All
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div className="relative">
|
||||||
|
<Search className="pointer-events-none absolute top-1/2 left-3 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground" />
|
||||||
|
<Input
|
||||||
|
placeholder="Filter sources…"
|
||||||
|
value={filter}
|
||||||
|
onChange={(e) => setFilter(e.target.value)}
|
||||||
|
className="pl-8"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<ul className="flex flex-col gap-1">
|
||||||
|
{filteredSources.map((source) => (
|
||||||
|
<li
|
||||||
|
key={source.sourceCode}
|
||||||
|
className="flex items-center justify-between rounded-md border border-border px-3 py-2"
|
||||||
|
>
|
||||||
|
<div>
|
||||||
|
<span className="text-foreground text-sm">
|
||||||
|
{source.displayName}
|
||||||
|
</span>
|
||||||
|
<span className="ml-2 text-muted-foreground text-xs">
|
||||||
|
{source.creatureCount} creatures
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => handleClearSource(source.sourceCode)}
|
||||||
|
className="rounded-md p-1 text-muted-foreground transition-colors hover:bg-hover-destructive-bg hover:text-hover-destructive"
|
||||||
|
aria-label={`Remove ${source.displayName}`}
|
||||||
|
>
|
||||||
|
<Trash2 className="h-3.5 w-3.5" />
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,77 +1,367 @@
|
|||||||
import type { Creature } from "@initiative/domain";
|
import type { Creature, CreatureId } from "@initiative/domain";
|
||||||
import { X } from "lucide-react";
|
import { PanelRightClose, Pin, PinOff } from "lucide-react";
|
||||||
|
import type { ReactNode } from "react";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import { getSourceDisplayName } from "../adapters/bestiary-index-adapter.js";
|
||||||
|
import type { BulkImportState } from "../hooks/use-bulk-import.js";
|
||||||
|
import { useSwipeToDismiss } from "../hooks/use-swipe-to-dismiss.js";
|
||||||
|
import { cn } from "../lib/utils.js";
|
||||||
|
import { BulkImportPrompt } from "./bulk-import-prompt.js";
|
||||||
|
import { SourceFetchPrompt } from "./source-fetch-prompt.js";
|
||||||
|
import { SourceManager } from "./source-manager.js";
|
||||||
import { StatBlock } from "./stat-block.js";
|
import { StatBlock } from "./stat-block.js";
|
||||||
|
import { Button } from "./ui/button.js";
|
||||||
|
|
||||||
interface StatBlockPanelProps {
|
interface StatBlockPanelProps {
|
||||||
|
creatureId: CreatureId | null;
|
||||||
creature: Creature | null;
|
creature: Creature | null;
|
||||||
onClose: () => void;
|
isSourceCached: (sourceCode: string) => Promise<boolean>;
|
||||||
|
fetchAndCacheSource: (sourceCode: string, url: string) => Promise<void>;
|
||||||
|
uploadAndCacheSource: (
|
||||||
|
sourceCode: string,
|
||||||
|
jsonData: unknown,
|
||||||
|
) => Promise<void>;
|
||||||
|
refreshCache: () => Promise<void>;
|
||||||
|
panelRole: "browse" | "pinned";
|
||||||
|
isCollapsed: boolean;
|
||||||
|
onToggleCollapse: () => void;
|
||||||
|
onPin: () => void;
|
||||||
|
onUnpin: () => void;
|
||||||
|
showPinButton: boolean;
|
||||||
|
side: "left" | "right";
|
||||||
|
onDismiss: () => void;
|
||||||
|
bulkImportMode?: boolean;
|
||||||
|
bulkImportState?: BulkImportState;
|
||||||
|
onStartBulkImport?: (baseUrl: string) => void;
|
||||||
|
onBulkImportDone?: () => void;
|
||||||
|
sourceManagerMode?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function StatBlockPanel({ creature, onClose }: StatBlockPanelProps) {
|
function extractSourceCode(cId: CreatureId): string {
|
||||||
const [isDesktop, setIsDesktop] = useState(
|
const colonIndex = cId.indexOf(":");
|
||||||
() => window.matchMedia("(min-width: 1024px)").matches,
|
if (colonIndex === -1) return "";
|
||||||
|
return cId.slice(0, colonIndex).toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
function CollapsedTab({
|
||||||
|
creatureName,
|
||||||
|
side,
|
||||||
|
onToggleCollapse,
|
||||||
|
}: Readonly<{
|
||||||
|
creatureName: string;
|
||||||
|
side: "left" | "right";
|
||||||
|
onToggleCollapse: () => void;
|
||||||
|
}>) {
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onToggleCollapse}
|
||||||
|
className={cn(
|
||||||
|
"flex h-full w-[40px] cursor-pointer items-center justify-center text-muted-foreground hover:text-hover-neutral",
|
||||||
|
side === "right" ? "self-start" : "self-end",
|
||||||
|
)}
|
||||||
|
aria-label="Expand stat block panel"
|
||||||
|
>
|
||||||
|
<span className="writing-vertical-rl font-medium text-sm">
|
||||||
|
{creatureName}
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function PanelHeader({
|
||||||
|
panelRole,
|
||||||
|
showPinButton,
|
||||||
|
onToggleCollapse,
|
||||||
|
onPin,
|
||||||
|
onUnpin,
|
||||||
|
}: Readonly<{
|
||||||
|
panelRole: "browse" | "pinned";
|
||||||
|
showPinButton: boolean;
|
||||||
|
onToggleCollapse: () => void;
|
||||||
|
onPin: () => void;
|
||||||
|
onUnpin: () => void;
|
||||||
|
}>) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-center justify-between border-border border-b px-4 py-2">
|
||||||
|
<div className="flex items-center gap-1">
|
||||||
|
{panelRole === "browse" && (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon-sm"
|
||||||
|
onClick={onToggleCollapse}
|
||||||
|
className="text-muted-foreground"
|
||||||
|
aria-label="Collapse stat block panel"
|
||||||
|
>
|
||||||
|
<PanelRightClose className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-1">
|
||||||
|
{panelRole === "browse" && showPinButton && (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon-sm"
|
||||||
|
onClick={onPin}
|
||||||
|
className="text-muted-foreground"
|
||||||
|
aria-label="Pin creature"
|
||||||
|
>
|
||||||
|
<Pin className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{panelRole === "pinned" && (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon-sm"
|
||||||
|
onClick={onUnpin}
|
||||||
|
className="text-muted-foreground"
|
||||||
|
aria-label="Unpin creature"
|
||||||
|
>
|
||||||
|
<PinOff className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function DesktopPanel({
|
||||||
|
isCollapsed,
|
||||||
|
side,
|
||||||
|
creatureName,
|
||||||
|
panelRole,
|
||||||
|
showPinButton,
|
||||||
|
onToggleCollapse,
|
||||||
|
onPin,
|
||||||
|
onUnpin,
|
||||||
|
children,
|
||||||
|
}: Readonly<{
|
||||||
|
isCollapsed: boolean;
|
||||||
|
side: "left" | "right";
|
||||||
|
creatureName: string;
|
||||||
|
panelRole: "browse" | "pinned";
|
||||||
|
showPinButton: boolean;
|
||||||
|
onToggleCollapse: () => void;
|
||||||
|
onPin: () => void;
|
||||||
|
onUnpin: () => void;
|
||||||
|
children: ReactNode;
|
||||||
|
}>) {
|
||||||
|
const sideClasses = side === "left" ? "left-0 border-r" : "right-0 border-l";
|
||||||
|
const collapsedTranslate =
|
||||||
|
side === "right"
|
||||||
|
? "translate-x-[calc(100%-40px)]"
|
||||||
|
: "translate-x-[calc(-100%+40px)]";
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
"panel-glow fixed top-0 bottom-0 flex w-[400px] flex-col border-border bg-card transition-slide-panel",
|
||||||
|
sideClasses,
|
||||||
|
isCollapsed ? collapsedTranslate : "translate-x-0",
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{isCollapsed ? (
|
||||||
|
<CollapsedTab
|
||||||
|
creatureName={creatureName}
|
||||||
|
side={side}
|
||||||
|
onToggleCollapse={onToggleCollapse}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<PanelHeader
|
||||||
|
panelRole={panelRole}
|
||||||
|
showPinButton={showPinButton}
|
||||||
|
onToggleCollapse={onToggleCollapse}
|
||||||
|
onPin={onPin}
|
||||||
|
onUnpin={onUnpin}
|
||||||
|
/>
|
||||||
|
<div className="flex-1 overflow-y-auto p-4">{children}</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function MobileDrawer({
|
||||||
|
onDismiss,
|
||||||
|
children,
|
||||||
|
}: Readonly<{
|
||||||
|
onDismiss: () => void;
|
||||||
|
children: ReactNode;
|
||||||
|
}>) {
|
||||||
|
const { offsetX, isSwiping, handlers } = useSwipeToDismiss(onDismiss);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="fixed inset-0 z-50">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="fade-in absolute inset-0 animate-in bg-black/50"
|
||||||
|
onClick={onDismiss}
|
||||||
|
aria-label="Close stat block"
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
"panel-glow absolute top-0 right-0 bottom-0 w-[85%] max-w-md border-border border-l bg-card",
|
||||||
|
!isSwiping && "animate-slide-in-right",
|
||||||
|
)}
|
||||||
|
style={
|
||||||
|
isSwiping ? { transform: `translateX(${offsetX}px)` } : undefined
|
||||||
|
}
|
||||||
|
{...handlers}
|
||||||
|
>
|
||||||
|
<div className="flex items-center justify-between border-border border-b px-4 py-2">
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon-sm"
|
||||||
|
onClick={onDismiss}
|
||||||
|
className="text-muted-foreground"
|
||||||
|
aria-label="Collapse stat block panel"
|
||||||
|
>
|
||||||
|
<PanelRightClose className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div className="h-[calc(100%-41px)] overflow-y-auto p-4">
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function StatBlockPanel({
|
||||||
|
creatureId,
|
||||||
|
creature,
|
||||||
|
isSourceCached,
|
||||||
|
fetchAndCacheSource,
|
||||||
|
uploadAndCacheSource,
|
||||||
|
refreshCache,
|
||||||
|
panelRole,
|
||||||
|
isCollapsed,
|
||||||
|
onToggleCollapse,
|
||||||
|
onPin,
|
||||||
|
onUnpin,
|
||||||
|
showPinButton,
|
||||||
|
side,
|
||||||
|
onDismiss,
|
||||||
|
bulkImportMode,
|
||||||
|
bulkImportState,
|
||||||
|
onStartBulkImport,
|
||||||
|
onBulkImportDone,
|
||||||
|
sourceManagerMode,
|
||||||
|
}: Readonly<StatBlockPanelProps>) {
|
||||||
|
const [isDesktop, setIsDesktop] = useState(
|
||||||
|
() => globalThis.matchMedia("(min-width: 1024px)").matches,
|
||||||
|
);
|
||||||
|
const [needsFetch, setNeedsFetch] = useState(false);
|
||||||
|
const [checkingCache, setCheckingCache] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const mq = window.matchMedia("(min-width: 1024px)");
|
const mq = globalThis.matchMedia("(min-width: 1024px)");
|
||||||
const handler = (e: MediaQueryListEvent) => setIsDesktop(e.matches);
|
const handler = (e: MediaQueryListEvent) => setIsDesktop(e.matches);
|
||||||
mq.addEventListener("change", handler);
|
mq.addEventListener("change", handler);
|
||||||
return () => mq.removeEventListener("change", handler);
|
return () => mq.removeEventListener("change", handler);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (!creature) return null;
|
useEffect(() => {
|
||||||
|
if (!creatureId || creature) {
|
||||||
|
setNeedsFetch(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (isDesktop) {
|
const sourceCode = extractSourceCode(creatureId);
|
||||||
|
if (!sourceCode) {
|
||||||
|
setNeedsFetch(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setCheckingCache(true);
|
||||||
|
void isSourceCached(sourceCode).then((cached) => {
|
||||||
|
setNeedsFetch(!cached);
|
||||||
|
setCheckingCache(false);
|
||||||
|
});
|
||||||
|
}, [creatureId, creature, isSourceCached]);
|
||||||
|
|
||||||
|
if (!creatureId && !bulkImportMode && !sourceManagerMode) return null;
|
||||||
|
|
||||||
|
const sourceCode = creatureId ? extractSourceCode(creatureId) : "";
|
||||||
|
|
||||||
|
const handleSourceLoaded = async () => {
|
||||||
|
await refreshCache();
|
||||||
|
setNeedsFetch(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const renderContent = () => {
|
||||||
|
if (sourceManagerMode) {
|
||||||
|
return <SourceManager onCacheCleared={refreshCache} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
bulkImportMode &&
|
||||||
|
bulkImportState &&
|
||||||
|
onStartBulkImport &&
|
||||||
|
onBulkImportDone
|
||||||
|
) {
|
||||||
return (
|
return (
|
||||||
<div className="fixed top-0 right-0 bottom-0 flex w-[400px] flex-col border-l border-border bg-card">
|
<BulkImportPrompt
|
||||||
<div className="flex items-center justify-between border-b border-border px-4 py-2">
|
importState={bulkImportState}
|
||||||
<span className="text-sm font-semibold text-muted-foreground">
|
onStartImport={onStartBulkImport}
|
||||||
Stat Block
|
onDone={onBulkImportDone}
|
||||||
</span>
|
/>
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={onClose}
|
|
||||||
className="text-muted-foreground hover:text-hover-neutral"
|
|
||||||
>
|
|
||||||
<X className="h-4 w-4" />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<div className="flex-1 overflow-y-auto p-4">
|
|
||||||
<StatBlock creature={creature} />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mobile drawer
|
if (checkingCache) {
|
||||||
return (
|
return (
|
||||||
<div className="fixed inset-0 z-50">
|
<div className="p-4 text-muted-foreground text-sm">Loading...</div>
|
||||||
{/* Backdrop */}
|
);
|
||||||
<button
|
}
|
||||||
type="button"
|
|
||||||
className="absolute inset-0 bg-black/50 animate-in fade-in"
|
if (creature) {
|
||||||
onClick={onClose}
|
return <StatBlock creature={creature} />;
|
||||||
aria-label="Close stat block"
|
}
|
||||||
|
|
||||||
|
if (needsFetch && sourceCode) {
|
||||||
|
return (
|
||||||
|
<SourceFetchPrompt
|
||||||
|
sourceCode={sourceCode}
|
||||||
|
sourceDisplayName={getSourceDisplayName(sourceCode)}
|
||||||
|
fetchAndCacheSource={fetchAndCacheSource}
|
||||||
|
onSourceLoaded={handleSourceLoaded}
|
||||||
|
onUploadSource={uploadAndCacheSource}
|
||||||
/>
|
/>
|
||||||
{/* Drawer */}
|
);
|
||||||
<div className="absolute top-0 right-0 bottom-0 w-[85%] max-w-md animate-slide-in-right border-l border-border bg-card shadow-xl">
|
}
|
||||||
<div className="flex items-center justify-between border-b border-border px-4 py-2">
|
|
||||||
<span className="text-sm font-semibold text-muted-foreground">
|
return (
|
||||||
Stat Block
|
<div className="p-4 text-muted-foreground text-sm">
|
||||||
</span>
|
No stat block available
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={onClose}
|
|
||||||
className="text-muted-foreground hover:text-hover-neutral"
|
|
||||||
>
|
|
||||||
<X className="h-4 w-4" />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<div className="h-[calc(100%-41px)] overflow-y-auto p-4">
|
|
||||||
<StatBlock creature={creature} />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
let fallbackName = "Creature";
|
||||||
|
if (sourceManagerMode) fallbackName = "Sources";
|
||||||
|
else if (bulkImportMode) fallbackName = "Import All Sources";
|
||||||
|
const creatureName = creature?.name ?? fallbackName;
|
||||||
|
|
||||||
|
if (isDesktop) {
|
||||||
|
return (
|
||||||
|
<DesktopPanel
|
||||||
|
isCollapsed={isCollapsed}
|
||||||
|
side={side}
|
||||||
|
creatureName={creatureName}
|
||||||
|
panelRole={panelRole}
|
||||||
|
showPinButton={showPinButton}
|
||||||
|
onToggleCollapse={onToggleCollapse}
|
||||||
|
onPin={onPin}
|
||||||
|
onUnpin={onUnpin}
|
||||||
|
>
|
||||||
|
{renderContent()}
|
||||||
|
</DesktopPanel>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (panelRole === "pinned" || isCollapsed) return null;
|
||||||
|
|
||||||
|
return <MobileDrawer onDismiss={onDismiss}>{renderContent()}</MobileDrawer>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ function abilityMod(score: number): string {
|
|||||||
function PropertyLine({
|
function PropertyLine({
|
||||||
label,
|
label,
|
||||||
value,
|
value,
|
||||||
}: {
|
}: Readonly<{
|
||||||
label: string;
|
label: string;
|
||||||
value: string | undefined;
|
value: string | undefined;
|
||||||
}) {
|
}>) {
|
||||||
if (!value) return null;
|
if (!value) return null;
|
||||||
return (
|
return (
|
||||||
<div className="text-sm">
|
<div className="text-sm">
|
||||||
@@ -30,11 +30,11 @@ function PropertyLine({
|
|||||||
|
|
||||||
function SectionDivider() {
|
function SectionDivider() {
|
||||||
return (
|
return (
|
||||||
<div className="my-2 h-px bg-gradient-to-r from-amber-800/60 via-amber-700/40 to-transparent" />
|
<div className="my-2 h-px bg-gradient-to-r from-stat-divider-from via-stat-divider-via to-transparent" />
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function StatBlock({ creature }: StatBlockProps) {
|
export function StatBlock({ creature }: Readonly<StatBlockProps>) {
|
||||||
const abilities = [
|
const abilities = [
|
||||||
{ label: "STR", score: creature.abilities.str },
|
{ label: "STR", score: creature.abilities.str },
|
||||||
{ label: "DEX", score: creature.abilities.dex },
|
{ label: "DEX", score: creature.abilities.dex },
|
||||||
@@ -54,11 +54,11 @@ export function StatBlock({ creature }: StatBlockProps) {
|
|||||||
<div className="space-y-1 text-foreground">
|
<div className="space-y-1 text-foreground">
|
||||||
{/* Header */}
|
{/* Header */}
|
||||||
<div>
|
<div>
|
||||||
<h2 className="text-xl font-bold text-amber-400">{creature.name}</h2>
|
<h2 className="font-bold text-stat-heading text-xl">{creature.name}</h2>
|
||||||
<p className="text-sm italic text-muted-foreground">
|
<p className="text-muted-foreground text-sm italic">
|
||||||
{creature.size} {creature.type}, {creature.alignment}
|
{creature.size} {creature.type}, {creature.alignment}
|
||||||
</p>
|
</p>
|
||||||
<p className="text-xs text-muted-foreground">
|
<p className="text-muted-foreground text-xs">
|
||||||
{creature.sourceDisplayName}
|
{creature.sourceDisplayName}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
@@ -69,7 +69,7 @@ export function StatBlock({ creature }: StatBlockProps) {
|
|||||||
<div className="space-y-0.5 text-sm">
|
<div className="space-y-0.5 text-sm">
|
||||||
<div>
|
<div>
|
||||||
<span className="font-semibold">Armor Class</span> {creature.ac}
|
<span className="font-semibold">Armor Class</span> {creature.ac}
|
||||||
{creature.acSource && (
|
{!!creature.acSource && (
|
||||||
<span className="text-muted-foreground">
|
<span className="text-muted-foreground">
|
||||||
{" "}
|
{" "}
|
||||||
({creature.acSource})
|
({creature.acSource})
|
||||||
@@ -194,7 +194,7 @@ export function StatBlock({ creature }: StatBlockProps) {
|
|||||||
{creature.actions && creature.actions.length > 0 && (
|
{creature.actions && creature.actions.length > 0 && (
|
||||||
<>
|
<>
|
||||||
<SectionDivider />
|
<SectionDivider />
|
||||||
<h3 className="text-base font-bold text-amber-400">Actions</h3>
|
<h3 className="font-bold text-base text-stat-heading">Actions</h3>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
{creature.actions.map((a) => (
|
{creature.actions.map((a) => (
|
||||||
<div key={a.name} className="text-sm">
|
<div key={a.name} className="text-sm">
|
||||||
@@ -209,7 +209,9 @@ export function StatBlock({ creature }: StatBlockProps) {
|
|||||||
{creature.bonusActions && creature.bonusActions.length > 0 && (
|
{creature.bonusActions && creature.bonusActions.length > 0 && (
|
||||||
<>
|
<>
|
||||||
<SectionDivider />
|
<SectionDivider />
|
||||||
<h3 className="text-base font-bold text-amber-400">Bonus Actions</h3>
|
<h3 className="font-bold text-base text-stat-heading">
|
||||||
|
Bonus Actions
|
||||||
|
</h3>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
{creature.bonusActions.map((a) => (
|
{creature.bonusActions.map((a) => (
|
||||||
<div key={a.name} className="text-sm">
|
<div key={a.name} className="text-sm">
|
||||||
@@ -224,7 +226,7 @@ export function StatBlock({ creature }: StatBlockProps) {
|
|||||||
{creature.reactions && creature.reactions.length > 0 && (
|
{creature.reactions && creature.reactions.length > 0 && (
|
||||||
<>
|
<>
|
||||||
<SectionDivider />
|
<SectionDivider />
|
||||||
<h3 className="text-base font-bold text-amber-400">Reactions</h3>
|
<h3 className="font-bold text-base text-stat-heading">Reactions</h3>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
{creature.reactions.map((a) => (
|
{creature.reactions.map((a) => (
|
||||||
<div key={a.name} className="text-sm">
|
<div key={a.name} className="text-sm">
|
||||||
@@ -236,13 +238,13 @@ export function StatBlock({ creature }: StatBlockProps) {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Legendary Actions */}
|
{/* Legendary Actions */}
|
||||||
{creature.legendaryActions && (
|
{!!creature.legendaryActions && (
|
||||||
<>
|
<>
|
||||||
<SectionDivider />
|
<SectionDivider />
|
||||||
<h3 className="text-base font-bold text-amber-400">
|
<h3 className="font-bold text-base text-stat-heading">
|
||||||
Legendary Actions
|
Legendary Actions
|
||||||
</h3>
|
</h3>
|
||||||
<p className="text-sm italic text-muted-foreground">
|
<p className="text-muted-foreground text-sm italic">
|
||||||
{creature.legendaryActions.preamble}
|
{creature.legendaryActions.preamble}
|
||||||
</p>
|
</p>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
|
|||||||
49
apps/web/src/components/toast.tsx
Normal file
49
apps/web/src/components/toast.tsx
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import { X } from "lucide-react";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
import { createPortal } from "react-dom";
|
||||||
|
import { Button } from "./ui/button.js";
|
||||||
|
|
||||||
|
interface ToastProps {
|
||||||
|
message: string;
|
||||||
|
progress?: number;
|
||||||
|
onDismiss: () => void;
|
||||||
|
autoDismissMs?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Toast({
|
||||||
|
message,
|
||||||
|
progress,
|
||||||
|
onDismiss,
|
||||||
|
autoDismissMs,
|
||||||
|
}: ToastProps) {
|
||||||
|
useEffect(() => {
|
||||||
|
if (autoDismissMs === undefined) return;
|
||||||
|
const timer = setTimeout(onDismiss, autoDismissMs);
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}, [autoDismissMs, onDismiss]);
|
||||||
|
|
||||||
|
return createPortal(
|
||||||
|
<div className="fixed bottom-4 left-4 z-50">
|
||||||
|
<div className="card-glow flex items-center gap-3 rounded-lg border border-border bg-card px-4 py-3">
|
||||||
|
<span className="text-foreground text-sm">{message}</span>
|
||||||
|
{progress !== undefined && (
|
||||||
|
<div className="h-2 w-24 overflow-hidden rounded-full bg-muted">
|
||||||
|
<div
|
||||||
|
className="h-full rounded-full bg-primary transition-all"
|
||||||
|
style={{ width: `${Math.round(progress * 100)}%` }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon-sm"
|
||||||
|
onClick={onDismiss}
|
||||||
|
className="text-muted-foreground"
|
||||||
|
>
|
||||||
|
<X className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>,
|
||||||
|
document.body,
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,14 +1,13 @@
|
|||||||
import type { Encounter } from "@initiative/domain";
|
import type { Encounter } from "@initiative/domain";
|
||||||
import { StepBack, StepForward, Trash2 } from "lucide-react";
|
import { StepBack, StepForward, Trash2 } from "lucide-react";
|
||||||
import { D20Icon } from "./d20-icon";
|
|
||||||
import { Button } from "./ui/button";
|
import { Button } from "./ui/button";
|
||||||
|
import { ConfirmButton } from "./ui/confirm-button";
|
||||||
|
|
||||||
interface TurnNavigationProps {
|
interface TurnNavigationProps {
|
||||||
encounter: Encounter;
|
encounter: Encounter;
|
||||||
onAdvanceTurn: () => void;
|
onAdvanceTurn: () => void;
|
||||||
onRetreatTurn: () => void;
|
onRetreatTurn: () => void;
|
||||||
onClearEncounter: () => void;
|
onClearEncounter: () => void;
|
||||||
onRollAllInitiative: () => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function TurnNavigation({
|
export function TurnNavigation({
|
||||||
@@ -16,18 +15,16 @@ export function TurnNavigation({
|
|||||||
onAdvanceTurn,
|
onAdvanceTurn,
|
||||||
onRetreatTurn,
|
onRetreatTurn,
|
||||||
onClearEncounter,
|
onClearEncounter,
|
||||||
onRollAllInitiative,
|
}: Readonly<TurnNavigationProps>) {
|
||||||
}: TurnNavigationProps) {
|
|
||||||
const hasCombatants = encounter.combatants.length > 0;
|
const hasCombatants = encounter.combatants.length > 0;
|
||||||
const isAtStart = encounter.roundNumber === 1 && encounter.activeIndex === 0;
|
const isAtStart = encounter.roundNumber === 1 && encounter.activeIndex === 0;
|
||||||
const activeCombatant = encounter.combatants[encounter.activeIndex];
|
const activeCombatant = encounter.combatants[encounter.activeIndex];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex items-center justify-between rounded-md border border-border bg-card px-4 py-3">
|
<div className="card-glow flex items-center gap-3 rounded-lg border border-border bg-card px-4 py-3">
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="ghost"
|
||||||
size="icon"
|
size="icon"
|
||||||
className="h-8 w-8 text-foreground border-foreground hover:text-hover-action hover:border-hover-action hover:bg-transparent"
|
|
||||||
onClick={onRetreatTurn}
|
onClick={onRetreatTurn}
|
||||||
disabled={!hasCombatants || isAtStart}
|
disabled={!hasCombatants || isAtStart}
|
||||||
title="Previous turn"
|
title="Previous turn"
|
||||||
@@ -36,46 +33,28 @@ export function TurnNavigation({
|
|||||||
<StepBack className="h-5 w-5" />
|
<StepBack className="h-5 w-5" />
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<div className="text-center text-sm">
|
<div className="flex min-w-0 flex-1 items-center justify-center gap-2 text-sm">
|
||||||
{activeCombatant ? (
|
<span className="shrink-0 rounded-full bg-muted px-2 py-0.5 font-semibold text-foreground text-sm">
|
||||||
<>
|
R{encounter.roundNumber}
|
||||||
<span className="font-medium">Round {encounter.roundNumber}</span>
|
|
||||||
<span className="text-muted-foreground">
|
|
||||||
{" "}
|
|
||||||
— {activeCombatant.name}
|
|
||||||
</span>
|
</span>
|
||||||
</>
|
{activeCombatant ? (
|
||||||
|
<span className="truncate font-medium">{activeCombatant.name}</span>
|
||||||
) : (
|
) : (
|
||||||
<span className="text-muted-foreground">No combatants</span>
|
<span className="text-muted-foreground">No combatants</span>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex flex-shrink-0 items-center gap-3">
|
||||||
<div className="flex items-center gap-0">
|
<ConfirmButton
|
||||||
<Button
|
icon={<Trash2 className="h-5 w-5" />}
|
||||||
variant="ghost"
|
label="Clear encounter"
|
||||||
size="icon"
|
onConfirm={onClearEncounter}
|
||||||
className="h-8 w-8 text-muted-foreground hover:text-hover-action"
|
|
||||||
onClick={onRollAllInitiative}
|
|
||||||
title="Roll all initiative"
|
|
||||||
aria-label="Roll all initiative"
|
|
||||||
>
|
|
||||||
<D20Icon className="h-6 w-6" />
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
variant="ghost"
|
|
||||||
size="icon"
|
|
||||||
className="h-8 w-8 text-muted-foreground hover:text-hover-destructive"
|
|
||||||
onClick={onClearEncounter}
|
|
||||||
disabled={!hasCombatants}
|
disabled={!hasCombatants}
|
||||||
>
|
className="text-muted-foreground"
|
||||||
<Trash2 className="h-5 w-5" />
|
/>
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="ghost"
|
||||||
size="icon"
|
size="icon"
|
||||||
className="h-8 w-8 text-foreground border-foreground hover:text-hover-action hover:border-hover-action hover:bg-transparent"
|
|
||||||
onClick={onAdvanceTurn}
|
onClick={onAdvanceTurn}
|
||||||
disabled={!hasCombatants}
|
disabled={!hasCombatants}
|
||||||
title="Next turn"
|
title="Next turn"
|
||||||
|
|||||||
@@ -9,13 +9,14 @@ const buttonVariants = cva(
|
|||||||
variant: {
|
variant: {
|
||||||
default: "bg-primary text-primary-foreground hover:bg-primary/90",
|
default: "bg-primary text-primary-foreground hover:bg-primary/90",
|
||||||
outline:
|
outline:
|
||||||
"border border-border bg-transparent hover:bg-hover-neutral-bg hover:text-hover-neutral",
|
"border border-border bg-background/50 text-foreground hover:bg-hover-neutral-bg hover:text-hover-neutral",
|
||||||
ghost: "hover:bg-hover-neutral-bg hover:text-hover-neutral",
|
ghost:
|
||||||
|
"text-foreground hover:bg-hover-neutral-bg hover:text-hover-neutral",
|
||||||
},
|
},
|
||||||
size: {
|
size: {
|
||||||
default: "h-9 px-4 py-2",
|
default: "h-8 px-3 text-xs",
|
||||||
sm: "h-8 px-3 text-xs",
|
|
||||||
icon: "h-8 w-8",
|
icon: "h-8 w-8",
|
||||||
|
"icon-sm": "h-6 w-6",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
defaultVariants: {
|
defaultVariants: {
|
||||||
|
|||||||
118
apps/web/src/components/ui/confirm-button.tsx
Normal file
118
apps/web/src/components/ui/confirm-button.tsx
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
import { Check } from "lucide-react";
|
||||||
|
import {
|
||||||
|
type ReactElement,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from "react";
|
||||||
|
import { cn } from "../../lib/utils";
|
||||||
|
import { Button } from "./button";
|
||||||
|
|
||||||
|
interface ConfirmButtonProps {
|
||||||
|
readonly onConfirm: () => void;
|
||||||
|
readonly icon: ReactElement;
|
||||||
|
readonly label: string;
|
||||||
|
readonly size?: "icon" | "icon-sm";
|
||||||
|
readonly className?: string;
|
||||||
|
readonly disabled?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const REVERT_TIMEOUT_MS = 5_000;
|
||||||
|
|
||||||
|
export function ConfirmButton({
|
||||||
|
onConfirm,
|
||||||
|
icon,
|
||||||
|
label,
|
||||||
|
size = "icon",
|
||||||
|
className,
|
||||||
|
disabled,
|
||||||
|
}: ConfirmButtonProps) {
|
||||||
|
const [isConfirming, setIsConfirming] = useState(false);
|
||||||
|
const timerRef = useRef<ReturnType<typeof setTimeout>>(undefined);
|
||||||
|
const wrapperRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
const revert = useCallback(() => {
|
||||||
|
setIsConfirming(false);
|
||||||
|
clearTimeout(timerRef.current);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Cleanup timer on unmount
|
||||||
|
useEffect(() => {
|
||||||
|
return () => clearTimeout(timerRef.current);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Click-outside listener when confirming
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isConfirming) return;
|
||||||
|
|
||||||
|
function handleMouseDown(e: MouseEvent) {
|
||||||
|
if (
|
||||||
|
wrapperRef.current &&
|
||||||
|
!wrapperRef.current.contains(e.target as Node)
|
||||||
|
) {
|
||||||
|
revert();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleEscapeKey(e: KeyboardEvent) {
|
||||||
|
if (e.key === "Escape") {
|
||||||
|
revert();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener("mousedown", handleMouseDown);
|
||||||
|
document.addEventListener("keydown", handleEscapeKey);
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener("mousedown", handleMouseDown);
|
||||||
|
document.removeEventListener("keydown", handleEscapeKey);
|
||||||
|
};
|
||||||
|
}, [isConfirming, revert]);
|
||||||
|
|
||||||
|
const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
|
||||||
|
if (e.key === "Enter" || e.key === " ") {
|
||||||
|
e.stopPropagation();
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleClick = useCallback(
|
||||||
|
(e: React.MouseEvent) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
if (disabled) return;
|
||||||
|
|
||||||
|
if (isConfirming) {
|
||||||
|
revert();
|
||||||
|
onConfirm();
|
||||||
|
} else {
|
||||||
|
setIsConfirming(true);
|
||||||
|
clearTimeout(timerRef.current);
|
||||||
|
timerRef.current = setTimeout(revert, REVERT_TIMEOUT_MS);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[isConfirming, disabled, onConfirm, revert],
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div ref={wrapperRef} className="inline-flex">
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size={size}
|
||||||
|
className={cn(
|
||||||
|
className,
|
||||||
|
isConfirming
|
||||||
|
? "animate-confirm-pulse rounded-md bg-destructive text-primary-foreground hover:bg-destructive hover:text-primary-foreground"
|
||||||
|
: "hover:text-hover-destructive",
|
||||||
|
)}
|
||||||
|
onClick={handleClick}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
|
onBlur={revert}
|
||||||
|
disabled={disabled}
|
||||||
|
aria-label={isConfirming ? `Confirm ${label.toLowerCase()}` : label}
|
||||||
|
title={isConfirming ? `Confirm ${label.toLowerCase()}` : label}
|
||||||
|
>
|
||||||
|
{isConfirming ? <Check size={16} /> : null}
|
||||||
|
{!isConfirming && icon}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,19 +1,21 @@
|
|||||||
import { forwardRef, type InputHTMLAttributes } from "react";
|
import type { InputHTMLAttributes, RefObject } from "react";
|
||||||
import { cn } from "../../lib/utils";
|
import { cn } from "../../lib/utils";
|
||||||
|
|
||||||
type InputProps = InputHTMLAttributes<HTMLInputElement>;
|
type InputProps = InputHTMLAttributes<HTMLInputElement>;
|
||||||
|
|
||||||
export const Input = forwardRef<HTMLInputElement, InputProps>(
|
export const Input = ({
|
||||||
({ className, ...props }, ref) => {
|
className,
|
||||||
|
ref,
|
||||||
|
...props
|
||||||
|
}: InputProps & { ref?: RefObject<HTMLInputElement | null> }) => {
|
||||||
return (
|
return (
|
||||||
<input
|
<input
|
||||||
ref={ref}
|
ref={ref}
|
||||||
className={cn(
|
className={cn(
|
||||||
"flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm text-foreground shadow-sm transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-primary disabled:cursor-not-allowed disabled:opacity-50",
|
"flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-foreground text-sm shadow-sm transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-primary disabled:cursor-not-allowed disabled:opacity-50",
|
||||||
className,
|
className,
|
||||||
)}
|
)}
|
||||||
{...props}
|
{...props}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
},
|
};
|
||||||
);
|
|
||||||
|
|||||||
73
apps/web/src/components/ui/overflow-menu.tsx
Normal file
73
apps/web/src/components/ui/overflow-menu.tsx
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import { EllipsisVertical } from "lucide-react";
|
||||||
|
import { type ReactNode, useEffect, useRef, useState } from "react";
|
||||||
|
import { Button } from "./button";
|
||||||
|
|
||||||
|
export interface OverflowMenuItem {
|
||||||
|
readonly icon: ReactNode;
|
||||||
|
readonly label: string;
|
||||||
|
readonly onClick: () => void;
|
||||||
|
readonly disabled?: boolean;
|
||||||
|
readonly keepOpen?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OverflowMenuProps {
|
||||||
|
readonly items: readonly OverflowMenuItem[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OverflowMenu({ items }: OverflowMenuProps) {
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open) return;
|
||||||
|
function handleMouseDown(e: MouseEvent) {
|
||||||
|
if (ref.current && !ref.current.contains(e.target as Node)) {
|
||||||
|
setOpen(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function handleKeyDown(e: KeyboardEvent) {
|
||||||
|
if (e.key === "Escape") setOpen(false);
|
||||||
|
}
|
||||||
|
document.addEventListener("mousedown", handleMouseDown);
|
||||||
|
document.addEventListener("keydown", handleKeyDown);
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener("mousedown", handleMouseDown);
|
||||||
|
document.removeEventListener("keydown", handleKeyDown);
|
||||||
|
};
|
||||||
|
}, [open]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div ref={ref} className="relative">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
className="text-muted-foreground hover:text-hover-neutral"
|
||||||
|
onClick={() => setOpen((o) => !o)}
|
||||||
|
aria-label="More actions"
|
||||||
|
title="More actions"
|
||||||
|
>
|
||||||
|
<EllipsisVertical className="h-5 w-5" />
|
||||||
|
</Button>
|
||||||
|
{!!open && (
|
||||||
|
<div className="card-glow absolute right-0 bottom-full z-50 mb-1 min-w-48 rounded-lg border border-border bg-card py-1">
|
||||||
|
{items.map((item) => (
|
||||||
|
<button
|
||||||
|
key={item.label}
|
||||||
|
type="button"
|
||||||
|
className="flex w-full items-center gap-2 px-3 py-1.5 text-left text-foreground text-sm hover:bg-hover-neutral-bg disabled:pointer-events-none disabled:opacity-50"
|
||||||
|
disabled={item.disabled}
|
||||||
|
onClick={() => {
|
||||||
|
item.onClick();
|
||||||
|
if (!item.keepOpen) setOpen(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{item.icon}
|
||||||
|
{item.label}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
225
apps/web/src/hooks/__tests__/use-encounter.test.ts
Normal file
225
apps/web/src/hooks/__tests__/use-encounter.test.ts
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import type { BestiaryIndexEntry, PlayerCharacter } from "@initiative/domain";
|
||||||
|
import { combatantId, creatureId, playerCharacterId } from "@initiative/domain";
|
||||||
|
import { act, renderHook } from "@testing-library/react";
|
||||||
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { useEncounter } from "../use-encounter.js";
|
||||||
|
|
||||||
|
vi.mock("../../persistence/encounter-storage.js", () => ({
|
||||||
|
loadEncounter: vi.fn().mockReturnValue(null),
|
||||||
|
saveEncounter: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
const { loadEncounter: mockLoad, saveEncounter: mockSave } =
|
||||||
|
await vi.importMock<typeof import("../../persistence/encounter-storage.js")>(
|
||||||
|
"../../persistence/encounter-storage.js",
|
||||||
|
);
|
||||||
|
|
||||||
|
describe("useEncounter", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
mockLoad.mockReturnValue(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("initializes with empty encounter when persistence returns null", () => {
|
||||||
|
const { result } = renderHook(() => useEncounter());
|
||||||
|
|
||||||
|
expect(result.current.encounter.combatants).toEqual([]);
|
||||||
|
expect(result.current.encounter.activeIndex).toBe(0);
|
||||||
|
expect(result.current.encounter.roundNumber).toBe(1);
|
||||||
|
expect(result.current.isEmpty).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("initializes from stored encounter", () => {
|
||||||
|
const stored = {
|
||||||
|
combatants: [{ id: combatantId("c-1"), name: "Goblin" }],
|
||||||
|
activeIndex: 0,
|
||||||
|
roundNumber: 2,
|
||||||
|
};
|
||||||
|
mockLoad.mockReturnValue(stored);
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useEncounter());
|
||||||
|
|
||||||
|
expect(result.current.encounter.combatants).toHaveLength(1);
|
||||||
|
expect(result.current.encounter.roundNumber).toBe(2);
|
||||||
|
expect(result.current.isEmpty).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("addCombatant adds a combatant with incremental IDs and persists", () => {
|
||||||
|
const { result } = renderHook(() => useEncounter());
|
||||||
|
|
||||||
|
act(() => result.current.addCombatant("Goblin"));
|
||||||
|
act(() => result.current.addCombatant("Orc"));
|
||||||
|
|
||||||
|
expect(result.current.encounter.combatants).toHaveLength(2);
|
||||||
|
expect(result.current.encounter.combatants[0].name).toBe("Goblin");
|
||||||
|
expect(result.current.encounter.combatants[1].name).toBe("Orc");
|
||||||
|
expect(result.current.isEmpty).toBe(false);
|
||||||
|
expect(mockSave).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removeCombatant removes a combatant and persists", () => {
|
||||||
|
const { result } = renderHook(() => useEncounter());
|
||||||
|
|
||||||
|
act(() => result.current.addCombatant("Goblin"));
|
||||||
|
const id = result.current.encounter.combatants[0].id;
|
||||||
|
|
||||||
|
act(() => result.current.removeCombatant(id));
|
||||||
|
|
||||||
|
expect(result.current.encounter.combatants).toHaveLength(0);
|
||||||
|
expect(result.current.isEmpty).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("advanceTurn and retreatTurn update encounter state", () => {
|
||||||
|
const { result } = renderHook(() => useEncounter());
|
||||||
|
|
||||||
|
act(() => result.current.addCombatant("Goblin"));
|
||||||
|
act(() => result.current.addCombatant("Orc"));
|
||||||
|
|
||||||
|
const initialActive = result.current.encounter.activeIndex;
|
||||||
|
|
||||||
|
act(() => result.current.advanceTurn());
|
||||||
|
expect(result.current.encounter.activeIndex).not.toBe(initialActive);
|
||||||
|
|
||||||
|
act(() => result.current.retreatTurn());
|
||||||
|
expect(result.current.encounter.activeIndex).toBe(initialActive);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clearEncounter resets to empty and resets ID counter", () => {
|
||||||
|
const { result } = renderHook(() => useEncounter());
|
||||||
|
|
||||||
|
act(() => result.current.addCombatant("Goblin"));
|
||||||
|
act(() => result.current.clearEncounter());
|
||||||
|
|
||||||
|
expect(result.current.encounter.combatants).toHaveLength(0);
|
||||||
|
expect(result.current.isEmpty).toBe(true);
|
||||||
|
|
||||||
|
// After clear, IDs restart from c-1
|
||||||
|
act(() => result.current.addCombatant("Orc"));
|
||||||
|
expect(result.current.encounter.combatants[0].id).toBe("c-1");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("addCombatant with opts applies initiative, ac, maxHp", () => {
|
||||||
|
const { result } = renderHook(() => useEncounter());
|
||||||
|
|
||||||
|
act(() =>
|
||||||
|
result.current.addCombatant("Goblin", {
|
||||||
|
initiative: 15,
|
||||||
|
ac: 13,
|
||||||
|
maxHp: 7,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const goblin = result.current.encounter.combatants[0];
|
||||||
|
expect(goblin.initiative).toBe(15);
|
||||||
|
expect(goblin.ac).toBe(13);
|
||||||
|
expect(goblin.maxHp).toBe(7);
|
||||||
|
expect(goblin.currentHp).toBe(7);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("derived flags: hasCreatureCombatants and canRollAllInitiative", () => {
|
||||||
|
const { result } = renderHook(() => useEncounter());
|
||||||
|
|
||||||
|
// No creatures yet
|
||||||
|
expect(result.current.hasCreatureCombatants).toBe(false);
|
||||||
|
expect(result.current.canRollAllInitiative).toBe(false);
|
||||||
|
|
||||||
|
// Add from bestiary to get a creature combatant
|
||||||
|
const entry: BestiaryIndexEntry = {
|
||||||
|
name: "Goblin",
|
||||||
|
source: "MM",
|
||||||
|
ac: 15,
|
||||||
|
hp: 7,
|
||||||
|
dex: 14,
|
||||||
|
cr: "1/4",
|
||||||
|
initiativeProficiency: 0,
|
||||||
|
size: "Small",
|
||||||
|
type: "humanoid",
|
||||||
|
};
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.addFromBestiary(entry);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.current.hasCreatureCombatants).toBe(true);
|
||||||
|
expect(result.current.canRollAllInitiative).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("addFromBestiary adds combatant with HP, AC, creatureId", () => {
|
||||||
|
const { result } = renderHook(() => useEncounter());
|
||||||
|
|
||||||
|
const entry: BestiaryIndexEntry = {
|
||||||
|
name: "Goblin",
|
||||||
|
source: "MM",
|
||||||
|
ac: 15,
|
||||||
|
hp: 7,
|
||||||
|
dex: 14,
|
||||||
|
cr: "1/4",
|
||||||
|
initiativeProficiency: 0,
|
||||||
|
size: "Small",
|
||||||
|
type: "humanoid",
|
||||||
|
};
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.addFromBestiary(entry);
|
||||||
|
});
|
||||||
|
|
||||||
|
const combatant = result.current.encounter.combatants[0];
|
||||||
|
expect(combatant.name).toBe("Goblin");
|
||||||
|
expect(combatant.maxHp).toBe(7);
|
||||||
|
expect(combatant.currentHp).toBe(7);
|
||||||
|
expect(combatant.ac).toBe(15);
|
||||||
|
expect(combatant.creatureId).toBe(creatureId("mm:goblin"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("addFromBestiary auto-numbers duplicate names", () => {
|
||||||
|
const { result } = renderHook(() => useEncounter());
|
||||||
|
|
||||||
|
const entry: BestiaryIndexEntry = {
|
||||||
|
name: "Goblin",
|
||||||
|
source: "MM",
|
||||||
|
ac: 15,
|
||||||
|
hp: 7,
|
||||||
|
dex: 14,
|
||||||
|
cr: "1/4",
|
||||||
|
initiativeProficiency: 0,
|
||||||
|
size: "Small",
|
||||||
|
type: "humanoid",
|
||||||
|
};
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.addFromBestiary(entry);
|
||||||
|
});
|
||||||
|
act(() => {
|
||||||
|
result.current.addFromBestiary(entry);
|
||||||
|
});
|
||||||
|
|
||||||
|
const names = result.current.encounter.combatants.map((c) => c.name);
|
||||||
|
expect(names).toContain("Goblin 1");
|
||||||
|
expect(names).toContain("Goblin 2");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("addFromPlayerCharacter adds combatant with HP, AC, color, icon", () => {
|
||||||
|
const { result } = renderHook(() => useEncounter());
|
||||||
|
|
||||||
|
const pc: PlayerCharacter = {
|
||||||
|
id: playerCharacterId("pc-1"),
|
||||||
|
name: "Aria",
|
||||||
|
ac: 16,
|
||||||
|
maxHp: 30,
|
||||||
|
color: "blue",
|
||||||
|
icon: "sword",
|
||||||
|
};
|
||||||
|
|
||||||
|
act(() => result.current.addFromPlayerCharacter(pc));
|
||||||
|
|
||||||
|
const combatant = result.current.encounter.combatants[0];
|
||||||
|
expect(combatant.name).toBe("Aria");
|
||||||
|
expect(combatant.maxHp).toBe(30);
|
||||||
|
expect(combatant.currentHp).toBe(30);
|
||||||
|
expect(combatant.ac).toBe(16);
|
||||||
|
expect(combatant.color).toBe("blue");
|
||||||
|
expect(combatant.icon).toBe("sword");
|
||||||
|
expect(combatant.playerCharacterId).toBe(playerCharacterId("pc-1"));
|
||||||
|
});
|
||||||
|
});
|
||||||
100
apps/web/src/hooks/__tests__/use-player-characters.test.ts
Normal file
100
apps/web/src/hooks/__tests__/use-player-characters.test.ts
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import { playerCharacterId } from "@initiative/domain";
|
||||||
|
import { act, renderHook } from "@testing-library/react";
|
||||||
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { usePlayerCharacters } from "../use-player-characters.js";
|
||||||
|
|
||||||
|
vi.mock("../../persistence/player-character-storage.js", () => ({
|
||||||
|
loadPlayerCharacters: vi.fn().mockReturnValue([]),
|
||||||
|
savePlayerCharacters: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
const { loadPlayerCharacters: mockLoad, savePlayerCharacters: mockSave } =
|
||||||
|
await vi.importMock<
|
||||||
|
typeof import("../../persistence/player-character-storage.js")
|
||||||
|
>("../../persistence/player-character-storage.js");
|
||||||
|
|
||||||
|
describe("usePlayerCharacters", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
mockLoad.mockReturnValue([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("initializes with characters from persistence", () => {
|
||||||
|
const stored = [
|
||||||
|
{
|
||||||
|
id: playerCharacterId("pc-1"),
|
||||||
|
name: "Aria",
|
||||||
|
ac: 16,
|
||||||
|
maxHp: 30,
|
||||||
|
color: undefined,
|
||||||
|
icon: undefined,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
mockLoad.mockReturnValue(stored);
|
||||||
|
|
||||||
|
const { result } = renderHook(() => usePlayerCharacters());
|
||||||
|
|
||||||
|
expect(result.current.characters).toEqual(stored);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("createCharacter adds a character and persists", () => {
|
||||||
|
const { result } = renderHook(() => usePlayerCharacters());
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.createCharacter("Vex", 15, 28, undefined, undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.current.characters).toHaveLength(1);
|
||||||
|
expect(result.current.characters[0].name).toBe("Vex");
|
||||||
|
expect(result.current.characters[0].ac).toBe(15);
|
||||||
|
expect(result.current.characters[0].maxHp).toBe(28);
|
||||||
|
expect(mockSave).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("createCharacter returns domain error for empty name", () => {
|
||||||
|
const { result } = renderHook(() => usePlayerCharacters());
|
||||||
|
|
||||||
|
let error: unknown;
|
||||||
|
act(() => {
|
||||||
|
error = result.current.createCharacter("", 15, 28, undefined, undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(error).toMatchObject({ kind: "domain-error" });
|
||||||
|
expect(result.current.characters).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("editCharacter updates character and persists", () => {
|
||||||
|
const { result } = renderHook(() => usePlayerCharacters());
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.createCharacter("Vex", 15, 28, undefined, undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
const id = result.current.characters[0].id;
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.editCharacter(id, { name: "Vex'ahlia" });
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.current.characters[0].name).toBe("Vex'ahlia");
|
||||||
|
expect(mockSave).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("deleteCharacter removes character and persists", () => {
|
||||||
|
const { result } = renderHook(() => usePlayerCharacters());
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.createCharacter("Vex", 15, 28, undefined, undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
const id = result.current.characters[0].id;
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.deleteCharacter(id);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.current.characters).toHaveLength(0);
|
||||||
|
expect(mockSave).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
159
apps/web/src/hooks/__tests__/use-side-panel-state.test.ts
Normal file
159
apps/web/src/hooks/__tests__/use-side-panel-state.test.ts
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import { creatureId } from "@initiative/domain";
|
||||||
|
import { act, renderHook } from "@testing-library/react";
|
||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import { useSidePanelState } from "../use-side-panel-state.js";
|
||||||
|
|
||||||
|
function mockMatchMedia(matches: boolean) {
|
||||||
|
const listeners: Array<(e: MediaQueryListEvent) => void> = [];
|
||||||
|
const mql = {
|
||||||
|
matches,
|
||||||
|
addEventListener: vi.fn(
|
||||||
|
(_event: string, handler: (e: MediaQueryListEvent) => void) => {
|
||||||
|
listeners.push(handler);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
removeEventListener: vi.fn(),
|
||||||
|
};
|
||||||
|
globalThis.matchMedia = vi.fn().mockReturnValue(mql) as typeof matchMedia;
|
||||||
|
return { mql, listeners };
|
||||||
|
}
|
||||||
|
|
||||||
|
const CREATURE_A = creatureId("creature-a");
|
||||||
|
|
||||||
|
describe("useSidePanelState", () => {
|
||||||
|
it("starts with closed panel, no selection, not collapsed", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
expect(result.current.panelView).toEqual({ mode: "closed" });
|
||||||
|
expect(result.current.selectedCreatureId).toBeNull();
|
||||||
|
expect(result.current.isRightPanelCollapsed).toBe(false);
|
||||||
|
expect(result.current.bulkImportMode).toBe(false);
|
||||||
|
expect(result.current.sourceManagerMode).toBe(false);
|
||||||
|
expect(result.current.pinnedCreatureId).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("showCreature sets creature mode and selectedCreatureId", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
act(() => result.current.showCreature(CREATURE_A));
|
||||||
|
|
||||||
|
expect(result.current.panelView).toEqual({
|
||||||
|
mode: "creature",
|
||||||
|
creatureId: CREATURE_A,
|
||||||
|
});
|
||||||
|
expect(result.current.selectedCreatureId).toBe(CREATURE_A);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("showBulkImport sets bulk-import mode, selectedCreatureId null", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
act(() => result.current.showBulkImport());
|
||||||
|
|
||||||
|
expect(result.current.panelView).toEqual({ mode: "bulk-import" });
|
||||||
|
expect(result.current.selectedCreatureId).toBeNull();
|
||||||
|
expect(result.current.bulkImportMode).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("showSourceManager sets source-manager mode", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
act(() => result.current.showSourceManager());
|
||||||
|
|
||||||
|
expect(result.current.panelView).toEqual({ mode: "source-manager" });
|
||||||
|
expect(result.current.sourceManagerMode).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("dismissPanel sets mode to closed", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
act(() => result.current.showCreature(CREATURE_A));
|
||||||
|
act(() => result.current.dismissPanel());
|
||||||
|
|
||||||
|
expect(result.current.panelView).toEqual({ mode: "closed" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("toggleCollapse flips isRightPanelCollapsed", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
expect(result.current.isRightPanelCollapsed).toBe(false);
|
||||||
|
|
||||||
|
act(() => result.current.toggleCollapse());
|
||||||
|
expect(result.current.isRightPanelCollapsed).toBe(true);
|
||||||
|
|
||||||
|
act(() => result.current.toggleCollapse());
|
||||||
|
expect(result.current.isRightPanelCollapsed).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("showCreature resets collapse state", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
act(() => result.current.toggleCollapse());
|
||||||
|
expect(result.current.isRightPanelCollapsed).toBe(true);
|
||||||
|
|
||||||
|
act(() => result.current.showCreature(CREATURE_A));
|
||||||
|
expect(result.current.isRightPanelCollapsed).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("togglePin pins the selected creature", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
act(() => result.current.showCreature(CREATURE_A));
|
||||||
|
act(() => result.current.togglePin());
|
||||||
|
|
||||||
|
expect(result.current.pinnedCreatureId).toBe(CREATURE_A);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("togglePin unpins when already pinned to same creature", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
act(() => result.current.showCreature(CREATURE_A));
|
||||||
|
act(() => result.current.togglePin());
|
||||||
|
act(() => result.current.togglePin());
|
||||||
|
|
||||||
|
expect(result.current.pinnedCreatureId).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("togglePin does nothing when no creature is selected", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
act(() => result.current.togglePin());
|
||||||
|
|
||||||
|
expect(result.current.pinnedCreatureId).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("unpin clears pinned creature", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
act(() => result.current.showCreature(CREATURE_A));
|
||||||
|
act(() => result.current.togglePin());
|
||||||
|
act(() => result.current.unpin());
|
||||||
|
|
||||||
|
expect(result.current.pinnedCreatureId).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("isWideDesktop reflects matchMedia result", () => {
|
||||||
|
mockMatchMedia(true);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
expect(result.current.isWideDesktop).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("isWideDesktop is false on narrow viewport", () => {
|
||||||
|
mockMatchMedia(false);
|
||||||
|
const { result } = renderHook(() => useSidePanelState());
|
||||||
|
|
||||||
|
expect(result.current.isWideDesktop).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,62 +1,134 @@
|
|||||||
import type { Creature, CreatureId } from "@initiative/domain";
|
import type {
|
||||||
import { useEffect, useMemo, useRef, useState } from "react";
|
BestiaryIndexEntry,
|
||||||
import { normalizeBestiary } from "../adapters/bestiary-adapter.js";
|
Creature,
|
||||||
|
CreatureId,
|
||||||
|
} from "@initiative/domain";
|
||||||
|
import { useCallback, useEffect, useState } from "react";
|
||||||
|
import {
|
||||||
|
normalizeBestiary,
|
||||||
|
setSourceDisplayNames,
|
||||||
|
} from "../adapters/bestiary-adapter.js";
|
||||||
|
import * as bestiaryCache from "../adapters/bestiary-cache.js";
|
||||||
|
import {
|
||||||
|
getSourceDisplayName,
|
||||||
|
loadBestiaryIndex,
|
||||||
|
} from "../adapters/bestiary-index-adapter.js";
|
||||||
|
|
||||||
|
export interface SearchResult extends BestiaryIndexEntry {
|
||||||
|
readonly sourceDisplayName: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface BestiaryHook {
|
interface BestiaryHook {
|
||||||
search: (query: string) => Creature[];
|
search: (query: string) => SearchResult[];
|
||||||
getCreature: (id: CreatureId) => Creature | undefined;
|
getCreature: (id: CreatureId) => Creature | undefined;
|
||||||
allCreatures: Creature[];
|
|
||||||
isLoaded: boolean;
|
isLoaded: boolean;
|
||||||
|
isSourceCached: (sourceCode: string) => Promise<boolean>;
|
||||||
|
fetchAndCacheSource: (sourceCode: string, url: string) => Promise<void>;
|
||||||
|
uploadAndCacheSource: (
|
||||||
|
sourceCode: string,
|
||||||
|
jsonData: unknown,
|
||||||
|
) => Promise<void>;
|
||||||
|
refreshCache: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useBestiary(): BestiaryHook {
|
export function useBestiary(): BestiaryHook {
|
||||||
const [creatures, setCreatures] = useState<Creature[]>([]);
|
|
||||||
const [isLoaded, setIsLoaded] = useState(false);
|
const [isLoaded, setIsLoaded] = useState(false);
|
||||||
const creatureMapRef = useRef<Map<string, Creature>>(new Map());
|
const [creatureMap, setCreatureMap] = useState(
|
||||||
const loadAttempted = useRef(false);
|
() => new Map<CreatureId, Creature>(),
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (loadAttempted.current) return;
|
const index = loadBestiaryIndex();
|
||||||
loadAttempted.current = true;
|
setSourceDisplayNames(index.sources as Record<string, string>);
|
||||||
|
if (index.creatures.length > 0) {
|
||||||
import("../../../../data/bestiary/xmm.json")
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: raw JSON shape varies per entry
|
|
||||||
.then((mod: any) => {
|
|
||||||
const raw = mod.default ?? mod;
|
|
||||||
try {
|
|
||||||
const normalized = normalizeBestiary(raw);
|
|
||||||
const map = new Map<string, Creature>();
|
|
||||||
for (const c of normalized) {
|
|
||||||
map.set(c.id, c);
|
|
||||||
}
|
|
||||||
creatureMapRef.current = map;
|
|
||||||
setCreatures(normalized);
|
|
||||||
setIsLoaded(true);
|
setIsLoaded(true);
|
||||||
} catch {
|
|
||||||
// Normalization failed — bestiary unavailable
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.catch(() => {
|
void bestiaryCache.loadAllCachedCreatures().then((map) => {
|
||||||
// Import failed — bestiary unavailable
|
setCreatureMap(map);
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const search = useMemo(() => {
|
const search = useCallback((query: string): SearchResult[] => {
|
||||||
return (query: string): Creature[] => {
|
|
||||||
if (query.length < 2) return [];
|
if (query.length < 2) return [];
|
||||||
const lower = query.toLowerCase();
|
const lower = query.toLowerCase();
|
||||||
return creatures
|
const index = loadBestiaryIndex();
|
||||||
|
return index.creatures
|
||||||
.filter((c) => c.name.toLowerCase().includes(lower))
|
.filter((c) => c.name.toLowerCase().includes(lower))
|
||||||
.sort((a, b) => a.name.localeCompare(b.name))
|
.sort((a, b) => a.name.localeCompare(b.name))
|
||||||
.slice(0, 10);
|
.slice(0, 10)
|
||||||
};
|
.map((c) => ({
|
||||||
}, [creatures]);
|
...c,
|
||||||
|
sourceDisplayName: getSourceDisplayName(c.source),
|
||||||
const getCreature = useMemo(() => {
|
}));
|
||||||
return (id: CreatureId): Creature | undefined => {
|
|
||||||
return creatureMapRef.current.get(id);
|
|
||||||
};
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return { search, getCreature, allCreatures: creatures, isLoaded };
|
const getCreature = useCallback(
|
||||||
|
(id: CreatureId): Creature | undefined => {
|
||||||
|
return creatureMap.get(id);
|
||||||
|
},
|
||||||
|
[creatureMap],
|
||||||
|
);
|
||||||
|
|
||||||
|
const isSourceCachedFn = useCallback(
|
||||||
|
(sourceCode: string): Promise<boolean> => {
|
||||||
|
return bestiaryCache.isSourceCached(sourceCode);
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
const fetchAndCacheSource = useCallback(
|
||||||
|
async (sourceCode: string, url: string): Promise<void> => {
|
||||||
|
const response = await fetch(url);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed to fetch: ${response.status} ${response.statusText}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const json = await response.json();
|
||||||
|
const creatures = normalizeBestiary(json);
|
||||||
|
const displayName = getSourceDisplayName(sourceCode);
|
||||||
|
await bestiaryCache.cacheSource(sourceCode, displayName, creatures);
|
||||||
|
setCreatureMap((prev) => {
|
||||||
|
const next = new Map(prev);
|
||||||
|
for (const c of creatures) {
|
||||||
|
next.set(c.id, c);
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
const uploadAndCacheSource = useCallback(
|
||||||
|
async (sourceCode: string, jsonData: unknown): Promise<void> => {
|
||||||
|
// biome-ignore lint/suspicious/noExplicitAny: raw JSON shape varies
|
||||||
|
const creatures = normalizeBestiary(jsonData as any);
|
||||||
|
const displayName = getSourceDisplayName(sourceCode);
|
||||||
|
await bestiaryCache.cacheSource(sourceCode, displayName, creatures);
|
||||||
|
setCreatureMap((prev) => {
|
||||||
|
const next = new Map(prev);
|
||||||
|
for (const c of creatures) {
|
||||||
|
next.set(c.id, c);
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
const refreshCache = useCallback(async (): Promise<void> => {
|
||||||
|
const map = await bestiaryCache.loadAllCachedCreatures();
|
||||||
|
setCreatureMap(map);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return {
|
||||||
|
search,
|
||||||
|
getCreature,
|
||||||
|
isLoaded,
|
||||||
|
isSourceCached: isSourceCachedFn,
|
||||||
|
fetchAndCacheSource,
|
||||||
|
uploadAndCacheSource,
|
||||||
|
refreshCache,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
128
apps/web/src/hooks/use-bulk-import.ts
Normal file
128
apps/web/src/hooks/use-bulk-import.ts
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
import { useCallback, useRef, useState } from "react";
|
||||||
|
import {
|
||||||
|
getAllSourceCodes,
|
||||||
|
getDefaultFetchUrl,
|
||||||
|
} from "../adapters/bestiary-index-adapter.js";
|
||||||
|
|
||||||
|
const BATCH_SIZE = 6;
|
||||||
|
|
||||||
|
export interface BulkImportState {
|
||||||
|
readonly status: "idle" | "loading" | "complete" | "partial-failure";
|
||||||
|
readonly total: number;
|
||||||
|
readonly completed: number;
|
||||||
|
readonly failed: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const IDLE_STATE: BulkImportState = {
|
||||||
|
status: "idle",
|
||||||
|
total: 0,
|
||||||
|
completed: 0,
|
||||||
|
failed: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
interface BulkImportHook {
|
||||||
|
state: BulkImportState;
|
||||||
|
startImport: (
|
||||||
|
baseUrl: string,
|
||||||
|
fetchAndCacheSource: (sourceCode: string, url: string) => Promise<void>,
|
||||||
|
isSourceCached: (sourceCode: string) => Promise<boolean>,
|
||||||
|
refreshCache: () => Promise<void>,
|
||||||
|
) => void;
|
||||||
|
reset: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useBulkImport(): BulkImportHook {
|
||||||
|
const [state, setState] = useState<BulkImportState>(IDLE_STATE);
|
||||||
|
const countersRef = useRef({ completed: 0, failed: 0 });
|
||||||
|
|
||||||
|
const startImport = useCallback(
|
||||||
|
(
|
||||||
|
baseUrl: string,
|
||||||
|
fetchAndCacheSource: (sourceCode: string, url: string) => Promise<void>,
|
||||||
|
isSourceCached: (sourceCode: string) => Promise<boolean>,
|
||||||
|
refreshCache: () => Promise<void>,
|
||||||
|
) => {
|
||||||
|
const allCodes = getAllSourceCodes();
|
||||||
|
const total = allCodes.length;
|
||||||
|
|
||||||
|
countersRef.current = { completed: 0, failed: 0 };
|
||||||
|
setState({ status: "loading", total, completed: 0, failed: 0 });
|
||||||
|
|
||||||
|
void (async () => {
|
||||||
|
const cacheChecks = await Promise.all(
|
||||||
|
allCodes.map(async (code) => ({
|
||||||
|
code,
|
||||||
|
cached: await isSourceCached(code),
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
|
const alreadyCached = cacheChecks.filter((c) => c.cached).length;
|
||||||
|
const uncached = cacheChecks.filter((c) => !c.cached);
|
||||||
|
|
||||||
|
countersRef.current.completed = alreadyCached;
|
||||||
|
|
||||||
|
if (uncached.length === 0) {
|
||||||
|
setState({
|
||||||
|
status: "complete",
|
||||||
|
total,
|
||||||
|
completed: total,
|
||||||
|
failed: 0,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setState((s) => ({ ...s, completed: alreadyCached }));
|
||||||
|
|
||||||
|
const batches: { code: string }[][] = [];
|
||||||
|
for (let i = 0; i < uncached.length; i += BATCH_SIZE) {
|
||||||
|
batches.push(uncached.slice(i, i + BATCH_SIZE));
|
||||||
|
}
|
||||||
|
|
||||||
|
await batches.reduce(
|
||||||
|
(chain, batch) =>
|
||||||
|
chain.then(() =>
|
||||||
|
Promise.allSettled(
|
||||||
|
batch.map(async ({ code }) => {
|
||||||
|
const url = getDefaultFetchUrl(code, baseUrl);
|
||||||
|
try {
|
||||||
|
await fetchAndCacheSource(code, url);
|
||||||
|
countersRef.current.completed++;
|
||||||
|
} catch (err) {
|
||||||
|
countersRef.current.failed++;
|
||||||
|
console.warn(
|
||||||
|
`[bulk-import] FAILED ${code} (${url}):`,
|
||||||
|
err instanceof Error ? err.message : err,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
setState({
|
||||||
|
status: "loading",
|
||||||
|
total,
|
||||||
|
completed: countersRef.current.completed,
|
||||||
|
failed: countersRef.current.failed,
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Promise.resolve() as Promise<unknown>,
|
||||||
|
);
|
||||||
|
|
||||||
|
await refreshCache();
|
||||||
|
|
||||||
|
const { completed, failed } = countersRef.current;
|
||||||
|
setState({
|
||||||
|
status: failed > 0 ? "partial-failure" : "complete",
|
||||||
|
total,
|
||||||
|
completed,
|
||||||
|
failed,
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
const reset = useCallback(() => {
|
||||||
|
setState(IDLE_STATE);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { state, startImport, reset };
|
||||||
|
}
|
||||||
@@ -14,16 +14,18 @@ import {
|
|||||||
toggleConditionUseCase,
|
toggleConditionUseCase,
|
||||||
} from "@initiative/application";
|
} from "@initiative/application";
|
||||||
import type {
|
import type {
|
||||||
|
BestiaryIndexEntry,
|
||||||
CombatantId,
|
CombatantId,
|
||||||
ConditionId,
|
ConditionId,
|
||||||
Creature,
|
CreatureId,
|
||||||
DomainEvent,
|
DomainEvent,
|
||||||
Encounter,
|
Encounter,
|
||||||
|
PlayerCharacter,
|
||||||
} from "@initiative/domain";
|
} from "@initiative/domain";
|
||||||
import {
|
import {
|
||||||
combatantId,
|
combatantId,
|
||||||
createEncounter,
|
|
||||||
isDomainError,
|
isDomainError,
|
||||||
|
creatureId as makeCreatureId,
|
||||||
resolveCreatureName,
|
resolveCreatureName,
|
||||||
} from "@initiative/domain";
|
} from "@initiative/domain";
|
||||||
import { useCallback, useEffect, useRef, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
@@ -32,30 +34,24 @@ import {
|
|||||||
saveEncounter,
|
saveEncounter,
|
||||||
} from "../persistence/encounter-storage.js";
|
} from "../persistence/encounter-storage.js";
|
||||||
|
|
||||||
function createDemoEncounter(): Encounter {
|
const COMBATANT_ID_REGEX = /^c-(\d+)$/;
|
||||||
const result = createEncounter([
|
|
||||||
{ id: combatantId("1"), name: "Aria" },
|
|
||||||
{ id: combatantId("2"), name: "Brak" },
|
|
||||||
{ id: combatantId("3"), name: "Cael" },
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (isDomainError(result)) {
|
const EMPTY_ENCOUNTER: Encounter = {
|
||||||
throw new Error(`Failed to create demo encounter: ${result.message}`);
|
combatants: [],
|
||||||
}
|
activeIndex: 0,
|
||||||
|
roundNumber: 1,
|
||||||
return result;
|
};
|
||||||
}
|
|
||||||
|
|
||||||
function initializeEncounter(): Encounter {
|
function initializeEncounter(): Encounter {
|
||||||
const stored = loadEncounter();
|
const stored = loadEncounter();
|
||||||
if (stored !== null) return stored;
|
if (stored !== null) return stored;
|
||||||
return createDemoEncounter();
|
return EMPTY_ENCOUNTER;
|
||||||
}
|
}
|
||||||
|
|
||||||
function deriveNextId(encounter: Encounter): number {
|
function deriveNextId(encounter: Encounter): number {
|
||||||
let max = 0;
|
let max = 0;
|
||||||
for (const c of encounter.combatants) {
|
for (const c of encounter.combatants) {
|
||||||
const match = /^c-(\d+)$/.exec(c.id);
|
const match = COMBATANT_ID_REGEX.exec(c.id);
|
||||||
if (match) {
|
if (match) {
|
||||||
const n = Number.parseInt(match[1], 10);
|
const n = Number.parseInt(match[1], 10);
|
||||||
if (n > max) max = n;
|
if (n > max) max = n;
|
||||||
@@ -64,6 +60,33 @@ function deriveNextId(encounter: Encounter): number {
|
|||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface CombatantOpts {
|
||||||
|
initiative?: number;
|
||||||
|
ac?: number;
|
||||||
|
maxHp?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyCombatantOpts(
|
||||||
|
makeStore: () => EncounterStore,
|
||||||
|
id: ReturnType<typeof combatantId>,
|
||||||
|
opts: CombatantOpts,
|
||||||
|
): DomainEvent[] {
|
||||||
|
const events: DomainEvent[] = [];
|
||||||
|
if (opts.maxHp !== undefined) {
|
||||||
|
const r = setHpUseCase(makeStore(), id, opts.maxHp);
|
||||||
|
if (!isDomainError(r)) events.push(...r);
|
||||||
|
}
|
||||||
|
if (opts.ac !== undefined) {
|
||||||
|
const r = setAcUseCase(makeStore(), id, opts.ac);
|
||||||
|
if (!isDomainError(r)) events.push(...r);
|
||||||
|
}
|
||||||
|
if (opts.initiative !== undefined) {
|
||||||
|
const r = setInitiativeUseCase(makeStore(), id, opts.initiative);
|
||||||
|
if (!isDomainError(r)) events.push(...r);
|
||||||
|
}
|
||||||
|
return events;
|
||||||
|
}
|
||||||
|
|
||||||
export function useEncounter() {
|
export function useEncounter() {
|
||||||
const [encounter, setEncounter] = useState<Encounter>(initializeEncounter);
|
const [encounter, setEncounter] = useState<Encounter>(initializeEncounter);
|
||||||
const [events, setEvents] = useState<DomainEvent[]>([]);
|
const [events, setEvents] = useState<DomainEvent[]>([]);
|
||||||
@@ -107,7 +130,7 @@ export function useEncounter() {
|
|||||||
const nextId = useRef(deriveNextId(encounter));
|
const nextId = useRef(deriveNextId(encounter));
|
||||||
|
|
||||||
const addCombatant = useCallback(
|
const addCombatant = useCallback(
|
||||||
(name: string) => {
|
(name: string, opts?: CombatantOpts) => {
|
||||||
const id = combatantId(`c-${++nextId.current}`);
|
const id = combatantId(`c-${++nextId.current}`);
|
||||||
const result = addCombatantUseCase(makeStore(), id, name);
|
const result = addCombatantUseCase(makeStore(), id, name);
|
||||||
|
|
||||||
@@ -115,6 +138,13 @@ export function useEncounter() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (opts) {
|
||||||
|
const optEvents = applyCombatantOpts(makeStore, id, opts);
|
||||||
|
if (optEvents.length > 0) {
|
||||||
|
setEvents((prev) => [...prev, ...optEvents]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setEvents((prev) => [...prev, ...result]);
|
setEvents((prev) => [...prev, ...result]);
|
||||||
},
|
},
|
||||||
[makeStore],
|
[makeStore],
|
||||||
@@ -225,10 +255,6 @@ export function useEncounter() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const clearEncounter = useCallback(() => {
|
const clearEncounter = useCallback(() => {
|
||||||
if (!window.confirm("Clear the entire encounter? This cannot be undone.")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = clearEncounterUseCase(makeStore());
|
const result = clearEncounterUseCase(makeStore());
|
||||||
|
|
||||||
if (isDomainError(result)) {
|
if (isDomainError(result)) {
|
||||||
@@ -240,11 +266,11 @@ export function useEncounter() {
|
|||||||
}, [makeStore]);
|
}, [makeStore]);
|
||||||
|
|
||||||
const addFromBestiary = useCallback(
|
const addFromBestiary = useCallback(
|
||||||
(creature: Creature) => {
|
(entry: BestiaryIndexEntry): CreatureId | null => {
|
||||||
const store = makeStore();
|
const store = makeStore();
|
||||||
const existingNames = store.get().combatants.map((c) => c.name);
|
const existingNames = store.get().combatants.map((c) => c.name);
|
||||||
const { newName, renames } = resolveCreatureName(
|
const { newName, renames } = resolveCreatureName(
|
||||||
creature.name,
|
entry.name,
|
||||||
existingNames,
|
existingNames,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -259,40 +285,111 @@ export function useEncounter() {
|
|||||||
// Add combatant with resolved name
|
// Add combatant with resolved name
|
||||||
const id = combatantId(`c-${++nextId.current}`);
|
const id = combatantId(`c-${++nextId.current}`);
|
||||||
const addResult = addCombatantUseCase(makeStore(), id, newName);
|
const addResult = addCombatantUseCase(makeStore(), id, newName);
|
||||||
if (isDomainError(addResult)) return;
|
if (isDomainError(addResult)) return null;
|
||||||
|
|
||||||
// Set HP
|
// Set HP
|
||||||
const hpResult = setHpUseCase(makeStore(), id, creature.hp.average);
|
const hpResult = setHpUseCase(makeStore(), id, entry.hp);
|
||||||
if (!isDomainError(hpResult)) {
|
if (!isDomainError(hpResult)) {
|
||||||
setEvents((prev) => [...prev, ...hpResult]);
|
setEvents((prev) => [...prev, ...hpResult]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set AC
|
// Set AC
|
||||||
if (creature.ac > 0) {
|
if (entry.ac > 0) {
|
||||||
const acResult = setAcUseCase(makeStore(), id, creature.ac);
|
const acResult = setAcUseCase(makeStore(), id, entry.ac);
|
||||||
if (!isDomainError(acResult)) {
|
if (!isDomainError(acResult)) {
|
||||||
setEvents((prev) => [...prev, ...acResult]);
|
setEvents((prev) => [...prev, ...acResult]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set creatureId on the combatant
|
// Derive creatureId from source + name
|
||||||
|
const slug = entry.name
|
||||||
|
.toLowerCase()
|
||||||
|
.replaceAll(/[^a-z0-9]+/g, "-")
|
||||||
|
.replaceAll(/(^-|-$)/g, "");
|
||||||
|
const cId = makeCreatureId(`${entry.source.toLowerCase()}:${slug}`);
|
||||||
|
|
||||||
|
// Set creatureId on the combatant (use store.save to keep ref in sync for batch calls)
|
||||||
const currentEncounter = store.get();
|
const currentEncounter = store.get();
|
||||||
const updated = {
|
store.save({
|
||||||
...currentEncounter,
|
...currentEncounter,
|
||||||
combatants: currentEncounter.combatants.map((c) =>
|
combatants: currentEncounter.combatants.map((c) =>
|
||||||
c.id === id ? { ...c, creatureId: creature.id } : c,
|
c.id === id ? { ...c, creatureId: cId } : c,
|
||||||
),
|
),
|
||||||
};
|
});
|
||||||
setEncounter(updated);
|
|
||||||
|
setEvents((prev) => [...prev, ...addResult]);
|
||||||
|
|
||||||
|
return cId;
|
||||||
|
},
|
||||||
|
[makeStore],
|
||||||
|
);
|
||||||
|
|
||||||
|
const addFromPlayerCharacter = useCallback(
|
||||||
|
(pc: PlayerCharacter) => {
|
||||||
|
const store = makeStore();
|
||||||
|
const existingNames = store.get().combatants.map((c) => c.name);
|
||||||
|
const { newName, renames } = resolveCreatureName(pc.name, existingNames);
|
||||||
|
|
||||||
|
for (const { from, to } of renames) {
|
||||||
|
const target = store.get().combatants.find((c) => c.name === from);
|
||||||
|
if (target) {
|
||||||
|
editCombatantUseCase(makeStore(), target.id, to);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const id = combatantId(`c-${++nextId.current}`);
|
||||||
|
const addResult = addCombatantUseCase(makeStore(), id, newName);
|
||||||
|
if (isDomainError(addResult)) return;
|
||||||
|
|
||||||
|
// Set HP
|
||||||
|
const hpResult = setHpUseCase(makeStore(), id, pc.maxHp);
|
||||||
|
if (!isDomainError(hpResult)) {
|
||||||
|
setEvents((prev) => [...prev, ...hpResult]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set AC
|
||||||
|
if (pc.ac > 0) {
|
||||||
|
const acResult = setAcUseCase(makeStore(), id, pc.ac);
|
||||||
|
if (!isDomainError(acResult)) {
|
||||||
|
setEvents((prev) => [...prev, ...acResult]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set color, icon, and playerCharacterId on the combatant
|
||||||
|
const currentEncounter = store.get();
|
||||||
|
store.save({
|
||||||
|
...currentEncounter,
|
||||||
|
combatants: currentEncounter.combatants.map((c) =>
|
||||||
|
c.id === id
|
||||||
|
? {
|
||||||
|
...c,
|
||||||
|
color: pc.color,
|
||||||
|
icon: pc.icon,
|
||||||
|
playerCharacterId: pc.id,
|
||||||
|
}
|
||||||
|
: c,
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
setEvents((prev) => [...prev, ...addResult]);
|
setEvents((prev) => [...prev, ...addResult]);
|
||||||
},
|
},
|
||||||
[makeStore, editCombatant],
|
[makeStore],
|
||||||
|
);
|
||||||
|
|
||||||
|
const isEmpty = encounter.combatants.length === 0;
|
||||||
|
const hasCreatureCombatants = encounter.combatants.some(
|
||||||
|
(c) => c.creatureId != null,
|
||||||
|
);
|
||||||
|
const canRollAllInitiative = encounter.combatants.some(
|
||||||
|
(c) => c.creatureId != null && c.initiative == null,
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
encounter,
|
encounter,
|
||||||
events,
|
events,
|
||||||
|
isEmpty,
|
||||||
|
hasCreatureCombatants,
|
||||||
|
canRollAllInitiative,
|
||||||
advanceTurn,
|
advanceTurn,
|
||||||
retreatTurn,
|
retreatTurn,
|
||||||
addCombatant,
|
addCombatant,
|
||||||
@@ -306,6 +403,7 @@ export function useEncounter() {
|
|||||||
toggleCondition,
|
toggleCondition,
|
||||||
toggleConcentration,
|
toggleConcentration,
|
||||||
addFromBestiary,
|
addFromBestiary,
|
||||||
|
addFromPlayerCharacter,
|
||||||
makeStore,
|
makeStore,
|
||||||
} as const;
|
} as const;
|
||||||
}
|
}
|
||||||
|
|||||||
108
apps/web/src/hooks/use-player-characters.ts
Normal file
108
apps/web/src/hooks/use-player-characters.ts
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
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 {
|
||||||
|
loadPlayerCharacters,
|
||||||
|
savePlayerCharacters,
|
||||||
|
} from "../persistence/player-character-storage.js";
|
||||||
|
|
||||||
|
function initializeCharacters(): PlayerCharacter[] {
|
||||||
|
return loadPlayerCharacters();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function usePlayerCharacters() {
|
||||||
|
const [characters, setCharacters] =
|
||||||
|
useState<PlayerCharacter[]>(initializeCharacters);
|
||||||
|
const charactersRef = useRef(characters);
|
||||||
|
charactersRef.current = characters;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
savePlayerCharacters(characters);
|
||||||
|
}, [characters]);
|
||||||
|
|
||||||
|
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,
|
||||||
|
) => {
|
||||||
|
const id = generatePcId();
|
||||||
|
const result = createPlayerCharacterUseCase(
|
||||||
|
makeStore(),
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
ac,
|
||||||
|
maxHp,
|
||||||
|
color,
|
||||||
|
icon,
|
||||||
|
);
|
||||||
|
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,
|
||||||
|
makeStore,
|
||||||
|
} as const;
|
||||||
|
}
|
||||||
101
apps/web/src/hooks/use-side-panel-state.ts
Normal file
101
apps/web/src/hooks/use-side-panel-state.ts
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
import type { CreatureId } from "@initiative/domain";
|
||||||
|
import { useCallback, useEffect, useState } from "react";
|
||||||
|
|
||||||
|
type PanelView =
|
||||||
|
| { mode: "closed" }
|
||||||
|
| { mode: "creature"; creatureId: CreatureId }
|
||||||
|
| { mode: "bulk-import" }
|
||||||
|
| { mode: "source-manager" };
|
||||||
|
|
||||||
|
interface SidePanelState {
|
||||||
|
panelView: PanelView;
|
||||||
|
selectedCreatureId: CreatureId | null;
|
||||||
|
bulkImportMode: boolean;
|
||||||
|
sourceManagerMode: boolean;
|
||||||
|
isRightPanelCollapsed: boolean;
|
||||||
|
pinnedCreatureId: CreatureId | null;
|
||||||
|
isWideDesktop: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SidePanelActions {
|
||||||
|
showCreature: (creatureId: CreatureId) => void;
|
||||||
|
showBulkImport: () => void;
|
||||||
|
showSourceManager: () => void;
|
||||||
|
dismissPanel: () => void;
|
||||||
|
toggleCollapse: () => void;
|
||||||
|
togglePin: () => void;
|
||||||
|
unpin: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useSidePanelState(): SidePanelState & SidePanelActions {
|
||||||
|
const [panelView, setPanelView] = useState<PanelView>({ mode: "closed" });
|
||||||
|
const [isRightPanelCollapsed, setIsRightPanelCollapsed] = useState(false);
|
||||||
|
const [pinnedCreatureId, setPinnedCreatureId] = useState<CreatureId | null>(
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
const [isWideDesktop, setIsWideDesktop] = useState(
|
||||||
|
() => globalThis.matchMedia("(min-width: 1280px)").matches,
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const mq = globalThis.matchMedia("(min-width: 1280px)");
|
||||||
|
const handler = (e: MediaQueryListEvent) => setIsWideDesktop(e.matches);
|
||||||
|
mq.addEventListener("change", handler);
|
||||||
|
return () => mq.removeEventListener("change", handler);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const selectedCreatureId =
|
||||||
|
panelView.mode === "creature" ? panelView.creatureId : null;
|
||||||
|
|
||||||
|
const showCreature = useCallback((creatureId: CreatureId) => {
|
||||||
|
setPanelView({ mode: "creature", creatureId });
|
||||||
|
setIsRightPanelCollapsed(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const showBulkImport = useCallback(() => {
|
||||||
|
setPanelView({ mode: "bulk-import" });
|
||||||
|
setIsRightPanelCollapsed(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const showSourceManager = useCallback(() => {
|
||||||
|
setPanelView({ mode: "source-manager" });
|
||||||
|
setIsRightPanelCollapsed(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const dismissPanel = useCallback(() => {
|
||||||
|
setPanelView({ mode: "closed" });
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const toggleCollapse = useCallback(() => {
|
||||||
|
setIsRightPanelCollapsed((f) => !f);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const togglePin = useCallback(() => {
|
||||||
|
if (selectedCreatureId) {
|
||||||
|
setPinnedCreatureId((prev) =>
|
||||||
|
prev === selectedCreatureId ? null : selectedCreatureId,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}, [selectedCreatureId]);
|
||||||
|
|
||||||
|
const unpin = useCallback(() => {
|
||||||
|
setPinnedCreatureId(null);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return {
|
||||||
|
panelView,
|
||||||
|
selectedCreatureId,
|
||||||
|
bulkImportMode: panelView.mode === "bulk-import",
|
||||||
|
sourceManagerMode: panelView.mode === "source-manager",
|
||||||
|
isRightPanelCollapsed,
|
||||||
|
pinnedCreatureId,
|
||||||
|
isWideDesktop,
|
||||||
|
showCreature,
|
||||||
|
showBulkImport,
|
||||||
|
showSourceManager,
|
||||||
|
dismissPanel,
|
||||||
|
toggleCollapse,
|
||||||
|
togglePin,
|
||||||
|
unpin,
|
||||||
|
};
|
||||||
|
}
|
||||||
72
apps/web/src/hooks/use-swipe-to-dismiss.ts
Normal file
72
apps/web/src/hooks/use-swipe-to-dismiss.ts
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
import { useCallback, useRef, useState } from "react";
|
||||||
|
|
||||||
|
const DISMISS_THRESHOLD = 0.35;
|
||||||
|
const VELOCITY_THRESHOLD = 0.5;
|
||||||
|
|
||||||
|
interface SwipeState {
|
||||||
|
offsetX: number;
|
||||||
|
isSwiping: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useSwipeToDismiss(onDismiss: () => void) {
|
||||||
|
const [swipe, setSwipe] = useState<SwipeState>({
|
||||||
|
offsetX: 0,
|
||||||
|
isSwiping: false,
|
||||||
|
});
|
||||||
|
const startX = useRef(0);
|
||||||
|
const startY = useRef(0);
|
||||||
|
const startTime = useRef(0);
|
||||||
|
const panelWidth = useRef(0);
|
||||||
|
const directionLocked = useRef<"horizontal" | "vertical" | null>(null);
|
||||||
|
|
||||||
|
const onTouchStart = useCallback((e: React.TouchEvent) => {
|
||||||
|
const touch = e.touches[0];
|
||||||
|
startX.current = touch.clientX;
|
||||||
|
startY.current = touch.clientY;
|
||||||
|
startTime.current = Date.now();
|
||||||
|
directionLocked.current = null;
|
||||||
|
const el = e.currentTarget as HTMLElement;
|
||||||
|
panelWidth.current = el.getBoundingClientRect().width;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const onTouchMove = useCallback((e: React.TouchEvent) => {
|
||||||
|
const touch = e.touches[0];
|
||||||
|
const dx = touch.clientX - startX.current;
|
||||||
|
const dy = touch.clientY - startY.current;
|
||||||
|
|
||||||
|
if (!directionLocked.current) {
|
||||||
|
if (Math.abs(dx) < 10 && Math.abs(dy) < 10) return;
|
||||||
|
directionLocked.current =
|
||||||
|
Math.abs(dx) > Math.abs(dy) ? "horizontal" : "vertical";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (directionLocked.current === "vertical") return;
|
||||||
|
|
||||||
|
const clampedX = Math.max(0, dx);
|
||||||
|
setSwipe({ offsetX: clampedX, isSwiping: true });
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const onTouchEnd = useCallback(() => {
|
||||||
|
if (directionLocked.current !== "horizontal") {
|
||||||
|
setSwipe({ offsetX: 0, isSwiping: false });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const elapsed = (Date.now() - startTime.current) / 1000;
|
||||||
|
const velocity = swipe.offsetX / elapsed / panelWidth.current;
|
||||||
|
const ratio =
|
||||||
|
panelWidth.current > 0 ? swipe.offsetX / panelWidth.current : 0;
|
||||||
|
|
||||||
|
if (ratio > DISMISS_THRESHOLD || velocity > VELOCITY_THRESHOLD) {
|
||||||
|
onDismiss();
|
||||||
|
}
|
||||||
|
|
||||||
|
setSwipe({ offsetX: 0, isSwiping: false });
|
||||||
|
}, [swipe.offsetX, onDismiss]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
offsetX: swipe.offsetX,
|
||||||
|
isSwiping: swipe.isSwiping,
|
||||||
|
handlers: { onTouchStart, onTouchMove, onTouchEnd },
|
||||||
|
};
|
||||||
|
}
|
||||||
98
apps/web/src/hooks/use-theme.ts
Normal file
98
apps/web/src/hooks/use-theme.ts
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
import { useCallback, useEffect, useSyncExternalStore } from "react";
|
||||||
|
|
||||||
|
type ThemePreference = "system" | "light" | "dark";
|
||||||
|
type ResolvedTheme = "light" | "dark";
|
||||||
|
|
||||||
|
const STORAGE_KEY = "initiative:theme";
|
||||||
|
|
||||||
|
const listeners = new Set<() => void>();
|
||||||
|
let currentPreference: ThemePreference = loadPreference();
|
||||||
|
|
||||||
|
function loadPreference(): ThemePreference {
|
||||||
|
try {
|
||||||
|
const raw = localStorage.getItem(STORAGE_KEY);
|
||||||
|
if (raw === "light" || raw === "dark" || raw === "system") return raw;
|
||||||
|
} catch {
|
||||||
|
// storage unavailable
|
||||||
|
}
|
||||||
|
return "system";
|
||||||
|
}
|
||||||
|
|
||||||
|
function savePreference(pref: ThemePreference): void {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(STORAGE_KEY, pref);
|
||||||
|
} catch {
|
||||||
|
// quota exceeded or storage unavailable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSystemTheme(): ResolvedTheme {
|
||||||
|
if (typeof globalThis.matchMedia !== "function") return "dark";
|
||||||
|
return globalThis.matchMedia("(prefers-color-scheme: light)").matches
|
||||||
|
? "light"
|
||||||
|
: "dark";
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolve(pref: ThemePreference): ResolvedTheme {
|
||||||
|
return pref === "system" ? getSystemTheme() : pref;
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyTheme(resolved: ResolvedTheme): void {
|
||||||
|
document.documentElement.dataset.theme = resolved;
|
||||||
|
}
|
||||||
|
|
||||||
|
function notifyAll(): void {
|
||||||
|
for (const listener of listeners) {
|
||||||
|
listener();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply on load
|
||||||
|
applyTheme(resolve(currentPreference));
|
||||||
|
|
||||||
|
// Listen for OS preference changes
|
||||||
|
if (typeof globalThis.matchMedia === "function") {
|
||||||
|
globalThis
|
||||||
|
.matchMedia("(prefers-color-scheme: light)")
|
||||||
|
.addEventListener("change", () => {
|
||||||
|
if (currentPreference === "system") {
|
||||||
|
applyTheme(resolve("system"));
|
||||||
|
notifyAll();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function subscribe(callback: () => void): () => void {
|
||||||
|
listeners.add(callback);
|
||||||
|
return () => listeners.delete(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSnapshot(): ThemePreference {
|
||||||
|
return currentPreference;
|
||||||
|
}
|
||||||
|
|
||||||
|
const CYCLE: ThemePreference[] = ["system", "light", "dark"];
|
||||||
|
|
||||||
|
export function useTheme() {
|
||||||
|
const preference = useSyncExternalStore(subscribe, getSnapshot);
|
||||||
|
const resolved = resolve(preference);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
applyTheme(resolved);
|
||||||
|
}, [resolved]);
|
||||||
|
|
||||||
|
const setPreference = useCallback((pref: ThemePreference) => {
|
||||||
|
currentPreference = pref;
|
||||||
|
savePreference(pref);
|
||||||
|
applyTheme(resolve(pref));
|
||||||
|
notifyAll();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const cycleTheme = useCallback(() => {
|
||||||
|
const idx = CYCLE.indexOf(currentPreference);
|
||||||
|
const next = CYCLE[(idx + 1) % CYCLE.length];
|
||||||
|
setPreference(next);
|
||||||
|
}, [setPreference]);
|
||||||
|
|
||||||
|
return { preference, resolved, setPreference, cycleTheme } as const;
|
||||||
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
@import "tailwindcss";
|
@import "tailwindcss";
|
||||||
|
|
||||||
@theme {
|
@theme {
|
||||||
--color-background: #0f172a;
|
--color-background: #0e1a2e;
|
||||||
--color-foreground: #e2e8f0;
|
--color-foreground: #e2e8f0;
|
||||||
--color-muted: #64748b;
|
--color-muted: #7a8ba4;
|
||||||
--color-muted-foreground: #94a3b8;
|
--color-muted-foreground: #94a3b8;
|
||||||
--color-card: #1e293b;
|
--color-card: #1a2e4a;
|
||||||
--color-card-foreground: #e2e8f0;
|
--color-card-foreground: #e2e8f0;
|
||||||
--color-border: #334155;
|
--color-border: #2a5088;
|
||||||
--color-input: #334155;
|
--color-input: #2a5088;
|
||||||
--color-primary: #3b82f6;
|
--color-primary: #3b82f6;
|
||||||
--color-primary-foreground: #ffffff;
|
--color-primary-foreground: #ffffff;
|
||||||
--color-accent: #3b82f6;
|
--color-accent: #3b82f6;
|
||||||
@@ -16,15 +16,50 @@
|
|||||||
--color-hover-neutral: var(--color-primary);
|
--color-hover-neutral: var(--color-primary);
|
||||||
--color-hover-action: var(--color-primary);
|
--color-hover-action: var(--color-primary);
|
||||||
--color-hover-destructive: var(--color-destructive);
|
--color-hover-destructive: var(--color-destructive);
|
||||||
--color-hover-neutral-bg: var(--color-card);
|
--color-hover-neutral-bg: oklch(0.623 0.214 259 / 0.15);
|
||||||
--color-hover-action-bg: var(--color-muted);
|
--color-hover-action-bg: var(--color-muted);
|
||||||
--color-hover-destructive-bg: transparent;
|
--color-hover-destructive-bg: transparent;
|
||||||
|
--color-stat-heading: #fbbf24;
|
||||||
|
--color-stat-divider-from: oklch(0.5 0.1 65 / 0.6);
|
||||||
|
--color-stat-divider-via: oklch(0.5 0.1 65 / 0.4);
|
||||||
|
--color-hp-damage-hover-bg: oklch(0.25 0.05 25);
|
||||||
|
--color-hp-heal-hover-bg: oklch(0.25 0.05 155);
|
||||||
|
--color-active-row-bg: oklch(0.623 0.214 259 / 0.1);
|
||||||
|
--color-active-row-border: oklch(0.623 0.214 259 / 0.4);
|
||||||
--radius-sm: 0.25rem;
|
--radius-sm: 0.25rem;
|
||||||
--radius-md: 0.375rem;
|
--radius-md: 0.5rem;
|
||||||
--radius-lg: 0.5rem;
|
--radius-lg: 0.75rem;
|
||||||
--font-sans: "Inter", ui-sans-serif, system-ui, sans-serif;
|
--font-sans: "Inter", ui-sans-serif, system-ui, sans-serif;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[data-theme="light"] {
|
||||||
|
--color-background: #eeecea;
|
||||||
|
--color-foreground: #374151;
|
||||||
|
--color-muted: #e0ddd9;
|
||||||
|
--color-muted-foreground: #6b7280;
|
||||||
|
--color-card: #f7f6f4;
|
||||||
|
--color-card-foreground: #374151;
|
||||||
|
--color-border: #ddd9d5;
|
||||||
|
--color-input: #cdc8c3;
|
||||||
|
--color-primary: #2563eb;
|
||||||
|
--color-primary-foreground: #ffffff;
|
||||||
|
--color-accent: #2563eb;
|
||||||
|
--color-destructive: #dc2626;
|
||||||
|
--color-hover-neutral: var(--color-primary);
|
||||||
|
--color-hover-action: var(--color-primary);
|
||||||
|
--color-hover-destructive: var(--color-destructive);
|
||||||
|
--color-hover-neutral-bg: oklch(0.623 0.214 259 / 0.08);
|
||||||
|
--color-hover-action-bg: var(--color-muted);
|
||||||
|
--color-hover-destructive-bg: transparent;
|
||||||
|
--color-stat-heading: #92400e;
|
||||||
|
--color-stat-divider-from: oklch(0.55 0.1 65 / 0.5);
|
||||||
|
--color-stat-divider-via: oklch(0.55 0.1 65 / 0.25);
|
||||||
|
--color-hp-damage-hover-bg: #fef2f2;
|
||||||
|
--color-hp-heal-hover-bg: #ecfdf5;
|
||||||
|
--color-active-row-bg: oklch(0.623 0.214 259 / 0.08);
|
||||||
|
--color-active-row-border: oklch(0.623 0.214 259 / 0.25);
|
||||||
|
}
|
||||||
|
|
||||||
@keyframes concentration-shake {
|
@keyframes concentration-shake {
|
||||||
0% {
|
0% {
|
||||||
translate: 0;
|
translate: 0;
|
||||||
@@ -68,12 +103,139 @@
|
|||||||
animation: slide-in-right 200ms ease-out;
|
animation: slide-in-right 200ms ease-out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@keyframes confirm-pulse {
|
||||||
|
0% {
|
||||||
|
scale: 1;
|
||||||
|
}
|
||||||
|
50% {
|
||||||
|
scale: 1.15;
|
||||||
|
}
|
||||||
|
100% {
|
||||||
|
scale: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes settle-to-bottom {
|
||||||
|
from {
|
||||||
|
transform: translateY(-40vh);
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
40% {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
transform: translateY(0);
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@utility animate-settle-to-bottom {
|
||||||
|
animation: settle-to-bottom 700ms cubic-bezier(0.22, 1, 0.36, 1) backwards;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes rise-to-center {
|
||||||
|
from {
|
||||||
|
transform: translateY(40vh);
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
40% {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
transform: translateY(0);
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@utility animate-rise-to-center {
|
||||||
|
animation: rise-to-center 700ms cubic-bezier(0.22, 1, 0.36, 1) backwards;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes slide-down-in {
|
||||||
|
from {
|
||||||
|
transform: translateY(-100%);
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
transform: translateY(0);
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@utility animate-slide-down-in {
|
||||||
|
animation: slide-down-in 700ms cubic-bezier(0.22, 1, 0.36, 1) backwards;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes slide-up-out {
|
||||||
|
from {
|
||||||
|
transform: translateY(0);
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
60% {
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
transform: translateY(-100%);
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@utility animate-slide-up-out {
|
||||||
|
animation: slide-up-out 700ms cubic-bezier(0.22, 1, 0.36, 1) forwards;
|
||||||
|
}
|
||||||
|
|
||||||
|
@custom-variant pointer-coarse (@media (pointer: coarse));
|
||||||
|
|
||||||
|
@utility animate-confirm-pulse {
|
||||||
|
animation: confirm-pulse 300ms ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
@utility transition-slide-panel {
|
||||||
|
transition: translate 200ms ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
@utility writing-vertical-rl {
|
||||||
|
writing-mode: vertical-rl;
|
||||||
|
}
|
||||||
|
|
||||||
@utility animate-concentration-pulse {
|
@utility animate-concentration-pulse {
|
||||||
animation:
|
animation:
|
||||||
concentration-shake 450ms ease-out,
|
concentration-shake 450ms ease-out,
|
||||||
concentration-glow 1200ms ease-out;
|
concentration-glow 1200ms ease-out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@utility card-glow {
|
||||||
|
background-image: radial-gradient(
|
||||||
|
ellipse at 50% 50%,
|
||||||
|
oklch(0.35 0.05 250 / 0.5) 0%,
|
||||||
|
transparent 70%
|
||||||
|
);
|
||||||
|
box-shadow:
|
||||||
|
0 0 15px -2px oklch(0.623 0.214 259 / 0.2),
|
||||||
|
inset 0 1px 0 0 oklch(0.7 0.15 259 / 0.1);
|
||||||
|
|
||||||
|
[data-theme="light"] & {
|
||||||
|
background-image: none;
|
||||||
|
box-shadow: 0 1px 3px 0 oklch(0 0 0 / 0.08);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@utility panel-glow {
|
||||||
|
background-image: linear-gradient(
|
||||||
|
to bottom,
|
||||||
|
oklch(0.35 0.05 250 / 0.4) 0%,
|
||||||
|
transparent 40%
|
||||||
|
);
|
||||||
|
box-shadow:
|
||||||
|
0 0 20px -2px oklch(0.623 0.214 259 / 0.15),
|
||||||
|
inset 0 1px 0 0 oklch(0.7 0.15 259 / 0.1);
|
||||||
|
|
||||||
|
[data-theme="light"] & {
|
||||||
|
background-image: none;
|
||||||
|
box-shadow: -1px 0 6px 0 oklch(0 0 0 / 0.1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
* {
|
* {
|
||||||
scrollbar-color: var(--color-border) transparent;
|
scrollbar-color: var(--color-border) transparent;
|
||||||
scrollbar-width: thin;
|
scrollbar-width: thin;
|
||||||
@@ -81,6 +243,16 @@
|
|||||||
|
|
||||||
body {
|
body {
|
||||||
background-color: var(--color-background);
|
background-color: var(--color-background);
|
||||||
|
background-image: radial-gradient(
|
||||||
|
ellipse at 50% 40%,
|
||||||
|
oklch(0.26 0.055 250) 0%,
|
||||||
|
var(--color-background) 70%
|
||||||
|
);
|
||||||
|
background-attachment: fixed;
|
||||||
color: var(--color-foreground);
|
color: var(--color-foreground);
|
||||||
font-family: var(--font-sans);
|
font-family: var(--font-sans);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[data-theme="light"] body {
|
||||||
|
background-image: none;
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,231 @@
|
|||||||
|
import type { PlayerCharacter } from "@initiative/domain";
|
||||||
|
import { playerCharacterId } from "@initiative/domain";
|
||||||
|
import { beforeEach, describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
loadPlayerCharacters,
|
||||||
|
savePlayerCharacters,
|
||||||
|
} from "../player-character-storage.js";
|
||||||
|
|
||||||
|
const STORAGE_KEY = "initiative:player-characters";
|
||||||
|
|
||||||
|
function createMockLocalStorage() {
|
||||||
|
const store = new Map<string, string>();
|
||||||
|
return {
|
||||||
|
getItem: (key: string) => store.get(key) ?? null,
|
||||||
|
setItem: (key: string, value: string) => store.set(key, value),
|
||||||
|
removeItem: (key: string) => store.delete(key),
|
||||||
|
clear: () => store.clear(),
|
||||||
|
get length() {
|
||||||
|
return store.size;
|
||||||
|
},
|
||||||
|
key: (_index: number) => null,
|
||||||
|
store,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function makePC(overrides?: Partial<PlayerCharacter>): PlayerCharacter {
|
||||||
|
return {
|
||||||
|
id: playerCharacterId("pc-1"),
|
||||||
|
name: "Aragorn",
|
||||||
|
ac: 16,
|
||||||
|
maxHp: 120,
|
||||||
|
color: "green",
|
||||||
|
icon: "sword",
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("player-character-storage", () => {
|
||||||
|
let mockStorage: ReturnType<typeof createMockLocalStorage>;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
mockStorage = createMockLocalStorage();
|
||||||
|
Object.defineProperty(globalThis, "localStorage", {
|
||||||
|
value: mockStorage,
|
||||||
|
writable: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("round-trip save/load", () => {
|
||||||
|
it("saves and loads a single character", () => {
|
||||||
|
const pc = makePC();
|
||||||
|
savePlayerCharacters([pc]);
|
||||||
|
const loaded = loadPlayerCharacters();
|
||||||
|
expect(loaded).toEqual([pc]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("saves and loads multiple characters", () => {
|
||||||
|
const pcs = [
|
||||||
|
makePC({ id: playerCharacterId("pc-1"), name: "Aragorn" }),
|
||||||
|
makePC({
|
||||||
|
id: playerCharacterId("pc-2"),
|
||||||
|
name: "Legolas",
|
||||||
|
ac: 14,
|
||||||
|
maxHp: 90,
|
||||||
|
color: "blue",
|
||||||
|
icon: "eye",
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
savePlayerCharacters(pcs);
|
||||||
|
const loaded = loadPlayerCharacters();
|
||||||
|
expect(loaded).toEqual(pcs);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("empty storage", () => {
|
||||||
|
it("returns empty array when no data exists", () => {
|
||||||
|
expect(loadPlayerCharacters()).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("corrupt JSON", () => {
|
||||||
|
it("returns empty array for invalid JSON", () => {
|
||||||
|
mockStorage.setItem(STORAGE_KEY, "not-json{{{");
|
||||||
|
expect(loadPlayerCharacters()).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns empty array for non-array JSON", () => {
|
||||||
|
mockStorage.setItem(STORAGE_KEY, '{"foo": "bar"}');
|
||||||
|
expect(loadPlayerCharacters()).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("per-character validation", () => {
|
||||||
|
it("discards character with missing name", () => {
|
||||||
|
mockStorage.setItem(
|
||||||
|
STORAGE_KEY,
|
||||||
|
JSON.stringify([
|
||||||
|
{ id: "pc-1", ac: 10, maxHp: 50, color: "blue", icon: "sword" },
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loadPlayerCharacters()).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("discards character with empty name", () => {
|
||||||
|
mockStorage.setItem(
|
||||||
|
STORAGE_KEY,
|
||||||
|
JSON.stringify([
|
||||||
|
{
|
||||||
|
id: "pc-1",
|
||||||
|
name: "",
|
||||||
|
ac: 10,
|
||||||
|
maxHp: 50,
|
||||||
|
color: "blue",
|
||||||
|
icon: "sword",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loadPlayerCharacters()).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("discards character with invalid color", () => {
|
||||||
|
mockStorage.setItem(
|
||||||
|
STORAGE_KEY,
|
||||||
|
JSON.stringify([
|
||||||
|
{
|
||||||
|
id: "pc-1",
|
||||||
|
name: "Test",
|
||||||
|
ac: 10,
|
||||||
|
maxHp: 50,
|
||||||
|
color: "neon",
|
||||||
|
icon: "sword",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loadPlayerCharacters()).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("discards character with invalid icon", () => {
|
||||||
|
mockStorage.setItem(
|
||||||
|
STORAGE_KEY,
|
||||||
|
JSON.stringify([
|
||||||
|
{
|
||||||
|
id: "pc-1",
|
||||||
|
name: "Test",
|
||||||
|
ac: 10,
|
||||||
|
maxHp: 50,
|
||||||
|
color: "blue",
|
||||||
|
icon: "banana",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loadPlayerCharacters()).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("discards character with negative AC", () => {
|
||||||
|
mockStorage.setItem(
|
||||||
|
STORAGE_KEY,
|
||||||
|
JSON.stringify([
|
||||||
|
{
|
||||||
|
id: "pc-1",
|
||||||
|
name: "Test",
|
||||||
|
ac: -1,
|
||||||
|
maxHp: 50,
|
||||||
|
color: "blue",
|
||||||
|
icon: "sword",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loadPlayerCharacters()).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("discards character with maxHp of 0", () => {
|
||||||
|
mockStorage.setItem(
|
||||||
|
STORAGE_KEY,
|
||||||
|
JSON.stringify([
|
||||||
|
{
|
||||||
|
id: "pc-1",
|
||||||
|
name: "Test",
|
||||||
|
ac: 10,
|
||||||
|
maxHp: 0,
|
||||||
|
color: "blue",
|
||||||
|
icon: "sword",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loadPlayerCharacters()).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps valid characters and discards invalid ones", () => {
|
||||||
|
mockStorage.setItem(
|
||||||
|
STORAGE_KEY,
|
||||||
|
JSON.stringify([
|
||||||
|
{
|
||||||
|
id: "pc-1",
|
||||||
|
name: "Valid",
|
||||||
|
ac: 10,
|
||||||
|
maxHp: 50,
|
||||||
|
color: "blue",
|
||||||
|
icon: "sword",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "pc-2",
|
||||||
|
name: "",
|
||||||
|
ac: 10,
|
||||||
|
maxHp: 50,
|
||||||
|
color: "blue",
|
||||||
|
icon: "sword",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
const loaded = loadPlayerCharacters();
|
||||||
|
expect(loaded).toHaveLength(1);
|
||||||
|
expect(loaded[0].name).toBe("Valid");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("storage errors", () => {
|
||||||
|
it("save silently catches errors", () => {
|
||||||
|
Object.defineProperty(globalThis, "localStorage", {
|
||||||
|
value: {
|
||||||
|
setItem: () => {
|
||||||
|
throw new Error("QuotaExceeded");
|
||||||
|
},
|
||||||
|
getItem: () => null,
|
||||||
|
},
|
||||||
|
writable: true,
|
||||||
|
});
|
||||||
|
expect(() => savePlayerCharacters([makePC()])).not.toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -5,7 +5,10 @@ import {
|
|||||||
creatureId,
|
creatureId,
|
||||||
type Encounter,
|
type Encounter,
|
||||||
isDomainError,
|
isDomainError,
|
||||||
|
playerCharacterId,
|
||||||
VALID_CONDITION_IDS,
|
VALID_CONDITION_IDS,
|
||||||
|
VALID_PLAYER_COLORS,
|
||||||
|
VALID_PLAYER_ICONS,
|
||||||
} from "@initiative/domain";
|
} from "@initiative/domain";
|
||||||
|
|
||||||
const STORAGE_KEY = "initiative:encounter";
|
const STORAGE_KEY = "initiative:encounter";
|
||||||
@@ -18,6 +21,93 @@ export function saveEncounter(encounter: Encounter): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function validateAc(value: unknown): number | undefined {
|
||||||
|
return typeof value === "number" && Number.isInteger(value) && value >= 0
|
||||||
|
? value
|
||||||
|
: undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateConditions(value: unknown): ConditionId[] | undefined {
|
||||||
|
if (!Array.isArray(value)) return undefined;
|
||||||
|
const valid = value.filter(
|
||||||
|
(v): v is ConditionId =>
|
||||||
|
typeof v === "string" && VALID_CONDITION_IDS.has(v),
|
||||||
|
);
|
||||||
|
return valid.length > 0 ? valid : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateCreatureId(value: unknown) {
|
||||||
|
return typeof value === "string" && value.length > 0
|
||||||
|
? creatureId(value)
|
||||||
|
: undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateHp(
|
||||||
|
rawMaxHp: unknown,
|
||||||
|
rawCurrentHp: unknown,
|
||||||
|
): { maxHp: number; currentHp: number } | undefined {
|
||||||
|
if (
|
||||||
|
typeof rawMaxHp !== "number" ||
|
||||||
|
!Number.isInteger(rawMaxHp) ||
|
||||||
|
rawMaxHp < 1
|
||||||
|
) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const validCurrentHp =
|
||||||
|
typeof rawCurrentHp === "number" &&
|
||||||
|
Number.isInteger(rawCurrentHp) &&
|
||||||
|
rawCurrentHp >= 0 &&
|
||||||
|
rawCurrentHp <= rawMaxHp;
|
||||||
|
return {
|
||||||
|
maxHp: rawMaxHp,
|
||||||
|
currentHp: validCurrentHp ? rawCurrentHp : rawMaxHp,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function rehydrateCombatant(c: unknown) {
|
||||||
|
const entry = c as Record<string, unknown>;
|
||||||
|
const base = {
|
||||||
|
id: combatantId(entry.id as string),
|
||||||
|
name: entry.name as string,
|
||||||
|
initiative:
|
||||||
|
typeof entry.initiative === "number" ? entry.initiative : undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
const color =
|
||||||
|
typeof entry.color === "string" && VALID_PLAYER_COLORS.has(entry.color)
|
||||||
|
? entry.color
|
||||||
|
: undefined;
|
||||||
|
const icon =
|
||||||
|
typeof entry.icon === "string" && VALID_PLAYER_ICONS.has(entry.icon)
|
||||||
|
? entry.icon
|
||||||
|
: undefined;
|
||||||
|
const pcId =
|
||||||
|
typeof entry.playerCharacterId === "string" &&
|
||||||
|
entry.playerCharacterId.length > 0
|
||||||
|
? playerCharacterId(entry.playerCharacterId)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
const shared = {
|
||||||
|
...base,
|
||||||
|
ac: validateAc(entry.ac),
|
||||||
|
conditions: validateConditions(entry.conditions),
|
||||||
|
isConcentrating: entry.isConcentrating === true ? true : undefined,
|
||||||
|
creatureId: validateCreatureId(entry.creatureId),
|
||||||
|
color,
|
||||||
|
icon,
|
||||||
|
playerCharacterId: pcId,
|
||||||
|
};
|
||||||
|
|
||||||
|
const hp = validateHp(entry.maxHp, entry.currentHp);
|
||||||
|
return hp ? { ...shared, ...hp } : shared;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValidCombatantEntry(c: unknown): boolean {
|
||||||
|
if (typeof c !== "object" || c === null || Array.isArray(c)) return false;
|
||||||
|
const entry = c as Record<string, unknown>;
|
||||||
|
return typeof entry.id === "string" && typeof entry.name === "string";
|
||||||
|
}
|
||||||
|
|
||||||
export function loadEncounter(): Encounter | null {
|
export function loadEncounter(): Encounter | null {
|
||||||
try {
|
try {
|
||||||
const raw = localStorage.getItem(STORAGE_KEY);
|
const raw = localStorage.getItem(STORAGE_KEY);
|
||||||
@@ -45,82 +135,9 @@ export function loadEncounter(): Encounter | null {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const c of combatants) {
|
if (!combatants.every(isValidCombatantEntry)) return null;
|
||||||
if (typeof c !== "object" || c === null || Array.isArray(c)) return null;
|
|
||||||
const entry = c as Record<string, unknown>;
|
|
||||||
if (typeof entry.id !== "string") return null;
|
|
||||||
if (typeof entry.name !== "string") return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const rehydrated = combatants.map((c) => {
|
const rehydrated = combatants.map(rehydrateCombatant);
|
||||||
const entry = c as Record<string, unknown>;
|
|
||||||
const base = {
|
|
||||||
id: combatantId(entry.id as string),
|
|
||||||
name: entry.name as string,
|
|
||||||
initiative:
|
|
||||||
typeof entry.initiative === "number" ? entry.initiative : undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Validate AC field
|
|
||||||
const ac = entry.ac;
|
|
||||||
const validAc =
|
|
||||||
typeof ac === "number" && Number.isInteger(ac) && ac >= 0
|
|
||||||
? ac
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
// Validate conditions field
|
|
||||||
const rawConditions = entry.conditions;
|
|
||||||
const validConditions: ConditionId[] | undefined = Array.isArray(
|
|
||||||
rawConditions,
|
|
||||||
)
|
|
||||||
? (rawConditions.filter(
|
|
||||||
(v): v is ConditionId =>
|
|
||||||
typeof v === "string" && VALID_CONDITION_IDS.has(v),
|
|
||||||
) as ConditionId[])
|
|
||||||
: undefined;
|
|
||||||
const conditions =
|
|
||||||
validConditions && validConditions.length > 0
|
|
||||||
? validConditions
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
// Validate isConcentrating field
|
|
||||||
const isConcentrating = entry.isConcentrating === true ? true : undefined;
|
|
||||||
|
|
||||||
// Validate creatureId field
|
|
||||||
const rawCreatureId = entry.creatureId;
|
|
||||||
const validCreatureId =
|
|
||||||
typeof rawCreatureId === "string" && rawCreatureId.length > 0
|
|
||||||
? creatureId(rawCreatureId)
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
// Validate and attach HP fields if valid
|
|
||||||
const maxHp = entry.maxHp;
|
|
||||||
const currentHp = entry.currentHp;
|
|
||||||
if (typeof maxHp === "number" && Number.isInteger(maxHp) && maxHp >= 1) {
|
|
||||||
const validCurrentHp =
|
|
||||||
typeof currentHp === "number" &&
|
|
||||||
Number.isInteger(currentHp) &&
|
|
||||||
currentHp >= 0 &&
|
|
||||||
currentHp <= maxHp;
|
|
||||||
return {
|
|
||||||
...base,
|
|
||||||
ac: validAc,
|
|
||||||
conditions,
|
|
||||||
isConcentrating,
|
|
||||||
creatureId: validCreatureId,
|
|
||||||
maxHp,
|
|
||||||
currentHp: validCurrentHp ? currentHp : maxHp,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...base,
|
|
||||||
ac: validAc,
|
|
||||||
conditions,
|
|
||||||
isConcentrating,
|
|
||||||
creatureId: validCreatureId,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = createEncounter(
|
const result = createEncounter(
|
||||||
rehydrated,
|
rehydrated,
|
||||||
|
|||||||
77
apps/web/src/persistence/player-character-storage.ts
Normal file
77
apps/web/src/persistence/player-character-storage.ts
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
import type { PlayerCharacter } from "@initiative/domain";
|
||||||
|
import {
|
||||||
|
playerCharacterId,
|
||||||
|
VALID_PLAYER_COLORS,
|
||||||
|
VALID_PLAYER_ICONS,
|
||||||
|
} from "@initiative/domain";
|
||||||
|
|
||||||
|
const STORAGE_KEY = "initiative:player-characters";
|
||||||
|
|
||||||
|
export function savePlayerCharacters(characters: PlayerCharacter[]): void {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(characters));
|
||||||
|
} catch {
|
||||||
|
// Silently swallow errors (quota exceeded, storage unavailable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValidOptionalMember(
|
||||||
|
value: unknown,
|
||||||
|
valid: ReadonlySet<string>,
|
||||||
|
): boolean {
|
||||||
|
return value === undefined || (typeof value === "string" && valid.has(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
function rehydrateCharacter(raw: unknown): PlayerCharacter | null {
|
||||||
|
if (typeof raw !== "object" || raw === null || Array.isArray(raw))
|
||||||
|
return null;
|
||||||
|
const entry = raw as Record<string, unknown>;
|
||||||
|
|
||||||
|
if (typeof entry.id !== "string" || entry.id.length === 0) return null;
|
||||||
|
if (typeof entry.name !== "string" || entry.name.trim().length === 0)
|
||||||
|
return null;
|
||||||
|
if (
|
||||||
|
typeof entry.ac !== "number" ||
|
||||||
|
!Number.isInteger(entry.ac) ||
|
||||||
|
entry.ac < 0
|
||||||
|
)
|
||||||
|
return null;
|
||||||
|
if (
|
||||||
|
typeof entry.maxHp !== "number" ||
|
||||||
|
!Number.isInteger(entry.maxHp) ||
|
||||||
|
entry.maxHp < 1
|
||||||
|
)
|
||||||
|
return null;
|
||||||
|
if (!isValidOptionalMember(entry.color, VALID_PLAYER_COLORS)) return null;
|
||||||
|
if (!isValidOptionalMember(entry.icon, VALID_PLAYER_ICONS)) return null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: playerCharacterId(entry.id),
|
||||||
|
name: entry.name,
|
||||||
|
ac: entry.ac,
|
||||||
|
maxHp: entry.maxHp,
|
||||||
|
color: entry.color as PlayerCharacter["color"],
|
||||||
|
icon: entry.icon as PlayerCharacter["icon"],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function loadPlayerCharacters(): PlayerCharacter[] {
|
||||||
|
try {
|
||||||
|
const raw = localStorage.getItem(STORAGE_KEY);
|
||||||
|
if (raw === null) return [];
|
||||||
|
|
||||||
|
const parsed: unknown = JSON.parse(raw);
|
||||||
|
if (!Array.isArray(parsed)) return [];
|
||||||
|
|
||||||
|
const characters: PlayerCharacter[] = [];
|
||||||
|
for (const item of parsed) {
|
||||||
|
const pc = rehydrateCharacter(item);
|
||||||
|
if (pc !== null) {
|
||||||
|
characters.push(pc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return characters;
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
108
biome.json
108
biome.json
@@ -1,12 +1,14 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://biomejs.dev/schemas/2.0.0/schema.json",
|
"$schema": "https://biomejs.dev/schemas/2.4.7/schema.json",
|
||||||
"files": {
|
"files": {
|
||||||
"includes": [
|
"includes": [
|
||||||
"**",
|
"**",
|
||||||
"!**/dist/**",
|
"!**/dist",
|
||||||
"!.claude/**",
|
"!.claude",
|
||||||
"!.specify/**",
|
"!.specify",
|
||||||
"!specs/**"
|
"!specs",
|
||||||
|
"!coverage",
|
||||||
|
"!.pnpm-store"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"assist": {
|
"assist": {
|
||||||
@@ -19,6 +21,12 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"css": {
|
||||||
|
"parser": {
|
||||||
|
"cssModules": false,
|
||||||
|
"tailwindDirectives": true
|
||||||
|
}
|
||||||
|
},
|
||||||
"formatter": {
|
"formatter": {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"indentStyle": "tab",
|
"indentStyle": "tab",
|
||||||
@@ -27,7 +35,95 @@
|
|||||||
"linter": {
|
"linter": {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"rules": {
|
"rules": {
|
||||||
"recommended": true
|
"recommended": true,
|
||||||
|
"a11y": {
|
||||||
|
"noNoninteractiveElementInteractions": "error"
|
||||||
|
},
|
||||||
|
"complexity": {
|
||||||
|
"noExcessiveCognitiveComplexity": {
|
||||||
|
"level": "error",
|
||||||
|
"options": {
|
||||||
|
"maxAllowedComplexity": 15
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"noUselessStringConcat": "error"
|
||||||
|
},
|
||||||
|
"correctness": {
|
||||||
|
"noNestedComponentDefinitions": "error",
|
||||||
|
"noReactPropAssignments": "error"
|
||||||
|
},
|
||||||
|
"nursery": {
|
||||||
|
"noConditionalExpect": "error",
|
||||||
|
"noDuplicatedSpreadProps": "error",
|
||||||
|
"noFloatingPromises": "error",
|
||||||
|
"noLeakedRender": "error",
|
||||||
|
"noMisusedPromises": "error",
|
||||||
|
"noNestedPromises": "error",
|
||||||
|
"noReturnAssign": "error",
|
||||||
|
"noScriptUrl": "error",
|
||||||
|
"noShadow": "error",
|
||||||
|
"noUnnecessaryConditions": "error",
|
||||||
|
"noUselessReturn": "error",
|
||||||
|
"useArraySome": "error",
|
||||||
|
"useArraySortCompare": "error",
|
||||||
|
"useAwaitThenable": "error",
|
||||||
|
"useErrorCause": "error",
|
||||||
|
"useExhaustiveSwitchCases": "error",
|
||||||
|
"useFind": "error",
|
||||||
|
"useGlobalThis": "error",
|
||||||
|
"useNullishCoalescing": "error",
|
||||||
|
"useRegexpExec": "error",
|
||||||
|
"useSortedClasses": "error",
|
||||||
|
"useSpread": "error"
|
||||||
|
},
|
||||||
|
"performance": {
|
||||||
|
"noAwaitInLoops": "error",
|
||||||
|
"useTopLevelRegex": "error"
|
||||||
|
},
|
||||||
|
"style": {
|
||||||
|
"noCommonJs": "error",
|
||||||
|
"noDoneCallback": "error",
|
||||||
|
"noExportedImports": "error",
|
||||||
|
"noInferrableTypes": "error",
|
||||||
|
"noNamespace": "error",
|
||||||
|
"noNegationElse": "error",
|
||||||
|
"noNestedTernary": "error",
|
||||||
|
"noParameterAssign": "error",
|
||||||
|
"noSubstr": "error",
|
||||||
|
"noUnusedTemplateLiteral": "error",
|
||||||
|
"noUselessElse": "error",
|
||||||
|
"noYodaExpression": "error",
|
||||||
|
"useAsConstAssertion": "error",
|
||||||
|
"useAtIndex": "error",
|
||||||
|
"useCollapsedElseIf": "error",
|
||||||
|
"useCollapsedIf": "error",
|
||||||
|
"useConsistentBuiltinInstantiation": "error",
|
||||||
|
"useDefaultParameterLast": "error",
|
||||||
|
"useExplicitLengthCheck": "error",
|
||||||
|
"useForOf": "error",
|
||||||
|
"useFragmentSyntax": "error",
|
||||||
|
"useNumberNamespace": "error",
|
||||||
|
"useSelfClosingElements": "error",
|
||||||
|
"useShorthandAssign": "error",
|
||||||
|
"useThrowNewError": "error",
|
||||||
|
"useThrowOnlyError": "error",
|
||||||
|
"useTrimStartEnd": "error"
|
||||||
|
},
|
||||||
|
"suspicious": {
|
||||||
|
"noAlert": "error",
|
||||||
|
"noConstantBinaryExpressions": "error",
|
||||||
|
"noDeprecatedImports": "error",
|
||||||
|
"noEvolvingTypes": "error",
|
||||||
|
"noImportCycles": "error",
|
||||||
|
"noReactForwardRef": "error",
|
||||||
|
"noSkippedTests": "error",
|
||||||
|
"noTemplateCurlyInString": "error",
|
||||||
|
"noTsIgnore": "error",
|
||||||
|
"noUnusedExpressions": "error",
|
||||||
|
"noVar": "error",
|
||||||
|
"useAwait": "error",
|
||||||
|
"useErrorMessage": "error"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
36540
data/bestiary/index.json
Normal file
36540
data/bestiary/index.json
Normal file
File diff suppressed because it is too large
Load Diff
63266
data/bestiary/xmm.json
63266
data/bestiary/xmm.json
File diff suppressed because it is too large
Load Diff
4
docs/agents/.gitkeep
Normal file
4
docs/agents/.gitkeep
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# Agent Artifacts
|
||||||
|
|
||||||
|
Research reports and implementation plans generated by RPI skills.
|
||||||
|
|
||||||
0
docs/agents/plans/.gitkeep
Normal file
0
docs/agents/plans/.gitkeep
Normal file
0
docs/agents/research/.gitkeep
Normal file
0
docs/agents/research/.gitkeep
Normal file
176
docs/agents/research/2026-03-13-action-bars-and-buttons.md
Normal file
176
docs/agents/research/2026-03-13-action-bars-and-buttons.md
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
---
|
||||||
|
date: "2026-03-13T14:39:15.661886+00:00"
|
||||||
|
git_commit: 75778884bd1be7d135b2f5ea9b8a8e77a0149f7b
|
||||||
|
branch: main
|
||||||
|
topic: "Action Bars Setup — Top Bar and Bottom Bar Buttons"
|
||||||
|
tags: [research, codebase, action-bar, turn-navigation, layout, buttons]
|
||||||
|
status: complete
|
||||||
|
---
|
||||||
|
|
||||||
|
# Research: Action Bars Setup — Top Bar and Bottom Bar Buttons
|
||||||
|
|
||||||
|
## Research Question
|
||||||
|
|
||||||
|
How are the top and bottom action bars set up, what buttons do they contain, and how are their actions wired?
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
The application has two primary bar components that frame the encounter tracker UI:
|
||||||
|
|
||||||
|
1. **Top bar** — `TurnNavigation` (`turn-navigation.tsx`) — turn controls, round/combatant display, and encounter-wide actions.
|
||||||
|
2. **Bottom bar** — `ActionBar` (`action-bar.tsx`) — combatant input, bestiary search, stat block browsing, bulk import, and player character management.
|
||||||
|
|
||||||
|
Both bars share the same visual container styling (`flex items-center gap-3 rounded-md border border-border bg-card px-4 py-3`). They are laid out in `App.tsx` within a flex column, with a scrollable combatant list between them. When the encounter is empty, only the ActionBar is shown (centered in the viewport); the TurnNavigation appears with an animation when the first combatant is added.
|
||||||
|
|
||||||
|
## Detailed Findings
|
||||||
|
|
||||||
|
### Layout Structure (`App.tsx:243-344`)
|
||||||
|
|
||||||
|
The bars live inside a `max-w-2xl` centered column:
|
||||||
|
|
||||||
|
```
|
||||||
|
┌──────────────────────────────────┐
|
||||||
|
│ TurnNavigation (pt-8, shrink-0) │ ← top bar, conditionally shown
|
||||||
|
├──────────────────────────────────┤
|
||||||
|
│ SourceManager (optional inline) │ ← toggled by Library button in top bar
|
||||||
|
├──────────────────────────────────┤
|
||||||
|
│ Combatant list (flex-1, │ ← scrollable
|
||||||
|
│ overflow-y-auto) │
|
||||||
|
├──────────────────────────────────┤
|
||||||
|
│ ActionBar (pb-8, shrink-0) │ ← bottom bar
|
||||||
|
└──────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
**Empty state**: When `encounter.combatants.length === 0`, the top bar is hidden and the ActionBar is vertically centered in a `flex items-center justify-center` wrapper with `pb-[15%]` offset. It receives `autoFocus` in this state.
|
||||||
|
|
||||||
|
**Animation** (`useActionBarAnimation`, `App.tsx:30-66`): Manages transitions between empty and populated states:
|
||||||
|
- Empty → populated: ActionBar plays `animate-settle-to-bottom`, TurnNavigation plays `animate-slide-down-in`.
|
||||||
|
- Populated → empty: ActionBar plays `animate-rise-to-center`, TurnNavigation plays `animate-slide-up-out` (with `absolute` positioning during exit).
|
||||||
|
|
||||||
|
The `showTopBar` flag is `true` when either combatants exist or the top bar exit animation is still running.
|
||||||
|
|
||||||
|
### Top Bar — TurnNavigation (`turn-navigation.tsx`)
|
||||||
|
|
||||||
|
**Props interface** (`turn-navigation.tsx:7-14`):
|
||||||
|
- `encounter: Encounter` — full encounter state
|
||||||
|
- `onAdvanceTurn`, `onRetreatTurn` — turn navigation callbacks
|
||||||
|
- `onClearEncounter` — destructive clear with confirmation
|
||||||
|
- `onRollAllInitiative` — rolls initiative for all combatants
|
||||||
|
- `onOpenSourceManager` — toggles source manager panel
|
||||||
|
|
||||||
|
**Layout**: Left–Center–Right structure:
|
||||||
|
|
||||||
|
```
|
||||||
|
[ ◀ Prev ] | [ R1 Active Combatant Name ] | [ 🎲 📚 🗑 ] [ Next ▶ ]
|
||||||
|
```
|
||||||
|
|
||||||
|
**Buttons (left to right)**:
|
||||||
|
|
||||||
|
| # | Icon | Component | Variant | Action | Disabled when |
|
||||||
|
|---|------|-----------|---------|--------|---------------|
|
||||||
|
| 1 | `StepBack` | `Button` | default | `onRetreatTurn` | No combatants OR at round 1 index 0 |
|
||||||
|
| 2 | `D20Icon` | `Button` | ghost | `onRollAllInitiative` | Never |
|
||||||
|
| 3 | `Library` | `Button` | ghost | `onOpenSourceManager` | Never |
|
||||||
|
| 4 | `Trash2` | `ConfirmButton` | — | `onClearEncounter` | No combatants |
|
||||||
|
| 5 | `StepForward` | `Button` | default | `onAdvanceTurn` | No combatants |
|
||||||
|
|
||||||
|
**Center section** (`turn-navigation.tsx:40-49`): Displays a round badge (`R{n}` in a `rounded-full bg-muted` span) and the active combatant's name (truncated). Falls back to "No combatants" in muted text.
|
||||||
|
|
||||||
|
**Button grouping**: Buttons 2-4 are grouped in a `gap-0` div (tight spacing), while button 5 (Next) is separated by the outer `gap-3`.
|
||||||
|
|
||||||
|
**Wiring in App.tsx** (`App.tsx:251-258`):
|
||||||
|
- `onAdvanceTurn` → `advanceTurn` from `useEncounter()`
|
||||||
|
- `onRetreatTurn` → `retreatTurn` from `useEncounter()`
|
||||||
|
- `onClearEncounter` → `clearEncounter` from `useEncounter()`
|
||||||
|
- `onRollAllInitiative` → `handleRollAllInitiative` → calls `rollAllInitiativeUseCase(makeStore(), rollDice, getCreature)`
|
||||||
|
- `onOpenSourceManager` → toggles `sourceManagerOpen` state
|
||||||
|
|
||||||
|
### Bottom Bar — ActionBar (`action-bar.tsx`)
|
||||||
|
|
||||||
|
**Props interface** (`action-bar.tsx:20-36`):
|
||||||
|
- `onAddCombatant` — adds custom combatant with optional init/AC/maxHP
|
||||||
|
- `onAddFromBestiary` — adds creature from search result
|
||||||
|
- `bestiarySearch` — search function returning `SearchResult[]`
|
||||||
|
- `bestiaryLoaded` — whether bestiary index is loaded
|
||||||
|
- `onViewStatBlock` — opens stat block panel for a creature
|
||||||
|
- `onBulkImport` — triggers bulk source import mode
|
||||||
|
- `bulkImportDisabled` — disables import button during loading
|
||||||
|
- `inputRef` — external ref to the name input
|
||||||
|
- `playerCharacters` — list of player characters for quick-add
|
||||||
|
- `onAddFromPlayerCharacter` — adds a player character to encounter
|
||||||
|
- `onManagePlayers` — opens player management modal
|
||||||
|
- `autoFocus` — auto-focuses input (used in empty state)
|
||||||
|
|
||||||
|
**Layout**: Form with input, contextual fields, submit button, and action icons:
|
||||||
|
|
||||||
|
```
|
||||||
|
[ + Add combatants... ] [ Init ] [ AC ] [ MaxHP ] [ Add ] [ 👥 👁 📥 ]
|
||||||
|
```
|
||||||
|
|
||||||
|
The Init/AC/MaxHP fields only appear when the input has 2+ characters and no bestiary suggestions are showing.
|
||||||
|
|
||||||
|
**Buttons (left to right)**:
|
||||||
|
|
||||||
|
| # | Icon | Component | Variant | Action | Condition |
|
||||||
|
|---|------|-----------|---------|--------|-----------|
|
||||||
|
| 1 | — | `Button` | sm | Form submit → `handleAdd` | Always shown |
|
||||||
|
| 2 | `Users` | `Button` | ghost | `onManagePlayers` | Only if `onManagePlayers` provided |
|
||||||
|
| 3 | `Eye` | `Button` | ghost | Toggle stat block viewer dropdown | Only if `bestiaryLoaded && onViewStatBlock` |
|
||||||
|
| 4 | `Import` | `Button` | ghost | `onBulkImport` | Only if `bestiaryLoaded && onBulkImport` |
|
||||||
|
|
||||||
|
**Button grouping**: Buttons 2-4 are grouped in a `gap-0` div, mirroring the top bar's icon button grouping.
|
||||||
|
|
||||||
|
**Suggestion dropdown** (`action-bar.tsx:267-410`): Opens above the input when 2+ chars are typed and results exist. Contains:
|
||||||
|
- A "Add as custom" escape row at the top (with `Esc` keyboard hint)
|
||||||
|
- **Players section**: Lists matching player characters with colored icons; clicking adds them directly via `onAddFromPlayerCharacter`
|
||||||
|
- **Bestiary section**: Lists search results; clicking queues a creature. Queued creatures show:
|
||||||
|
- `Minus` button — decrements count (removes queue at 0)
|
||||||
|
- Count badge — current queued count
|
||||||
|
- `Plus` button — increments count
|
||||||
|
- `Check` button — confirms and adds all queued copies
|
||||||
|
|
||||||
|
**Stat block viewer dropdown** (`action-bar.tsx:470-513`): A separate search dropdown anchored to the Eye button. Has its own input, search results, and keyboard navigation. Selecting a result calls `onViewStatBlock`.
|
||||||
|
|
||||||
|
**Keyboard handling** (`action-bar.tsx:168-186`):
|
||||||
|
- Arrow Up/Down — navigate suggestion list
|
||||||
|
- Enter — queue selected suggestion or confirm queued batch
|
||||||
|
- Escape — clear suggestions and queue
|
||||||
|
|
||||||
|
**Wiring in App.tsx** (`App.tsx:269-282` and `328-340`):
|
||||||
|
- `onAddCombatant` → `addCombatant` from `useEncounter()`
|
||||||
|
- `onAddFromBestiary` → `handleAddFromBestiary` → `addFromBestiary` from `useEncounter()`
|
||||||
|
- `bestiarySearch` → `search` from `useBestiary()`
|
||||||
|
- `onViewStatBlock` → `handleViewStatBlock` → constructs `CreatureId` and sets `selectedCreatureId`
|
||||||
|
- `onBulkImport` → `handleBulkImport` → sets `bulkImportMode` and clears selection
|
||||||
|
- `onAddFromPlayerCharacter` → `addFromPlayerCharacter` from `useEncounter()`
|
||||||
|
- `onManagePlayers` → opens `managementOpen` state (shows `PlayerManagement` modal)
|
||||||
|
|
||||||
|
### Shared UI Primitives
|
||||||
|
|
||||||
|
**`Button`** (`ui/button.tsx`): CVA-based component with variants (`default`, `outline`, `ghost`) and sizes (`default`, `sm`, `icon`). Both bars use `size="icon"` with `variant="ghost"` for their icon button clusters, and `size="icon"` with default variant for the primary navigation buttons (Prev/Next in top bar).
|
||||||
|
|
||||||
|
**`ConfirmButton`** (`ui/confirm-button.tsx`): Two-click destructive action button. First click shows a red pulsing confirmation state with a Check icon; second click fires `onConfirm`. Auto-reverts after 5 seconds. Supports Escape and click-outside cancellation. Used for Clear Encounter in the top bar.
|
||||||
|
|
||||||
|
### Hover Color Convention
|
||||||
|
|
||||||
|
Both bars use consistent hover color classes on their ghost icon buttons:
|
||||||
|
- `hover:text-hover-action` — used on the D20 (roll initiative) button, suggesting an action/accent color
|
||||||
|
- `hover:text-hover-neutral` — used on Library, Users, Eye, Import buttons, suggesting a neutral/informational color
|
||||||
|
|
||||||
|
## Code References
|
||||||
|
|
||||||
|
- `apps/web/src/components/turn-navigation.tsx` — Top bar component (93 lines)
|
||||||
|
- `apps/web/src/components/action-bar.tsx` — Bottom bar component (533 lines)
|
||||||
|
- `apps/web/src/App.tsx:30-66` — `useActionBarAnimation` hook for bar transitions
|
||||||
|
- `apps/web/src/App.tsx:243-344` — Layout structure with both bars
|
||||||
|
- `apps/web/src/components/ui/button.tsx` — Shared Button component
|
||||||
|
- `apps/web/src/components/ui/confirm-button.tsx` — Two-step confirmation button
|
||||||
|
- `apps/web/src/components/d20-icon.tsx` — Custom D20 dice SVG icon
|
||||||
|
|
||||||
|
## Architecture Documentation
|
||||||
|
|
||||||
|
The bars follow the app's adapter-layer convention: they are pure presentational React components that receive all behavior via callback props. No business logic lives in either bar — they delegate to handlers defined in `App.tsx`, which in turn call use-case functions from the application layer or manipulate local UI state.
|
||||||
|
|
||||||
|
Both bars are rendered twice in `App.tsx` (once in the empty-state branch, once in the populated branch) rather than being conditionally repositioned, which simplifies the animation logic.
|
||||||
|
|
||||||
|
The `ActionBar` is the more complex of the two, managing multiple pieces of local state (input value, suggestions, queued creatures, custom fields, stat block viewer) while `TurnNavigation` is fully stateless — all its data comes from the `encounter` prop.
|
||||||
188
docs/agents/research/2026-03-13-css-classes-buttons-hover.md
Normal file
188
docs/agents/research/2026-03-13-css-classes-buttons-hover.md
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
---
|
||||||
|
date: "2026-03-13T15:35:07.699570+00:00"
|
||||||
|
git_commit: bd398080008349b47726d0016f4b03587f453833
|
||||||
|
branch: main
|
||||||
|
topic: "CSS class usage, button categorization, and hover effects across all components"
|
||||||
|
tags: [research, codebase, css, tailwind, buttons, hover, ui]
|
||||||
|
status: complete
|
||||||
|
---
|
||||||
|
|
||||||
|
# Research: CSS Class Usage, Button Categorization, and Hover Effects
|
||||||
|
|
||||||
|
## Research Question
|
||||||
|
How are CSS classes used across all components? How are buttons categorized — are there primary and secondary buttons? What hover effects exist, and are they unified?
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
The project uses **Tailwind CSS v4** with a custom dark theme defined in `index.css` via `@theme`. All class merging goes through a `cn()` utility (clsx + tailwind-merge). Buttons are built on a shared `Button` component using **class-variance-authority (CVA)** with three variants: **default** (primary), **outline**, and **ghost**. Hover effects are partially unified through semantic color tokens (`hover-neutral`, `hover-action`, `hover-destructive`) defined in the theme, but several components use **one-off hardcoded hover colors** that bypass the token system.
|
||||||
|
|
||||||
|
## Detailed Findings
|
||||||
|
|
||||||
|
### Theme System (`index.css`)
|
||||||
|
|
||||||
|
All colors are defined as CSS custom properties via Tailwind v4's `@theme` directive (`index.css:3-26`):
|
||||||
|
|
||||||
|
| Token | Value | Purpose |
|
||||||
|
|---|---|---|
|
||||||
|
| `--color-background` | `#0f172a` | Page background |
|
||||||
|
| `--color-foreground` | `#e2e8f0` | Default text |
|
||||||
|
| `--color-muted` | `#64748b` | Subdued elements |
|
||||||
|
| `--color-muted-foreground` | `#94a3b8` | Secondary text |
|
||||||
|
| `--color-card` | `#1e293b` | Card/panel surfaces |
|
||||||
|
| `--color-border` | `#334155` | Borders |
|
||||||
|
| `--color-primary` | `#3b82f6` | Primary actions (blue) |
|
||||||
|
| `--color-accent` | `#3b82f6` | Accent (same as primary) |
|
||||||
|
| `--color-destructive` | `#ef4444` | Destructive actions (red) |
|
||||||
|
|
||||||
|
**Hover tokens** (semantic layer for hover states):
|
||||||
|
|
||||||
|
| Token | Resolves to | Usage |
|
||||||
|
|---|---|---|
|
||||||
|
| `hover-neutral` | `primary` (blue) | Text color on neutral hover |
|
||||||
|
| `hover-action` | `primary` (blue) | Text color on action hover |
|
||||||
|
| `hover-destructive` | `destructive` (red) | Text color on destructive hover |
|
||||||
|
| `hover-neutral-bg` | `card` (slate) | Background on neutral hover |
|
||||||
|
| `hover-action-bg` | `muted` | Background on action hover |
|
||||||
|
| `hover-destructive-bg` | `transparent` | Background on destructive hover |
|
||||||
|
|
||||||
|
### Button Component (`components/ui/button.tsx`)
|
||||||
|
|
||||||
|
Uses CVA with three variants and three sizes:
|
||||||
|
|
||||||
|
**Variants:**
|
||||||
|
|
||||||
|
| Variant | Base styles | Hover |
|
||||||
|
|---|---|---|
|
||||||
|
| `default` (primary) | `bg-primary text-primary-foreground` | `hover:bg-primary/90` |
|
||||||
|
| `outline` | `border border-border bg-transparent` | `hover:bg-hover-neutral-bg hover:text-hover-neutral` |
|
||||||
|
| `ghost` | (no background/border) | `hover:bg-hover-neutral-bg hover:text-hover-neutral` |
|
||||||
|
|
||||||
|
**Sizes:**
|
||||||
|
|
||||||
|
| Size | Classes |
|
||||||
|
|---|---|
|
||||||
|
| `default` | `h-9 px-4 py-2` |
|
||||||
|
| `sm` | `h-8 px-3 text-xs` |
|
||||||
|
| `icon` | `h-8 w-8` |
|
||||||
|
|
||||||
|
All variants share: `rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary disabled:pointer-events-none disabled:opacity-50`.
|
||||||
|
|
||||||
|
There is **no "secondary" variant** — the outline variant is the closest equivalent.
|
||||||
|
|
||||||
|
### Composite Button Components
|
||||||
|
|
||||||
|
**ConfirmButton** (`components/ui/confirm-button.tsx`):
|
||||||
|
- Wraps `Button variant="ghost" size="icon"`
|
||||||
|
- Default state: `hover:text-hover-destructive` (uses token)
|
||||||
|
- Confirming state: `bg-destructive text-primary-foreground animate-confirm-pulse hover:bg-destructive hover:text-primary-foreground`
|
||||||
|
|
||||||
|
**OverflowMenu** (`components/ui/overflow-menu.tsx`):
|
||||||
|
- Trigger: `Button variant="ghost" size="icon"` with `text-muted-foreground hover:text-hover-neutral`
|
||||||
|
- Menu items: raw `<button>` elements with `hover:bg-muted/20` (**not using the token system**)
|
||||||
|
|
||||||
|
### Button Usage Across Components
|
||||||
|
|
||||||
|
| Component | Button type | Variant/Style |
|
||||||
|
|---|---|---|
|
||||||
|
| `action-bar.tsx:556` | `<Button type="submit">` | default (primary) — "Add" |
|
||||||
|
| `action-bar.tsx:561` | `<Button type="button">` | default (primary) — "Roll all" |
|
||||||
|
| `turn-navigation.tsx:25,54` | `<Button size="icon">` | default — prev/next turn |
|
||||||
|
| `turn-navigation.tsx:47` | `<ConfirmButton>` | ghost+destructive — clear encounter |
|
||||||
|
| `source-fetch-prompt.tsx:91` | `<Button size="sm">` | default — "Load" |
|
||||||
|
| `source-fetch-prompt.tsx:106` | `<Button size="sm" variant="outline">` | outline — "Upload file" |
|
||||||
|
| `bulk-import-prompt.tsx:31,45,106` | `<Button size="sm">` | default — "Done"/"Load All" |
|
||||||
|
| `source-manager.tsx:50` | `<Button size="sm" variant="outline">` | outline — "Clear all" |
|
||||||
|
| `hp-adjust-popover.tsx:112` | `<Button variant="ghost" size="icon">` | ghost + custom red — damage |
|
||||||
|
| `hp-adjust-popover.tsx:124` | `<Button variant="ghost" size="icon">` | ghost + custom green — heal |
|
||||||
|
| `player-management.tsx:67` | `<Button>` | default — "Create first player" |
|
||||||
|
| `player-management.tsx:113` | `<Button variant="ghost">` | ghost — "Add player" |
|
||||||
|
| `create-player-modal.tsx:177` | `<Button variant="ghost">` | ghost — "Cancel" |
|
||||||
|
| `create-player-modal.tsx:180` | `<Button type="submit">` | default — "Save"/"Create" |
|
||||||
|
| `combatant-row.tsx:625` | `<ConfirmButton>` | ghost+destructive — remove combatant |
|
||||||
|
|
||||||
|
**Raw `<button>` elements** (not using the Button component):
|
||||||
|
- `action-bar.tsx` — suggestion items, count increment/decrement, browse toggle, custom add (all inline-styled)
|
||||||
|
- `combatant-row.tsx` — editable name, HP display, AC, initiative, concentration toggle
|
||||||
|
- `stat-block-panel.tsx` — fold/close/pin/unpin buttons
|
||||||
|
- `condition-picker.tsx` — condition items
|
||||||
|
- `condition-tags.tsx` — condition tags, add condition button
|
||||||
|
- `toast.tsx` — dismiss button
|
||||||
|
- `player-management.tsx` — close modal, edit player
|
||||||
|
- `create-player-modal.tsx` — close modal
|
||||||
|
- `color-palette.tsx` — color swatches
|
||||||
|
- `icon-grid.tsx` — icon options
|
||||||
|
|
||||||
|
### Hover Effects Inventory
|
||||||
|
|
||||||
|
**Using semantic tokens (unified):**
|
||||||
|
|
||||||
|
| Hover class | Meaning | Used in |
|
||||||
|
|---|---|---|
|
||||||
|
| `hover:bg-hover-neutral-bg` | Neutral background highlight | button.tsx (outline/ghost), action-bar.tsx, condition-picker.tsx, condition-tags.tsx |
|
||||||
|
| `hover:text-hover-neutral` | Text turns primary blue | button.tsx (outline/ghost), action-bar.tsx, combatant-row.tsx, stat-block-panel.tsx, ac-shield.tsx, toast.tsx, overflow-menu.tsx, condition-tags.tsx |
|
||||||
|
| `hover:text-hover-action` | Action text (same as neutral) | action-bar.tsx (overflow trigger) |
|
||||||
|
| `hover:text-hover-destructive` | Destructive text turns red | confirm-button.tsx, source-manager.tsx |
|
||||||
|
| `hover:bg-hover-destructive-bg` | Destructive background (transparent) | source-manager.tsx |
|
||||||
|
|
||||||
|
**One-off / hardcoded hover colors (NOT using tokens):**
|
||||||
|
|
||||||
|
| Hover class | Used in | Context |
|
||||||
|
|---|---|---|
|
||||||
|
| `hover:bg-primary/90` | button.tsx (default variant) | Primary button darken |
|
||||||
|
| `hover:bg-accent/20` | action-bar.tsx | Suggestion highlight, custom add |
|
||||||
|
| `hover:bg-accent/40` | action-bar.tsx | Count +/- buttons, confirm queued |
|
||||||
|
| `hover:bg-muted/20` | overflow-menu.tsx | Menu item highlight |
|
||||||
|
| `hover:bg-red-950` | hp-adjust-popover.tsx | Damage button |
|
||||||
|
| `hover:text-red-300` | hp-adjust-popover.tsx | Damage button text |
|
||||||
|
| `hover:bg-emerald-950` | hp-adjust-popover.tsx | Heal button |
|
||||||
|
| `hover:text-emerald-300` | hp-adjust-popover.tsx | Heal button text |
|
||||||
|
| `hover:text-foreground` | player-management.tsx, create-player-modal.tsx, icon-grid.tsx | Close/edit buttons |
|
||||||
|
| `hover:bg-background/50` | player-management.tsx | Player row hover |
|
||||||
|
| `hover:bg-card` | icon-grid.tsx | Icon option hover |
|
||||||
|
| `hover:border-hover-destructive` | source-manager.tsx | Clear all button border |
|
||||||
|
| `hover:scale-110` | color-palette.tsx | Color swatch enlarge |
|
||||||
|
| `hover:bg-destructive` | confirm-button.tsx (confirming state) | Maintain red bg on hover |
|
||||||
|
| `hover:text-primary-foreground` | confirm-button.tsx (confirming state) | Maintain white text on hover |
|
||||||
|
|
||||||
|
### Hover unification assessment
|
||||||
|
|
||||||
|
The hover token system (`hover-neutral`, `hover-action`, `hover-destructive`) provides a consistent pattern for the most common interactions. The `Button` component's outline and ghost variants use these tokens, and many inline buttons in action-bar, combatant-row, stat-block-panel, and condition components also use them.
|
||||||
|
|
||||||
|
However, there are notable gaps:
|
||||||
|
1. **HP adjust popover** uses hardcoded red/green colors (`red-950`, `emerald-950`) instead of tokens
|
||||||
|
2. **Overflow menu items** use `hover:bg-muted/20` instead of `hover:bg-hover-neutral-bg`
|
||||||
|
3. **Player management modals** use `hover:text-foreground` and `hover:bg-background/50` instead of the semantic tokens
|
||||||
|
4. **Action-bar suggestion items** use `hover:bg-accent/20` and `hover:bg-accent/40` — accent-specific patterns not in the token system
|
||||||
|
5. **Icon grid** and **color palette** use their own hover patterns (`hover:bg-card`, `hover:scale-110`)
|
||||||
|
|
||||||
|
## Code References
|
||||||
|
|
||||||
|
- `apps/web/src/index.css:3-26` — Theme color definitions including hover tokens
|
||||||
|
- `apps/web/src/components/ui/button.tsx:1-38` — Button component with CVA variants
|
||||||
|
- `apps/web/src/components/ui/confirm-button.tsx:93-115` — ConfirmButton with destructive hover states
|
||||||
|
- `apps/web/src/components/ui/overflow-menu.tsx:38-72` — OverflowMenu with non-token hover
|
||||||
|
- `apps/web/src/components/hp-adjust-popover.tsx:117-129` — Hardcoded red/green hover colors
|
||||||
|
- `apps/web/src/components/action-bar.tsx:80-188` — Mixed token and accent-based hovers
|
||||||
|
- `apps/web/src/components/combatant-row.tsx:147-629` — Inline buttons with token hovers
|
||||||
|
- `apps/web/src/components/player-management.tsx:58-98` — Non-token hover patterns
|
||||||
|
- `apps/web/src/components/stat-block-panel.tsx:55-109` — Consistent token usage
|
||||||
|
- `apps/web/src/lib/utils.ts:1-5` — `cn()` utility (clsx + twMerge)
|
||||||
|
|
||||||
|
## Architecture Documentation
|
||||||
|
|
||||||
|
The styling architecture follows this pattern:
|
||||||
|
|
||||||
|
1. **Theme layer**: `index.css` defines all color tokens via `@theme`, including semantic hover tokens
|
||||||
|
2. **Component layer**: `Button` (CVA) provides the shared button abstraction with three variants
|
||||||
|
3. **Composite layer**: `ConfirmButton` and `OverflowMenu` wrap `Button` with additional behavior
|
||||||
|
4. **Usage layer**: Components use either `Button` component or raw `<button>` elements with inline Tailwind classes
|
||||||
|
|
||||||
|
The `cn()` utility from `lib/utils.ts` is used in 9+ components for conditional class merging.
|
||||||
|
|
||||||
|
Custom animations are defined in `index.css` via `@keyframes` + `@utility` pairs: slide-in-right, confirm-pulse, settle-to-bottom, rise-to-center, slide-down-in, slide-up-out, concentration-pulse.
|
||||||
|
|
||||||
|
## Open Questions
|
||||||
|
|
||||||
|
1. The `hover-action` and `hover-action-bg` tokens are defined but rarely used — `hover-action` appears only once in `action-bar.tsx:565`. Is this intentional or an incomplete migration?
|
||||||
|
2. The `accent` color (`#3b82f6`) is identical to `primary` — are they intended to diverge in the future, or is this redundancy?
|
||||||
|
3. Should the hardcoded HP adjust colors (red/emerald) be promoted to theme tokens (e.g., `hover-damage`, `hover-heal`)?
|
||||||
9
nginx.conf
Normal file
9
nginx.conf
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
root /usr/share/nginx/html;
|
||||||
|
index index.html;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
try_files $uri $uri/ /index.html;
|
||||||
|
}
|
||||||
|
}
|
||||||
15
package.json
15
package.json
@@ -1,11 +1,19 @@
|
|||||||
{
|
{
|
||||||
"private": true,
|
"private": true,
|
||||||
"packageManager": "pnpm@10.6.0",
|
"packageManager": "pnpm@10.6.0",
|
||||||
|
"pnpm": {
|
||||||
|
"overrides": {
|
||||||
|
"undici": ">=7.24.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@biomejs/biome": "2.0.0",
|
"@biomejs/biome": "2.4.7",
|
||||||
|
"@vitest/coverage-v8": "^3.2.4",
|
||||||
"jscpd": "^4.0.8",
|
"jscpd": "^4.0.8",
|
||||||
"knip": "^5.85.0",
|
"knip": "^5.85.0",
|
||||||
"lefthook": "^1.11.0",
|
"lefthook": "^1.11.0",
|
||||||
|
"oxlint": "^1.55.0",
|
||||||
|
"oxlint-tsgolint": "^0.16.0",
|
||||||
"typescript": "^5.8.0",
|
"typescript": "^5.8.0",
|
||||||
"vitest": "^3.0.0"
|
"vitest": "^3.0.0"
|
||||||
},
|
},
|
||||||
@@ -20,6 +28,9 @@
|
|||||||
"test:watch": "vitest",
|
"test:watch": "vitest",
|
||||||
"knip": "knip",
|
"knip": "knip",
|
||||||
"jscpd": "jscpd",
|
"jscpd": "jscpd",
|
||||||
"check": "knip && biome check . && tsc --build && vitest run && jscpd"
|
"oxlint": "oxlint --tsconfig apps/web/tsconfig.json --type-aware",
|
||||||
|
"check:ignores": "node scripts/check-lint-ignores.mjs",
|
||||||
|
"check:classnames": "node scripts/check-cn-classnames.mjs",
|
||||||
|
"check": "pnpm audit --audit-level=high && knip && biome check . && oxlint --tsconfig apps/web/tsconfig.json --type-aware && node scripts/check-lint-ignores.mjs && node scripts/check-cn-classnames.mjs && tsc --build && vitest run && jscpd"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
54
packages/application/src/__tests__/helpers.ts
Normal file
54
packages/application/src/__tests__/helpers.ts
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import type { Encounter, PlayerCharacter } from "@initiative/domain";
|
||||||
|
import { isDomainError } from "@initiative/domain";
|
||||||
|
import type { EncounterStore, PlayerCharacterStore } from "../ports.js";
|
||||||
|
|
||||||
|
export function requireSaved<T>(value: T | null): T {
|
||||||
|
if (value === null) throw new Error("Expected store.saved to be non-null");
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function expectSuccess<T>(
|
||||||
|
result: T,
|
||||||
|
): asserts result is Exclude<T, { kind: "domain-error" }> {
|
||||||
|
if (isDomainError(result)) {
|
||||||
|
throw new Error(`Expected success, got domain error: ${result.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function expectError(result: unknown): asserts result is {
|
||||||
|
kind: "domain-error";
|
||||||
|
code: string;
|
||||||
|
message: string;
|
||||||
|
} {
|
||||||
|
if (!isDomainError(result)) {
|
||||||
|
throw new Error("Expected domain error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stubEncounterStore(
|
||||||
|
initial: Encounter,
|
||||||
|
): EncounterStore & { saved: Encounter | null } {
|
||||||
|
const stub = {
|
||||||
|
saved: null as Encounter | null,
|
||||||
|
get: () => initial,
|
||||||
|
save: (e: Encounter) => {
|
||||||
|
stub.saved = e;
|
||||||
|
stub.get = () => e;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return stub;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stubPlayerCharacterStore(
|
||||||
|
initial: readonly PlayerCharacter[],
|
||||||
|
): PlayerCharacterStore & { saved: readonly PlayerCharacter[] | null } {
|
||||||
|
const stub = {
|
||||||
|
saved: null as readonly PlayerCharacter[] | null,
|
||||||
|
getAll: () => [...initial],
|
||||||
|
save: (characters: PlayerCharacter[]) => {
|
||||||
|
stub.saved = characters;
|
||||||
|
stub.getAll = () => [...characters];
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return stub;
|
||||||
|
}
|
||||||
@@ -0,0 +1,195 @@
|
|||||||
|
import {
|
||||||
|
type Creature,
|
||||||
|
combatantId,
|
||||||
|
createEncounter,
|
||||||
|
creatureId,
|
||||||
|
isDomainError,
|
||||||
|
} from "@initiative/domain";
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { rollAllInitiativeUseCase } from "../roll-all-initiative-use-case.js";
|
||||||
|
import {
|
||||||
|
expectError,
|
||||||
|
expectSuccess,
|
||||||
|
requireSaved,
|
||||||
|
stubEncounterStore,
|
||||||
|
} from "./helpers.js";
|
||||||
|
|
||||||
|
const CREATURE_A = creatureId("creature-a");
|
||||||
|
const CREATURE_B = creatureId("creature-b");
|
||||||
|
|
||||||
|
function makeCreature(id: string, dex = 14): Creature {
|
||||||
|
return {
|
||||||
|
id: creatureId(id),
|
||||||
|
name: `Creature ${id}`,
|
||||||
|
source: "mm",
|
||||||
|
sourceDisplayName: "Monster Manual",
|
||||||
|
size: "Medium",
|
||||||
|
type: "humanoid",
|
||||||
|
alignment: "neutral",
|
||||||
|
ac: 12,
|
||||||
|
hp: { average: 10, formula: "2d8+2" },
|
||||||
|
speed: "30 ft.",
|
||||||
|
abilities: { str: 10, dex, con: 10, int: 10, wis: 10, cha: 10 },
|
||||||
|
cr: "1",
|
||||||
|
initiativeProficiency: 0,
|
||||||
|
proficiencyBonus: 2,
|
||||||
|
passive: 10,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function encounterWithCombatants(
|
||||||
|
combatants: Array<{
|
||||||
|
name: string;
|
||||||
|
creatureId?: string;
|
||||||
|
initiative?: number;
|
||||||
|
}>,
|
||||||
|
) {
|
||||||
|
const result = createEncounter(
|
||||||
|
combatants.map((c) => ({
|
||||||
|
id: combatantId(c.name),
|
||||||
|
name: c.name,
|
||||||
|
creatureId: c.creatureId ? creatureId(c.creatureId) : undefined,
|
||||||
|
initiative: c.initiative,
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
if (isDomainError(result)) throw new Error("Setup failed");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("rollAllInitiativeUseCase", () => {
|
||||||
|
it("skips combatants without creatureId", () => {
|
||||||
|
const enc = encounterWithCombatants([
|
||||||
|
{ name: "Fighter" },
|
||||||
|
{ name: "Goblin", creatureId: "creature-a" },
|
||||||
|
]);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const creature = makeCreature("creature-a");
|
||||||
|
|
||||||
|
const result = rollAllInitiativeUseCase(
|
||||||
|
store,
|
||||||
|
() => 10,
|
||||||
|
(id) => (id === CREATURE_A ? creature : undefined),
|
||||||
|
);
|
||||||
|
|
||||||
|
expectSuccess(result);
|
||||||
|
expect(result.events.length).toBeGreaterThan(0);
|
||||||
|
const saved = requireSaved(store.saved);
|
||||||
|
const fighter = saved.combatants.find((c) => c.name === "Fighter");
|
||||||
|
const goblin = saved.combatants.find((c) => c.name === "Goblin");
|
||||||
|
expect(fighter?.initiative).toBeUndefined();
|
||||||
|
expect(goblin?.initiative).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips combatants that already have initiative", () => {
|
||||||
|
const enc = encounterWithCombatants([
|
||||||
|
{ name: "Goblin", creatureId: "creature-a", initiative: 15 },
|
||||||
|
]);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
|
||||||
|
const result = rollAllInitiativeUseCase(
|
||||||
|
store,
|
||||||
|
() => 10,
|
||||||
|
() => makeCreature("creature-a"),
|
||||||
|
);
|
||||||
|
|
||||||
|
expectSuccess(result);
|
||||||
|
expect(result.events).toHaveLength(0);
|
||||||
|
expect(requireSaved(store.saved).combatants[0].initiative).toBe(15);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("counts skippedNoSource when creature lookup returns undefined", () => {
|
||||||
|
const enc = encounterWithCombatants([
|
||||||
|
{ name: "Unknown", creatureId: "missing" },
|
||||||
|
]);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
|
||||||
|
const result = rollAllInitiativeUseCase(
|
||||||
|
store,
|
||||||
|
() => 10,
|
||||||
|
() => undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
expectSuccess(result);
|
||||||
|
expect(result.skippedNoSource).toBe(1);
|
||||||
|
expect(result.events).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("accumulates events from multiple setInitiative calls", () => {
|
||||||
|
const enc = encounterWithCombatants([
|
||||||
|
{ name: "A", creatureId: "creature-a" },
|
||||||
|
{ name: "B", creatureId: "creature-b" },
|
||||||
|
]);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const creatureA = makeCreature("creature-a");
|
||||||
|
const creatureB = makeCreature("creature-b");
|
||||||
|
|
||||||
|
const result = rollAllInitiativeUseCase(
|
||||||
|
store,
|
||||||
|
() => 10,
|
||||||
|
(id) => {
|
||||||
|
if (id === CREATURE_A) return creatureA;
|
||||||
|
if (id === CREATURE_B) return creatureB;
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expectSuccess(result);
|
||||||
|
expect(result.events).toHaveLength(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns early with domain error on invalid dice roll", () => {
|
||||||
|
const enc = encounterWithCombatants([
|
||||||
|
{ name: "A", creatureId: "creature-a" },
|
||||||
|
{ name: "B", creatureId: "creature-b" },
|
||||||
|
]);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
|
||||||
|
// rollDice returns 0 (invalid — must be 1–20), triggers early return
|
||||||
|
const result = rollAllInitiativeUseCase(
|
||||||
|
store,
|
||||||
|
() => 0,
|
||||||
|
(id) => {
|
||||||
|
if (id === CREATURE_A) return makeCreature("creature-a");
|
||||||
|
if (id === CREATURE_B) return makeCreature("creature-b");
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expectError(result);
|
||||||
|
expect(result.code).toBe("invalid-dice-roll");
|
||||||
|
// Store should NOT have been saved since the loop aborted
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("saves encounter once at the end", () => {
|
||||||
|
const enc = encounterWithCombatants([
|
||||||
|
{ name: "A", creatureId: "creature-a" },
|
||||||
|
{ name: "B", creatureId: "creature-b" },
|
||||||
|
]);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const creatureA = makeCreature("creature-a");
|
||||||
|
const creatureB = makeCreature("creature-b");
|
||||||
|
|
||||||
|
let saveCount = 0;
|
||||||
|
const originalSave = store.save.bind(store);
|
||||||
|
store.save = (e) => {
|
||||||
|
saveCount++;
|
||||||
|
originalSave(e);
|
||||||
|
};
|
||||||
|
|
||||||
|
rollAllInitiativeUseCase(
|
||||||
|
store,
|
||||||
|
() => 10,
|
||||||
|
(id) => {
|
||||||
|
if (id === CREATURE_A) return creatureA;
|
||||||
|
if (id === CREATURE_B) return creatureB;
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(saveCount).toBe(1);
|
||||||
|
const saved = requireSaved(store.saved);
|
||||||
|
expect(saved.combatants[0].initiative).toBeDefined();
|
||||||
|
expect(saved.combatants[1].initiative).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
import {
|
||||||
|
type Creature,
|
||||||
|
type CreatureId,
|
||||||
|
combatantId,
|
||||||
|
createEncounter,
|
||||||
|
creatureId,
|
||||||
|
isDomainError,
|
||||||
|
} from "@initiative/domain";
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { addCombatantUseCase } from "../add-combatant-use-case.js";
|
||||||
|
import { rollInitiativeUseCase } from "../roll-initiative-use-case.js";
|
||||||
|
import { expectError, requireSaved, stubEncounterStore } from "./helpers.js";
|
||||||
|
|
||||||
|
const GOBLIN_ID = creatureId("goblin");
|
||||||
|
|
||||||
|
function makeCreature(overrides?: Partial<Creature>): Creature {
|
||||||
|
return {
|
||||||
|
id: GOBLIN_ID,
|
||||||
|
name: "Goblin",
|
||||||
|
source: "mm",
|
||||||
|
sourceDisplayName: "Monster Manual",
|
||||||
|
size: "Small",
|
||||||
|
type: "humanoid",
|
||||||
|
alignment: "neutral evil",
|
||||||
|
ac: 15,
|
||||||
|
hp: { average: 7, formula: "2d6" },
|
||||||
|
speed: "30 ft.",
|
||||||
|
abilities: { str: 8, dex: 14, con: 10, int: 10, wis: 8, cha: 8 },
|
||||||
|
cr: "1/4",
|
||||||
|
initiativeProficiency: 0,
|
||||||
|
proficiencyBonus: 2,
|
||||||
|
passive: 9,
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function encounterWithCreatureLink(name: string, creature: CreatureId) {
|
||||||
|
const enc = createEncounter([]);
|
||||||
|
if (isDomainError(enc)) throw new Error("Setup failed");
|
||||||
|
const id = combatantId(name);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
addCombatantUseCase(store, id, name);
|
||||||
|
const saved = requireSaved(store.saved);
|
||||||
|
const result = createEncounter(
|
||||||
|
saved.combatants.map((c) =>
|
||||||
|
c.id === id ? { ...c, creatureId: creature } : c,
|
||||||
|
),
|
||||||
|
saved.activeIndex,
|
||||||
|
saved.roundNumber,
|
||||||
|
);
|
||||||
|
if (isDomainError(result)) throw new Error("Setup failed");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("rollInitiativeUseCase", () => {
|
||||||
|
it("returns domain error when combatant not found", () => {
|
||||||
|
const enc = createEncounter([]);
|
||||||
|
if (isDomainError(enc)) throw new Error("Setup failed");
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
|
||||||
|
const result = rollInitiativeUseCase(
|
||||||
|
store,
|
||||||
|
combatantId("unknown"),
|
||||||
|
10,
|
||||||
|
() => undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
expectError(result);
|
||||||
|
expect(result.code).toBe("combatant-not-found");
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error when combatant has no creature link", () => {
|
||||||
|
const enc = createEncounter([]);
|
||||||
|
if (isDomainError(enc)) throw new Error("Setup failed");
|
||||||
|
const store1 = stubEncounterStore(enc);
|
||||||
|
addCombatantUseCase(store1, combatantId("Fighter"), "Fighter");
|
||||||
|
|
||||||
|
const store = stubEncounterStore(requireSaved(store1.saved));
|
||||||
|
const result = rollInitiativeUseCase(
|
||||||
|
store,
|
||||||
|
combatantId("Fighter"),
|
||||||
|
10,
|
||||||
|
() => undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
expectError(result);
|
||||||
|
expect(result.code).toBe("no-creature-link");
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error when creature not found in getter", () => {
|
||||||
|
const enc = encounterWithCreatureLink("Goblin", GOBLIN_ID);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
|
||||||
|
const result = rollInitiativeUseCase(
|
||||||
|
store,
|
||||||
|
combatantId("Goblin"),
|
||||||
|
10,
|
||||||
|
() => undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
expectError(result);
|
||||||
|
expect(result.code).toBe("creature-not-found");
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calculates initiative from creature and saves", () => {
|
||||||
|
const creature = makeCreature();
|
||||||
|
const enc = encounterWithCreatureLink("Goblin", GOBLIN_ID);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
|
||||||
|
// Dex 14 -> modifier +2, CR 1/4 -> PB 2, initiativeProficiency 0
|
||||||
|
// So initiative modifier = 2 + 0*2 = 2
|
||||||
|
// Roll 10 + modifier 2 = 12
|
||||||
|
const result = rollInitiativeUseCase(
|
||||||
|
store,
|
||||||
|
combatantId("Goblin"),
|
||||||
|
10,
|
||||||
|
(id) => (id === GOBLIN_ID ? creature : undefined),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
expect(requireSaved(store.saved).combatants[0].initiative).toBe(12);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("applies initiative proficiency bonus correctly", () => {
|
||||||
|
// CR 5 -> PB 3, dex 16 -> mod +3, initiativeProficiency 1
|
||||||
|
// modifier = 3 + 1*3 = 6, roll 8 + 6 = 14
|
||||||
|
const creature = makeCreature({
|
||||||
|
abilities: {
|
||||||
|
str: 10,
|
||||||
|
dex: 16,
|
||||||
|
con: 10,
|
||||||
|
int: 10,
|
||||||
|
wis: 10,
|
||||||
|
cha: 10,
|
||||||
|
},
|
||||||
|
cr: "5",
|
||||||
|
initiativeProficiency: 1,
|
||||||
|
});
|
||||||
|
const enc = encounterWithCreatureLink("Monster", GOBLIN_ID);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
|
||||||
|
const result = rollInitiativeUseCase(
|
||||||
|
store,
|
||||||
|
combatantId("Monster"),
|
||||||
|
8,
|
||||||
|
(id) => (id === GOBLIN_ID ? creature : undefined),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
expect(requireSaved(store.saved).combatants[0].initiative).toBe(14);
|
||||||
|
});
|
||||||
|
});
|
||||||
388
packages/application/src/__tests__/use-cases.test.ts
Normal file
388
packages/application/src/__tests__/use-cases.test.ts
Normal file
@@ -0,0 +1,388 @@
|
|||||||
|
import {
|
||||||
|
type ConditionId,
|
||||||
|
combatantId,
|
||||||
|
createEncounter,
|
||||||
|
isDomainError,
|
||||||
|
playerCharacterId,
|
||||||
|
} from "@initiative/domain";
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { addCombatantUseCase } from "../add-combatant-use-case.js";
|
||||||
|
import { adjustHpUseCase } from "../adjust-hp-use-case.js";
|
||||||
|
import { advanceTurnUseCase } from "../advance-turn-use-case.js";
|
||||||
|
import { clearEncounterUseCase } from "../clear-encounter-use-case.js";
|
||||||
|
import { createPlayerCharacterUseCase } from "../create-player-character-use-case.js";
|
||||||
|
import { deletePlayerCharacterUseCase } from "../delete-player-character-use-case.js";
|
||||||
|
import { editCombatantUseCase } from "../edit-combatant-use-case.js";
|
||||||
|
import { editPlayerCharacterUseCase } from "../edit-player-character-use-case.js";
|
||||||
|
import { removeCombatantUseCase } from "../remove-combatant-use-case.js";
|
||||||
|
import { retreatTurnUseCase } from "../retreat-turn-use-case.js";
|
||||||
|
import { setAcUseCase } from "../set-ac-use-case.js";
|
||||||
|
import { setHpUseCase } from "../set-hp-use-case.js";
|
||||||
|
import { setInitiativeUseCase } from "../set-initiative-use-case.js";
|
||||||
|
import { toggleConcentrationUseCase } from "../toggle-concentration-use-case.js";
|
||||||
|
import { toggleConditionUseCase } from "../toggle-condition-use-case.js";
|
||||||
|
import {
|
||||||
|
requireSaved,
|
||||||
|
stubEncounterStore,
|
||||||
|
stubPlayerCharacterStore,
|
||||||
|
} from "./helpers.js";
|
||||||
|
|
||||||
|
const ID_A = combatantId("a");
|
||||||
|
|
||||||
|
function emptyEncounter() {
|
||||||
|
const result = createEncounter([]);
|
||||||
|
if (isDomainError(result)) throw new Error("Test setup failed");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function encounterWith(...names: string[]) {
|
||||||
|
let enc = emptyEncounter();
|
||||||
|
for (const name of names) {
|
||||||
|
const id = combatantId(name);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const result = addCombatantUseCase(store, id, name);
|
||||||
|
if (isDomainError(result)) throw new Error(`Setup failed: ${name}`);
|
||||||
|
enc = requireSaved(store.saved);
|
||||||
|
}
|
||||||
|
return enc;
|
||||||
|
}
|
||||||
|
|
||||||
|
function encounterWithHp(name: string, maxHp: number) {
|
||||||
|
const enc = encounterWith(name);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const id = combatantId(name);
|
||||||
|
setHpUseCase(store, id, maxHp);
|
||||||
|
return requireSaved(store.saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
function createPc(name: string) {
|
||||||
|
const store = stubPlayerCharacterStore([]);
|
||||||
|
const id = playerCharacterId("pc-1");
|
||||||
|
createPlayerCharacterUseCase(store, id, name, 15, 40, undefined, undefined);
|
||||||
|
return { id, characters: requireSaved(store.saved) };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("addCombatantUseCase", () => {
|
||||||
|
it("adds a combatant and saves", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = addCombatantUseCase(store, ID_A, "Goblin");
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
const saved = requireSaved(store.saved);
|
||||||
|
expect(saved.combatants).toHaveLength(1);
|
||||||
|
expect(saved.combatants[0].name).toBe("Goblin");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for empty name", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = addCombatantUseCase(store, ID_A, "");
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("adjustHpUseCase", () => {
|
||||||
|
it("adjusts HP and saves", () => {
|
||||||
|
const enc = encounterWithHp("Goblin", 10);
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const result = adjustHpUseCase(store, combatantId("Goblin"), -3);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
const saved = requireSaved(store.saved);
|
||||||
|
expect(saved.combatants[0].currentHp).toBe(7);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for unknown combatant", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = adjustHpUseCase(store, ID_A, -5);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("advanceTurnUseCase", () => {
|
||||||
|
it("advances turn and saves", () => {
|
||||||
|
const enc = encounterWith("A", "B");
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const result = advanceTurnUseCase(store);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
const saved = requireSaved(store.saved);
|
||||||
|
expect(saved.activeIndex).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error on empty encounter", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = advanceTurnUseCase(store);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("clearEncounterUseCase", () => {
|
||||||
|
it("clears encounter and saves", () => {
|
||||||
|
const enc = encounterWith("Goblin");
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const result = clearEncounterUseCase(store);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
const saved = requireSaved(store.saved);
|
||||||
|
expect(saved.combatants).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("editCombatantUseCase", () => {
|
||||||
|
it("edits combatant name and saves", () => {
|
||||||
|
const enc = encounterWith("Goblin");
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const result = editCombatantUseCase(
|
||||||
|
store,
|
||||||
|
combatantId("Goblin"),
|
||||||
|
"Hobgoblin",
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
const saved = requireSaved(store.saved);
|
||||||
|
expect(saved.combatants[0].name).toBe("Hobgoblin");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for unknown combatant", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = editCombatantUseCase(store, ID_A, "X");
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("removeCombatantUseCase", () => {
|
||||||
|
it("removes combatant and saves", () => {
|
||||||
|
const enc = encounterWith("Goblin");
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const result = removeCombatantUseCase(store, combatantId("Goblin"));
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
const saved = requireSaved(store.saved);
|
||||||
|
expect(saved.combatants).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for unknown combatant", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = removeCombatantUseCase(store, ID_A);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("retreatTurnUseCase", () => {
|
||||||
|
it("retreats turn and saves", () => {
|
||||||
|
const enc = encounterWith("A", "B");
|
||||||
|
const store1 = stubEncounterStore(enc);
|
||||||
|
advanceTurnUseCase(store1);
|
||||||
|
const store = stubEncounterStore(requireSaved(store1.saved));
|
||||||
|
const result = retreatTurnUseCase(store);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
expect(store.saved).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error on empty encounter", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = retreatTurnUseCase(store);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("setAcUseCase", () => {
|
||||||
|
it("sets AC and saves", () => {
|
||||||
|
const enc = encounterWith("Goblin");
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const result = setAcUseCase(store, combatantId("Goblin"), 15);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
expect(requireSaved(store.saved).combatants[0].ac).toBe(15);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for unknown combatant", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = setAcUseCase(store, ID_A, 15);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("setHpUseCase", () => {
|
||||||
|
it("sets max HP and saves", () => {
|
||||||
|
const enc = encounterWith("Goblin");
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const result = setHpUseCase(store, combatantId("Goblin"), 20);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
expect(requireSaved(store.saved).combatants[0].maxHp).toBe(20);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for unknown combatant", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = setHpUseCase(store, ID_A, 20);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("setInitiativeUseCase", () => {
|
||||||
|
it("sets initiative and saves", () => {
|
||||||
|
const enc = encounterWith("Goblin");
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const result = setInitiativeUseCase(store, combatantId("Goblin"), 15);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
expect(requireSaved(store.saved).combatants[0].initiative).toBe(15);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for unknown combatant", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = setInitiativeUseCase(store, ID_A, 15);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("toggleConcentrationUseCase", () => {
|
||||||
|
it("toggles concentration and saves", () => {
|
||||||
|
const enc = encounterWith("Wizard");
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const result = toggleConcentrationUseCase(store, combatantId("Wizard"));
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
expect(requireSaved(store.saved).combatants[0].isConcentrating).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for unknown combatant", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = toggleConcentrationUseCase(store, ID_A);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("toggleConditionUseCase", () => {
|
||||||
|
it("toggles condition and saves", () => {
|
||||||
|
const enc = encounterWith("Goblin");
|
||||||
|
const store = stubEncounterStore(enc);
|
||||||
|
const result = toggleConditionUseCase(
|
||||||
|
store,
|
||||||
|
combatantId("Goblin"),
|
||||||
|
"blinded" as ConditionId,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
expect(requireSaved(store.saved).combatants[0].conditions).toContain(
|
||||||
|
"blinded",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for unknown combatant", () => {
|
||||||
|
const store = stubEncounterStore(emptyEncounter());
|
||||||
|
const result = toggleConditionUseCase(
|
||||||
|
store,
|
||||||
|
ID_A,
|
||||||
|
"blinded" as ConditionId,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("createPlayerCharacterUseCase", () => {
|
||||||
|
it("creates a player character and saves", () => {
|
||||||
|
const store = stubPlayerCharacterStore([]);
|
||||||
|
const id = playerCharacterId("pc-1");
|
||||||
|
const result = createPlayerCharacterUseCase(
|
||||||
|
store,
|
||||||
|
id,
|
||||||
|
"Gandalf",
|
||||||
|
15,
|
||||||
|
40,
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
expect(requireSaved(store.saved)).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for invalid input", () => {
|
||||||
|
const store = stubPlayerCharacterStore([]);
|
||||||
|
const id = playerCharacterId("pc-1");
|
||||||
|
const result = createPlayerCharacterUseCase(
|
||||||
|
store,
|
||||||
|
id,
|
||||||
|
"",
|
||||||
|
15,
|
||||||
|
40,
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("deletePlayerCharacterUseCase", () => {
|
||||||
|
it("deletes a player character and saves", () => {
|
||||||
|
const { id, characters } = createPc("Gandalf");
|
||||||
|
const store = stubPlayerCharacterStore(characters);
|
||||||
|
const result = deletePlayerCharacterUseCase(store, id);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
expect(requireSaved(store.saved)).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for unknown character", () => {
|
||||||
|
const store = stubPlayerCharacterStore([]);
|
||||||
|
const result = deletePlayerCharacterUseCase(
|
||||||
|
store,
|
||||||
|
playerCharacterId("unknown"),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("editPlayerCharacterUseCase", () => {
|
||||||
|
it("edits a player character and saves", () => {
|
||||||
|
const { id, characters } = createPc("Gandalf");
|
||||||
|
const store = stubPlayerCharacterStore(characters);
|
||||||
|
const result = editPlayerCharacterUseCase(store, id, {
|
||||||
|
name: "Gandalf the White",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(false);
|
||||||
|
expect(requireSaved(store.saved)[0].name).toBe("Gandalf the White");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns domain error for unknown character", () => {
|
||||||
|
const store = stubPlayerCharacterStore([]);
|
||||||
|
const result = editPlayerCharacterUseCase(
|
||||||
|
store,
|
||||||
|
playerCharacterId("unknown"),
|
||||||
|
{ name: "X" },
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(isDomainError(result)).toBe(true);
|
||||||
|
expect(store.saved).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
36
packages/application/src/create-player-character-use-case.ts
Normal file
36
packages/application/src/create-player-character-use-case.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import {
|
||||||
|
createPlayerCharacter,
|
||||||
|
type DomainError,
|
||||||
|
type DomainEvent,
|
||||||
|
isDomainError,
|
||||||
|
type PlayerCharacterId,
|
||||||
|
} from "@initiative/domain";
|
||||||
|
import type { PlayerCharacterStore } from "./ports.js";
|
||||||
|
|
||||||
|
export function createPlayerCharacterUseCase(
|
||||||
|
store: PlayerCharacterStore,
|
||||||
|
id: PlayerCharacterId,
|
||||||
|
name: string,
|
||||||
|
ac: number,
|
||||||
|
maxHp: number,
|
||||||
|
color: string | undefined,
|
||||||
|
icon: string | undefined,
|
||||||
|
): DomainEvent[] | DomainError {
|
||||||
|
const characters = store.getAll();
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
characters,
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
ac,
|
||||||
|
maxHp,
|
||||||
|
color,
|
||||||
|
icon,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isDomainError(result)) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
store.save([...result.characters]);
|
||||||
|
return result.events;
|
||||||
|
}
|
||||||
23
packages/application/src/delete-player-character-use-case.ts
Normal file
23
packages/application/src/delete-player-character-use-case.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import {
|
||||||
|
type DomainError,
|
||||||
|
type DomainEvent,
|
||||||
|
deletePlayerCharacter,
|
||||||
|
isDomainError,
|
||||||
|
type PlayerCharacterId,
|
||||||
|
} from "@initiative/domain";
|
||||||
|
import type { PlayerCharacterStore } from "./ports.js";
|
||||||
|
|
||||||
|
export function deletePlayerCharacterUseCase(
|
||||||
|
store: PlayerCharacterStore,
|
||||||
|
id: PlayerCharacterId,
|
||||||
|
): DomainEvent[] | DomainError {
|
||||||
|
const characters = store.getAll();
|
||||||
|
const result = deletePlayerCharacter(characters, id);
|
||||||
|
|
||||||
|
if (isDomainError(result)) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
store.save([...result.characters]);
|
||||||
|
return result.events;
|
||||||
|
}
|
||||||
32
packages/application/src/edit-player-character-use-case.ts
Normal file
32
packages/application/src/edit-player-character-use-case.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import {
|
||||||
|
type DomainError,
|
||||||
|
type DomainEvent,
|
||||||
|
editPlayerCharacter,
|
||||||
|
isDomainError,
|
||||||
|
type PlayerCharacterId,
|
||||||
|
} from "@initiative/domain";
|
||||||
|
import type { PlayerCharacterStore } from "./ports.js";
|
||||||
|
|
||||||
|
interface EditFields {
|
||||||
|
readonly name?: string;
|
||||||
|
readonly ac?: number;
|
||||||
|
readonly maxHp?: number;
|
||||||
|
readonly color?: string | null;
|
||||||
|
readonly icon?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function editPlayerCharacterUseCase(
|
||||||
|
store: PlayerCharacterStore,
|
||||||
|
id: PlayerCharacterId,
|
||||||
|
fields: EditFields,
|
||||||
|
): DomainEvent[] | DomainError {
|
||||||
|
const characters = store.getAll();
|
||||||
|
const result = editPlayerCharacter(characters, id, fields);
|
||||||
|
|
||||||
|
if (isDomainError(result)) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
store.save([...result.characters]);
|
||||||
|
return result.events;
|
||||||
|
}
|
||||||
@@ -2,11 +2,21 @@ export { addCombatantUseCase } from "./add-combatant-use-case.js";
|
|||||||
export { adjustHpUseCase } from "./adjust-hp-use-case.js";
|
export { adjustHpUseCase } from "./adjust-hp-use-case.js";
|
||||||
export { advanceTurnUseCase } from "./advance-turn-use-case.js";
|
export { advanceTurnUseCase } from "./advance-turn-use-case.js";
|
||||||
export { clearEncounterUseCase } from "./clear-encounter-use-case.js";
|
export { clearEncounterUseCase } from "./clear-encounter-use-case.js";
|
||||||
|
export { createPlayerCharacterUseCase } from "./create-player-character-use-case.js";
|
||||||
|
export { deletePlayerCharacterUseCase } from "./delete-player-character-use-case.js";
|
||||||
export { editCombatantUseCase } from "./edit-combatant-use-case.js";
|
export { editCombatantUseCase } from "./edit-combatant-use-case.js";
|
||||||
export type { EncounterStore } from "./ports.js";
|
export { editPlayerCharacterUseCase } from "./edit-player-character-use-case.js";
|
||||||
|
export type {
|
||||||
|
BestiarySourceCache,
|
||||||
|
EncounterStore,
|
||||||
|
PlayerCharacterStore,
|
||||||
|
} from "./ports.js";
|
||||||
export { removeCombatantUseCase } from "./remove-combatant-use-case.js";
|
export { removeCombatantUseCase } from "./remove-combatant-use-case.js";
|
||||||
export { retreatTurnUseCase } from "./retreat-turn-use-case.js";
|
export { retreatTurnUseCase } from "./retreat-turn-use-case.js";
|
||||||
export { rollAllInitiativeUseCase } from "./roll-all-initiative-use-case.js";
|
export {
|
||||||
|
type RollAllResult,
|
||||||
|
rollAllInitiativeUseCase,
|
||||||
|
} from "./roll-all-initiative-use-case.js";
|
||||||
export { rollInitiativeUseCase } from "./roll-initiative-use-case.js";
|
export { rollInitiativeUseCase } from "./roll-initiative-use-case.js";
|
||||||
export { setAcUseCase } from "./set-ac-use-case.js";
|
export { setAcUseCase } from "./set-ac-use-case.js";
|
||||||
export { setHpUseCase } from "./set-hp-use-case.js";
|
export { setHpUseCase } from "./set-hp-use-case.js";
|
||||||
|
|||||||
@@ -1,6 +1,21 @@
|
|||||||
import type { Encounter } from "@initiative/domain";
|
import type {
|
||||||
|
Creature,
|
||||||
|
CreatureId,
|
||||||
|
Encounter,
|
||||||
|
PlayerCharacter,
|
||||||
|
} from "@initiative/domain";
|
||||||
|
|
||||||
export interface EncounterStore {
|
export interface EncounterStore {
|
||||||
get(): Encounter;
|
get(): Encounter;
|
||||||
save(encounter: Encounter): void;
|
save(encounter: Encounter): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface BestiarySourceCache {
|
||||||
|
getCreature(creatureId: CreatureId): Creature | undefined;
|
||||||
|
isSourceCached(sourceCode: string): boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PlayerCharacterStore {
|
||||||
|
getAll(): PlayerCharacter[];
|
||||||
|
save(characters: PlayerCharacter[]): void;
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,20 +10,29 @@ import {
|
|||||||
} from "@initiative/domain";
|
} from "@initiative/domain";
|
||||||
import type { EncounterStore } from "./ports.js";
|
import type { EncounterStore } from "./ports.js";
|
||||||
|
|
||||||
|
export interface RollAllResult {
|
||||||
|
events: DomainEvent[];
|
||||||
|
skippedNoSource: number;
|
||||||
|
}
|
||||||
|
|
||||||
export function rollAllInitiativeUseCase(
|
export function rollAllInitiativeUseCase(
|
||||||
store: EncounterStore,
|
store: EncounterStore,
|
||||||
rollDice: () => number,
|
rollDice: () => number,
|
||||||
getCreature: (id: CreatureId) => Creature | undefined,
|
getCreature: (id: CreatureId) => Creature | undefined,
|
||||||
): DomainEvent[] | DomainError {
|
): RollAllResult | DomainError {
|
||||||
let encounter = store.get();
|
let encounter = store.get();
|
||||||
const allEvents: DomainEvent[] = [];
|
const allEvents: DomainEvent[] = [];
|
||||||
|
let skippedNoSource = 0;
|
||||||
|
|
||||||
for (const combatant of encounter.combatants) {
|
for (const combatant of encounter.combatants) {
|
||||||
if (!combatant.creatureId) continue;
|
if (!combatant.creatureId) continue;
|
||||||
if (combatant.initiative !== undefined) continue;
|
if (combatant.initiative !== undefined) continue;
|
||||||
|
|
||||||
const creature = getCreature(combatant.creatureId);
|
const creature = getCreature(combatant.creatureId);
|
||||||
if (!creature) continue;
|
if (!creature) {
|
||||||
|
skippedNoSource++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const { modifier } = calculateInitiative({
|
const { modifier } = calculateInitiative({
|
||||||
dexScore: creature.abilities.dex,
|
dexScore: creature.abilities.dex,
|
||||||
@@ -47,5 +56,5 @@ export function rollAllInitiativeUseCase(
|
|||||||
}
|
}
|
||||||
|
|
||||||
store.save(encounter);
|
store.save(encounter);
|
||||||
return allEvents;
|
return { events: allEvents, skippedNoSource };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
|
|||||||
import { addCombatant } from "../add-combatant.js";
|
import { addCombatant } from "../add-combatant.js";
|
||||||
import type { Combatant, Encounter } from "../types.js";
|
import type { Combatant, Encounter } from "../types.js";
|
||||||
import { combatantId, isDomainError } from "../types.js";
|
import { combatantId, isDomainError } from "../types.js";
|
||||||
|
import { expectDomainError } from "./test-helpers.js";
|
||||||
|
|
||||||
// --- Helpers ---
|
// --- Helpers ---
|
||||||
|
|
||||||
@@ -112,20 +113,14 @@ describe("addCombatant", () => {
|
|||||||
const e = enc([A, B]);
|
const e = enc([A, B]);
|
||||||
const result = addCombatant(e, combatantId("x"), "");
|
const result = addCombatant(e, combatantId("x"), "");
|
||||||
|
|
||||||
expect(isDomainError(result)).toBe(true);
|
expectDomainError(result, "invalid-name");
|
||||||
if (isDomainError(result)) {
|
|
||||||
expect(result.code).toBe("invalid-name");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("scenario 6: whitespace-only name returns error", () => {
|
it("scenario 6: whitespace-only name returns error", () => {
|
||||||
const e = enc([A, B]);
|
const e = enc([A, B]);
|
||||||
const result = addCombatant(e, combatantId("x"), " ");
|
const result = addCombatant(e, combatantId("x"), " ");
|
||||||
|
|
||||||
expect(isDomainError(result)).toBe(true);
|
expectDomainError(result, "invalid-name");
|
||||||
if (isDomainError(result)) {
|
|
||||||
expect(result.code).toBe("invalid-name");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -146,12 +141,10 @@ describe("addCombatant", () => {
|
|||||||
for (const e of scenarios) {
|
for (const e of scenarios) {
|
||||||
const result = successResult(e, "new", "New");
|
const result = successResult(e, "new", "New");
|
||||||
const { combatants, activeIndex } = result.encounter;
|
const { combatants, activeIndex } = result.encounter;
|
||||||
if (combatants.length > 0) {
|
// After adding a combatant, list is always non-empty
|
||||||
|
expect(combatants.length).toBeGreaterThan(0);
|
||||||
expect(activeIndex).toBeGreaterThanOrEqual(0);
|
expect(activeIndex).toBeGreaterThanOrEqual(0);
|
||||||
expect(activeIndex).toBeLessThan(combatants.length);
|
expect(activeIndex).toBeLessThan(combatants.length);
|
||||||
} else {
|
|
||||||
expect(activeIndex).toBe(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -188,7 +181,7 @@ describe("addCombatant", () => {
|
|||||||
it("INV-7: new combatant is always appended at the end", () => {
|
it("INV-7: new combatant is always appended at the end", () => {
|
||||||
const e = enc([A, B]);
|
const e = enc([A, B]);
|
||||||
const { encounter } = successResult(e, "C", "C");
|
const { encounter } = successResult(e, "C", "C");
|
||||||
expect(encounter.combatants[encounter.combatants.length - 1]).toEqual({
|
expect(encounter.combatants.at(-1)).toEqual({
|
||||||
id: combatantId("C"),
|
id: combatantId("C"),
|
||||||
name: "C",
|
name: "C",
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
|
|||||||
import { adjustHp } from "../adjust-hp.js";
|
import { adjustHp } from "../adjust-hp.js";
|
||||||
import type { Combatant, Encounter } from "../types.js";
|
import type { Combatant, Encounter } from "../types.js";
|
||||||
import { combatantId, isDomainError } from "../types.js";
|
import { combatantId, isDomainError } from "../types.js";
|
||||||
|
import { expectDomainError } from "./test-helpers.js";
|
||||||
|
|
||||||
function makeCombatant(
|
function makeCombatant(
|
||||||
name: string,
|
name: string,
|
||||||
@@ -101,37 +102,25 @@ describe("adjustHp", () => {
|
|||||||
it("returns error for nonexistent combatant", () => {
|
it("returns error for nonexistent combatant", () => {
|
||||||
const e = enc([makeCombatant("A", { maxHp: 20, currentHp: 10 })]);
|
const e = enc([makeCombatant("A", { maxHp: 20, currentHp: 10 })]);
|
||||||
const result = adjustHp(e, combatantId("Z"), -1);
|
const result = adjustHp(e, combatantId("Z"), -1);
|
||||||
expect(isDomainError(result)).toBe(true);
|
expectDomainError(result, "combatant-not-found");
|
||||||
if (isDomainError(result)) {
|
|
||||||
expect(result.code).toBe("combatant-not-found");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns error when combatant has no HP tracking", () => {
|
it("returns error when combatant has no HP tracking", () => {
|
||||||
const e = enc([makeCombatant("A")]);
|
const e = enc([makeCombatant("A")]);
|
||||||
const result = adjustHp(e, combatantId("A"), -1);
|
const result = adjustHp(e, combatantId("A"), -1);
|
||||||
expect(isDomainError(result)).toBe(true);
|
expectDomainError(result, "no-hp-tracking");
|
||||||
if (isDomainError(result)) {
|
|
||||||
expect(result.code).toBe("no-hp-tracking");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns error for zero delta", () => {
|
it("returns error for zero delta", () => {
|
||||||
const e = enc([makeCombatant("A", { maxHp: 20, currentHp: 10 })]);
|
const e = enc([makeCombatant("A", { maxHp: 20, currentHp: 10 })]);
|
||||||
const result = adjustHp(e, combatantId("A"), 0);
|
const result = adjustHp(e, combatantId("A"), 0);
|
||||||
expect(isDomainError(result)).toBe(true);
|
expectDomainError(result, "zero-delta");
|
||||||
if (isDomainError(result)) {
|
|
||||||
expect(result.code).toBe("zero-delta");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns error for non-integer delta", () => {
|
it("returns error for non-integer delta", () => {
|
||||||
const e = enc([makeCombatant("A", { maxHp: 20, currentHp: 10 })]);
|
const e = enc([makeCombatant("A", { maxHp: 20, currentHp: 10 })]);
|
||||||
const result = adjustHp(e, combatantId("A"), 1.5);
|
const result = adjustHp(e, combatantId("A"), 1.5);
|
||||||
expect(isDomainError(result)).toBe(true);
|
expectDomainError(result, "invalid-delta");
|
||||||
if (isDomainError(result)) {
|
|
||||||
expect(result.code).toBe("invalid-delta");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
createEncounter,
|
createEncounter,
|
||||||
type Encounter,
|
type Encounter,
|
||||||
} from "../types.js";
|
} from "../types.js";
|
||||||
|
import { expectDomainError } from "./test-helpers.js";
|
||||||
|
|
||||||
// --- Helpers ---
|
// --- Helpers ---
|
||||||
|
|
||||||
@@ -150,10 +151,7 @@ describe("advanceTurn", () => {
|
|||||||
};
|
};
|
||||||
const result = advanceTurn(enc);
|
const result = advanceTurn(enc);
|
||||||
|
|
||||||
expect(isDomainError(result)).toBe(true);
|
expectDomainError(result, "invalid-encounter");
|
||||||
if (isDomainError(result)) {
|
|
||||||
expect(result.code).toBe("invalid-encounter");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("scenario 8: three advances on [A,B,C] completes a full round cycle", () => {
|
it("scenario 8: three advances on [A,B,C] completes a full round cycle", () => {
|
||||||
@@ -169,9 +167,9 @@ describe("advanceTurn", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("invariants", () => {
|
describe("invariants", () => {
|
||||||
it("INV-1: createEncounter rejects empty combatant list", () => {
|
it("INV-1: createEncounter accepts empty combatant list", () => {
|
||||||
const result = createEncounter([]);
|
const result = createEncounter([]);
|
||||||
expect(isDomainError(result)).toBe(true);
|
expect(isDomainError(result)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("INV-2: activeIndex always in bounds across all scenarios", () => {
|
it("INV-2: activeIndex always in bounds across all scenarios", () => {
|
||||||
|
|||||||
244
packages/domain/src/__tests__/create-player-character.test.ts
Normal file
244
packages/domain/src/__tests__/create-player-character.test.ts
Normal file
@@ -0,0 +1,244 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { createPlayerCharacter } from "../create-player-character.js";
|
||||||
|
import type { PlayerCharacter } from "../player-character-types.js";
|
||||||
|
import { playerCharacterId } from "../player-character-types.js";
|
||||||
|
import { isDomainError } from "../types.js";
|
||||||
|
import { expectDomainError } from "./test-helpers.js";
|
||||||
|
|
||||||
|
const id = playerCharacterId("pc-1");
|
||||||
|
|
||||||
|
function success(
|
||||||
|
characters: readonly PlayerCharacter[],
|
||||||
|
name: string,
|
||||||
|
ac: number,
|
||||||
|
maxHp: number,
|
||||||
|
color = "blue",
|
||||||
|
icon = "sword",
|
||||||
|
) {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
characters,
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
ac,
|
||||||
|
maxHp,
|
||||||
|
color,
|
||||||
|
icon,
|
||||||
|
);
|
||||||
|
if (isDomainError(result)) {
|
||||||
|
throw new Error(`Expected success, got error: ${result.message}`);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("createPlayerCharacter", () => {
|
||||||
|
it("creates a valid player character", () => {
|
||||||
|
const { characters, events } = success(
|
||||||
|
[],
|
||||||
|
"Aragorn",
|
||||||
|
16,
|
||||||
|
120,
|
||||||
|
"green",
|
||||||
|
"shield",
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(characters).toHaveLength(1);
|
||||||
|
expect(characters[0]).toEqual({
|
||||||
|
id,
|
||||||
|
name: "Aragorn",
|
||||||
|
ac: 16,
|
||||||
|
maxHp: 120,
|
||||||
|
color: "green",
|
||||||
|
icon: "shield",
|
||||||
|
});
|
||||||
|
expect(events).toEqual([
|
||||||
|
{
|
||||||
|
type: "PlayerCharacterCreated",
|
||||||
|
playerCharacterId: id,
|
||||||
|
name: "Aragorn",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("trims whitespace from name", () => {
|
||||||
|
const { characters } = success([], " Gandalf ", 12, 80);
|
||||||
|
expect(characters[0].name).toBe("Gandalf");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("appends to existing characters", () => {
|
||||||
|
const existing: PlayerCharacter = {
|
||||||
|
id: playerCharacterId("pc-0"),
|
||||||
|
name: "Legolas",
|
||||||
|
ac: 14,
|
||||||
|
maxHp: 90,
|
||||||
|
color: "green",
|
||||||
|
icon: "eye",
|
||||||
|
};
|
||||||
|
const { characters } = success([existing], "Gimli", 18, 100, "red", "axe");
|
||||||
|
expect(characters).toHaveLength(2);
|
||||||
|
expect(characters[0]).toEqual(existing);
|
||||||
|
expect(characters[1].name).toBe("Gimli");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects empty name", () => {
|
||||||
|
const result = createPlayerCharacter([], id, "", 10, 50, "blue", "sword");
|
||||||
|
expectDomainError(result, "invalid-name");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects whitespace-only name", () => {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
[],
|
||||||
|
id,
|
||||||
|
" ",
|
||||||
|
10,
|
||||||
|
50,
|
||||||
|
"blue",
|
||||||
|
"sword",
|
||||||
|
);
|
||||||
|
expectDomainError(result, "invalid-name");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects negative AC", () => {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
[],
|
||||||
|
id,
|
||||||
|
"Test",
|
||||||
|
-1,
|
||||||
|
50,
|
||||||
|
"blue",
|
||||||
|
"sword",
|
||||||
|
);
|
||||||
|
expectDomainError(result, "invalid-ac");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects non-integer AC", () => {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
[],
|
||||||
|
id,
|
||||||
|
"Test",
|
||||||
|
10.5,
|
||||||
|
50,
|
||||||
|
"blue",
|
||||||
|
"sword",
|
||||||
|
);
|
||||||
|
expectDomainError(result, "invalid-ac");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("allows AC of 0", () => {
|
||||||
|
const { characters } = success([], "Test", 0, 50);
|
||||||
|
expect(characters[0].ac).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects maxHp of 0", () => {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
[],
|
||||||
|
id,
|
||||||
|
"Test",
|
||||||
|
10,
|
||||||
|
0,
|
||||||
|
"blue",
|
||||||
|
"sword",
|
||||||
|
);
|
||||||
|
expectDomainError(result, "invalid-max-hp");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects negative maxHp", () => {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
[],
|
||||||
|
id,
|
||||||
|
"Test",
|
||||||
|
10,
|
||||||
|
-5,
|
||||||
|
"blue",
|
||||||
|
"sword",
|
||||||
|
);
|
||||||
|
expectDomainError(result, "invalid-max-hp");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects non-integer maxHp", () => {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
[],
|
||||||
|
id,
|
||||||
|
"Test",
|
||||||
|
10,
|
||||||
|
50.5,
|
||||||
|
"blue",
|
||||||
|
"sword",
|
||||||
|
);
|
||||||
|
expectDomainError(result, "invalid-max-hp");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects invalid color", () => {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
[],
|
||||||
|
id,
|
||||||
|
"Test",
|
||||||
|
10,
|
||||||
|
50,
|
||||||
|
"neon",
|
||||||
|
"sword",
|
||||||
|
);
|
||||||
|
expectDomainError(result, "invalid-color");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects invalid icon", () => {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
[],
|
||||||
|
id,
|
||||||
|
"Test",
|
||||||
|
10,
|
||||||
|
50,
|
||||||
|
"blue",
|
||||||
|
"banana",
|
||||||
|
);
|
||||||
|
expectDomainError(result, "invalid-icon");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("allows undefined color", () => {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
[],
|
||||||
|
id,
|
||||||
|
"Test",
|
||||||
|
10,
|
||||||
|
50,
|
||||||
|
undefined,
|
||||||
|
"sword",
|
||||||
|
);
|
||||||
|
if (isDomainError(result)) throw new Error(result.message);
|
||||||
|
expect(result.characters[0].color).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("allows undefined icon", () => {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
[],
|
||||||
|
id,
|
||||||
|
"Test",
|
||||||
|
10,
|
||||||
|
50,
|
||||||
|
"blue",
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
if (isDomainError(result)) throw new Error(result.message);
|
||||||
|
expect(result.characters[0].icon).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("allows both color and icon undefined", () => {
|
||||||
|
const result = createPlayerCharacter(
|
||||||
|
[],
|
||||||
|
id,
|
||||||
|
"Test",
|
||||||
|
10,
|
||||||
|
50,
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
if (isDomainError(result)) throw new Error(result.message);
|
||||||
|
expect(result.characters[0].color).toBeUndefined();
|
||||||
|
expect(result.characters[0].icon).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("emits exactly one event on success", () => {
|
||||||
|
const { events } = success([], "Test", 10, 50);
|
||||||
|
expect(events).toHaveLength(1);
|
||||||
|
expect(events[0].type).toBe("PlayerCharacterCreated");
|
||||||
|
});
|
||||||
|
});
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user