import { useEffect, useState, useCallback, useRef, forwardRef, } from '@wordpress/element'; import { Icon } from '@wordpress/icons'; import { TabGroup, TabList, TabPanels, TabPanel, Tab } from '@headlessui/react'; import classNames from 'classnames'; import { CardContent } from '@assist/components/dashboard/CardContent'; import { CardsTitle } from '@assist/components/dashboard/CardsTitle'; import { useTours } from '@assist/hooks/useTours'; import { useTasksStore } from '@assist/state/tasks'; import { Bullet, Check } from '@assist/svg'; export const DesktopCards = ({ className, tasks, totalCompleted }) => { const { isCompleted } = useTasksStore(); const { finishedTour } = useTours(); const [nextTask, setNextTask] = useState(-1); const tabRefs = useRef([]); const findNextTask = useCallback( (currentIndex = 0) => { // Re sort based on current index const sortedTasks = tasks .slice(currentIndex) .concat(tasks.slice(0, currentIndex)); const next = sortedTasks.findIndex(({ slug }) => !isCompleted(slug)); // Find the index of the next incomplete task from the original list return tasks.findIndex(({ slug }) => slug === sortedTasks?.[next]?.slug); }, [tasks, isCompleted], ); useEffect(() => { if (totalCompleted === tasks.length || totalCompleted === 0) return; setNextTask((prev) => findNextTask(prev + 1)); }, [tasks?.length, totalCompleted, findNextTask]); useEffect(() => { tabRefs.current?.[nextTask]?.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'start', }); }, [nextTask]); return (