|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { createContext, useContext, useEffect, useState } from "react" |
|
4
|
import { usePrevious } from "@uidotdev/usehooks" |
|
5
|
import { useSidebar } from "../Sidebar" |
|
6
|
import { isSidebarItemLink } from "../../utils/sidebar-utils" |
|
7
|
import { Sidebar } from "types" |
|
8
|
|
|
9
|
export type Page = { |
|
10
|
title: string |
|
11
|
description?: string |
|
12
|
parentTitle?: string |
|
13
|
link: string |
|
14
|
} |
|
15
|
|
|
16
|
export type PaginationContextType = { |
|
17
|
nextPage?: Page |
|
18
|
previousPage?: Page |
|
19
|
} |
|
20
|
|
|
21
|
export const PaginationContext = createContext<PaginationContextType | null>( |
|
22
|
null |
|
23
|
) |
|
24
|
|
|
25
|
type SidebarItemWithParent = Sidebar.InteractiveSidebarItem & { |
|
26
|
parent?: Sidebar.InteractiveSidebarItem |
|
27
|
} |
|
28
|
|
|
29
|
type SearchItemsResult = { |
|
30
|
foundActive: boolean |
|
31
|
prevItem?: SidebarItemWithParent |
|
32
|
nextItem?: SidebarItemWithParent |
|
33
|
} |
|
34
|
|
|
35
|
export type PaginationProviderProps = { |
|
36
|
children?: React.ReactNode |
|
37
|
} |
|
38
|
|
|
39
|
export const PaginationProvider = ({ children }: PaginationProviderProps) => { |
|
40
|
const { shownSidebar, activePath } = useSidebar() |
|
41
|
const previousActivePath = usePrevious(activePath) |
|
42
|
const [nextPage, setNextPage] = useState<Page | undefined>() |
|
43
|
const [prevPage, setPrevPage] = useState<Page | undefined>() |
|
44
|
|
|
45
|
const getFirstChild = ( |
|
46
|
item: Sidebar.InteractiveSidebarItem |
|
47
|
): SidebarItemWithParent | undefined => { |
|
48
|
const children = getChildrenWithPages(item) |
|
49
|
if (!children?.length) { |
|
50
|
return undefined |
|
51
|
} |
|
52
|
|
|
53
|
return isSidebarItemLink(children[0]) |
|
54
|
? { |
|
55
|
...children[0], |
|
56
|
parent: item, |
|
57
|
} |
|
58
|
: getFirstChild(children[0]) |
|
59
|
} |
|
60
|
|
|
61
|
const getChildrenWithPages = ( |
|
62
|
item: Sidebar.InteractiveSidebarItem |
|
63
|
): SidebarItemWithParent[] | undefined => { |
|
64
|
return item.children?.filter( |
|
65
|
(childItem) => |
|
66
|
isSidebarItemLink(childItem) || |
|
67
|
(childItem.type !== "separator" && |
|
68
|
getChildrenWithPages(childItem)?.length) |
|
69
|
) as SidebarItemWithParent[] |
|
70
|
} |
|
71
|
|
|
72
|
const getPrevItem = ( |
|
73
|
items: Sidebar.SidebarItem[], |
|
74
|
index: number |
|
75
|
): SidebarItemWithParent | undefined => { |
|
76
|
let foundItem: SidebarItemWithParent | undefined |
|
77
|
items |
|
78
|
.slice(0, index) |
|
79
|
.reverse() |
|
80
|
.some((item) => { |
|
81
|
if (item.type === "separator") { |
|
82
|
return false |
|
83
|
} |
|
84
|
if (item.children?.length) { |
|
85
|
const childItem = getPrevItem(item.children, item.children.length) |
|
86
|
if (childItem) { |
|
87
|
foundItem = { |
|
88
|
...childItem, |
|
89
|
parent: item, |
|
90
|
} |
|
91
|
} |
|
92
|
} else if (isSidebarItemLink(item)) { |
|
93
|
foundItem = item |
|
94
|
} |
|
95
|
|
|
96
|
return foundItem !== undefined |
|
97
|
}) |
|
98
|
|
|
99
|
return foundItem |
|
100
|
} |
|
101
|
|
|
102
|
const getNextItem = ( |
|
103
|
items: Sidebar.SidebarItem[], |
|
104
|
index: number |
|
105
|
): SidebarItemWithParent | undefined => { |
|
106
|
let foundItem: SidebarItemWithParent | undefined |
|
107
|
items.slice(index + 1).some((item) => { |
|
108
|
if (item.type === "separator") { |
|
109
|
return false |
|
110
|
} |
|
111
|
|
|
112
|
if (isSidebarItemLink(item)) { |
|
113
|
foundItem = item |
|
114
|
} else if (item.children?.length) { |
|
115
|
const childItem = getNextItem(item.children, -1) |
|
116
|
if (childItem) { |
|
117
|
foundItem = { |
|
118
|
...childItem, |
|
119
|
parent: item, |
|
120
|
} |
|
121
|
} |
|
122
|
} |
|
123
|
|
|
124
|
return foundItem !== undefined |
|
125
|
}) |
|
126
|
|
|
127
|
return foundItem |
|
128
|
} |
|
129
|
|
|
130
|
const searchItems = ( |
|
131
|
currentItems: Sidebar.SidebarItem[] |
|
132
|
): SearchItemsResult => { |
|
133
|
const result: SearchItemsResult = { |
|
134
|
foundActive: false, |
|
135
|
} |
|
136
|
|
|
137
|
result.foundActive = currentItems.some((item, index) => { |
|
138
|
if (isSidebarItemLink(item) && item.path === activePath) { |
|
139
|
if (index !== 0) { |
|
140
|
result.prevItem = getPrevItem(currentItems, index) |
|
141
|
} |
|
142
|
|
|
143
|
if (item.children?.length) { |
|
144
|
result.nextItem = getFirstChild(item) |
|
145
|
} |
|
146
|
|
|
147
|
if (!result.nextItem && index !== currentItems.length - 1) { |
|
148
|
result.nextItem = getNextItem(currentItems, index) |
|
149
|
} |
|
150
|
return true |
|
151
|
} |
|
152
|
|
|
153
|
if (item.type !== "separator" && item.children?.length) { |
|
154
|
const childrenResult = searchItems(item.children) |
|
155
|
|
|
156
|
if (childrenResult.foundActive) { |
|
157
|
result.prevItem = childrenResult.prevItem |
|
158
|
result.nextItem = childrenResult.nextItem |
|
159
|
if (!result.prevItem) { |
|
160
|
result.prevItem = isSidebarItemLink(item) |
|
161
|
? item |
|
162
|
: getPrevItem(currentItems, index) |
|
163
|
} |
|
164
|
|
|
165
|
if (!result.nextItem && index !== currentItems.length - 1) { |
|
166
|
result.nextItem = getNextItem(currentItems, index) |
|
167
|
} |
|
168
|
|
|
169
|
return true |
|
170
|
} |
|
171
|
} |
|
172
|
|
|
173
|
return false |
|
174
|
}) |
|
175
|
|
|
176
|
return result |
|
177
|
} |
|
178
|
|
|
179
|
useEffect(() => { |
|
180
|
if (activePath !== previousActivePath) { |
|
181
|
const sidebarItems = |
|
182
|
shownSidebar && "items" in shownSidebar |
|
183
|
? shownSidebar.items |
|
184
|
: shownSidebar?.children || [] |
|
185
|
const result = searchItems(sidebarItems) |
|
186
|
setPrevPage( |
|
187
|
result.prevItem |
|
188
|
? { |
|
189
|
title: result.prevItem.title, |
|
190
|
link: isSidebarItemLink(result.prevItem) |
|
191
|
? result.prevItem.path |
|
192
|
: "", |
|
193
|
parentTitle: result.prevItem.parent?.title, |
|
194
|
} |
|
195
|
: undefined |
|
196
|
) |
|
197
|
setNextPage( |
|
198
|
result.nextItem |
|
199
|
? { |
|
200
|
title: result.nextItem.title, |
|
201
|
link: isSidebarItemLink(result.nextItem) |
|
202
|
? result.nextItem.path |
|
203
|
: "", |
|
204
|
parentTitle: result.nextItem?.parent?.title, |
|
205
|
} |
|
206
|
: undefined |
|
207
|
) |
|
208
|
} |
|
209
|
}, [activePath, previousActivePath]) |
|
210
|
|
|
211
|
return ( |
|
212
|
<PaginationContext.Provider |
|
213
|
value={{ |
|
214
|
previousPage: prevPage, |
|
215
|
nextPage, |
|
216
|
}} |
|
217
|
> |
|
218
|
{children} |
|
219
|
</PaginationContext.Provider> |
|
220
|
) |
|
221
|
} |
|
222
|
|
|
223
|
export const usePagination = (): PaginationContextType => { |
|
224
|
const context = useContext(PaginationContext) |
|
225
|
|
|
226
|
if (!context) { |
|
227
|
throw new Error("usePagination must be used inside a PaginationProvider") |
|
228
|
} |
|
229
|
|
|
230
|
return context |
|
231
|
} |