import type { DragEndEvent } from "@dnd-kit/core"; import { closestCenter, DndContext, KeyboardSensor, PointerSensor, useSensor, useSensors, } from "@dnd-kit/core"; import { SortableContext, sortableKeyboardCoordinates, useSortable, verticalListSortingStrategy, } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; import clsx from "clsx"; import { useTranslation } from "react-i18next"; import { Image, WeaponImage } from "~/components/Image"; import { mainWeaponParams } from "~/features/build-analyzer/core/utils"; import type { MainWeaponId } from "~/modules/in-game-lists/types"; import { abilityImageUrl, specialWeaponImageUrl, subWeaponImageUrl, } from "~/utils/urls"; import { MAX_WEAPONS } from "../comp-analyzer-constants"; import styles from "./SelectedWeapons.module.css"; interface SelectedWeaponsProps { selectedWeaponIds: MainWeaponId[]; onRemove: (index: number) => void; onReorder: (newIds: MainWeaponId[]) => void; } export function SelectedWeapons({ selectedWeaponIds, onRemove, onReorder, }: SelectedWeaponsProps) { const { t } = useTranslation(["weapons", "analyzer"]); const sensors = useSensors( useSensor(PointerSensor), useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates, }), ); const handleDragEnd = (event: DragEndEvent) => { const { active, over } = event; if (over && active.id !== over.id) { const oldIndex = selectedWeaponIds.indexOf(active.id as MainWeaponId); const newIndex = selectedWeaponIds.indexOf(over.id as MainWeaponId); const newIds = [...selectedWeaponIds]; const [removed] = newIds.splice(oldIndex, 1); newIds.splice(newIndex, 0, removed); onReorder(newIds); } }; const emptySlotCount = MAX_WEAPONS - selectedWeaponIds.length; const showDragHandle = selectedWeaponIds.length > 1; return (
{selectedWeaponIds.map((weaponId, index) => ( ))} {Array.from({ length: emptySlotCount }, (_, i) => (
{t("analyzer:comp.pickWeapon")}
))}
); } interface SortableWeaponRowProps { weaponId: MainWeaponId; index: number; onRemove: (index: number) => void; showDragHandle: boolean; } function SortableWeaponRow({ weaponId, index, onRemove, showDragHandle, }: SortableWeaponRowProps) { const { t } = useTranslation(["weapons", "analyzer"]); const { attributes, listeners, setNodeRef, transform, transition, isDragging, } = useSortable({ id: weaponId }); const style = { transform: CSS.Transform.toString(transform), transition, }; const params = mainWeaponParams(weaponId); return (
{t(`weapons:MAIN_${weaponId}`)} {showDragHandle ? ( ) : null}
{t(`weapons:SUB_${params.subWeaponId}`)}
{t(`weapons:SPECIAL_${params.specialWeaponId}`)}
); }