File size: 10,797 Bytes
509e21e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"use client"

import { useState } from "react"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import { Badge } from "@/components/ui/badge"
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
import { Brain, Shield, HelpCircle } from "lucide-react"
import { Label } from "@/components/ui/label"
import { Textarea } from "@/components/ui/textarea"

interface Category {
  id: string
  name: string
  type: "capability" | "risk"
  description: string
}

interface CategorySelectionProps {
  categories: Category[]
  selectedCategories: string[]
  onSelectionChange: (categories: string[], excludedReasons: Record<string, string>) => void
}

export function CategorySelection({ categories, selectedCategories, onSelectionChange }: CategorySelectionProps) {
  const allCategoryIds = categories.map((c) => c.id)
  const [localSelection, setLocalSelection] = useState<string[]>(
    selectedCategories && selectedCategories.length > 0 ? selectedCategories : allCategoryIds,
  )
  const [localReasons, setLocalReasons] = useState<Record<string, string>>({})

  const capabilityCategories = categories.filter((c) => c.type === "capability")
  const riskCategories = categories.filter((c) => c.type === "risk")

  const handleCategoryToggle = (categoryId: string, checked: boolean) => {
    setLocalSelection((prev) => {
      if (checked) {
        // if re-selecting, remove any existing reason
        setLocalReasons((r) => {
          const copy = { ...r }
          delete copy[categoryId]
          return copy
        })
        return [...prev, categoryId]
      }
  // when unselecting, initialize an empty reason entry to make it required
  setLocalReasons((r) => ({ ...r, [categoryId]: r[categoryId] || "" }))
  return prev.filter((id) => id !== categoryId)
    })
  }

  const handleSelectAll = (type: "capability" | "risk") => {
    const categoryIds = categories.filter((c) => c.type === type).map((c) => c.id)
    const allSelected = categoryIds.every((id) => localSelection.includes(id))

    if (allSelected) {
      // deselect these categories and initialize empty reasons for them
      setLocalSelection((prev) => {
        const next = prev.filter((id) => !categoryIds.includes(id))
        setLocalReasons((r) => {
          const copy = { ...r }
          categoryIds.forEach((id) => {
            if (!(id in copy)) copy[id] = ""
          })
          return copy
        })
        return next
      })
    } else {
      setLocalSelection((prev) => {
        // when selecting these, remove any reasons for them
        setLocalReasons((r) => {
          const copy = { ...r }
          categoryIds.forEach((id) => delete copy[id])
          return copy
        })
        return [...new Set([...prev, ...categoryIds])]
      })
    }
  }

  const handleContinue = () => {
  onSelectionChange(localSelection, localReasons)
  }

  return (
    <TooltipProvider>
      <div className="h-full min-h-0 flex flex-col">
        <Card>
          <CardHeader>
            <CardTitle className="font-heading">Category Applicability Assessment</CardTitle>
            <CardDescription>
              Select which categories are applicable to your AI system. Only complete the detailed evaluation for
              categories marked as applicable.
            </CardDescription>
          </CardHeader>
          <CardContent>
            <div className="flex items-center justify-between mb-4">
              <div className="flex items-center gap-2">
                <span className="text-sm font-medium">Selected Categories:</span>
                <Badge variant="secondary">{localSelection.length}/20</Badge>
              </div>
            </div>
          </CardContent>
        </Card>

        <div className="flex-1 overflow-auto space-y-6 py-4 pr-2 min-h-0">
          {/* Capability Categories */}
          <Card>
          <CardHeader>
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <Brain className="h-5 w-5 text-accent" />
                <CardTitle className="font-heading">Capability Categories</CardTitle>
              </div>
              <Button variant="outline" size="sm" onClick={() => handleSelectAll("capability")}>
                {capabilityCategories.every((c) => localSelection.includes(c.id)) ? "Deselect All" : "Select All"}
              </Button>
            </div>
            <CardDescription>Core functional capabilities and performance areas</CardDescription>
          </CardHeader>
          <CardContent>
            <div className="grid grid-cols-1 md:grid-cols-2 gap-3">
              {capabilityCategories.map((category) => (
                <div key={category.id} className="p-3 rounded-lg border hover:bg-muted/50 transition-colors">
                  <div className="flex items-center space-x-3">
                    <Checkbox
                      id={category.id}
                      checked={localSelection.includes(category.id)}
                      onCheckedChange={(checked) => handleCategoryToggle(category.id, checked as boolean)}
                    />
                    <label htmlFor={category.id} className="text-sm font-medium cursor-pointer flex-1">
                      {category.name}
                    </label>
                    <Tooltip>
                      <TooltipTrigger>
                        <HelpCircle className="h-4 w-4 text-muted-foreground" />
                      </TooltipTrigger>
                      <TooltipContent className="max-w-sm">
                        <p>{category.description}</p>
                      </TooltipContent>
                    </Tooltip>
                  </div>

                  {/* If unchecked, require a reason */}
                  {!localSelection.includes(category.id) && (
                    <div className="mt-3">
                      <div className="ml-0 p-4 bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 rounded-lg">
                        <Label className="text-sm font-medium text-yellow-800 dark:text-yellow-200">Reason for excluding this category *</Label>
                        <Textarea
                          placeholder="Brief reason why this category is not applicable"
                          value={localReasons[category.id] || ""}
                          onChange={(e) => setLocalReasons((prev) => ({ ...prev, [category.id]: e.target.value }))}
                          rows={2}
                          className="mt-2 border-yellow-300 dark:border-yellow-700"
                          required
                        />
                      </div>
                    </div>
                  )}
                </div>
              ))}
            </div>
          </CardContent>
          </Card>

          {/* Risk Categories */}
          <Card>
          <CardHeader>
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <Shield className="h-5 w-5 text-destructive" />
                <CardTitle className="font-heading">Risk Categories</CardTitle>
              </div>
              <Button variant="outline" size="sm" onClick={() => handleSelectAll("risk")}>
                {riskCategories.every((c) => localSelection.includes(c.id)) ? "Deselect All" : "Select All"}
              </Button>
            </div>
            <CardDescription>Potential risks, safety concerns, and ethical considerations</CardDescription>
          </CardHeader>
          <CardContent>
            <div className="grid grid-cols-1 md:grid-cols-2 gap-3">
              {riskCategories.map((category) => (
                <div key={category.id} className="p-3 rounded-lg border hover:bg-muted/50 transition-colors">
                  <div className="flex items-center space-x-3">
                    <Checkbox
                      id={category.id}
                      checked={localSelection.includes(category.id)}
                      onCheckedChange={(checked) => handleCategoryToggle(category.id, checked as boolean)}
                    />
                    <label htmlFor={category.id} className="text-sm font-medium cursor-pointer flex-1">
                      {category.name}
                    </label>
                    <Tooltip>
                      <TooltipTrigger>
                        <HelpCircle className="h-4 w-4 text-muted-foreground" />
                      </TooltipTrigger>
                      <TooltipContent className="max-w-sm">
                        <p>{category.description}</p>
                      </TooltipContent>
                    </Tooltip>
                  </div>

                  {!localSelection.includes(category.id) && (
                    <div className="mt-3">
                      <div className="ml-0 p-4 bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 rounded-lg">
                        <Label className="text-sm font-medium text-yellow-800 dark:text-yellow-200">Reason for excluding this category *</Label>
                        <Textarea
                          placeholder="Brief reason why this category is not applicable"
                          value={localReasons[category.id] || ""}
                          onChange={(e) => setLocalReasons((prev) => ({ ...prev, [category.id]: e.target.value }))}
                          rows={2}
                          className="mt-2 border-yellow-300 dark:border-yellow-700"
                          required
                        />
                      </div>
                    </div>
                  )}
                </div>
              ))}
            </div>
          </CardContent>
          </Card>
        </div>
        <div className="flex justify-center py-4">
          <div className="flex flex-col items-center w-full">
            {Object.keys(localReasons).some((id) => !localSelection.includes(id) && !(localReasons[id] || "")) && (
              <p className="text-sm text-destructive mb-2">Please provide reasons for all excluded categories.</p>
            )}
            <Button
              onClick={handleContinue}
              disabled={
                localSelection.length === 0 ||
                Object.keys(localReasons).some((id) => !localSelection.includes(id) && !(localReasons[id] || ""))
              }
              size="lg"
            >
              Continue to Evaluation ({localSelection.length} categories selected)
            </Button>
          </div>
        </div>
      </div>
    </TooltipProvider>
  )
}