Skip to content

Commit 2a081fb

Browse files
committed
fix: update InstructionsWizard to handle null values for currentStep and currentQuestion; improve answer selection logic
1 parent 974463e commit 2a081fb

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

components/instructions-wizard.tsx

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -292,35 +292,23 @@ export function InstructionsWizard({ onClose, selectedFileId }: InstructionsWiza
292292
[dynamicSteps]
293293
)
294294

295-
const currentStep = wizardSteps[currentStepIndex]
296-
const currentQuestion = currentStep?.questions[currentQuestionIndex]
295+
const currentStep = wizardSteps[currentStepIndex] ?? null
296+
const currentQuestion = currentStep?.questions[currentQuestionIndex] ?? null
297297

298298
const totalQuestions = useMemo(
299299
() => wizardSteps.reduce((count, step) => count + step.questions.length, 0),
300300
[wizardSteps]
301301
)
302302

303-
if (!currentStep || !currentQuestion) {
304-
return null
305-
}
306-
307-
const currentAnswerValue = responses[currentQuestion.id]
308-
309-
const isAnswerSelected = (value: string) => {
310-
if (currentQuestion.allowMultiple) {
311-
return Array.isArray(currentAnswerValue) && currentAnswerValue.includes(value)
312-
}
313-
314-
return currentAnswerValue === value
315-
}
303+
const currentAnswerValue = currentQuestion ? responses[currentQuestion.id] : undefined
316304

317305
const defaultAnswer = useMemo(
318-
() => currentQuestion.answers.find((answer) => answer.isDefault),
306+
() => currentQuestion?.answers.find((answer) => answer.isDefault) ?? null,
319307
[currentQuestion]
320308
)
321309

322310
const isDefaultSelected = useMemo(() => {
323-
if (!defaultAnswer) {
311+
if (!currentQuestion || !defaultAnswer) {
324312
return false
325313
}
326314

@@ -329,10 +317,11 @@ export function InstructionsWizard({ onClose, selectedFileId }: InstructionsWiza
329317
}
330318

331319
return currentAnswerValue === defaultAnswer.value
332-
}, [currentAnswerValue, currentQuestion.allowMultiple, defaultAnswer])
320+
}, [currentAnswerValue, currentQuestion, defaultAnswer])
333321

334322
const canUseDefault = Boolean(
335323
!isComplete &&
324+
currentQuestion &&
336325
defaultAnswer &&
337326
!defaultAnswer.disabled &&
338327
(!isDefaultSelected || currentQuestion.allowMultiple)
@@ -342,6 +331,18 @@ export function InstructionsWizard({ onClose, selectedFileId }: InstructionsWiza
342331
? `Use default (${defaultAnswer.label})`
343332
: "Use default"
344333

334+
if (!currentStep || !currentQuestion) {
335+
return null
336+
}
337+
338+
const isAnswerSelected = (value: string) => {
339+
if (currentQuestion.allowMultiple) {
340+
return Array.isArray(currentAnswerValue) && currentAnswerValue.includes(value)
341+
}
342+
343+
return currentAnswerValue === value
344+
}
345+
345346
const advanceToNextQuestion = () => {
346347
const isLastQuestionInStep = currentQuestionIndex === currentStep.questions.length - 1
347348
const isLastStep = currentStepIndex === wizardSteps.length - 1

lib/__tests__/template-config.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('template-config', () => {
137137
})
138138

139139
it('should have valid template and outputFileName for all combinations', () => {
140-
Object.entries(templateCombinations).forEach(([key, config]) => {
140+
Object.values(templateCombinations).forEach((config) => {
141141
expect(config.template).toBeTruthy()
142142
expect(config.outputFileName).toBeTruthy()
143143
expect(typeof config.template).toBe('string')

0 commit comments

Comments
 (0)