|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import Papa from 'papaparse'; |
| 3 | +import { useRouter } from 'next/router'; |
| 4 | + |
| 5 | +const Sidebar = () => { |
| 6 | + const router = useRouter(); |
| 7 | + // State to track the selected button, conference data, and dropdown visibility |
| 8 | + const [selectedButton, setSelectedButton] = useState(null); |
| 9 | + const [conferences, setConferences] = useState([]); |
| 10 | + const [dropdownVisible, setDropdownVisible] = useState(false); |
| 11 | + |
| 12 | + // Function to handle button click and update selectedButton state |
| 13 | + const handleButtonClick = (buttonName) => { |
| 14 | + setSelectedButton(buttonName); |
| 15 | + router.push('/'); |
| 16 | + if (buttonName === 'conference') { |
| 17 | + // Toggle dropdown visibility |
| 18 | + setDropdownVisible(!dropdownVisible); |
| 19 | + // If "Conference" button is clicked, fetch and set conference data |
| 20 | + if (!dropdownVisible) { |
| 21 | + fetchConferences(); |
| 22 | + } |
| 23 | + } else { |
| 24 | + // Hide dropdown if another button is clicked |
| 25 | + setDropdownVisible(false); |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + // Function to fetch conference data from CSV file |
| 30 | + const fetchConferences = () => { |
| 31 | + fetch('https://raw.githubusercontent.com/cncf-tags/cloud-native-ai/main/cncf-youtube-channel-summarizer/data/sample_cncf_video_summary.csv') |
| 32 | + .then((response) => { |
| 33 | + if (!response.ok) { |
| 34 | + throw new Error('Network response was not ok'); |
| 35 | + } |
| 36 | + return response.text(); |
| 37 | + }) |
| 38 | + .then((text) => { |
| 39 | + // Parse CSV data using PapaParse and extract 'conference_name' column |
| 40 | + Papa.parse(text, { |
| 41 | + header: true, |
| 42 | + skipEmptyLines: true, |
| 43 | + complete: (result) => { |
| 44 | + const uniqueConferences = new Set(); |
| 45 | + const conferencesData = result.data.reduce((acc, row) => { |
| 46 | + const name = row['conference_name'].trim(); |
| 47 | + if (!uniqueConferences.has(name)) { |
| 48 | + uniqueConferences.add(name); |
| 49 | + acc.push({ name }); |
| 50 | + } |
| 51 | + return acc; |
| 52 | + }, []); |
| 53 | + setConferences(conferencesData); |
| 54 | + }, |
| 55 | + }); |
| 56 | + }) |
| 57 | + .catch((error) => { |
| 58 | + console.error('Error fetching conference data:', error); |
| 59 | + }); |
| 60 | + }; |
| 61 | + |
| 62 | + // Function to handle click on dropdown button |
| 63 | + const handleDropdownButtonClick = () => { |
| 64 | + // Navigate to the new page when dropdown button is clicked |
| 65 | + router.push('/conferences/NewPage'); |
| 66 | + }; |
| 67 | + |
| 68 | + return ( |
| 69 | + <> |
| 70 | + <aside className="fixed top-0 left-0 w-64 h-full" aria-label="Sidenav"> |
| 71 | + <div className="overflow-y-auto py-5 px-3 h-full bg-white border-r border-gray-200 dark:bg-gray-800 dark:border-gray-700"> |
| 72 | + <ul className="space-y-2"> |
| 73 | + <li> |
| 74 | + <a href="#" className={`flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700 ${selectedButton === 'dashboard' ? 'bg-blue-500 text-black' : ''}`} onClick={() => handleButtonClick('dashboard')}> |
| 75 | + <span className="ml-3">Dashboard</span> |
| 76 | + </a> |
| 77 | + </li> |
| 78 | + <li> |
| 79 | + <button type="button" className={`flex items-center p-2 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700 ${selectedButton === 'conference' ? 'bg-blue-500 text-black' : ''}`} onClick={() => handleButtonClick('conference')}> |
| 80 | + <span className="flex-1 ml-3 text-left whitespace-nowrap">Conference</span> |
| 81 | + <svg aria-hidden="true" className="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fillRule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clipRule="evenodd"></path></svg> |
| 82 | + </button> |
| 83 | + {/* Render conference list if the "Conference" button is selected and dropdown is visible */} |
| 84 | + {selectedButton === 'conference' && dropdownVisible && ( |
| 85 | + <ul className="pl-6 mt-2"> |
| 86 | + {/* Render conference items */} |
| 87 | + {conferences.map((conference, index) => ( |
| 88 | + <li key={index} className="py-1"> |
| 89 | + <button type="button" className="text-left text-gray-900 dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700 w-full p-1 rounded-lg transition" onClick={handleDropdownButtonClick}> |
| 90 | + {conference.name} |
| 91 | + </button> |
| 92 | + </li> |
| 93 | + ))} |
| 94 | + </ul> |
| 95 | + )} |
| 96 | + </li> |
| 97 | + </ul> |
| 98 | + </div> |
| 99 | + </aside> |
| 100 | + </> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +export default Sidebar; |
0 commit comments