- Rename `label` to `question` in profiling data (35 questions) to match ScopeProfilingQuestion type — fixes missing question headings - Sync ScopeWizardTab props with page.tsx (onEvaluate/canEvaluate/isEvaluating instead of onComplete/companyProfile/currentLevel) - Load companyProfile from SDK context instead of expecting it as prop - Auto-prefill from company profile on mount when answers are empty - Add "Aus Profil" badge for prefilled questions - Replace title-only helpText tooltip with click-to-expand visible info box - Fix ScopeQuestionBlockId to match actual block IDs in data - Add `order` field to ScopeQuestionBlock type - Fix completionStats to count against total required questions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
460 lines
18 KiB
TypeScript
460 lines
18 KiB
TypeScript
'use client'
|
|
import React, { useState, useCallback, useEffect, useMemo } from 'react'
|
|
import type { ScopeProfilingAnswer, ScopeProfilingQuestion } from '@/lib/sdk/compliance-scope-types'
|
|
import { SCOPE_QUESTION_BLOCKS, getBlockProgress, getTotalProgress, getAnswerValue, prefillFromCompanyProfile } from '@/lib/sdk/compliance-scope-profiling'
|
|
import { useSDK } from '@/lib/sdk'
|
|
|
|
interface ScopeWizardTabProps {
|
|
answers: ScopeProfilingAnswer[]
|
|
onAnswersChange: (answers: ScopeProfilingAnswer[]) => void
|
|
onEvaluate: () => void
|
|
canEvaluate: boolean
|
|
isEvaluating: boolean
|
|
completionStats: { total: number; answered: number; percentage: number; isComplete: boolean }
|
|
}
|
|
|
|
export function ScopeWizardTab({
|
|
answers,
|
|
onAnswersChange,
|
|
onEvaluate,
|
|
canEvaluate,
|
|
isEvaluating,
|
|
completionStats,
|
|
}: ScopeWizardTabProps) {
|
|
const [currentBlockIndex, setCurrentBlockIndex] = useState(0)
|
|
const [expandedHelp, setExpandedHelp] = useState<Set<string>>(new Set())
|
|
const currentBlock = SCOPE_QUESTION_BLOCKS[currentBlockIndex]
|
|
const totalProgress = getTotalProgress(answers)
|
|
|
|
// Load companyProfile from SDK context
|
|
const { state: sdkState } = useSDK()
|
|
const companyProfile = sdkState.companyProfile
|
|
|
|
// Track which question IDs were prefilled from profile
|
|
const [prefilledIds, setPrefilledIds] = useState<Set<string>>(new Set())
|
|
|
|
// Auto-prefill from company profile on mount if answers are empty
|
|
useEffect(() => {
|
|
if (companyProfile && answers.length === 0) {
|
|
const prefilled = prefillFromCompanyProfile(companyProfile)
|
|
if (prefilled.length > 0) {
|
|
onAnswersChange(prefilled)
|
|
setPrefilledIds(new Set(prefilled.map(a => a.questionId)))
|
|
}
|
|
}
|
|
// Only run on mount
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [])
|
|
|
|
const handleAnswerChange = useCallback(
|
|
(questionId: string, value: string | string[] | boolean | number) => {
|
|
const existingIndex = answers.findIndex((a) => a.questionId === questionId)
|
|
if (existingIndex >= 0) {
|
|
const newAnswers = [...answers]
|
|
newAnswers[existingIndex] = { questionId, value }
|
|
onAnswersChange(newAnswers)
|
|
} else {
|
|
onAnswersChange([...answers, { questionId, value }])
|
|
}
|
|
// Remove from prefilled set when user manually changes
|
|
if (prefilledIds.has(questionId)) {
|
|
setPrefilledIds(prev => {
|
|
const next = new Set(prev)
|
|
next.delete(questionId)
|
|
return next
|
|
})
|
|
}
|
|
},
|
|
[answers, onAnswersChange, prefilledIds]
|
|
)
|
|
|
|
const handlePrefillFromProfile = useCallback(() => {
|
|
if (!companyProfile) return
|
|
const prefilled = prefillFromCompanyProfile(companyProfile)
|
|
// Merge with existing answers: prefilled values for questions not yet answered
|
|
const existingIds = new Set(answers.map(a => a.questionId))
|
|
const newAnswers = [...answers]
|
|
const newPrefilledIds = new Set(prefilledIds)
|
|
for (const pa of prefilled) {
|
|
if (!existingIds.has(pa.questionId)) {
|
|
newAnswers.push(pa)
|
|
newPrefilledIds.add(pa.questionId)
|
|
}
|
|
}
|
|
onAnswersChange(newAnswers)
|
|
setPrefilledIds(newPrefilledIds)
|
|
}, [companyProfile, answers, onAnswersChange, prefilledIds])
|
|
|
|
const handleNext = useCallback(() => {
|
|
if (currentBlockIndex < SCOPE_QUESTION_BLOCKS.length - 1) {
|
|
setCurrentBlockIndex(currentBlockIndex + 1)
|
|
} else if (canEvaluate) {
|
|
onEvaluate()
|
|
}
|
|
}, [currentBlockIndex, canEvaluate, onEvaluate])
|
|
|
|
const handleBack = useCallback(() => {
|
|
if (currentBlockIndex > 0) {
|
|
setCurrentBlockIndex(currentBlockIndex - 1)
|
|
}
|
|
}, [currentBlockIndex])
|
|
|
|
const toggleHelp = useCallback((questionId: string) => {
|
|
setExpandedHelp(prev => {
|
|
const next = new Set(prev)
|
|
if (next.has(questionId)) {
|
|
next.delete(questionId)
|
|
} else {
|
|
next.add(questionId)
|
|
}
|
|
return next
|
|
})
|
|
}, [])
|
|
|
|
// Check if a question was prefilled from company profile
|
|
const isPrefilledFromProfile = useCallback((questionId: string) => {
|
|
return prefilledIds.has(questionId)
|
|
}, [prefilledIds])
|
|
|
|
const renderHelpText = (question: ScopeProfilingQuestion) => {
|
|
if (!question.helpText) return null
|
|
|
|
return (
|
|
<>
|
|
<button
|
|
type="button"
|
|
className="ml-2 text-blue-400 hover:text-blue-600 inline-flex items-center"
|
|
onClick={(e) => {
|
|
e.preventDefault()
|
|
toggleHelp(question.id)
|
|
}}
|
|
>
|
|
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
<path
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
strokeWidth={2}
|
|
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
|
|
/>
|
|
</svg>
|
|
</button>
|
|
{expandedHelp.has(question.id) && (
|
|
<div className="flex items-start gap-2 mt-2 p-2.5 bg-blue-50 rounded-lg text-xs text-blue-700 leading-relaxed">
|
|
<svg className="w-4 h-4 mt-0.5 flex-shrink-0 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
<path
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
strokeWidth={2}
|
|
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
|
|
/>
|
|
</svg>
|
|
<span>{question.helpText}</span>
|
|
</div>
|
|
)}
|
|
</>
|
|
)
|
|
}
|
|
|
|
const renderPrefilledBadge = (questionId: string) => {
|
|
if (!isPrefilledFromProfile(questionId)) return null
|
|
return (
|
|
<span className="ml-2 inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-700">
|
|
Aus Profil
|
|
</span>
|
|
)
|
|
}
|
|
|
|
const renderQuestion = (question: ScopeProfilingQuestion) => {
|
|
const currentValue = getAnswerValue(answers, question.id)
|
|
|
|
switch (question.type) {
|
|
case 'boolean':
|
|
return (
|
|
<div className="space-y-2">
|
|
<div className="flex items-start justify-between">
|
|
<div className="flex items-center flex-wrap gap-1">
|
|
<span className="text-sm font-medium text-gray-900">
|
|
{question.question}
|
|
</span>
|
|
{question.required && <span className="text-red-500 ml-1">*</span>}
|
|
{renderPrefilledBadge(question.id)}
|
|
{renderHelpText(question)}
|
|
</div>
|
|
</div>
|
|
<div className="flex gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={() => handleAnswerChange(question.id, true)}
|
|
className={`flex-1 py-2 px-4 rounded-lg border-2 transition-all ${
|
|
currentValue === true
|
|
? 'border-purple-500 bg-purple-50 text-purple-700 font-medium'
|
|
: 'border-gray-300 bg-white text-gray-700 hover:border-gray-400'
|
|
}`}
|
|
>
|
|
Ja
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => handleAnswerChange(question.id, false)}
|
|
className={`flex-1 py-2 px-4 rounded-lg border-2 transition-all ${
|
|
currentValue === false
|
|
? 'border-purple-500 bg-purple-50 text-purple-700 font-medium'
|
|
: 'border-gray-300 bg-white text-gray-700 hover:border-gray-400'
|
|
}`}
|
|
>
|
|
Nein
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)
|
|
|
|
case 'single':
|
|
return (
|
|
<div className="space-y-2">
|
|
<div className="flex items-center flex-wrap gap-1">
|
|
<span className="text-sm font-medium text-gray-900">
|
|
{question.question}
|
|
</span>
|
|
{question.required && <span className="text-red-500 ml-1">*</span>}
|
|
{renderPrefilledBadge(question.id)}
|
|
{renderHelpText(question)}
|
|
</div>
|
|
<div className="space-y-2">
|
|
{question.options?.map((option) => (
|
|
<button
|
|
key={option.value}
|
|
type="button"
|
|
onClick={() => handleAnswerChange(question.id, option.value)}
|
|
className={`w-full text-left py-3 px-4 rounded-lg border-2 transition-all ${
|
|
currentValue === option.value
|
|
? 'border-purple-500 bg-purple-50 text-purple-700 font-medium'
|
|
: 'border-gray-300 bg-white text-gray-700 hover:border-gray-400'
|
|
}`}
|
|
>
|
|
{option.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)
|
|
|
|
case 'multi':
|
|
return (
|
|
<div className="space-y-2">
|
|
<div className="flex items-center flex-wrap gap-1">
|
|
<span className="text-sm font-medium text-gray-900">
|
|
{question.question}
|
|
</span>
|
|
{question.required && <span className="text-red-500 ml-1">*</span>}
|
|
{renderPrefilledBadge(question.id)}
|
|
{renderHelpText(question)}
|
|
</div>
|
|
<div className="space-y-2">
|
|
{question.options?.map((option) => {
|
|
const selectedValues = Array.isArray(currentValue) ? currentValue as string[] : []
|
|
const isChecked = selectedValues.includes(option.value)
|
|
return (
|
|
<label
|
|
key={option.value}
|
|
className={`flex items-center gap-3 py-3 px-4 rounded-lg border-2 cursor-pointer transition-all ${
|
|
isChecked
|
|
? 'border-purple-500 bg-purple-50'
|
|
: 'border-gray-300 bg-white hover:border-gray-400'
|
|
}`}
|
|
>
|
|
<input
|
|
type="checkbox"
|
|
checked={isChecked}
|
|
onChange={(e) => {
|
|
const newValues = e.target.checked
|
|
? [...selectedValues, option.value]
|
|
: selectedValues.filter((v) => v !== option.value)
|
|
handleAnswerChange(question.id, newValues)
|
|
}}
|
|
className="w-5 h-5 text-purple-600 border-gray-300 rounded focus:ring-purple-500"
|
|
/>
|
|
<span className={isChecked ? 'text-purple-700 font-medium' : 'text-gray-700'}>
|
|
{option.label}
|
|
</span>
|
|
</label>
|
|
)
|
|
})}
|
|
</div>
|
|
</div>
|
|
)
|
|
|
|
case 'number':
|
|
return (
|
|
<div className="space-y-2">
|
|
<div className="flex items-center flex-wrap gap-1">
|
|
<span className="text-sm font-medium text-gray-900">
|
|
{question.question}
|
|
</span>
|
|
{question.required && <span className="text-red-500 ml-1">*</span>}
|
|
{renderPrefilledBadge(question.id)}
|
|
{renderHelpText(question)}
|
|
</div>
|
|
<input
|
|
type="number"
|
|
value={currentValue != null ? String(currentValue) : ''}
|
|
onChange={(e) => handleAnswerChange(question.id, parseInt(e.target.value, 10))}
|
|
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent"
|
|
placeholder="Zahl eingeben"
|
|
/>
|
|
</div>
|
|
)
|
|
|
|
case 'text':
|
|
return (
|
|
<div className="space-y-2">
|
|
<div className="flex items-center flex-wrap gap-1">
|
|
<span className="text-sm font-medium text-gray-900">
|
|
{question.question}
|
|
</span>
|
|
{question.required && <span className="text-red-500 ml-1">*</span>}
|
|
{renderPrefilledBadge(question.id)}
|
|
{renderHelpText(question)}
|
|
</div>
|
|
<input
|
|
type="text"
|
|
value={currentValue != null ? String(currentValue) : ''}
|
|
onChange={(e) => handleAnswerChange(question.id, e.target.value)}
|
|
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent"
|
|
placeholder="Text eingeben"
|
|
/>
|
|
</div>
|
|
)
|
|
|
|
default:
|
|
return null
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex gap-6 h-full">
|
|
{/* Left Sidebar - Block Navigation */}
|
|
<div className="w-64 flex-shrink-0">
|
|
<div className="bg-white rounded-xl border border-gray-200 p-4 sticky top-4">
|
|
<h3 className="text-sm font-semibold text-gray-900 mb-3">Fortschritt</h3>
|
|
<div className="space-y-2">
|
|
{SCOPE_QUESTION_BLOCKS.map((block, idx) => {
|
|
const progress = getBlockProgress(answers, block.id)
|
|
const isActive = idx === currentBlockIndex
|
|
return (
|
|
<button
|
|
key={block.id}
|
|
type="button"
|
|
onClick={() => setCurrentBlockIndex(idx)}
|
|
className={`w-full text-left px-3 py-2 rounded-lg transition-all ${
|
|
isActive
|
|
? 'bg-purple-50 border-2 border-purple-500'
|
|
: 'bg-gray-50 border border-gray-200 hover:border-gray-300'
|
|
}`}
|
|
>
|
|
<div className="flex items-center justify-between mb-1">
|
|
<span className={`text-sm font-medium ${isActive ? 'text-purple-700' : 'text-gray-700'}`}>
|
|
{block.title}
|
|
</span>
|
|
<span className={`text-xs font-semibold ${isActive ? 'text-purple-600' : 'text-gray-500'}`}>
|
|
{progress}%
|
|
</span>
|
|
</div>
|
|
<div className="w-full bg-gray-200 rounded-full h-1.5 overflow-hidden">
|
|
<div
|
|
className={`h-full transition-all ${isActive ? 'bg-purple-500' : 'bg-gray-400'}`}
|
|
style={{ width: `${progress}%` }}
|
|
/>
|
|
</div>
|
|
</button>
|
|
)
|
|
})}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Main Content Area */}
|
|
<div className="flex-1 space-y-6">
|
|
{/* Progress Bar */}
|
|
<div className="bg-white rounded-xl border border-gray-200 p-4">
|
|
<div className="flex items-center justify-between mb-2">
|
|
<span className="text-sm font-medium text-gray-700">Gesamtfortschritt</span>
|
|
<div className="flex items-center gap-3">
|
|
<span className="text-sm text-gray-500">
|
|
{completionStats.answered} / {completionStats.total} Fragen
|
|
</span>
|
|
<span className="text-sm font-bold text-gray-900">{totalProgress}%</span>
|
|
</div>
|
|
</div>
|
|
<div className="w-full bg-gray-200 rounded-full h-3 overflow-hidden">
|
|
<div
|
|
className="h-full bg-gradient-to-r from-purple-500 to-blue-500 transition-all duration-500"
|
|
style={{ width: `${totalProgress}%` }}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Current Block */}
|
|
<div className="bg-white rounded-xl border border-gray-200 p-6">
|
|
<div className="flex items-start justify-between mb-6">
|
|
<div>
|
|
<h2 className="text-2xl font-bold text-gray-900 mb-2">{currentBlock.title}</h2>
|
|
<p className="text-gray-600">{currentBlock.description}</p>
|
|
</div>
|
|
{companyProfile && (
|
|
<button
|
|
type="button"
|
|
onClick={handlePrefillFromProfile}
|
|
className="px-4 py-2 text-sm bg-blue-50 text-blue-700 border border-blue-300 rounded-lg hover:bg-blue-100 transition-colors whitespace-nowrap"
|
|
>
|
|
Aus Profil uebernehmen
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
{/* Questions */}
|
|
<div className="space-y-6">
|
|
{currentBlock.questions.map((question) => (
|
|
<div key={question.id} className="border-b border-gray-100 pb-6 last:border-b-0 last:pb-0">
|
|
{renderQuestion(question)}
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Navigation Buttons */}
|
|
<div className="flex items-center justify-between bg-white rounded-xl border border-gray-200 p-4">
|
|
<button
|
|
type="button"
|
|
onClick={handleBack}
|
|
disabled={currentBlockIndex === 0}
|
|
className="px-6 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
|
>
|
|
Zurueck
|
|
</button>
|
|
<span className="text-sm text-gray-600">
|
|
Block {currentBlockIndex + 1} von {SCOPE_QUESTION_BLOCKS.length}
|
|
</span>
|
|
{currentBlockIndex === SCOPE_QUESTION_BLOCKS.length - 1 ? (
|
|
<button
|
|
type="button"
|
|
onClick={onEvaluate}
|
|
disabled={!canEvaluate || isEvaluating}
|
|
className="px-6 py-2 bg-purple-600 text-white rounded-lg hover:bg-purple-700 transition-colors font-medium disabled:opacity-50 disabled:cursor-not-allowed"
|
|
>
|
|
{isEvaluating ? 'Evaluiere...' : 'Auswertung starten'}
|
|
</button>
|
|
) : (
|
|
<button
|
|
type="button"
|
|
onClick={handleNext}
|
|
className="px-6 py-2 bg-purple-600 text-white rounded-lg hover:bg-purple-700 transition-colors font-medium"
|
|
>
|
|
Weiter
|
|
</button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|