"use client" import { cn } from "@mdxui/primitives/lib/utils" import { Button } from "@mdxui/primitives/button" import { Textarea } from "@mdxui/primitives/textarea" import { Label } from "@mdxui/primitives/label" import { Checkbox } from "@mdxui/primitives/checkbox" import { useState } from "react" import type { ResolutionStatus, SatisfactionRating, ResolutionData, ResolutionUser } from "./types" const resolutionOptions: Array<{ value: ResolutionStatus; label: string }> = [ { value: 'yes', label: 'Yes' }, { value: 'partially', label: 'Partially' }, { value: 'no', label: 'No' }, ] const satisfactionOptions: SatisfactionRating[] = [1, 2, 3, 4, 5] interface ResolutionContentProps { ticketId?: string showFollowUp?: boolean user?: ResolutionUser context?: Record captureMetadata?: boolean onSubmit?: (data: ResolutionData) => Promise | void apiEndpoint?: string submitLabel?: string onSuccess: () => void onError: (error: string) => void } function captureCurrentMetadata() { return { url: window.location.href, timestamp: new Date().toISOString(), viewport: { width: window.innerWidth, height: window.innerHeight }, userAgent: navigator.userAgent, } } export function ResolutionContent({ ticketId, showFollowUp = true, user, context, captureMetadata = true, onSubmit, apiEndpoint, submitLabel = "Submit", onSuccess, onError, }: ResolutionContentProps) { const [resolved, setResolved] = useState(null) const [satisfaction, setSatisfaction] = useState(null) const [feedback, setFeedback] = useState("") const [stillNeedsHelp, setStillNeedsHelp] = useState("") const [wantsFollowUp, setWantsFollowUp] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false) const showStillNeedsHelp = resolved === 'no' || resolved === 'partially' const isValid = resolved !== null && satisfaction !== null const handleSubmit = async () => { if (!resolved || !satisfaction) return setIsSubmitting(true) const data: ResolutionData = { resolved, satisfaction, feedback: feedback.trim() || undefined, stillNeedsHelp: showStillNeedsHelp ? stillNeedsHelp.trim() || undefined : undefined, wantsFollowUp: showFollowUp ? wantsFollowUp : undefined, ticketId, user, context, metadata: captureMetadata ? captureCurrentMetadata() : undefined, } try { if (onSubmit) { await onSubmit(data) } else if (apiEndpoint) { const response = await fetch(apiEndpoint, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }) if (!response.ok) { throw new Error(`Failed to submit: ${response.statusText}`) } } onSuccess() } catch (err) { onError(err instanceof Error ? err.message : "Failed to submit resolution") } finally { setIsSubmitting(false) } } return (
{/* Resolution status */}
{resolutionOptions.map(({ value, label }) => ( ))}
{/* Satisfaction rating */}
{satisfactionOptions.map((rating) => ( ))}
Very dissatisfied Very satisfied
{/* Still needs help (conditional) */} {showStillNeedsHelp && (