import React, { useState } from 'react';
import { HelpCircle, Mail, MessageSquare, Book, ChevronRight, ChevronDown, Video, PlayCircle, Filter, Search, LifeBuoy } from 'lucide-react';
import { Card, CardHeader, CardTitle, CardContent } from "../../components/ui/card";
import { YOUTUBE_RESOURCES, VIDEO_CATEGORIES } from '../../data/youtubeResources';
import useAuthStore from "../../store/authStore";
const FAQItem = ({ faq }) => {
const [isOpen, setIsOpen] = useState(false);
return (
setIsOpen(!isOpen)}
>
{isOpen && (
)}
);
};
const Help = () => {
const { profile } = useAuthStore();
const [activeTab, setActiveTab] = useState('All');
const [videos, setVideos] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [searchQuery, setSearchQuery] = useState('');
const [debouncedSearch, setDebouncedSearch] = useState('');
const generateMailto = () => {
const email = "bonthalamadhavi1@gmail.com";
const subject = encodeURIComponent("Support Request: [Issue Summary]");
const fullName = profile?.full_name || "User";
const bodyTemplate = `Hi HelpDesk.AI Support Team,
I am writing to report an issue regarding:
[ENTER YOUR ISSUE HERE]
Regards,
${fullName}`;
return `mailto:${email}?subject=${subject}&body=${encodeURIComponent(bodyTemplate)}`;
};
// Debounce the search query to avoid hammering the YouTube API quota on every keystroke
React.useEffect(() => {
const handler = setTimeout(() => {
setDebouncedSearch(searchQuery);
}, 800);
return () => clearTimeout(handler);
}, [searchQuery]);
React.useEffect(() => {
const fetchVideos = async () => {
setIsLoading(true);
const cacheKey = `yt_videos_v3_${activeTab}_${debouncedSearch}`;
const cacheTimeKey = `yt_videos_time_v3_${activeTab}_${debouncedSearch}`;
try {
const cachedData = localStorage.getItem(cacheKey);
const cacheTimestamp = localStorage.getItem(cacheTimeKey);
// Cache valid for 24 hours to prevent API quota exhaustion
if (cachedData && cacheTimestamp && (Date.now() - parseInt(cacheTimestamp)) < 86400000) {
setVideos(JSON.parse(cachedData));
setIsLoading(false);
return;
}
const API_KEY = import.meta.env.VITE_YOUTUBE_API_KEY;
if (!API_KEY) {
throw new Error("No API Key");
}
const query = debouncedSearch
? `IT helpdesk troubleshooting ${debouncedSearch}`
: activeTab === 'All'
? 'IT helpdesk troubleshooting'
: `IT helpdesk ${activeTab.toLowerCase()} troubleshooting`;
const response = await fetch(
`https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=12&q=${encodeURIComponent(query)}&type=video&key=${API_KEY}`
);
if (!response.ok) { throw new Error("API Error"); }
const data = await response.json();
// Decode HTML entities in YouTube titles
const decodeText = (str) => {
return str.replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, "&");
};
const fetchedVideos = data.items.map(item => ({
id: item.id.videoId,
title: decodeText(item.snippet.title),
description: item.snippet.description,
category: activeTab === 'All' ? 'General' : activeTab,
url: `https://www.youtube.com/watch?v=${item.id.videoId}`,
thumbnail_url: item.snippet.thumbnails?.high?.url || item.snippet.thumbnails?.default?.url
}));
localStorage.setItem(cacheKey, JSON.stringify(fetchedVideos));
localStorage.setItem(cacheTimeKey, Date.now().toString());
setVideos(fetchedVideos);
} catch (error) {
console.warn("YouTube API fallback:", error);
// When API fails, use the static resources but format them to match the new API structure
const fallbackList = activeTab === 'All' ? YOUTUBE_RESOURCES : YOUTUBE_RESOURCES.filter(v => v.category === activeTab);
// Map the static resources to match the structure expected by the modern UI cards
const formattedFallback = fallbackList.map(item => {
// Extract video ID from the static URL to construct the identical structure
const videoId = item.url.split('v=')[1];
return {
id: videoId || item.id,
title: item.title,
description: item.description,
category: item.category,
url: item.url,
thumbnail_url: item.thumbnail_url
};
});
setVideos(formattedFallback);
} finally {
setIsLoading(false);
}
};
fetchVideos();
}, [activeTab, debouncedSearch]);
const faqs = [
{
q: "How does the AI categorization work?",
a: "When you submit a ticket, our DistilBERT model analyzes the text and metadata to instantly route it to the correct support team, bypassing manual triage."
},
{
q: "Can I reopen a resolved ticket?",
a: "Yes. If an issue reoccurs within 7 days, you can click 'Reopen Ticket' directly from your dashboard to alert the assigned agent."
},
{
q: "Where do I track my active requests?",
a: "Navigate to the 'My Tickets' page from the top navigation. You can filter by status, priority, or search the resolution logs directly."
}
];
return (
{/* Premium Hero Banner */}
{/* Decorative background flare */}
24/7 Support Center
How can we help you today?
Search our **Solution Library**, watch curated IT tutorials, or connect with a support agent instantly.
{/* Faux Search Bar representing the Hub */}
{/* Main Content Hub */}
{/* Left Column: Resources (Takes 3 columns) */}
{/* Interactive UI: Video Hub */}
Curated Video Tutorials
Resolve common issues instantly with expert guides.
{/* Modern Tab Filter */}
{VIDEO_CATEGORIES.map((category) => (
))}
{/* Resource Library Grid */}
{/* Top FAQ Section inside Hub */}
Frequently Asked Questions
{faqs.map((faq, index) => (
))}
{/* Right Column: Contact Sidebar (Takes 1 column) */}
Still Need Help?
Our dedicated support teams are ready to assist you.
{/* System Status Sidebar Entry */}
System Status
All services operational
);
};
export default Help;