import { Icon, type StoredValue, TabContent as BaseTabContent, type TabContentProps, type TabItem as BaseTabItem, Tabs as BaseTabs, type TabsProps as BaseTabsProps, type TabsRef, type TabsStorage, } from "@yaakapp-internal/ui"; import classNames from "classnames"; import type { ReactNode } from "react"; import { forwardRef, memo, useMemo } from "react"; import { useKeyValue } from "../../../hooks/useKeyValue"; import { ErrorBoundary } from "../../ErrorBoundary"; import { Button } from "../Button"; import type { RadioDropdownProps } from "../RadioDropdown"; import { RadioDropdown } from "../RadioDropdown"; export type { TabsRef }; export type TabItem = | Extract | { value: string; options: Omit; leftSlot?: ReactNode; rightSlot?: ReactNode; }; type Props = Omit & { tabs: TabItem[] }; export const Tabs = forwardRef(function Tabs({ tabs, ...props }, ref) { const baseTabs = useMemo(() => tabs.map(toBaseTab), [tabs]); return ; }); export const TabContent = memo(function TabContent(props: Omit) { return ; }); function toBaseTab(tab: TabItem): BaseTabItem { if (!("options" in tab)) return tab; const { options } = tab; return { value: tab.value, leftSlot: tab.leftSlot, rightSlot: tab.rightSlot, renderButton: ({ buttonProps, isActive }) => { const option = options.items.find((i) => "value" in i && i.value === options.value); return ( ); }, }; } function useTabsStoredValue(key: string | string[] | null, fallback: T) { const { value, set } = useKeyValue({ namespace: "no_sync", key: key ?? ["tabs", "default"], fallback, }); return [value ?? fallback, set] as const; } /** * Programmatically set the active tab for a Tabs component that uses storageKey + activeTabKey. * This is useful when you need to change the tab from outside the component (e.g., in response to an event). */ export async function setActiveTab({ storageKey, activeTabKey, value, }: { storageKey: string; activeTabKey: string; value: string; }): Promise { const { getKeyValue, setKeyValue } = await import("../../../lib/keyValueStore"); const current = getKeyValue({ namespace: "no_sync", key: storageKey, fallback: { order: [], activeTabs: {} }, }); await setKeyValue({ namespace: "no_sync", key: storageKey, value: { ...current, activeTabs: { ...current.activeTabs, [activeTabKey]: value }, }, }); }