File size: 4,932 Bytes
b074e91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React, { useState } from "react";
import type { RoundMeResponse } from "../apiclient/data-contracts";
import { PlayingCard } from "./PlayingCard";

export interface Props {
  hand: RoundMeResponse["hand"];
  onCardClick?: (card: RoundMeResponse["hand"][number], index: number) => void;
  selectedIndex?: number;
  highlightIndex?: number;
  onReorder?: (reorderedHand: RoundMeResponse["hand"]) => void;
}

export const HandStrip: React.FC<Props> = ({ hand, onCardClick, selectedIndex, highlightIndex, onReorder }) => {
  const [draggedIndex, setDraggedIndex] = useState<number | null>(null);
  const [dropTargetIndex, setDropTargetIndex] = useState<number | null>(null);
  const [touchStartIndex, setTouchStartIndex] = useState<number | null>(null);
  const [touchPosition, setTouchPosition] = useState<{ x: number; y: number } | null>(null);

  // Mouse/Desktop drag handlers
  const handleDragStart = (e: React.DragEvent, index: number) => {
    setDraggedIndex(index);
    e.dataTransfer.effectAllowed = "move";
    const card = hand[index];
    e.dataTransfer.setData('card', JSON.stringify(card));
  };

  const handleDragOver = (e: React.DragEvent, index: number) => {
    e.preventDefault();
    if (draggedIndex !== null && draggedIndex !== index) {
      setDropTargetIndex(index);
    }
  };

  const handleDragLeave = () => {
    setDropTargetIndex(null);
  };

  const handleDrop = (e: React.DragEvent, dropIndex: number) => {
    e.preventDefault();
    if (draggedIndex === null || draggedIndex === dropIndex) {
      setDraggedIndex(null);
      setDropTargetIndex(null);
      return;
    }

    const newHand = [...hand];
    const [draggedCard] = newHand.splice(draggedIndex, 1);
    newHand.splice(dropIndex, 0, draggedCard);

    if (onReorder) {
      onReorder(newHand);
    }

    setDraggedIndex(null);
    setDropTargetIndex(null);
  };

  const handleDragEnd = () => {
    setDraggedIndex(null);
    setDropTargetIndex(null);
  };

  // Touch/Mobile handlers
  const handleTouchStart = (e: React.TouchEvent, index: number) => {
    const touch = e.touches[0];
    setTouchStartIndex(index);
    setDraggedIndex(index);
    setTouchPosition({ x: touch.clientX, y: touch.clientY });
    console.log('πŸ“± Touch drag started for card', index);
  };

  const handleTouchMove = (e: React.TouchEvent) => {
    if (touchStartIndex === null) return;
    e.preventDefault(); // Prevent scrolling while dragging
    const touch = e.touches[0];
    setTouchPosition({ x: touch.clientX, y: touch.clientY });
    
    // Find which card is under the touch
    const element = document.elementFromPoint(touch.clientX, touch.clientY);
    const cardElement = element?.closest('[data-card-index]');
    if (cardElement) {
      const targetIndex = Number(cardElement.getAttribute('data-card-index'));
      if (targetIndex !== touchStartIndex) {
        setDropTargetIndex(targetIndex);
        console.log('πŸ“± Dragging over card', targetIndex);
      }
    }
  };

  const handleTouchEnd = () => {
    console.log('πŸ“± Touch drag ended', { touchStartIndex, dropTargetIndex });
    if (touchStartIndex !== null && dropTargetIndex !== null && touchStartIndex !== dropTargetIndex) {
      const newHand = [...hand];
      const [draggedCard] = newHand.splice(touchStartIndex, 1);
      newHand.splice(dropTargetIndex, 0, draggedCard);
      
      if (onReorder) {
        console.log('πŸ“± Reordering hand');
        onReorder(newHand);
      }
    }
    
    setTouchStartIndex(null);
    setDraggedIndex(null);
    setDropTargetIndex(null);
    setTouchPosition(null);
  };

  return (
    <div className="w-full overflow-x-auto">
      <div className="grid grid-cols-3 sm:grid-cols-5 md:grid-cols-7 gap-2 py-4">
        {hand.map((card, idx) => (
          <div
            key={`${card.code}-${idx}`}
            data-card-index={idx}
            draggable={!!onReorder}
            onDragStart={(e) => handleDragStart(e, idx)}
            onDragOver={(e) => handleDragOver(e, idx)}
            onDragLeave={handleDragLeave}
            onDrop={(e) => handleDrop(e, idx)}
            onDragEnd={handleDragEnd}
            onTouchStart={(e) => handleTouchStart(e, idx)}
            onTouchMove={handleTouchMove}
            onTouchEnd={handleTouchEnd}
            className={`transition-all duration-200 ${
              idx === draggedIndex ? 'opacity-50 scale-95' : ''
            } ${
              idx === dropTargetIndex ? 'scale-105 ring-2 ring-amber-400' : ''
            }`}
          >
            <PlayingCard
              card={card}
              onClick={onCardClick ? () => onCardClick(card, idx) : undefined}
              selected={selectedIndex === idx}
            />
            {idx === highlightIndex && (
              <div className="absolute -top-1 -right-1 w-3 h-3 bg-amber-400 rounded-full animate-ping" />
            )}
          </div>
        ))}
      </div>
    </div>
  );
};