import { Ionicons } from '@expo/vector-icons'; import type { ComponentProps } from 'react'; import { useEffect, useState } from 'react'; import { ActivityIndicator, Alert, Keyboard, Linking, Pressable, ScrollView, Text, TextInput, View, } from 'react-native'; import { dvfSearchUrl, meilleursAgentsUrlForVille, SECTOR_TOOLS, type SectorTool, } from '@/constants/rechercheMarche'; import { UI } from '@/constants/uiTheme'; import { useAnalyseSecteurForVille, useSaveAnalyseSecteur } from '@/hooks/useAnalysesSecteur'; import { formatPocketBaseError } from '@/utils/pocketbaseErrors'; type IonName = ComponentProps['name']; function resolveToolUrl(tool: SectorTool, ville: string): string { if (tool.id === 'ma') return meilleursAgentsUrlForVille(ville); if (tool.id === 'dvf') return dvfSearchUrl(ville); return tool.url; } export function SecteurTab() { const [ville, setVille] = useState(''); const [notes, setNotes] = useState(''); const secteurQ = useAnalyseSecteurForVille(ville); const saveMut = useSaveAnalyseSecteur(); useEffect(() => { if (secteurQ.data?.notes != null) setNotes(secteurQ.data.notes); }, [secteurQ.data?.id, secteurQ.data?.notes]); const onOpenTool = async (tool: SectorTool) => { const url = resolveToolUrl(tool, ville); const ok = await Linking.canOpenURL(url); if (!ok) { Alert.alert('Lien', 'Impossible d’ouvrir ce lien sur cet appareil.'); return; } void Linking.openURL(url); }; const onAnalyser = () => { const v = ville.trim(); if (!v) { Alert.alert('Ville', 'Indique une ville ou une commune pour cadrer l’analyse.'); return; } Keyboard.dismiss(); Alert.alert( 'Secteur', `Analyse pour « ${v} » : utilise les outils ci-dessous (données externes), puis consigne tes notes en bas de page.`, ); }; const onSaveNotes = async () => { const v = ville.trim(); if (!v) { Alert.alert('Ville', 'Renseigne la ville avant de sauvegarder les notes.'); return; } try { await saveMut.mutateAsync({ ville: v, notes }); } catch (e) { Alert.alert('Erreur', formatPocketBaseError(e)); } }; return ( Ville / commune Analyser Outils marché Données externes — ouverture dans le navigateur. {SECTOR_TOOLS.map((tool) => ( void onOpenTool(tool)} className="mb-3 flex-row items-center rounded-2xl border-2 bg-white p-4 active:opacity-90" style={{ borderColor: UI.border }} > {tool.title} {tool.description} Externe → ))} Notes secteur {secteurQ.isFetching ? : null} void onSaveNotes()} disabled={saveMut.isPending} className="mt-3 min-h-[52px] items-center justify-center rounded-2xl active:opacity-90" style={{ backgroundColor: UI.success }} > {saveMut.isPending ? ( ) : ( Sauvegarder )} ); }