nyk commited on
Commit
54ae8f3
·
unverified ·
1 Parent(s): a79a97a

fix: deduplicate sessions by sessionId and add task edit modal (#86)

Browse files

Deduplicate gateway sessions server-side using sessionId as primary key,
falling back to agent:key composite for sessions without sessionId. This
prevents duplicate React keys when OpenClaw tracks cron runs under the
same session ID as the parent session.

Also adds EditTaskModal to the task board panel with inline edit button
in the task detail modal, and improves CreateTaskModal error handling.

Cherry-picked and adapted from PR #77 by @arana198 .

Closes #80

src/app/api/sessions/route.ts CHANGED
@@ -12,14 +12,26 @@ export async function GET(request: NextRequest) {
12
  try {
13
  const gatewaySessions = getAllGatewaySessions()
14
 
15
- // If gateway sessions exist, return those
16
  if (gatewaySessions.length > 0) {
17
- const sessions = gatewaySessions.map((s) => {
 
 
 
 
 
 
 
 
 
 
 
 
18
  const total = s.totalTokens || 0
19
  const context = s.contextTokens || 35000
20
  const pct = context > 0 ? Math.round((total / context) * 100) : 0
21
  return {
22
- id: s.sessionId || s.key,
23
  key: s.key,
24
  agent: s.agent,
25
  kind: s.chatType || 'unknown',
 
12
  try {
13
  const gatewaySessions = getAllGatewaySessions()
14
 
15
+ // If gateway sessions exist, deduplicate and return those
16
  if (gatewaySessions.length > 0) {
17
+ // Deduplicate by sessionId — OpenClaw tracks cron runs under the same
18
+ // session ID as the parent session, causing duplicate React keys (#80).
19
+ // Keep the most recently updated entry when duplicates exist.
20
+ const sessionMap = new Map<string, (typeof gatewaySessions)[0]>()
21
+ for (const s of gatewaySessions) {
22
+ const id = s.sessionId || `${s.agent}:${s.key}`
23
+ const existing = sessionMap.get(id)
24
+ if (!existing || s.updatedAt > existing.updatedAt) {
25
+ sessionMap.set(id, s)
26
+ }
27
+ }
28
+
29
+ const sessions = Array.from(sessionMap.values()).map((s) => {
30
  const total = s.totalTokens || 0
31
  const context = s.contextTokens || 35000
32
  const pct = context > 0 ? Math.round((total / context) * 100) : 0
33
  return {
34
+ id: s.sessionId || `${s.agent}:${s.key}`,
35
  key: s.key,
36
  agent: s.agent,
37
  kind: s.chatType || 'unknown',
src/components/panels/task-board-panel.tsx CHANGED
@@ -70,6 +70,7 @@ export function TaskBoardPanel() {
70
  const [selectedTask, setSelectedTask] = useState<Task | null>(null)
71
  const [draggedTask, setDraggedTask] = useState<Task | null>(null)
72
  const [showCreateModal, setShowCreateModal] = useState(false)
 
73
  const dragCounter = useRef(0)
74
 
75
  // Fetch tasks and agents
@@ -416,12 +417,16 @@ export function TaskBoardPanel() {
416
  </div>
417
 
418
  {/* Task Detail Modal */}
419
- {selectedTask && (
420
  <TaskDetailModal
421
  task={selectedTask}
422
  agents={agents}
423
  onClose={() => setSelectedTask(null)}
424
  onUpdate={fetchData}
 
 
 
 
425
  />
426
  )}
427
 
@@ -433,21 +438,33 @@ export function TaskBoardPanel() {
433
  onCreated={fetchData}
434
  />
435
  )}
 
 
 
 
 
 
 
 
 
 
436
  </div>
437
  )
438
  }
439
 
440
  // Task Detail Modal Component (placeholder - would be implemented separately)
441
- function TaskDetailModal({
442
- task,
443
- agents,
444
- onClose,
445
- onUpdate
446
- }: {
 
447
  task: Task
448
  agents: Agent[]
449
  onClose: () => void
450
  onUpdate: () => void
 
451
  }) {
452
  const [comments, setComments] = useState<Comment[]>([])
453
  const [loadingComments, setLoadingComments] = useState(false)
@@ -588,12 +605,20 @@ function TaskDetailModal({
588
  <div className="p-6">
589
  <div className="flex justify-between items-start mb-4">
590
  <h3 className="text-xl font-bold text-foreground">{task.title}</h3>
591
- <button
592
- onClick={onClose}
593
- className="text-muted-foreground hover:text-foreground text-2xl transition-smooth"
594
- >
595
- ×
596
- </button>
 
 
 
 
 
 
 
 
597
  </div>
598
  <p className="text-foreground/80 mb-4">{task.description || 'No description'}</p>
599
  <div className="flex gap-2 mt-4">
@@ -795,7 +820,9 @@ function CreateTaskModal({
795
 
796
  const handleSubmit = async (e: React.FormEvent) => {
797
  e.preventDefault()
798
-
 
 
799
  try {
800
  const response = await fetch('/api/tasks', {
801
  method: 'POST',
@@ -807,8 +834,12 @@ function CreateTaskModal({
807
  })
808
  })
809
 
810
- if (!response.ok) throw new Error('Failed to create task')
811
-
 
 
 
 
812
  onCreated()
813
  onClose()
814
  } catch (error) {
@@ -908,3 +939,161 @@ function CreateTaskModal({
908
  </div>
909
  )
910
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  const [selectedTask, setSelectedTask] = useState<Task | null>(null)
71
  const [draggedTask, setDraggedTask] = useState<Task | null>(null)
72
  const [showCreateModal, setShowCreateModal] = useState(false)
73
+ const [editingTask, setEditingTask] = useState<Task | null>(null)
74
  const dragCounter = useRef(0)
75
 
76
  // Fetch tasks and agents
 
417
  </div>
418
 
419
  {/* Task Detail Modal */}
420
+ {selectedTask && !editingTask && (
421
  <TaskDetailModal
422
  task={selectedTask}
423
  agents={agents}
424
  onClose={() => setSelectedTask(null)}
425
  onUpdate={fetchData}
426
+ onEdit={(taskToEdit) => {
427
+ setEditingTask(taskToEdit)
428
+ setSelectedTask(null)
429
+ }}
430
  />
431
  )}
432
 
 
438
  onCreated={fetchData}
439
  />
440
  )}
441
+
442
+ {/* Edit Task Modal */}
443
+ {editingTask && (
444
+ <EditTaskModal
445
+ task={editingTask}
446
+ agents={agents}
447
+ onClose={() => setEditingTask(null)}
448
+ onUpdated={() => { fetchData(); setEditingTask(null) }}
449
+ />
450
+ )}
451
  </div>
452
  )
453
  }
454
 
455
  // Task Detail Modal Component (placeholder - would be implemented separately)
456
+ function TaskDetailModal({
457
+ task,
458
+ agents,
459
+ onClose,
460
+ onUpdate,
461
+ onEdit
462
+ }: {
463
  task: Task
464
  agents: Agent[]
465
  onClose: () => void
466
  onUpdate: () => void
467
+ onEdit: (task: Task) => void
468
  }) {
469
  const [comments, setComments] = useState<Comment[]>([])
470
  const [loadingComments, setLoadingComments] = useState(false)
 
605
  <div className="p-6">
606
  <div className="flex justify-between items-start mb-4">
607
  <h3 className="text-xl font-bold text-foreground">{task.title}</h3>
608
+ <div className="flex gap-2">
609
+ <button
610
+ onClick={() => onEdit(task)}
611
+ className="px-3 py-1.5 bg-primary/20 text-primary hover:bg-primary/30 rounded-md transition-smooth text-sm font-medium"
612
+ >
613
+ Edit
614
+ </button>
615
+ <button
616
+ onClick={onClose}
617
+ className="text-muted-foreground hover:text-foreground text-2xl transition-smooth"
618
+ >
619
+ ×
620
+ </button>
621
+ </div>
622
  </div>
623
  <p className="text-foreground/80 mb-4">{task.description || 'No description'}</p>
624
  <div className="flex gap-2 mt-4">
 
820
 
821
  const handleSubmit = async (e: React.FormEvent) => {
822
  e.preventDefault()
823
+
824
+ if (!formData.title.trim()) return
825
+
826
  try {
827
  const response = await fetch('/api/tasks', {
828
  method: 'POST',
 
834
  })
835
  })
836
 
837
+ if (!response.ok) {
838
+ const errorData = await response.json()
839
+ const errorMsg = errorData.details ? errorData.details.join(', ') : errorData.error
840
+ throw new Error(errorMsg)
841
+ }
842
+
843
  onCreated()
844
  onClose()
845
  } catch (error) {
 
939
  </div>
940
  )
941
  }
942
+
943
+ // Edit Task Modal Component
944
+ function EditTaskModal({
945
+ task,
946
+ agents,
947
+ onClose,
948
+ onUpdated
949
+ }: {
950
+ task: Task
951
+ agents: Agent[]
952
+ onClose: () => void
953
+ onUpdated: () => void
954
+ }) {
955
+ const [formData, setFormData] = useState({
956
+ title: task.title,
957
+ description: task.description || '',
958
+ priority: task.priority,
959
+ status: task.status,
960
+ assigned_to: task.assigned_to || '',
961
+ tags: task.tags ? task.tags.join(', ') : '',
962
+ })
963
+
964
+ const handleSubmit = async (e: React.FormEvent) => {
965
+ e.preventDefault()
966
+
967
+ if (!formData.title.trim()) return
968
+
969
+ try {
970
+ const response = await fetch(`/api/tasks/${task.id}`, {
971
+ method: 'PUT',
972
+ headers: { 'Content-Type': 'application/json' },
973
+ body: JSON.stringify({
974
+ ...formData,
975
+ tags: formData.tags ? formData.tags.split(',').map(t => t.trim()) : [],
976
+ assigned_to: formData.assigned_to || undefined
977
+ })
978
+ })
979
+
980
+ if (!response.ok) {
981
+ const errorData = await response.json()
982
+ const errorMsg = errorData.details ? errorData.details.join(', ') : errorData.error
983
+ throw new Error(errorMsg)
984
+ }
985
+
986
+ onUpdated()
987
+ } catch (error) {
988
+ console.error('Error updating task:', error)
989
+ }
990
+ }
991
+
992
+ return (
993
+ <div className="fixed inset-0 bg-black/60 backdrop-blur-sm flex items-center justify-center z-50 p-4">
994
+ <div className="bg-card border border-border rounded-lg max-w-md w-full">
995
+ <form onSubmit={handleSubmit} className="p-6">
996
+ <h3 className="text-xl font-bold text-foreground mb-4">Edit Task</h3>
997
+
998
+ <div className="space-y-4">
999
+ <div>
1000
+ <label className="block text-sm text-muted-foreground mb-1">Title</label>
1001
+ <input
1002
+ type="text"
1003
+ value={formData.title}
1004
+ onChange={(e) => setFormData(prev => ({ ...prev, title: e.target.value }))}
1005
+ className="w-full bg-surface-1 text-foreground border border-border rounded-md px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary/50"
1006
+ required
1007
+ />
1008
+ </div>
1009
+
1010
+ <div>
1011
+ <label className="block text-sm text-muted-foreground mb-1">Description</label>
1012
+ <textarea
1013
+ value={formData.description}
1014
+ onChange={(e) => setFormData(prev => ({ ...prev, description: e.target.value }))}
1015
+ className="w-full bg-surface-1 text-foreground border border-border rounded-md px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary/50"
1016
+ rows={3}
1017
+ />
1018
+ </div>
1019
+
1020
+ <div className="grid grid-cols-2 gap-4">
1021
+ <div>
1022
+ <label className="block text-sm text-muted-foreground mb-1">Status</label>
1023
+ <select
1024
+ value={formData.status}
1025
+ onChange={(e) => setFormData(prev => ({ ...prev, status: e.target.value as Task['status'] }))}
1026
+ className="w-full bg-surface-1 text-foreground border border-border rounded-md px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary/50"
1027
+ >
1028
+ <option value="inbox">Inbox</option>
1029
+ <option value="assigned">Assigned</option>
1030
+ <option value="in_progress">In Progress</option>
1031
+ <option value="review">Review</option>
1032
+ <option value="quality_review">Quality Review</option>
1033
+ <option value="done">Done</option>
1034
+ </select>
1035
+ </div>
1036
+
1037
+ <div>
1038
+ <label className="block text-sm text-muted-foreground mb-1">Priority</label>
1039
+ <select
1040
+ value={formData.priority}
1041
+ onChange={(e) => setFormData(prev => ({ ...prev, priority: e.target.value as Task['priority'] }))}
1042
+ className="w-full bg-surface-1 text-foreground border border-border rounded-md px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary/50"
1043
+ >
1044
+ <option value="low">Low</option>
1045
+ <option value="medium">Medium</option>
1046
+ <option value="high">High</option>
1047
+ <option value="urgent">Urgent</option>
1048
+ </select>
1049
+ </div>
1050
+ </div>
1051
+
1052
+ <div>
1053
+ <label className="block text-sm text-muted-foreground mb-1">Assign to</label>
1054
+ <select
1055
+ value={formData.assigned_to}
1056
+ onChange={(e) => setFormData(prev => ({ ...prev, assigned_to: e.target.value }))}
1057
+ className="w-full bg-surface-1 text-foreground border border-border rounded-md px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary/50"
1058
+ >
1059
+ <option value="">Unassigned</option>
1060
+ {agents.map(agent => (
1061
+ <option key={agent.name} value={agent.name}>
1062
+ {agent.name} ({agent.role})
1063
+ </option>
1064
+ ))}
1065
+ </select>
1066
+ </div>
1067
+
1068
+ <div>
1069
+ <label className="block text-sm text-muted-foreground mb-1">Tags (comma-separated)</label>
1070
+ <input
1071
+ type="text"
1072
+ value={formData.tags}
1073
+ onChange={(e) => setFormData(prev => ({ ...prev, tags: e.target.value }))}
1074
+ className="w-full bg-surface-1 text-foreground border border-border rounded-md px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary/50"
1075
+ placeholder="frontend, urgent, bug"
1076
+ />
1077
+ </div>
1078
+ </div>
1079
+
1080
+ <div className="flex gap-3 mt-6">
1081
+ <button
1082
+ type="submit"
1083
+ className="flex-1 bg-primary text-primary-foreground py-2 rounded-md hover:bg-primary/90 transition-smooth"
1084
+ >
1085
+ Save Changes
1086
+ </button>
1087
+ <button
1088
+ type="button"
1089
+ onClick={onClose}
1090
+ className="flex-1 bg-secondary text-muted-foreground py-2 rounded-md hover:bg-surface-2 transition-smooth"
1091
+ >
1092
+ Cancel
1093
+ </button>
1094
+ </div>
1095
+ </form>
1096
+ </div>
1097
+ </div>
1098
+ )
1099
+ }