1 import { Sidebar } from "types"
2
3 export const isSidebarItemLink = (
4 item: Sidebar.SidebarItem | undefined,
5 options?: {
6 checkRef?: boolean
7 checkExternal?: boolean
8 }
9 ): item is Sidebar.SidebarItemLink => {
10 const { checkRef = true, checkExternal = true } = options || {}
11
12 return (
13 item !== undefined &&
14 (item.type === "link" ||
15 (checkRef && item.type === "ref") ||
16 (checkExternal && item.type === "external"))
17 )
18 }
19
20 export const areSidebarItemsEqual = ({
21 itemA,
22 itemB,
23 compareTitles = true,
24 }: {
25 itemA: Sidebar.SidebarItem
26 itemB: Sidebar.SidebarItem
27 compareTitles?: boolean
28 }): boolean => {
29 if (itemA.type !== itemB.type) {
30 return false
31 }
32 // after this, we know that itemA and itemB have the same type
33 switch (itemA.type) {
34 case "separator":
35 return true
36 case "sidebar":
37 return (
38 itemA.sidebar_id === (itemB as Sidebar.SidebarItemSidebar).sidebar_id
39 )
40 case "category":
41 case "sub-category":
42 return compareTitles
43 ? itemA.title === (itemB as Sidebar.SidebarItemCategory).title
44 : false
45 case "link":
46 case "ref":
47 case "external": {
48 const hasSameTitle =
49 !compareTitles ||
50 itemA.title === (itemB as Sidebar.SidebarItemLink).title
51 const hasSamePath = itemA.path === (itemB as Sidebar.SidebarItemLink).path
52
53 return hasSameTitle && hasSamePath
54 }
55 }
56 }
57
58 export const findSidebarItem = ({
59 sidebarItems,
60 item,
61 checkChildren = true,
62 compareTitles = true,
63 }: {
64 sidebarItems: Sidebar.SidebarItem[]
65 item: Sidebar.SidebarItem
66 checkChildren?: boolean
67 compareTitles?: boolean
68 }): Sidebar.SidebarItem | undefined => {
69 let foundItem: Sidebar.SidebarItem | undefined
70 sidebarItems.some((i) => {
71 if (areSidebarItemsEqual({ itemA: i, itemB: item, compareTitles })) {
72 foundItem = i
73 } else if (checkChildren && "children" in i && i.children) {
74 foundItem = findSidebarItem({
75 sidebarItems: i.children,
76 item,
77 checkChildren,
78 compareTitles,
79 })
80 }
81
82 return foundItem !== undefined
83 })
84
85 return foundItem
86 }
87
88 export const getSidebarItemWithHistory = ({
89 sidebarItems,
90 item,
91 sidebarHistory = [],
92 checkChildren = true,
93 compareTitles = true,
94 }: {
95 sidebarItems: Sidebar.SidebarItem[]
96 item: Sidebar.SidebarItem
97 sidebarHistory?: string[]
98 checkChildren?: boolean
99 compareTitles?: boolean
100 }): {
101 item: Sidebar.SidebarItem | undefined
102 sidebarHistory: string[]
103 } => {
104 let foundItem: Sidebar.SidebarItem | undefined
105 sidebarItems.some((i) => {
106 if (areSidebarItemsEqual({ itemA: i, itemB: item, compareTitles })) {
107 foundItem = i
108 } else if (checkChildren && "children" in i && i.children) {
109 const result = getSidebarItemWithHistory({
110 sidebarItems: i.children,
111 item,
112 checkChildren,
113 compareTitles,
114 })
115
116 if (result.item) {
117 foundItem = result.item
118 if (i.type === "sidebar") {
119 sidebarHistory.push(i.sidebar_id)
120 }
121 sidebarHistory.push(...result.sidebarHistory)
122 }
123 }
124
125 return foundItem !== undefined
126 })
127
128 return { item: foundItem, sidebarHistory }
129 }