Skip to content

Commit

Permalink
fix(web): cancel drag when dragged item is moved
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWasTakenn committed May 3, 2023
1 parent 5e468cf commit e7cf89f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion web/src/components/inventory/InventorySlot.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { DragSource, Inventory, InventoryType, Slot, SlotWithItem } from '../../typings';
import { useDrag, useDrop } from 'react-dnd';
import { useDrag, useDragDropManager, useDrop } from 'react-dnd';
import { useAppDispatch, useAppSelector } from '../../store';
import WeightBar from '../utils/WeightBar';
import { onDrop } from '../../dnd/onDrop';
Expand All @@ -15,13 +15,16 @@ import SlotTooltip from './SlotTooltip';
import { setContextMenu } from '../../store/inventory';
import { imagepath } from '../../store/imagepath';
import { onCraft } from '../../dnd/onCraft';
import useNuiEvent from '../../hooks/useNuiEvent';
import { ItemsPayload } from '../../reducers/refreshSlots';

interface SlotProps {
inventory: Inventory;
item: Slot;
}

const InventorySlot: React.FC<SlotProps> = ({ inventory, item }) => {
const manager = useDragDropManager();
const isBusy = useAppSelector(selectIsBusy);
const dispatch = useAppDispatch();

Expand Down Expand Up @@ -87,6 +90,19 @@ const InventorySlot: React.FC<SlotProps> = ({ inventory, item }) => {
[isBusy, inventory, item]
);

useNuiEvent('refreshSlots', (data: { items?: ItemsPayload | ItemsPayload[] }) => {
if (!isDragging && !data.items) return;
if (!Array.isArray(data.items)) return;

const itemSlot = data.items.find(
(dataItem) => dataItem.item.slot === item.slot && dataItem.inventory === inventory.id
);

if (!itemSlot) return;

manager.dispatch({ type: 'dnd-core/END_DRAG' });
});

const connectRef = (element: HTMLDivElement) => drag(drop(element));

const handleContext = (event: React.MouseEvent<HTMLDivElement>) => {
Expand Down
2 changes: 1 addition & 1 deletion web/src/reducers/refreshSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { itemDurability } from '../helpers';
import { Items } from '../store/items';
import { InventoryType, Slot, State } from '../typings';

type ItemsPayload = { item: Slot; inventory?: InventoryType };
export type ItemsPayload = { item: Slot; inventory?: InventoryType };

interface Payload {
items?: ItemsPayload | ItemsPayload[];
Expand Down

0 comments on commit e7cf89f

Please sign in to comment.