docs: revise commerce modules overview pages (#10738)
* revise API Key Module overview * revise auth module * support ref sidebar items * remove examples * revise cart module * revise currency * revise customer module * revise fulfillment module * revise inventory module * revise order module * revise payment * revise pricing module * revise product module * revise promotion module * revise region module * revise sales channel module * revise stock location module * revise store module * revise tax module * revise user module * lint content + fix snippets
This commit is contained in:
@@ -7,7 +7,7 @@ import React, {
|
||||
useMemo,
|
||||
useState,
|
||||
} from "react"
|
||||
import { useSidebar } from "../Sidebar"
|
||||
import { isSidebarItemLink, useSidebar } from "../Sidebar"
|
||||
import { usePrevious } from "@uidotdev/usehooks"
|
||||
import { InteractiveSidebarItem, SidebarItem } from "types"
|
||||
|
||||
@@ -56,7 +56,7 @@ export const PaginationProvider = ({ children }: PaginationProviderProps) => {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return children[0].type === "link"
|
||||
return isSidebarItemLink(children[0])
|
||||
? {
|
||||
...children[0],
|
||||
parent: item,
|
||||
@@ -69,7 +69,7 @@ export const PaginationProvider = ({ children }: PaginationProviderProps) => {
|
||||
): SidebarItemWithParent[] | undefined => {
|
||||
return item.children?.filter(
|
||||
(childItem) =>
|
||||
childItem.type === "link" ||
|
||||
isSidebarItemLink(childItem) ||
|
||||
(childItem.type !== "separator" &&
|
||||
getChildrenWithPages(childItem)?.length)
|
||||
) as SidebarItemWithParent[]
|
||||
@@ -95,7 +95,7 @@ export const PaginationProvider = ({ children }: PaginationProviderProps) => {
|
||||
parent: item,
|
||||
}
|
||||
}
|
||||
} else if (item.type === "link") {
|
||||
} else if (isSidebarItemLink(item)) {
|
||||
foundItem = item
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ export const PaginationProvider = ({ children }: PaginationProviderProps) => {
|
||||
return false
|
||||
}
|
||||
|
||||
if (item.type === "link") {
|
||||
if (isSidebarItemLink(item)) {
|
||||
foundItem = item
|
||||
} else if (item.children?.length) {
|
||||
const childItem = getNextItem(item.children, -1)
|
||||
@@ -139,7 +139,7 @@ export const PaginationProvider = ({ children }: PaginationProviderProps) => {
|
||||
}
|
||||
|
||||
result.foundActive = currentItems.some((item, index) => {
|
||||
if (item.type === "link" && item.path === activePath) {
|
||||
if (isSidebarItemLink(item) && item.path === activePath) {
|
||||
if (index !== 0) {
|
||||
result.prevItem = getPrevItem(currentItems, index)
|
||||
}
|
||||
@@ -161,8 +161,9 @@ export const PaginationProvider = ({ children }: PaginationProviderProps) => {
|
||||
result.prevItem = childrenResult.prevItem
|
||||
result.nextItem = childrenResult.nextItem
|
||||
if (!result.prevItem) {
|
||||
result.prevItem =
|
||||
item.type === "link" ? item : getPrevItem(currentItems, index)
|
||||
result.prevItem = isSidebarItemLink(item)
|
||||
? item
|
||||
: getPrevItem(currentItems, index)
|
||||
}
|
||||
|
||||
if (!result.nextItem && index !== currentItems.length - 1) {
|
||||
@@ -186,7 +187,9 @@ export const PaginationProvider = ({ children }: PaginationProviderProps) => {
|
||||
result.prevItem
|
||||
? {
|
||||
title: result.prevItem.title,
|
||||
link: result.prevItem.type === "link" ? result.prevItem.path : "",
|
||||
link: isSidebarItemLink(result.prevItem)
|
||||
? result.prevItem.path
|
||||
: "",
|
||||
parentTitle:
|
||||
result.prevItem.parent?.type !== "separator"
|
||||
? result.prevItem.parent?.title
|
||||
@@ -198,7 +201,9 @@ export const PaginationProvider = ({ children }: PaginationProviderProps) => {
|
||||
result.nextItem
|
||||
? {
|
||||
title: result.nextItem.title,
|
||||
link: result.nextItem.type === "link" ? result.nextItem.path : "",
|
||||
link: isSidebarItemLink(result.nextItem)
|
||||
? result.nextItem.path
|
||||
: "",
|
||||
parentTitle:
|
||||
result.nextItem.parent?.type !== "separator"
|
||||
? result.nextItem.parent?.title
|
||||
|
||||
@@ -95,13 +95,26 @@ export type ActionType =
|
||||
|
||||
type LinksMap = Map<string, SidebarItemLinkWithParent>
|
||||
|
||||
export const isSidebarItemLink = (
|
||||
item: SidebarItem | undefined,
|
||||
checkRef = true
|
||||
): item is SidebarItemLink => {
|
||||
return (
|
||||
item !== undefined &&
|
||||
(item.type === "link" || (checkRef && item.type === "ref"))
|
||||
)
|
||||
}
|
||||
|
||||
const areItemsEqual = (itemA: SidebarItem, itemB: SidebarItem): boolean => {
|
||||
if (itemA.type === "separator" || itemB.type === "separator") {
|
||||
return false
|
||||
}
|
||||
const hasSameTitle = itemA.title === itemB.title
|
||||
const hasSamePath =
|
||||
itemA.type === "link" && itemB.type === "link" && itemA.path === itemB.path
|
||||
isSidebarItemLink(itemA) &&
|
||||
isSidebarItemLink(itemB) &&
|
||||
itemA.type === itemB.type &&
|
||||
itemA.path === itemB.path
|
||||
|
||||
return hasSameTitle || hasSamePath
|
||||
}
|
||||
@@ -116,8 +129,8 @@ const findItem = (
|
||||
if (i.type === "separator") {
|
||||
return false
|
||||
}
|
||||
if (areItemsEqual(item as SidebarItem, i) && i.type === "link") {
|
||||
foundItem = i
|
||||
if (areItemsEqual(item as SidebarItem, i)) {
|
||||
foundItem = i as SidebarItemLink
|
||||
} else if (checkChildren && i.children) {
|
||||
foundItem = findItem(i.children, item)
|
||||
if (foundItem && !foundItem.parentItem) {
|
||||
@@ -143,7 +156,7 @@ const getLinksMap = (
|
||||
return
|
||||
}
|
||||
|
||||
if (item.type === "link") {
|
||||
if (isSidebarItemLink(item)) {
|
||||
map.set(item.path, {
|
||||
...item,
|
||||
parentItem,
|
||||
@@ -246,7 +259,7 @@ export const reducer = (
|
||||
: [...(i.children || []), ...items],
|
||||
loaded: parent.changeLoaded
|
||||
? true
|
||||
: i.type === "link"
|
||||
: isSidebarItemLink(i)
|
||||
? i.loaded
|
||||
: true,
|
||||
}
|
||||
@@ -355,8 +368,8 @@ export const SidebarProvider = ({
|
||||
}
|
||||
|
||||
const isLinkActive = useCallback(
|
||||
(item: SidebarItem, checkChildren = false): boolean => {
|
||||
if (item.type !== "link") {
|
||||
(item: SidebarItem, checkChildren = false, checkRef = true): boolean => {
|
||||
if (!isSidebarItemLink(item, checkRef)) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -399,24 +412,28 @@ export const SidebarProvider = ({
|
||||
if (item.type === "separator") {
|
||||
return false
|
||||
}
|
||||
if (item.isChildSidebar && isLinkActive(item)) {
|
||||
if (item.isChildSidebar && isLinkActive(item, false, false)) {
|
||||
currentSidebar = item
|
||||
return true
|
||||
}
|
||||
if (!item.children?.length) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (!currentSidebar && item.children?.length) {
|
||||
const childSidebar =
|
||||
getCurrentSidebar(item.children) ||
|
||||
(activePath
|
||||
? findItem(item.children, {
|
||||
path: activePath || undefined,
|
||||
type: "link",
|
||||
})
|
||||
: undefined)
|
||||
const childSidebar =
|
||||
getCurrentSidebar(item.children) ||
|
||||
(activePath
|
||||
? findItem(item.children, {
|
||||
path: activePath,
|
||||
type: "link",
|
||||
})
|
||||
: undefined)
|
||||
|
||||
if (childSidebar) {
|
||||
currentSidebar = childSidebar.isChildSidebar ? childSidebar : item
|
||||
}
|
||||
}
|
||||
currentSidebar = childSidebar
|
||||
? childSidebar.isChildSidebar
|
||||
? childSidebar
|
||||
: item
|
||||
: undefined
|
||||
|
||||
return currentSidebar !== undefined
|
||||
})
|
||||
@@ -434,7 +451,7 @@ export const SidebarProvider = ({
|
||||
const previousSidebar = currentItems.previousSidebar || items
|
||||
|
||||
const backItem = previousSidebar.default.find(
|
||||
(item) => item.type === "link" && !item.isChildSidebar
|
||||
(item) => isSidebarItemLink(item) && !item.isChildSidebar
|
||||
) as SidebarItemLink
|
||||
|
||||
if (!backItem) {
|
||||
@@ -472,7 +489,7 @@ export const SidebarProvider = ({
|
||||
const handleScroll = () => {
|
||||
if (getScrolledTop(resolvedScrollableElement) === 0) {
|
||||
const firstItemPath =
|
||||
items.default.length && items.default[0].type === "link"
|
||||
items.default.length && isSidebarItemLink(items.default[0])
|
||||
? items.default[0].path
|
||||
: ""
|
||||
setActivePath(firstItemPath)
|
||||
@@ -548,8 +565,8 @@ export const SidebarProvider = ({
|
||||
) {
|
||||
const { children, ...parentItem } = currentSidebar
|
||||
const hasPreviousSidebar =
|
||||
currentItems?.previousSidebar?.parentItem?.type === "link" &&
|
||||
parentItem.type === "link" &&
|
||||
isSidebarItemLink(currentItems?.previousSidebar?.parentItem) &&
|
||||
isSidebarItemLink(parentItem) &&
|
||||
currentItems.previousSidebar.parentItem.path !== parentItem.path
|
||||
|
||||
setCurrentItems({
|
||||
|
||||
Reference in New Issue
Block a user