|
| 1 | +import { useDataRouterContext } from "../../hooks/useDataRouterContext.tsx"; |
| 2 | +import { useMatches, useNavigate } from "react-router-dom"; |
| 3 | +import { useCallback, useEffect, useState } from "react"; |
| 4 | +import { RouterState, AgnosticDataRouteMatch } from "@remix-run/router"; |
| 5 | +import { last, replaceAt, insertAt } from "src/utils/array-utils.ts"; |
| 6 | +import { TabModel, ValidTabMeta } from "src/lib/tabs/tabs.types.ts"; |
| 7 | +import { pathToLocation } from "src/lib/tabs/tabs.utils.ts"; |
| 8 | + |
| 9 | +export type TabbedNavigationMeta = { |
| 10 | + path: string; |
| 11 | + routeId: string; |
| 12 | +}; |
| 13 | + |
| 14 | +type ValidParams = Record<string, unknown>; |
| 15 | + |
| 16 | +type ValidID<Params extends ValidParams = ValidParams> = |
| 17 | + | string |
| 18 | + | { (props: { params: Params }): string }; |
| 19 | + |
| 20 | +type TabConfig< |
| 21 | + Params extends ValidParams = ValidParams, |
| 22 | + ID extends ValidID<Params> = ValidID<Params>, |
| 23 | +> = { |
| 24 | + routeId: string; |
| 25 | + id: ID; |
| 26 | + title: ({ params }: { params: Params }) => string; |
| 27 | +}; |
| 28 | + |
| 29 | +export class Tab< |
| 30 | + Params extends ValidParams = ValidParams, |
| 31 | + ID extends ValidID<Params> = ValidID<Params>, |
| 32 | +> { |
| 33 | + routeId: string; |
| 34 | + id: ID; |
| 35 | + title: ({ params }: { params: Params }) => string; |
| 36 | + constructor(props: TabConfig<Params, ID>) { |
| 37 | + const { id, routeId, title } = props; |
| 38 | + this.id = id; |
| 39 | + this.routeId = routeId; |
| 40 | + this.title = title; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export const useTabbedNavigation2 = < |
| 45 | + Meta extends ValidTabMeta = ValidTabMeta, |
| 46 | + Params extends ValidParams = ValidParams, |
| 47 | +>(options: { |
| 48 | + config: Tab<Params>[]; |
| 49 | + initialTabs?: TabModel<TabbedNavigationMeta & Meta>[]; |
| 50 | + initialStartPinnedTabs?: string[]; |
| 51 | + onCloseAllTabs: () => void; |
| 52 | + resolveTabMeta: (match: AgnosticDataRouteMatch) => Meta; |
| 53 | +}) => { |
| 54 | + const { |
| 55 | + resolveTabMeta, |
| 56 | + onCloseAllTabs, |
| 57 | + initialTabs = [], |
| 58 | + initialStartPinnedTabs = [], |
| 59 | + config, |
| 60 | + } = options; |
| 61 | + const [tabs, setTabs] = |
| 62 | + useState<TabModel<TabbedNavigationMeta & Meta>[]>(initialTabs); |
| 63 | + const [startPinnedTabs, setStartPinnedTabs] = useState<string[]>( |
| 64 | + initialStartPinnedTabs, |
| 65 | + ); |
| 66 | + const { router } = useDataRouterContext(); |
| 67 | + const navigate = useNavigate(); |
| 68 | + |
| 69 | + const setActiveTabId = (id: string | undefined) => { |
| 70 | + const tab = tabs.find((tab) => tab.id === id); |
| 71 | + if (tab) { |
| 72 | + navigate(pathToLocation(tab.meta.path)); |
| 73 | + } else { |
| 74 | + onCloseAllTabs?.(); |
| 75 | + } |
| 76 | + }; |
| 77 | + const updateTabs = useCallback( |
| 78 | + (state: RouterState) => { |
| 79 | + const { matches, location, navigation } = state; |
| 80 | + |
| 81 | + if (navigation.location) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const pairs = matches |
| 86 | + .slice() |
| 87 | + .reverse() |
| 88 | + .map((match) => { |
| 89 | + const def = config.find( |
| 90 | + (tabDef) => tabDef.routeId === match.route.id, |
| 91 | + ); |
| 92 | + return [def, match]; |
| 93 | + }) |
| 94 | + .filter(([def]) => !!def) as unknown as [Tab, AgnosticDataRouteMatch][]; |
| 95 | + |
| 96 | + pairs.forEach(([def, match]) => { |
| 97 | + setTabs((prevTabs) => { |
| 98 | + const tab = prevTabs.find( |
| 99 | + (tab) => |
| 100 | + tab.id === |
| 101 | + (typeof def.id === "function" ? def.id(match) : def.id), |
| 102 | + ); |
| 103 | + |
| 104 | + const { pathname } = last(matches); |
| 105 | + const { search } = location; |
| 106 | + const path = pathname + (search ? `${search}` : ""); |
| 107 | + |
| 108 | + if (tab) { |
| 109 | + // update the tab path |
| 110 | + const index = prevTabs.indexOf(tab); |
| 111 | + |
| 112 | + return replaceAt(prevTabs, index, { |
| 113 | + ...tab, |
| 114 | + meta: { |
| 115 | + ...tab.meta, |
| 116 | + path, |
| 117 | + ...resolveTabMeta(match), |
| 118 | + }, |
| 119 | + }); |
| 120 | + } else { |
| 121 | + const newTab: TabModel<TabbedNavigationMeta & Meta> = { |
| 122 | + id: typeof def.id === "function" ? def.id(match) : def.id, |
| 123 | + title: def.title(match), |
| 124 | + meta: { |
| 125 | + path, |
| 126 | + routeId: match.route.id, |
| 127 | + ...resolveTabMeta(match), |
| 128 | + }, |
| 129 | + }; |
| 130 | + |
| 131 | + // prepend a new tab |
| 132 | + return insertAt(prevTabs, startPinnedTabs.length, newTab); |
| 133 | + } |
| 134 | + }); |
| 135 | + }); |
| 136 | + }, |
| 137 | + [resolveTabMeta, startPinnedTabs, config], |
| 138 | + ); |
| 139 | + |
| 140 | + useEffect(() => { |
| 141 | + // fire immediately |
| 142 | + updateTabs(router.state); |
| 143 | + return router.subscribe(updateTabs); |
| 144 | + }, [router, updateTabs]); |
| 145 | + |
| 146 | + const matches = useMatches(); |
| 147 | + |
| 148 | + const activeTabId = matches |
| 149 | + .slice() |
| 150 | + .reverse() |
| 151 | + .find((match) => { |
| 152 | + return config.find((def) => def.routeId === match.id); |
| 153 | + })?.pathname; |
| 154 | + |
| 155 | + return { |
| 156 | + getTabsProps: () => ({ |
| 157 | + activeTabId, |
| 158 | + tabs, |
| 159 | + onTabsChange: setTabs, |
| 160 | + onActiveTabIdChange: setActiveTabId, |
| 161 | + hasControlledActiveTabId: true, |
| 162 | + startPinnedTabs, |
| 163 | + onStartPinnedTabsChange: setStartPinnedTabs, |
| 164 | + }), |
| 165 | + setTabs, |
| 166 | + setActiveTabId, |
| 167 | + tabs, |
| 168 | + activeTabId, |
| 169 | + }; |
| 170 | +}; |
0 commit comments