Untitled

 avatar
unknown
plain_text
2 months ago
934 B
2
Indexable
const processMenuItems = (items: MenuItem[], parentId?: string, flatStructure: FlatMenu = {}): FlatMenu => {
  return items.reduce((acc, item, index) => {
    const id = generateItemId(parentId, index);

    const newItem: FlatMenuItem = {
      id,
      path: item.path,
      active: item.active,
      title: item.title,
      childIds: [],
      parentId
    };

    if (item.items) {
      newItem.childIds = item.items.map((_, childIndex) => generateItemId(id, childIndex));
      Object.assign(acc, processMenuItems(item.items, id, acc));
    }

    return { ...acc, [id]: newItem };
  }, flatStructure);
};

const flattenMenu = (menu: MenuItem[]): FlatMenu => {
  const flatStructure = processMenuItems(menu);

  return {
    ...flatStructure,
    [MORE_BUTTON_ITEM_ID]: {
      id: MORE_BUTTON_ITEM_ID,
      path: '',
      active: false,
      title: 'More',
      childIds: [],
      childIdsCollection: {}
    }
  };
};
Editor is loading...
Leave a Comment