import type { LunaticOverviewItem, LunaticReducerState, PageTag, } from '../type'; import { type DependencyList, type ReactNode, useMemo } from 'react'; import { addIterationToPage, getPageTag, pageTagComparator, } from '../commons/pagination/page-tag'; export type InterpretedLunaticOverviewItem = { id: string; type: string; page: PageTag; label: ReactNode; description: ReactNode; reached: boolean; current: boolean; children: InterpretedLunaticOverviewItem[]; }; /** * Build a filled overview everytime the deps change. */ export const useOverview = ( { overview, executeExpression, pager, options, }: Pick< LunaticReducerState, 'executeExpression' | 'overview' | 'pager' | 'options' >, deps: DependencyList ) => { return useMemo( () => interpretOverview( overview, executeExpression, pager, // ignore active filters for showing every overview items options.activeFilters === 'DISABLED' ), // eslint-disable-next-line react-hooks/exhaustive-deps [...deps, overview, options.activeFilters] ); }; /** * Use Lunatic data to interpret the static overview (calculated on init) with the real data. */ const interpretOverview = ( overviewItems: LunaticOverviewItem[], executeExpression: LunaticReducerState['executeExpression'], pager?: LunaticReducerState['pager'], disabledFilters?: boolean ) => { // Flat structure of the overview let items = overviewItems.reduce( (acc, item) => interpretOverviewItem(acc, item, executeExpression, { pager, disabledFilters, }), [] as InterpretedLunaticOverviewItem[] ); // Sort using the page logic items = items.sort((a, b) => pageTagComparator(a.page, b.page)); // Build a tree with nested item, a subsequence will be a child of the previous sequence const rootItems = [] as typeof items; let parent = null as null | InterpretedLunaticOverviewItem; for (const item of items) { if (item.type === 'Sequence') { rootItems.push(item); parent = item; } else { parent?.children.push(item); } } if (pager) { applyCurrentPage(rootItems, getPageTag(pager)); } return rootItems; }; /** * Interpret expression inside an item (label & condition). */ const interpretOverviewItem = ( items: InterpretedLunaticOverviewItem[], item: LunaticOverviewItem, executeExpression: LunaticReducerState['executeExpression'], state: { pager?: LunaticReducerState['pager']; disabledFilters?: boolean; }, iteration?: number ): InterpretedLunaticOverviewItem[] => { // We reached a loop item, we need to add it multiple time if (item.iterations && iteration === undefined) { const iterations = executeExpression(item.iterations) ?? 0; return Array.from({ length: iterations }).reduce( (acc: InterpretedLunaticOverviewItem[], _, k) => { return interpretOverviewItem(acc, item, executeExpression, state, k); }, items ); } // if filters are disabled, item are visible and reached by default const isVisible = (() => { if (state.disabledFilters || !item.conditionFilter) return true; return Boolean( executeExpression(item.conditionFilter, { iteration: iteration, }) ); })(); if (!isVisible) { return items; } // Append the item to the list of items const page = addIterationToPage(item.pageTag, iteration); const reached = pageTagComparator(state.pager ? getPageTag(state.pager) : '-1', page) >= 0; return [ ...items, { id: item.id, type: item.type, label: executeExpression(item.label, { iteration }), description: item.description ? executeExpression(item.description, { iteration }) : undefined, children: [], reached: reached, page: page, current: false, }, ]; }; /** * Set the current property in the correct overview item. */ const applyCurrentPage = ( items: InterpretedLunaticOverviewItem[], currentPage: PageTag ): InterpretedLunaticOverviewItem[] => { // Look for the last item where page <= currentPage const currentItem = items.findLast( (item) => pageTagComparator(item.page, currentPage) <= 0 ); if (!currentItem) { return items; } // Make this page "current" currentItem.current = true; if (currentItem.children.length > 0) { applyCurrentPage(currentItem.children, currentPage); } return items; };