[PR #883] [MERGED] feat(game): drag-and-swap inventory items in Game Mode #4818

Closed
opened 2026-10-22 04:10:54 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/883
Author: @Romuromylus
Created: 5/15/2026
Status: ✅ Merged
Merged: 5/15/2026
Merged by: @Romuromylus

Base: staging ← Head: feat/inventory-drag-swap


📝 Commits (3)

  • ad64535 feat(game): drag-and-swap inventory items in Game Mode
  • 3449319 refactor(game-inventory): migrate drag-swap from HTML5 DnD to @dnd-kit/core
  • 6ee5ce6 fix(game-inventory): guard reorder rollback against stale revert

📊 Changes

4 files changed (+198 additions, -39 deletions)

View changed files

📝 packages/client/package.json (+1 -0)
📝 packages/client/src/components/game/GameInventory.tsx (+127 -39)
📝 packages/client/src/components/game/GameSurface.tsx (+33 -0)
📝 pnpm-lock.yaml (+37 -0)

📄 Description

Linked issue

Closes #861

Why this change

Game Mode players currently have no way to manually organize their inventory — add/rename/use/remove exist, but the slot order is whatever order items were acquired in. The issue (filed from Discord by drakonbane, triaged normal-scope by @cha1latte) asks for drag-and-swap so the grid can be tidied.

Per the triage note I scoped this slice to "drag/swap within the current inventory grid first"; cross-page reorder is deliberately deferred.

What changed

  • packages/client/src/components/game/GameInventory.tsx — filled slots are draggable via @dnd-kit/core. Dragging item A onto item B (within the current page) triggers a swap; empty slots are non-droppable. While dragging, the source slot dims and the hovered target gets an amber ring for visual feedback. Refactored the slot rendering into an InventorySlot subcomponent because useDraggable/useDroppable are hooks. New optional onReorderItem(fromIndex, toIndex) prop; behavior is unchanged when the prop is omitted.
  • packages/client/src/components/game/GameSurface.tsx — new handleReorderInventoryItem wired to the prop. Swaps two entries in gameInventory, applies the change optimistically, and persists via the existing updateChatMetadata mutation. On error, rolls back the local state and surfaces a toast.
  • packages/client/package.json — adds @dnd-kit/core@6.3.1 (MIT, ~12KB gzipped, zero GH advisories, ~16M weekly downloads, transitive footprint is only tslib via @dnd-kit/utilities + @dnd-kit/accessibility).

History note: the first commit used native HTML5 drag-and-drop with no new dependency. After review feedback flagged that HTML5 DnD events don't fire on iOS Safari, the second commit migrated to @dnd-kit/core to get real touch support. The MouseSensor is configured with a 4px distance threshold so quick clicks still select, and the TouchSensor uses a 200ms hold + 5px tolerance so swipe-to-scroll still works on mobile.

Design choices worth flagging:

  • No new server endpoint. gameInventory is array-ordered and normalizeGameInventoryItems preserves order, so the existing PATCH /chats/:id/metadata route round-trips a reorder unchanged.
  • Swap semantics, not insertion-shift. Drop on item B swaps A↔B. Matches the issue wording ("drag/swap") and avoids the edge-case complexity of "drop on empty slot → shift others right".
  • playerStats.inventory left untouched. That array is the agent-facing detailed inventory (with description, location). The agent indexes by name; its array order is not observable. Skipping the mirror avoids a second PATCH and a second rollback path.
  • aria-grabbed replaced with aria-pressed. aria-grabbed was deprecated in ARIA 1.1; current screen readers ignore it.
  • touch-action: none only on draggable slots. Required for TouchSensor activation to win over browser scroll gestures. The modal background, pagination row, and action bar remain pannable.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Desktop / mouse:

  • Manually verify dragging item A onto item B swaps their positions in the grid.
  • Manually verify the new order persists after closing/reopening the inventory panel (round-trip via chat metadata).
  • Manually verify the new order survives a full page reload (round-trip via server load).
  • Manually verify dragging onto an empty slot does nothing (no shift, no swap, no PATCH).
  • Manually verify clicking a slot (no mouse movement) still selects it — the 4px MouseSensor threshold means short clicks should not trigger drag.
  • Manually verify the source slot dims and the drop target shows a ring during drag.
  • Manually verify clicking a slot still selects it for rename / increment / decrement / use (no regression on existing interactions).
  • Manually verify a network error during the swap surfaces a toast and the grid snaps back to the previous order.

Mobile / touch:

  • Manually verify a 200ms+ press on a filled slot initiates drag (the TouchSensor delay).
  • Manually verify a quick tap on a filled slot just selects it (no accidental drag).
  • Manually verify swipe gestures over the slot grid don't initiate drag within the 200ms delay window — scroll-style touches should pass through.
  • Manually verify the drag works on at least one iOS Safari device and one Android Chrome device, since HTML5 DnD historically didn't fire on iOS Safari and that was the reason for switching to @dnd-kit/core.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/883 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/15/2026 **Status:** ✅ Merged **Merged:** 5/15/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `staging` ← **Head:** `feat/inventory-drag-swap` --- ### 📝 Commits (3) - [`ad64535`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ad64535c74fd617ddb8563dea42846f1c17488fa) feat(game): drag-and-swap inventory items in Game Mode - [`3449319`](https://github.com/Pasta-Devs/Marinara-Engine/commit/34493192f653062dd28c4d48fbe9336340196e12) refactor(game-inventory): migrate drag-swap from HTML5 DnD to @dnd-kit/core - [`6ee5ce6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6ee5ce6b019baf7374ddda8e6a2ac32306891a12) fix(game-inventory): guard reorder rollback against stale revert ### 📊 Changes **4 files changed** (+198 additions, -39 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/package.json` (+1 -0) 📝 `packages/client/src/components/game/GameInventory.tsx` (+127 -39) 📝 `packages/client/src/components/game/GameSurface.tsx` (+33 -0) 📝 `pnpm-lock.yaml` (+37 -0) </details> ### 📄 Description ## Linked issue Closes #861 ## Why this change Game Mode players currently have no way to manually organize their inventory — add/rename/use/remove exist, but the slot order is whatever order items were acquired in. The issue (filed from Discord by drakonbane, triaged `normal-scope` by @cha1latte) asks for drag-and-swap so the grid can be tidied. Per the triage note I scoped this slice to "drag/swap within the current inventory grid first"; cross-page reorder is deliberately deferred. ## What changed - `packages/client/src/components/game/GameInventory.tsx` — filled slots are draggable via `@dnd-kit/core`. Dragging item A onto item B (within the current page) triggers a swap; empty slots are non-droppable. While dragging, the source slot dims and the hovered target gets an amber ring for visual feedback. Refactored the slot rendering into an `InventorySlot` subcomponent because `useDraggable`/`useDroppable` are hooks. New optional `onReorderItem(fromIndex, toIndex)` prop; behavior is unchanged when the prop is omitted. - `packages/client/src/components/game/GameSurface.tsx` — new `handleReorderInventoryItem` wired to the prop. Swaps two entries in `gameInventory`, applies the change optimistically, and persists via the existing `updateChatMetadata` mutation. On error, rolls back the local state and surfaces a toast. - `packages/client/package.json` — adds `@dnd-kit/core@6.3.1` (MIT, ~12KB gzipped, zero GH advisories, ~16M weekly downloads, transitive footprint is only `tslib` via `@dnd-kit/utilities` + `@dnd-kit/accessibility`). History note: the first commit used native HTML5 drag-and-drop with no new dependency. After review feedback flagged that HTML5 DnD events don't fire on iOS Safari, the second commit migrated to `@dnd-kit/core` to get real touch support. The `MouseSensor` is configured with a 4px distance threshold so quick clicks still select, and the `TouchSensor` uses a 200ms hold + 5px tolerance so swipe-to-scroll still works on mobile. Design choices worth flagging: - **No new server endpoint.** `gameInventory` is array-ordered and `normalizeGameInventoryItems` preserves order, so the existing `PATCH /chats/:id/metadata` route round-trips a reorder unchanged. - **Swap semantics, not insertion-shift.** Drop on item B swaps A↔B. Matches the issue wording ("drag/swap") and avoids the edge-case complexity of "drop on empty slot → shift others right". - **`playerStats.inventory` left untouched.** That array is the agent-facing detailed inventory (with `description`, `location`). The agent indexes by name; its array order is not observable. Skipping the mirror avoids a second PATCH and a second rollback path. - **`aria-grabbed` replaced with `aria-pressed`.** `aria-grabbed` was deprecated in ARIA 1.1; current screen readers ignore it. - **`touch-action: none` only on draggable slots.** Required for `TouchSensor` activation to win over browser scroll gestures. The modal background, pagination row, and action bar remain pannable. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes Desktop / mouse: - Manually verify dragging item A onto item B swaps their positions in the grid. - Manually verify the new order persists after closing/reopening the inventory panel (round-trip via chat metadata). - Manually verify the new order survives a full page reload (round-trip via server load). - Manually verify dragging onto an empty slot does nothing (no shift, no swap, no PATCH). - Manually verify clicking a slot (no mouse movement) still selects it — the 4px MouseSensor threshold means short clicks should not trigger drag. - Manually verify the source slot dims and the drop target shows a ring during drag. - Manually verify clicking a slot still selects it for rename / increment / decrement / use (no regression on existing interactions). - Manually verify a network error during the swap surfaces a toast and the grid snaps back to the previous order. Mobile / touch: - Manually verify a 200ms+ press on a filled slot initiates drag (the TouchSensor delay). - Manually verify a quick tap on a filled slot just selects it (no accidental drag). - Manually verify swipe gestures over the slot grid don't initiate drag within the 200ms delay window — scroll-style touches should pass through. - Manually verify the drag works on at least one iOS Safari device and one Android Chrome device, since HTML5 DnD historically didn't fire on iOS Safari and that was the reason for switching to `@dnd-kit/core`. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:10:54 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#4818
No description provided.