|
| 1 | +import { useState } from "react"; |
| 2 | +import type { IssueRead, IssueStatus } from "@/api/issues"; |
| 3 | + |
| 4 | +export interface IssueListProps { |
| 5 | + issues: IssueRead[]; |
| 6 | + statusFilter: IssueStatus | null; |
| 7 | + onFilterChange: (status: IssueStatus | null) => void; |
| 8 | + onCreateIssue: (title: string, description?: string) => Promise<void>; |
| 9 | +} |
| 10 | + |
| 11 | +const issueStatusColors: Record<string, string> = { |
| 12 | + open: "bg-blue-100 text-blue-800", |
| 13 | + in_progress: "bg-yellow-100 text-yellow-800", |
| 14 | + closed: "bg-green-100 text-green-800", |
| 15 | +}; |
| 16 | + |
| 17 | +const filters: { label: string; value: IssueStatus | null }[] = [ |
| 18 | + { label: "All", value: null }, |
| 19 | + { label: "Open", value: "open" }, |
| 20 | + { label: "In Progress", value: "in_progress" }, |
| 21 | + { label: "Closed", value: "closed" }, |
| 22 | +]; |
| 23 | + |
| 24 | +function formatRelativeTime(dateStr: string): string { |
| 25 | + const now = Date.now(); |
| 26 | + const then = new Date(dateStr).getTime(); |
| 27 | + const diffMs = now - then; |
| 28 | + const diffSec = Math.floor(diffMs / 1000); |
| 29 | + if (diffSec < 60) return "just now"; |
| 30 | + const diffMin = Math.floor(diffSec / 60); |
| 31 | + if (diffMin < 60) return `${diffMin}m ago`; |
| 32 | + const diffHr = Math.floor(diffMin / 60); |
| 33 | + if (diffHr < 24) return `${diffHr}h ago`; |
| 34 | + const diffDay = Math.floor(diffHr / 24); |
| 35 | + return `${diffDay}d ago`; |
| 36 | +} |
| 37 | + |
| 38 | +export default function IssueList({ |
| 39 | + issues, |
| 40 | + statusFilter, |
| 41 | + onFilterChange, |
| 42 | + onCreateIssue, |
| 43 | +}: IssueListProps) { |
| 44 | + const [showForm, setShowForm] = useState(false); |
| 45 | + const [title, setTitle] = useState(""); |
| 46 | + const [description, setDescription] = useState(""); |
| 47 | + const [creating, setCreating] = useState(false); |
| 48 | + |
| 49 | + async function handleSubmit(e: React.FormEvent) { |
| 50 | + e.preventDefault(); |
| 51 | + if (!title.trim()) return; |
| 52 | + setCreating(true); |
| 53 | + try { |
| 54 | + await onCreateIssue(title.trim(), description.trim() || undefined); |
| 55 | + setTitle(""); |
| 56 | + setDescription(""); |
| 57 | + setShowForm(false); |
| 58 | + } finally { |
| 59 | + setCreating(false); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <div> |
| 65 | + <div className="flex items-center justify-between mb-3"> |
| 66 | + <div className="flex gap-1"> |
| 67 | + {filters.map((f) => ( |
| 68 | + <button |
| 69 | + key={f.label} |
| 70 | + onClick={() => onFilterChange(f.value)} |
| 71 | + className={`px-3 py-1 rounded text-sm font-medium ${ |
| 72 | + statusFilter === f.value |
| 73 | + ? "bg-blue-600 text-white" |
| 74 | + : "bg-gray-100 text-gray-700 hover:bg-gray-200" |
| 75 | + }`} |
| 76 | + > |
| 77 | + {f.label} |
| 78 | + </button> |
| 79 | + ))} |
| 80 | + </div> |
| 81 | + <button |
| 82 | + onClick={() => setShowForm(!showForm)} |
| 83 | + className="bg-blue-600 text-white px-3 py-1.5 rounded text-sm" |
| 84 | + > |
| 85 | + + New Issue |
| 86 | + </button> |
| 87 | + </div> |
| 88 | + |
| 89 | + {showForm && ( |
| 90 | + <form |
| 91 | + onSubmit={handleSubmit} |
| 92 | + className="mb-4 p-4 border border-gray-200 rounded-lg bg-white" |
| 93 | + > |
| 94 | + <div className="mb-3"> |
| 95 | + <label className="block text-sm font-medium text-gray-700 mb-1"> |
| 96 | + Title |
| 97 | + </label> |
| 98 | + <input |
| 99 | + type="text" |
| 100 | + value={title} |
| 101 | + onChange={(e) => setTitle(e.target.value)} |
| 102 | + placeholder="Issue title" |
| 103 | + required |
| 104 | + className="w-full border border-gray-300 rounded px-3 py-1.5 text-sm" |
| 105 | + /> |
| 106 | + </div> |
| 107 | + <div className="mb-3"> |
| 108 | + <label className="block text-sm font-medium text-gray-700 mb-1"> |
| 109 | + Description (optional) |
| 110 | + </label> |
| 111 | + <textarea |
| 112 | + value={description} |
| 113 | + onChange={(e) => setDescription(e.target.value)} |
| 114 | + placeholder="Issue description" |
| 115 | + rows={3} |
| 116 | + className="w-full border border-gray-300 rounded px-3 py-1.5 text-sm" |
| 117 | + /> |
| 118 | + </div> |
| 119 | + <div className="flex gap-2"> |
| 120 | + <button |
| 121 | + type="submit" |
| 122 | + disabled={creating || !title.trim()} |
| 123 | + className="bg-blue-600 text-white px-3 py-1.5 rounded text-sm disabled:opacity-50" |
| 124 | + > |
| 125 | + {creating ? "Creating..." : "Create Issue"} |
| 126 | + </button> |
| 127 | + <button |
| 128 | + type="button" |
| 129 | + onClick={() => setShowForm(false)} |
| 130 | + className="bg-gray-100 text-gray-700 px-3 py-1.5 rounded text-sm" |
| 131 | + > |
| 132 | + Cancel |
| 133 | + </button> |
| 134 | + </div> |
| 135 | + </form> |
| 136 | + )} |
| 137 | + |
| 138 | + {issues.length === 0 ? ( |
| 139 | + <p className="text-gray-500 text-sm">No issues found.</p> |
| 140 | + ) : ( |
| 141 | + <ul className="divide-y divide-gray-200 border border-gray-200 rounded-lg bg-white"> |
| 142 | + {issues.map((issue) => { |
| 143 | + const colorClass = |
| 144 | + issueStatusColors[issue.status] ?? "bg-gray-100 text-gray-700"; |
| 145 | + return ( |
| 146 | + <li key={issue.id} className="px-4 py-3"> |
| 147 | + <div className="flex items-center justify-between"> |
| 148 | + <span className="font-medium text-gray-900"> |
| 149 | + {issue.title} |
| 150 | + </span> |
| 151 | + <span |
| 152 | + className={`inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium ${colorClass}`} |
| 153 | + > |
| 154 | + {issue.status} |
| 155 | + </span> |
| 156 | + </div> |
| 157 | + <div className="mt-1 text-xs text-gray-500"> |
| 158 | + {formatRelativeTime(issue.created_at)} |
| 159 | + </div> |
| 160 | + </li> |
| 161 | + ); |
| 162 | + })} |
| 163 | + </ul> |
| 164 | + )} |
| 165 | + </div> |
| 166 | + ); |
| 167 | +} |
0 commit comments