import React, { useState, useMemo, useRef, useEffect } from 'react'; import { ChevronLeft, ChevronRight, Check } from 'lucide-react'; import type { IData } from '../App.interface'; import { getAutoSuggestionMarkers } from '../App.utility'; import Logo from '../assets/logo.png'; import './TimelineSelector.css'; interface TimelineSelectorProps { data: IData[]; selectedTags: string[]; setSelectedTags: React.Dispatch>; } const Header = () => { return (
React Native Performance Tracker Icon

React Native{' '} Performance Tracker

); }; const TimeLineSelector: React.FC = ({ data, selectedTags, setSelectedTags, }) => { const [canScrollLeft, setCanScrollLeft] = useState(false); const [canScrollRight, setCanScrollRight] = useState(false); const scrollContainerRef = useRef(null); const uniqueSortedData = useMemo( () => getAutoSuggestionMarkers(data), [data] ); const handleTagClick = (tag: string) => { setSelectedTags((prev) => { if (prev.includes(tag)) { return prev.filter((t) => t !== tag); } else if (prev.length < 2) { return [...prev, tag].sort((a, b) => { const indexA = uniqueSortedData.findIndex((t) => t === a); const indexB = uniqueSortedData.findIndex((t) => t === b); return indexA - indexB; }); } else { alert( '⚠️ Limit Reached! You can only select up to 2 markers. Please deselect one to select another. 😊' ); } return prev; }); }; const getColor = (index: number) => { const colors = [ '#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A', '#98D8C8', '#F06292', '#AED581', '#7986CB', '#4DB6AC', '#FFD54F', ]; return colors[index % colors.length]; }; const isDisabled = (index: number) => { if (selectedTags.length === 0) return false; const firstSelectedIndex = uniqueSortedData.findIndex( (tag) => tag === selectedTags[0] ); return index < firstSelectedIndex; }; useEffect(() => { const checkScroll = () => { if (scrollContainerRef.current) { const { scrollLeft, scrollWidth, clientWidth } = scrollContainerRef.current; setCanScrollLeft(scrollLeft > 0); setCanScrollRight(scrollLeft < scrollWidth - clientWidth); } }; const scrollContainer = scrollContainerRef.current; if (scrollContainer) { scrollContainer.addEventListener('scroll', checkScroll); checkScroll(); } return () => { if (scrollContainer) { scrollContainer.removeEventListener('scroll', checkScroll); } }; }, []); const handleScroll = (direction: 'left' | 'right') => { if (scrollContainerRef.current) { const scrollAmount = scrollContainerRef.current.clientWidth / 2; scrollContainerRef.current.scrollBy({ left: direction === 'left' ? -scrollAmount : scrollAmount, behavior: 'smooth', }); } }; // Calculate the width based on the number of items, with a minimum width and extra padding const svgWidth = Math.max(1000, uniqueSortedData.length * 150 + 300); return (
{uniqueSortedData.map((tag, index) => { const xPosition = (index / (uniqueSortedData.length - 1)) * (svgWidth - 300) + 150; const color = getColor(index); const isSelected = selectedTags.includes(tag); const disabled = isDisabled(index); const isEven = index % 2 === 0; return ( !disabled && handleTagClick(tag)} /> {isSelected && ( )} {tag} ); })}
); }; export default TimeLineSelector;