import { useEffect, useMemo, useState } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { SettingsGrid } from "@/components/settings-layout" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Switch } from "@/components/ui/switch" import { Textarea } from "@/components/ui/textarea" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover" import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command" import { cn } from "@/lib/utils" import { Check, ChevronsUpDown, Loader2, MessageSquare, Link, Shield } from "lucide-react" import { ContentSettings } from "../../types" const getApiUrl = () => (window as any).swiftCommerceData?.apiUrl || '/wp-json/swift-commerce/v1' const getNonce = () => (window as any).swiftCommerceData?.restNonce || '' interface PrivacyPage { id: number title: string url: string } interface GeneralTabProps { contentSettings: ContentSettings updateContentSettings: (key: K, value: ContentSettings[K]) => void } interface PageComboboxProps { pages: PrivacyPage[] selectedPageId: number loading: boolean onSelect: (page: PrivacyPage) => void } function PageCombobox({ pages, selectedPageId, loading, onSelect }: PageComboboxProps) { const [open, setOpen] = useState(false) const selectedPage = pages.find(page => page.id === selectedPageId) return ( No published pages found. {pages.map((page) => ( { onSelect(page) setOpen(false) }} > {page.title} ))} ) } export function GeneralTab({ contentSettings, updateContentSettings }: GeneralTabProps) { const [pages, setPages] = useState([]) const [pagesLoading, setPagesLoading] = useState(false) const privacyPolicySource = contentSettings.privacyPolicySource || 'custom' const selectedPageUrl = useMemo(() => { return pages.find(page => page.id === contentSettings.privacyPolicyPageId)?.url || contentSettings.privacyPolicyUrl }, [contentSettings.privacyPolicyPageId, contentSettings.privacyPolicyUrl, pages]) useEffect(() => { if (!contentSettings.showPrivacyLink || privacyPolicySource !== 'page' || pages.length > 0) { return } let isMounted = true const fetchPages = async () => { setPagesLoading(true) try { const response = await fetch(`${getApiUrl()}/cookie-consent/pages`, { headers: { 'X-WP-Nonce': getNonce(), }, }) if (!response.ok) { throw new Error('Failed to fetch pages') } const data = await response.json() if (isMounted && data.success && Array.isArray(data.pages)) { setPages(data.pages) } } catch (error) { console.error('Error fetching privacy policy pages:', error) } finally { if (isMounted) { setPagesLoading(false) } } } fetchPages() return () => { isMounted = false } }, [contentSettings.showPrivacyLink, pages.length, privacyPolicySource]) const updatePrivacyPolicySource = (source: ContentSettings['privacyPolicySource']) => { updateContentSettings('privacyPolicySource', source) } const selectPrivacyPage = (page: PrivacyPage) => { updateContentSettings('privacyPolicyPageId', page.id) updateContentSettings('privacyPolicyUrl', page.url) } return ( {/* Popup Content */} Popup Content Customize the text displayed in your cookie consent popup
updateContentSettings('title', e.target.value)} placeholder="We Value Your Privacy" />