Skip to content

Commit

Permalink
fix(docz): merge menus on useMenus
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Mar 11, 2019
1 parent d3b4bc6 commit af4afe2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
10 changes: 5 additions & 5 deletions core/docz/src/hooks/useMenus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useMemo, useContext } from 'react'
import { pipe, get, omit, flattenDepth } from 'lodash/fp'
import { pipe, get, omit, flattenDepth, unionBy } from 'lodash/fp'
import { ulid } from 'ulid'
import match from 'match-sorter'
import sort from 'array-sort'

import { compare, flatArrFromObject, mergeArrBy } from '../utils/helpers'
import { compare, flatArrFromObject } from '../utils/helpers'
import { Entry, MenuItem, doczState } from '../state'

const noMenu = (entry: Entry) => !entry.menu
Expand All @@ -25,9 +25,9 @@ const parseMenu = (entries: Entry[]) => (name: string) => ({
type Menus = MenuItem[]

const menusFromEntries = (entries: Entry[]) => {
const entriesWithoutMenu = entries.filter(noMenu).map(entryAsMenu)
const entriesWithoutMenu = entries.filter(noMenu).map(entryAsMenu) as any
const menus = flatArrFromObject(entries, 'menu').map(parseMenu(entries))
return [...entriesWithoutMenu, ...menus]
return unionBy('name', menus, entriesWithoutMenu)
}

const parseItemStr = (item: MenuItem | string) =>
Expand Down Expand Up @@ -56,7 +56,7 @@ const normalizeAndClean = pipe(
const mergeMenus = (entriesMenu: Menus, configMenu: Menus): Menus => {
const first = entriesMenu.map(normalizeAndClean)
const second = configMenu.map(normalizeAndClean)
const merged = mergeArrBy<MenuItem>('name', first, second)
const merged = unionBy('name', first, second) as MenuItem[]

return merged.map(item => {
if (!item.menu) return item
Expand Down
6 changes: 1 addition & 5 deletions core/docz/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get, unionBy } from 'lodash/fp'
import { get } from 'lodash/fp'

export const isFn = (value: any): boolean => typeof value === 'function'

Expand All @@ -16,7 +16,3 @@ export function compare<T>(a: T, b: T, reverse?: boolean): number {
if (a > b) return reverse ? -1 : 1
return 0
}

export function mergeArrBy<T>(prop: string, a: T[], b: T[]): T[] {
return unionBy<T>(prop, a)(b)
}

0 comments on commit af4afe2

Please sign in to comment.