|
| 1 | +import { useState } from "react"; |
| 2 | +import { Characteristic, useCharacteristics } from "@/hooks/useCharacteristics"; |
| 3 | +import { Button } from "@/components/ui/button"; |
| 4 | +import { Input } from "@/components/ui/input"; |
| 5 | +import { calculateLevelProgress } from "@/lib/levelCalculation"; |
| 6 | +import { Edit2, Trash2, Save, X, Star } from "lucide-react"; |
| 7 | + |
| 8 | +interface CharacteristicCardProps { |
| 9 | + characteristic: Characteristic; |
| 10 | +} |
| 11 | + |
| 12 | +const CharacteristicCard = ({ characteristic }: CharacteristicCardProps) => { |
| 13 | + const { updateCharacteristic, deleteCharacteristic } = useCharacteristics(); |
| 14 | + const [isEditing, setIsEditing] = useState(false); |
| 15 | + const [editName, setEditName] = useState(characteristic.name); |
| 16 | + const [editIcon, setEditIcon] = useState(characteristic.icon); |
| 17 | + const [editXP, setEditXP] = useState(characteristic.xp.toString()); |
| 18 | + |
| 19 | + const progress = calculateLevelProgress(characteristic.xp); |
| 20 | + |
| 21 | + const handleSave = () => { |
| 22 | + updateCharacteristic.mutate({ |
| 23 | + id: characteristic.id, |
| 24 | + name: editName, |
| 25 | + icon: editIcon, |
| 26 | + xp: parseInt(editXP) || 0, |
| 27 | + }); |
| 28 | + setIsEditing(false); |
| 29 | + }; |
| 30 | + |
| 31 | + const handleCancel = () => { |
| 32 | + setEditName(characteristic.name); |
| 33 | + setEditIcon(characteristic.icon); |
| 34 | + setEditXP(characteristic.xp.toString()); |
| 35 | + setIsEditing(false); |
| 36 | + }; |
| 37 | + |
| 38 | + const handleDelete = () => { |
| 39 | + if (window.confirm(`Delete characteristic "${characteristic.name}"?`)) { |
| 40 | + deleteCharacteristic.mutate(characteristic.id); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <div className="system-panel p-4 space-y-3"> |
| 46 | + {/* Header */} |
| 47 | + <div className="flex items-start justify-between"> |
| 48 | + <div className="flex items-center gap-3 flex-1"> |
| 49 | + {isEditing ? ( |
| 50 | + <> |
| 51 | + <Input |
| 52 | + value={editIcon} |
| 53 | + onChange={(e) => setEditIcon(e.target.value)} |
| 54 | + className="w-12 text-xl text-center bg-input border-border" |
| 55 | + maxLength={2} |
| 56 | + /> |
| 57 | + <Input |
| 58 | + value={editName} |
| 59 | + onChange={(e) => setEditName(e.target.value)} |
| 60 | + className="flex-1 bg-input border-border" |
| 61 | + placeholder="Name" |
| 62 | + /> |
| 63 | + </> |
| 64 | + ) : ( |
| 65 | + <> |
| 66 | + <div className="text-2xl">{characteristic.icon}</div> |
| 67 | + <div className="flex-1"> |
| 68 | + <h3 className="font-normal text-foreground"> |
| 69 | + {characteristic.name} |
| 70 | + </h3> |
| 71 | + <div className="flex items-center gap-2 mt-1"> |
| 72 | + <span className="text-xs text-muted-foreground"> |
| 73 | + Level {characteristic.level} |
| 74 | + </span> |
| 75 | + <Star className="w-3 h-3 text-primary/70 fill-primary/70" /> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + </> |
| 79 | + )} |
| 80 | + </div> |
| 81 | + |
| 82 | + {/* Actions */} |
| 83 | + <div className="flex items-center gap-1"> |
| 84 | + {isEditing ? ( |
| 85 | + <> |
| 86 | + <Button |
| 87 | + variant="ghost" |
| 88 | + size="icon" |
| 89 | + className="h-8 w-8 text-green-600 hover:text-green-700 hover:bg-green-50" |
| 90 | + onClick={handleSave} |
| 91 | + > |
| 92 | + <Save className="w-4 h-4" /> |
| 93 | + </Button> |
| 94 | + <Button |
| 95 | + variant="ghost" |
| 96 | + size="icon" |
| 97 | + className="h-8 w-8 text-muted-foreground hover:text-foreground" |
| 98 | + onClick={handleCancel} |
| 99 | + > |
| 100 | + <X className="w-4 h-4" /> |
| 101 | + </Button> |
| 102 | + </> |
| 103 | + ) : ( |
| 104 | + <> |
| 105 | + <Button |
| 106 | + variant="ghost" |
| 107 | + size="icon" |
| 108 | + className="h-8 w-8 text-muted-foreground hover:text-foreground" |
| 109 | + onClick={() => setIsEditing(true)} |
| 110 | + > |
| 111 | + <Edit2 className="w-3 h-3" /> |
| 112 | + </Button> |
| 113 | + <Button |
| 114 | + variant="ghost" |
| 115 | + size="icon" |
| 116 | + className="h-8 w-8 text-muted-foreground hover:text-red-600" |
| 117 | + onClick={handleDelete} |
| 118 | + > |
| 119 | + <Trash2 className="w-3 h-3" /> |
| 120 | + </Button> |
| 121 | + </> |
| 122 | + )} |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + |
| 126 | + {/* XP Input (when editing) */} |
| 127 | + {isEditing && ( |
| 128 | + <div> |
| 129 | + <label className="text-xs text-muted-foreground uppercase tracking-wider mb-1 block"> |
| 130 | + XP |
| 131 | + </label> |
| 132 | + <Input |
| 133 | + type="number" |
| 134 | + value={editXP} |
| 135 | + onChange={(e) => setEditXP(e.target.value)} |
| 136 | + className="bg-input border-border" |
| 137 | + placeholder="0" |
| 138 | + /> |
| 139 | + </div> |
| 140 | + )} |
| 141 | + |
| 142 | + {/* XP Progress Bar */} |
| 143 | + <div className="space-y-2"> |
| 144 | + <div className="flex items-center justify-between text-xs text-muted-foreground"> |
| 145 | + <span>{progress.xpInCurrentLevel} / {progress.xpNeededForNextLevel} XP</span> |
| 146 | + <span>{Math.round(progress.progressPercentage)}%</span> |
| 147 | + </div> |
| 148 | + <div className="h-1.5 bg-muted rounded-full overflow-hidden"> |
| 149 | + <div |
| 150 | + className="h-full bg-primary/70 transition-all duration-300" |
| 151 | + style={{ width: `${Math.min(progress.progressPercentage, 100)}%` }} |
| 152 | + /> |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + ); |
| 157 | +}; |
| 158 | + |
| 159 | +export default CharacteristicCard; |
0 commit comments