import { lazy, memo, useCallback, useMemo, useState, type FC, type ReactNode, type ComponentType, isValidElement, } from "react"; import { AnimatePresence } from "framer-motion"; import StyledTaskbarEntry from "./StyledTaskbarEntry"; import useTaskbarTransition from "./useTaskbarTransition"; import useTitlebarContextMenu from "../../Window/Titlebar/useTitlebarContextMenu"; import useNextFocusable from "../../Window/useNextFocusable"; import { useWindows } from "@/contexts/windows"; import { useSession } from "@/contexts/session"; import StyledTaskbarButton from "./StyledTEbutton"; import Icon from "@/styles/common/Icon"; import { DIV_BUTTON_PROPS } from "@/utils/constants"; import { isSafari, label } from "@/utils/functions"; import { useConfig } from "@/contexts/config"; const PeekWindow = lazy(() => import("./Peek/PeekWindow")); type TaskbarEntryProps = { icon: string | ReactNode | ComponentType; id: string; title: string; // eslint-disable-next-line @typescript-eslint/no-explicit-any [property: string]: any; }; const TaskbarEntry: FC = ({ icon, id, title, ...otherProps }) => { const nextFocusableId = useNextFocusable(id); const { foregroundId, setForegroundId, setPreviewID } = useSession(); const isForeground = id === foregroundId; const { linkElement, minimize, windows: { [id]: currentWindow }, } = useWindows(); const { enablePeek } = useConfig(); const { minimized, progress } = currentWindow || {}; const linkTaskbarEntry = useCallback( (taskbarEntry: HTMLButtonElement | HTMLDivElement | null) => { if (taskbarEntry) linkElement(id, "taskbarEntry", taskbarEntry); }, [id, linkElement] ); const [isPeekVisible, setIsPeekVisible] = useState(false); const hidePeek = (): void => { setIsPeekVisible(false); setPreviewID(false); }; const showPeek = (): void => { setIsPeekVisible(true); setPreviewID(id); }; const onClick = (): void => { if (minimized || isForeground) minimize(id); setForegroundId(isForeground ? nextFocusableId : id); }; const focusable = useMemo(() => (isSafari() ? DIV_BUTTON_PROPS : {}), []); const renderIcon = useMemo(() => { if (typeof icon === "string") { return ; } else if (isValidElement(icon)) { return
{icon}
; } else if (typeof icon === "function") { const IconComponent = icon; return (
); } }, [icon, title]); const stopPropagation = (event: React.MouseEvent) => { event.stopPropagation(); }; return ( {enablePeek && ( {isPeekVisible && } )}
{renderIcon}
{title}
); }; export default memo(TaskbarEntry);