import React from 'react'; import { AuthenticatedLayout } from '../layout/AuthenticatedLayout'; import { Card, CardContent, CardHeader, CardTitle } from '../ui/card'; import { Button } from '../ui/button'; import { Badge } from '../ui/badge'; import { toast } from "sonner"; import { ArrowLeft, Edit, Calendar, User, Tag, Globe, Copy, Share2 } from 'lucide-react'; import { Route } from '../../types/routes'; import { useGetFAQByIdQuery } from '../../store/services/contentManager.service'; interface ViewFAQProps { onNavigate: (route: Route) => void; onLogout: () => void; user: any; faqId?: string; } export function ViewFAQ({ onNavigate, onLogout, user, faqId }: ViewFAQProps) { console.log('🔍 ViewFAQ component rendered with ID:', faqId); // Use the FAQ by ID query const { data: faq, isLoading, error } = useGetFAQByIdQuery(faqId!, { skip: !faqId, }); console.log('📊 FAQ Query Result:', { data: faq, isLoading, error }); const handleEdit = () => { if (faq) { onNavigate(`/content/faqs/edit/${faq.id}`); } }; const handleCopyToClipboard = async () => { if (faq) { const text = `Q: ${faq.question}\nA: ${faq.answer}`; try { await navigator.clipboard.writeText(text); toast.success('FAQ copied to clipboard'); } catch (err) { toast.error('Failed to copy to clipboard'); } } }; const handleShare = async () => { if (faq) { const shareData = { title: faq.question, text: `Q: ${faq.question}\nA: ${faq.answer}`, url: window.location.href, }; if (navigator.share) { try { await navigator.share(shareData); } catch (err) { console.log('Error sharing:', err); } } else { // Fallback to clipboard handleCopyToClipboard(); } } }; const formatDate = (dateString: string) => { return new Date(dateString).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' }); }; // Handle loading state if (isLoading) { return (
{[...Array(4)].map((_, i) => (
))}
); } // Handle error state if (error) { return (
Error Loading FAQ

{error && 'status' in error ? `Error ${error.status}: Failed to load FAQ` : 'Failed to load FAQ. Please try again.'}

); } // Handle FAQ not found if (!faq) { return (
FAQ Not Found

The requested FAQ could not be found.

); } return (
{/* Header */}

View FAQ

Detailed view of the frequently asked question

{/* Main Content */}
{/* Question Card */}
Question

{faq.question}

{/* Answer Card */}
Answer

{faq.answer}

{/* Tags Card */} {(faq.tags.length > 0 || faq.globalTag.length > 0) && ( Tags & Categories {faq.tags.length > 0 && (
Tags
{faq.tags.map((tag, index) => ( {tag} ))}
)} {faq.globalTag.length > 0 && (
Global Tags
{faq.globalTag.map((tag, index) => ( {tag} ))}
)}
)}
{/* Sidebar */}
{/* FAQ Details */} FAQ Details
Category {faq.category || 'Uncategorized'}
FAQ ID {faq.id}
Created {formatDate(faq.createdAt)}
Last Updated {formatDate(faq.updatedAt)}
{/* Quick Actions */} Quick Actions {/* Statistics */} Content Info
Question Length {faq.question.length} chars
Answer Length {faq.answer.length} chars
Total Tags {faq.tags.length + faq.globalTag.length}
Status Published
); }