Skip to content

[033-pig-ipc-api-layer]: Extract pig/drag IPC into api/ layer - #39

Merged
archae0pteryx merged 2 commits into
mainfrom
chore/pig-ipc-api-layer
May 5, 2026
Merged

[033-pig-ipc-api-layer]: Extract pig/drag IPC into api/ layer#39
archae0pteryx merged 2 commits into
mainfrom
chore/pig-ipc-api-layer

Conversation

@archae0pteryx

@archae0pteryx archae0pteryx commented May 4, 2026

Copy link
Copy Markdown
Contributor

Closes issues/033-pig-ipc-api-layer.md

Completion promise: No invoke or listen calls remain in src/components/ or src/hooks/; all pig/drag IPC lives in src/api/pig.ts.

Summary

  • New src/api/pig.ts exports four typed wrappers: setPigDragActive, updatePigRects, subscribeGatherPigs, subscribeDisplayRegion
  • New src/types/pig.ts shares SpawnRegion and PigHitRect so api/ does not depend on hooks/
  • App.tsx and usePigMovement.ts drop direct tauri invoke/listen; subscriptions wrap a no-op fallback so cleanup cannot throw an unhandled rejection on strict-mode unmount
  • Drop stale ralph/ Taskfile include left over from dabed05

Test plan

  • task check green
  • No invoke/listen in production code under src/components or src/hooks (remaining grep hits are test mocks)

Summary by CodeRabbit

  • Refactor

    • Moved pig-related native interactions into a dedicated client API and centralized related type definitions.
    • Updated pig movement hook to expose a drag-control handler and subscribe to native pig/gathering events.
  • Tests

    • Aligned component tests with the updated pig movement API.
  • Chores

    • Removed an unused taskfile inclusion from build configuration.

@coderabbitai

coderabbitai Bot commented May 4, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 320a081d-01a7-4633-944d-f4081097d589

📥 Commits

Reviewing files that changed from the base of the PR and between e8d3b75 and 71f03b1.

📒 Files selected for processing (3)
  • src/components/App.test.tsx
  • src/components/App.tsx
  • src/hooks/usePigMovement.ts

📝 Walkthrough

Walkthrough

This PR extracts Tauri IPC (invoke/listen) into a frontend API module (src/api/pig.ts), centralizes pig-related types in src/types/pig.ts, and updates usePigMovement and App.tsx to use the new API functions and subscription helpers. Taskfile.yaml drops an external include.

Changes

Pig API Abstraction

Layer / File(s) Summary
Type Foundation
src/types/pig.ts
Adds SpawnRegion and PigHitRect interfaces for pig geometry.
API Wrapper
src/api/pig.ts
Introduces Unsubscribe type; adds setPigDragActive, updatePigRects, subscribeGatherPigs, and subscribeDisplayRegion which wrap Tauri invoke and listen.
Hook Implementation
src/hooks/usePigMovement.ts
Re-exports pig types; replaces invoke("update_pig_rects", ...) with updatePigRects(...); adds setDragActive that calls setPigDragActive; replaces inline listen usage with subscribeGatherPigs/subscribeDisplayRegion effects and updates PigMovementResult.
Component Integration & Tests
src/components/App.tsx, src/components/App.test.tsx
Removes direct Tauri wiring and event listeners from App.tsx; wires PigSprite.onSetDragActive to hook setDragActive; updates test mock to provide setDragActive.

Task Configuration

Layer / File(s) Summary
Build Configuration
Taskfile.yaml
Removes includes entry that referenced ralph/Taskfile.yaml.

Sequence Diagram(s)

sequenceDiagram
  participant App as App (React)
  participant Hook as usePigMovement
  participant API as src/api/pig.ts
  participant Rust as Tauri/Rust

  App->>Hook: user interactions (drag start/move/end)
  Hook->>API: updatePigRects(rects)
  API->>Rust: invoke update_pig_rects
  Rust-->>API: ack/event responses
  Rust->>API: emit gather-pigs / display-region events
  API->>Hook: subscribed callbacks invoked
  Hook->>App: update local state (pigs / region)
  App->>Hook: setDragActive(active)
  Hook->>API: setPigDragActive(active)
  API->>Rust: invoke set_pig_drag_active
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

  • killallgit/adhd-ranch#14: Related frontend API and Rust command boundary changes (update_pig_rects, set_pig_drag_active) that align with this PR.
  • killallgit/adhd-ranch#20: Modifies usePigMovement.ts and pig hit-rect handling—closely related to these hook changes.
  • killallgit/adhd-ranch#22: Also updates usePigMovement.ts API/behavior, overlapping with this PR's hook adjustments.

