import { useState } from 'react'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Badge } from '@/components/ui/badge'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { useToast } from '@/hooks/useToast'; import { enableRelatr, getRelatrConfig, disableRelatr } from '@/hooks/useRelatrScore'; import { useNostr } from '@nostrify/react'; import { Shield, ExternalLink, Search, CheckCircle2, ChevronDown, ChevronUp } from 'lucide-react'; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible'; import { useTranslation } from '@/contexts/language'; /** * RelatrSettings component for configuring Relatr global trust score integration * * Relatr publishes trust scores as NIP-85 Trusted Assertion events (kind 30382). * Auto-enabled by default for all users with ContextVM's official service. */ export function RelatrSettings() { const { toast } = useToast(); const { t } = useTranslation(); const { nostr } = useNostr(); const config = getRelatrConfig(); const [pubkey, setPubkey] = useState(config.servicePubkey); const [relay, setRelay] = useState(config.relay); const [isSearching, setIsSearching] = useState(false); const [isAdvancedOpen, setIsAdvancedOpen] = useState(false); const handleAutoDiscover = async () => { setIsSearching(true); toast({ title: t('settingsUi.relatr.searchingTitle'), description: t('settingsUi.relatr.searchingDesc'), }); try { // Search for profiles mentioning "relatr" or "contextvm" const profiles = await nostr.query( [{ kinds: [0], search: 'relatr', limit: 10 }], { signal: AbortSignal.timeout(10000) } ); console.log('[RelatrSettings] Found profiles:', profiles); // Look for official Relatr service for (const profile of profiles) { try { const metadata = JSON.parse(profile.content); const name = metadata.name?.toLowerCase() || ''; const about = metadata.about?.toLowerCase() || ''; const website = metadata.website?.toLowerCase() || ''; if ( name.includes('relatr') || about.includes('trust score') || website.includes('relatr.xyz') ) { setPubkey(profile.pubkey); toast({ title: t('settingsUi.relatr.foundTitle'), description: t('settingsUi.relatr.foundDesc', { name: metadata.name || t('settingsUi.relatr.unknown') }), }); return; } } catch { // Skip invalid JSON } } toast({ title: t('settingsUi.relatr.notFoundTitle'), description: t('settingsUi.relatr.notFoundDesc'), variant: 'destructive', }); } catch (error) { console.error('[RelatrSettings] Auto-discovery failed:', error); toast({ title: t('settingsUi.relatr.searchFailedTitle'), description: t('settingsUi.relatr.searchFailedDesc'), variant: 'destructive', }); } finally { setIsSearching(false); } }; const handleEnable = () => { if (!pubkey || pubkey.length !== 64) { toast({ title: t('settingsUi.relatr.invalidPubkeyTitle'), description: t('settingsUi.relatr.invalidPubkeyDesc'), variant: 'destructive', }); return; } enableRelatr(pubkey, relay); toast({ title: t('settingsUi.relatr.enabledTitle'), description: t('settingsUi.relatr.enabledDesc'), }); }; const handleDisable = () => { disableRelatr(); toast({ title: t('settingsUi.relatr.disabledTitle'), description: t('settingsUi.relatr.disabledDesc'), }); }; return ( {t('settingsUi.relatr.title')} {t('settingsUi.relatr.desc')} {/* Status - Always visible */}
{config.enabled ? ( <> {t('settingsUi.relatr.active')} ) : ( t('settingsUi.relatr.disabled') )}
{/* Info Alert */} {config.enabled ? ( <> {t('settingsUi.relatr.activeStrong')} {t('settingsUi.relatr.activeDesc')} ) : ( <> {t('settingsUi.relatr.disabledStrong')} {t('settingsUi.relatr.disabledHint')} )} {/* Quick Actions */} {config.enabled ? ( ) : ( )}
{/* Advanced Settings - Collapsible */} {/* Service Pubkey Input */}
setPubkey(e.target.value)} className="font-mono text-xs" maxLength={64} />

{t('settingsUi.relatr.pubkeyDefault')}

{/* Relay Input */}
setRelay(e.target.value)} className="font-mono text-xs" />

{t('settingsUi.relatr.relayDefault')}

{/* Apply Changes Button */} {/* Documentation Links */}

{t('settingsUi.relatr.learnMore')}

); }