|
1 | | -import { useState, useEffect } from "react"; |
| 1 | +import { useState, useEffect, useMemo } from "react"; |
2 | 2 | import { useNavigate, useParams } from "react-router"; |
3 | 3 | import { |
4 | 4 | ConnectionHandler, |
@@ -105,31 +105,26 @@ function NewRiskForm({ |
105 | 105 |
|
106 | 106 | const [createRisk, isInFlight] = useMutation(createRiskMutation); |
107 | 107 |
|
108 | | - // Get unique categories from risk templates |
109 | | - const categories = Array.from(new Set(riskTemplates.map(template => template.category))); |
| 108 | + const categories = useMemo( |
| 109 | + () => Array.from(new Set(riskTemplates.map(t => t.category))), |
| 110 | + [riskTemplates], |
| 111 | + ); |
110 | 112 |
|
111 | | - // Filter risks by selected category |
112 | | - const filteredRisks = riskTemplates.filter(template => |
113 | | - !selectedCategory || template.category === selectedCategory |
114 | | - ).map(template => ({ |
115 | | - ...template, |
116 | | - originalIndex: riskTemplates.findIndex(t => t.name === template.name && t.description === template.description) |
117 | | - })); |
| 113 | + const filteredRisks = useMemo( |
| 114 | + () => |
| 115 | + riskTemplates |
| 116 | + .map((t, idx) => ({ ...t, originalIndex: idx })) |
| 117 | + .filter(t => !selectedCategory || t.category === selectedCategory), |
| 118 | + [riskTemplates, selectedCategory], |
| 119 | + ); |
118 | 120 |
|
119 | 121 | // Handle category selection |
120 | 122 | const selectCategory = (category: string) => { |
121 | | - setSelectedCategory(category); |
122 | | - // Only reset template if we're changing categories |
123 | | - if (selectedCategory !== category) { |
124 | | - setSelectedTemplate(""); |
125 | | - } |
126 | | - // Focus on the risk dropdown after a short delay to ensure it's rendered |
127 | | - setTimeout(() => { |
128 | | - const selectTrigger = document.getElementById('template'); |
129 | | - if (selectTrigger) { |
130 | | - selectTrigger.focus(); |
131 | | - } |
132 | | - }, 0); |
| 123 | + setSelectedCategory(prev => { |
| 124 | + if (prev !== category) setSelectedTemplate(""); |
| 125 | + return category; |
| 126 | + }); |
| 127 | + // If focus management is required, attach a ref to <SelectTrigger> instead. |
133 | 128 | }; |
134 | 129 |
|
135 | 130 | useEffect(() => { |
@@ -176,6 +171,7 @@ function NewRiskForm({ |
176 | 171 | setName(template.name); |
177 | 172 | setDescription(template.description); |
178 | 173 | setTreatment("MITIGATED"); |
| 174 | + setSelectedCategory(template.category); |
179 | 175 | } |
180 | 176 | }; |
181 | 177 |
|
|
0 commit comments