Poem

🐰
Pigs in fields no longer roam alone,
Types in one burrow, APIs find home.
Hooks and App now call through one door,
Clean wiring hops and bugs hop no more. 🥕✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main refactoring objective: extracting pig/drag IPC calls into a dedicated API layer in src/api/, which aligns with all the primary changes (new pig.ts API module, removal of direct Tauri calls from App.tsx and usePigMovement.ts).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/pig-ipc-api-layer

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/api/pig.ts`:
- Line 3: The import of PigHitRect and SpawnRegion in api/pig.ts creates an
inverted dependency on the hooks layer; extract these interfaces into a new
shared types module (e.g., types/pig) that declares the SpawnRegion and
PigHitRect interfaces, then update api/pig.ts and usePigMovement to import
PigHitRect and SpawnRegion from that shared module instead of from
usePigMovement so the api layer no longer depends on the hooks layer.

In `@src/components/App.tsx`:
- Around line 33-36: The effect currently assigns unsubPromise =
subscribeGatherPigs(gather) (and similarly subscribeDisplayRegion) and returns a
cleanup that assumes the promise resolves; if the promise rejects this yields
unhandled rejections. Fix by handling promise rejections: when creating
unsubPromise (from subscribeGatherPigs and subscribeDisplayRegion) attach a
.catch handler or use an async IIFE with try/catch to log the error and set a
safe no-op unsubscribe; ensure the cleanup calls a defined function (e.g., a
no-op) if the subscription failed so unsubPromise.then(unsub => unsub()) cannot
throw. Reference subscribeGatherPigs, subscribeDisplayRegion and the
unsubPromise/cleanup logic in the effect.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 35cce2cb-fe2b-4006-b9fc-826cbaa2c8db

📥 Commits

Reviewing files that changed from the base of the PR and between 287eab2 and f34c735.

📒 Files selected for processing (3)
  • src/api/pig.ts
  • src/components/App.tsx
  • src/hooks/usePigMovement.ts

Comment thread src/api/pig.ts Outdated
Comment thread src/components/App.tsx Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/components/App.tsx`:
- Around line 28-46: Extract IPC side-effects and related state from App into a
new hook (e.g., usePigMovement): move the logic that calls setPigDragActive
(used by handleSetDragActive), subscribeGatherPigs(gather) and
subscribeDisplayRegion(setRegion) into the hook so the hook owns
subscribing/unsubscribing and error fallback handling (preserve the catch(() =>
() => {}) pattern and unsubPromise.then(unsub => unsub())); have the hook manage
any local state (gather/region) and expose a stable setDragActive callback
(wrapping setPigDragActive with proper error handling) and the region/gather
values; then update App to be view-only by removing direct calls to
setPigDragActive/subscribe* and using usePigMovement to get the handler and
state.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 17c07d3c-1f7e-402f-be3c-6cfcfea0d84f

📥 Commits

Reviewing files that changed from the base of the PR and between f34c735 and fa1e615.

📒 Files selected for processing (5)
  • ralph/PLAN.md
  • src/api/pig.ts
  • src/components/App.tsx
  • src/hooks/usePigMovement.ts
  • src/types/pig.ts

Comment thread src/components/App.tsx Outdated
Closes issues/033-pig-ipc-api-layer.md

Completion promise: No invoke or listen calls remain in
src/components/ or src/hooks/; all pig/drag IPC lives in src/api/pig.ts.

- New src/api/pig.ts: setPigDragActive, updatePigRects,
  subscribeGatherPigs, subscribeDisplayRegion typed wrappers
- New src/types/pig.ts: SpawnRegion, PigHitRect (shared so api/ does
  not import from hooks/)
- App.tsx + usePigMovement.ts: drop tauri invoke/listen imports, use
  pig api wrappers; subscriptions wrap a no-op fallback so cleanup
  cannot throw an unhandled rejection on strict-mode unmount
- Drop stale ralph/ Taskfile include left over from dabed05
@archae0pteryx
archae0pteryx force-pushed the chore/pig-ipc-api-layer branch from fa1e615 to e8d3b75 Compare May 5, 2026 02:29

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
src/components/App.tsx (1)

4-46: 🛠️ Refactor suggestion | 🟠 Major | 🏗️ Heavy lift

Move pig API side-effects out of App so the component stays view-only.

App still owns I/O (setPigDragActive and subscribe* effects). That responsibility should live in usePigMovement (or a dedicated hook), with App consuming only state/callbacks.

