import React, { useEffect, useRef } from 'react'
import { BLACK_CIRCLE, BULLET_OPERATOR } from '../constants/figures.js'
import { Box, Text } from '@anthropic/ink'
import type { SkillUpdate } from '../utils/hooks/skillImprovement.js'
import { normalizeFullWidthDigits } from '../utils/stringUtils.js'
import { isValidResponseInput } from './FeedbackSurvey/FeedbackSurveyView.js'
import type { FeedbackSurveyResponse } from './FeedbackSurvey/utils.js'
type Props = {
isOpen: boolean
skillName: string
updates: SkillUpdate[]
handleSelect: (selected: FeedbackSurveyResponse) => void
inputValue: string
setInputValue: (value: string) => void
}
export function SkillImprovementSurvey({
isOpen,
skillName,
updates,
handleSelect,
inputValue,
setInputValue,
}: Props): React.ReactNode {
if (!isOpen) {
return null
}
// Hide the survey if the user is typing anything other than a survey response
if (inputValue && !isValidResponseInput(inputValue)) {
return null
}
return (
)
}
type ViewProps = {
skillName: string
updates: SkillUpdate[]
onSelect: (option: FeedbackSurveyResponse) => void
inputValue: string
setInputValue: (value: string) => void
}
// Only 1 (apply) and 0 (dismiss) are valid for this survey
const VALID_INPUTS = ['0', '1'] as const
function isValidInput(input: string): boolean {
return (VALID_INPUTS as readonly string[]).includes(input)
}
function SkillImprovementSurveyView({
skillName,
updates,
onSelect,
inputValue,
setInputValue,
}: ViewProps): React.ReactNode {
const initialInputValue = useRef(inputValue)
useEffect(() => {
if (inputValue !== initialInputValue.current) {
const lastChar = normalizeFullWidthDigits(inputValue.slice(-1))
if (isValidInput(lastChar)) {
setInputValue(inputValue.slice(0, -1))
// Map: 1 = "good" (apply), 0 = "dismissed"
onSelect(lastChar === '1' ? 'good' : 'dismissed')
}
}
}, [inputValue, onSelect, setInputValue])
return (
{BLACK_CIRCLE}
Skill improvement suggested for "{skillName}"
{updates.map((u, i) => (
{BULLET_OPERATOR} {u.change}
))}
1: Apply
0: Dismiss
)
}