diff --git a/app/components/Navbar.tsx b/app/components/Navbar.tsx index 00096b1..4184555 100644 --- a/app/components/Navbar.tsx +++ b/app/components/Navbar.tsx @@ -1,10 +1,84 @@ 'use client'; import { Box, Button, ButtonIcon, SearchIcon, Input, InputField, Icon, Text, Link } from '@gluestack-ui/themed'; -import { UserRound } from 'lucide-react'; -import React from 'react'; +import { Search, UserRound } from 'lucide-react'; +import { useRouter } from 'next/router'; +import React, { useEffect, useState } from 'react'; +import Select, { components } from 'react-select'; + +interface Concept { + value: string; + label: string; +} const Navbar: React.FC = () => { + const [concepts, setConcepts] = useState([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + const [selectedTerm, setSelectedTerm] = useState(''); + const router = useRouter(); + + const fetchConcepts = async () => { + try { + setLoading(true); + setError(null); + const response = await fetch('/api/getAllConceptMap'); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); + + if (!data.allConceptMap || Object.keys(data.allConceptMap).length === 0) { + throw new Error('No concepts found in the response'); + } + + const conceptsArray: Concept[] = Object.entries(data.allConceptMap).map(([key, value]) => ({ + value: key, + label: String(value), + })); + + conceptsArray.sort((a, b) => a.label.localeCompare(b.label)); + + setConcepts(conceptsArray); + } catch (error) { + console.error('Errore durante il fetch dei concetti:', error); + setError('Failed to load concepts. Please try again later.'); + } finally { + setLoading(false); + } + }; + + useEffect(() => { + fetchConcepts(); + }, []); + + const handleNavigation = (selectedOption: Concept | null) => { + if (selectedOption) { + const parts = selectedOption.value.split('/'); + + if (parts.length >= 3) { + const vocabulary = parts[parts.length - 2]; + const term = parts[parts.length - 1]; + + router.push(`/${vocabulary}/${term}`); + } else { + console.warn("URL non valido: ", selectedOption.value); + router.push(selectedOption.value); + } + } + }; + + const DropdownIndicator = (props: any) => { + return ( + + handleNavigation(props.selectProps.value)} + /> + + ); + }; + return ( { Pilot Project - - - - - - + {loading ? ( + Loading concepts... + ) : error ? ( + {error} + ) : ( + +