As per coding guidelines src/components/**/*.{ts,tsx} should be view-only with no direct I/O, and src/hooks/**/*.{ts,tsx} should handle state + effects and call api/ clients.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/App.tsx` around lines 4 - 46, App currently performs I/O
(calls setPigDragActive and calls subscribeGatherPigs/subscribeDisplayRegion in
useEffect), which must be moved into the hook layer; remove handleSetDragActive
and the two subscribe useEffect blocks from App and instead implement those
side-effects inside usePigMovement (or a new dedicated hook) so the hook: 1)
watches drag state and calls setPigDragActive(active) (exposing only
startDrag/moveDrag/endDrag or a setter, not performing the API call in App), 2)
calls subscribeGatherPigs(gather) and manages the unsubscribe promise internally
(accepting the gather callback or using an internal gather handler), and 3)
calls subscribeDisplayRegion(setRegion) and handles unsubscribe internally;
update usePigMovement's signature to accept/return the necessary callbacks/state
(e.g., accept focuses and selectedId and return pigs, startDrag/moveDrag/endDrag
and any setter needed) so App becomes view-only and no longer imports or calls
setPigDragActive, subscribeGatherPigs, or subscribeDisplayRegion.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Duplicate comments:
In `@src/components/App.tsx`:
- Around line 4-46: App currently performs I/O (calls setPigDragActive and calls
subscribeGatherPigs/subscribeDisplayRegion in useEffect), which must be moved
into the hook layer; remove handleSetDragActive and the two subscribe useEffect
blocks from App and instead implement those side-effects inside usePigMovement
(or a new dedicated hook) so the hook: 1) watches drag state and calls
setPigDragActive(active) (exposing only startDrag/moveDrag/endDrag or a setter,
not performing the API call in App), 2) calls subscribeGatherPigs(gather) and
manages the unsubscribe promise internally (accepting the gather callback or
using an internal gather handler), and 3) calls
subscribeDisplayRegion(setRegion) and handles unsubscribe internally; update
usePigMovement's signature to accept/return the necessary callbacks/state (e.g.,
accept focuses and selectedId and return pigs, startDrag/moveDrag/endDrag and
any setter needed) so App becomes view-only and no longer imports or calls
setPigDragActive, subscribeGatherPigs, or subscribeDisplayRegion.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: ef1eb248-9fb6-45ac-a729-797a0bc9e4e3

📥 Commits

Reviewing files that changed from the base of the PR and between fa1e615 and e8d3b75.

📒 Files selected for processing (5)
  • Taskfile.yaml
  • src/api/pig.ts
  • src/components/App.tsx
  • src/hooks/usePigMovement.ts
  • src/types/pig.ts
💤 Files with no reviewable changes (1)
  • Taskfile.yaml

App was still calling setPigDragActive and subscribing to gather-pigs /
display-region directly. CLAUDE.md requires components to be view-only
and hooks to own state + effects. Hook now exposes setDragActive and
manages both subscriptions internally.
@archae0pteryx
archae0pteryx merged commit 9dc7ec1 into main May 5, 2026
2 checks passed
archae0pteryx added a commit that referenced this pull request May 5, 2026
- CHANGELOG: Unreleased entries for PRs #39 (033), #40 (034), #41
  (035), #42 (cleanup), and the Taskfile ralph-include fix
- CONTEXT: add TaskText and DomainError to the ubiquitous language
- issues/README: mark 033/034/035 as in-flight (PR open)
archae0pteryx added a commit that referenced this pull request May 5, 2026
- CHANGELOG: Unreleased entries for PRs #39 (033), #40 (034), #41
  (035), #42 (cleanup), and the Taskfile ralph-include fix
- CONTEXT: add TaskText and DomainError to the ubiquitous language
- issues/README: mark 033/034/035 as in-flight (PR open)
archae0pteryx added a commit that referenced this pull request May 5, 2026
- CHANGELOG: Unreleased entries for PRs #39 (033), #40 (034), #41
  (035), #42 (cleanup), and the Taskfile ralph-include fix
- CONTEXT: add TaskText and DomainError to the ubiquitous language
- issues/README: mark 033/034/035 as in-flight (PR open)
archae0pteryx added a commit that referenced this pull request May 5, 2026
…tus (#43)

- CHANGELOG: Unreleased entries for PRs #39 (033), #40 (034), #41
  (035), #42 (cleanup), and the Taskfile ralph-include fix
- CONTEXT: add TaskText and DomainError to the ubiquitous language
- issues/README: mark 033/034/035 as in-flight (PR open)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant