-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add support for nested web pages
1 parent
da2a462
commit b815221
Showing
18 changed files
with
304 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client'; | ||
import { cache } from 'react'; | ||
|
||
import { SidebarMenu } from '@/vibes/soul/sections/sidebar-menu'; | ||
import { StickySidebarLayout } from '@/vibes/soul/sections/sticky-sidebar-layout'; | ||
import { client } from '~/client'; | ||
import { graphql } from '~/client/graphql'; | ||
import { revalidate } from '~/client/revalidate-target'; | ||
|
||
interface Props extends React.PropsWithChildren { | ||
params: Promise<{ id: string }>; | ||
} | ||
|
||
const WebPageChildrenQuery = graphql(` | ||
query WebPageChildren($id: ID!) { | ||
node(id: $id) { | ||
... on WebPage { | ||
children(first: 20) { | ||
edges { | ||
node { | ||
name | ||
... on NormalPage { | ||
path | ||
} | ||
... on ContactPage { | ||
path | ||
} | ||
... on RawHtmlPage { | ||
path | ||
} | ||
... on ExternalLinkPage { | ||
link | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`); | ||
|
||
interface PageLink { | ||
label: string; | ||
href: string; | ||
} | ||
|
||
const getWebPageChildren = cache(async (id: string): Promise<PageLink[]> => { | ||
const { data } = await client.fetch({ | ||
document: WebPageChildrenQuery, | ||
variables: { id: decodeURIComponent(id) }, | ||
fetchOptions: { next: { revalidate } }, | ||
}); | ||
|
||
if (!data.node) { | ||
return []; | ||
} | ||
|
||
if (!('children' in data.node)) { | ||
return []; | ||
} | ||
|
||
const { children } = data.node; | ||
|
||
return removeEdgesAndNodes(children).reduce((acc: PageLink[], child) => { | ||
if ('path' in child) { | ||
return [...acc, { label: child.name, href: child.path }]; | ||
} | ||
|
||
if ('link' in child) { | ||
return [...acc, { label: child.name, href: child.link }]; | ||
} | ||
|
||
return acc; | ||
}, []); | ||
}); | ||
|
||
export default async function WebPageLayout({ params, children }: Props) { | ||
const { id } = await params; | ||
|
||
return ( | ||
<StickySidebarLayout | ||
sidebar={<SidebarMenu links={getWebPageChildren(id)} />} | ||
sidebarSize="small" | ||
> | ||
{children} | ||
</StickySidebarLayout> | ||
); | ||
} |
38 changes: 38 additions & 0 deletions
38
core/app/[locale]/(default)/webpages/[id]/normal/page-data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { cache } from 'react'; | ||
|
||
import { client } from '~/client'; | ||
import { graphql } from '~/client/graphql'; | ||
import { revalidate } from '~/client/revalidate-target'; | ||
import { BreadcrumbsWebPageFragment } from '~/components/breadcrumbs/fragment'; | ||
|
||
const NormalPageQuery = graphql( | ||
` | ||
query NormalPageQuery($id: ID!) { | ||
node(id: $id) { | ||
... on NormalPage { | ||
__typename | ||
name | ||
...BreadcrumbsFragment | ||
htmlBody | ||
entityId | ||
seo { | ||
pageTitle | ||
metaDescription | ||
metaKeywords | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
[BreadcrumbsWebPageFragment], | ||
); | ||
|
||
export const getWebpageData = cache(async (variables: { id: string }) => { | ||
const { data } = await client.fetch({ | ||
document: NormalPageQuery, | ||
variables, | ||
fetchOptions: { next: { revalidate } }, | ||
}); | ||
|
||
return data; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
core/app/[locale]/(default)/webpages/normal/[id]/page-data.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client'; | ||
import { ResultOf } from 'gql.tada'; | ||
|
||
import { | ||
BreadcrumbsCategoryFragment, | ||
BreadcrumbsWebPageFragment, | ||
} from '~/components/breadcrumbs/fragment'; | ||
import { Breadcrumb } from '~/vibes/soul/primitives/breadcrumbs'; | ||
|
||
type BreadcrumbsResult = | ||
| ResultOf<typeof BreadcrumbsWebPageFragment> | ||
| ResultOf<typeof BreadcrumbsCategoryFragment>; | ||
|
||
export const breadcrumbsTransformer = (breadcrumbs: BreadcrumbsResult['breadcrumbs']) => { | ||
return removeEdgesAndNodes(breadcrumbs).reduce<Breadcrumb[]>((acc, crumb) => { | ||
if (crumb.path) { | ||
return [...acc, { label: crumb.name, href: crumb.path }]; | ||
} | ||
|
||
return acc; | ||
}, []); | ||
}; | ||
|
||
export function truncateBreadcrumbs(breadcrumbs: Breadcrumb[], length: number): Breadcrumb[] { | ||
if (breadcrumbs.length < length) { | ||
return breadcrumbs; | ||
} | ||
|
||
const middleIndex = Math.floor(breadcrumbs.length / 2); | ||
const dropCount = breadcrumbs.length - length; | ||
const dropEach = Math.ceil(dropCount / 2); | ||
const dropLast = Math.floor(dropCount / 2); | ||
const [first, last] = [ | ||
breadcrumbs.slice(0, middleIndex - dropEach), | ||
breadcrumbs.slice(middleIndex + dropLast), | ||
]; | ||
|
||
last[0] = { label: '...', href: '#' }; | ||
|
||
return [...first, ...last]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Stream, Streamable } from '@/vibes/soul/lib/streamable'; | ||
|
||
import { SidebarMenuLink } from './sidebar-menu-link'; | ||
import { SidebarMenuSelect } from './sidebar-menu-select'; | ||
|
||
interface MenuLink { | ||
href: string; | ||
label: string; | ||
} | ||
|
||
interface Props { | ||
links: Streamable<MenuLink[]>; | ||
placeholderCount?: number; | ||
} | ||
|
||
export function SidebarMenu({ links: streamableLinks, placeholderCount = 5 }: Props) { | ||
return ( | ||
<Stream | ||
fallback={<SidebarMenuSkeleton placeholderCount={placeholderCount} />} | ||
value={streamableLinks} | ||
> | ||
{(links) => { | ||
if (!links.length) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<nav> | ||
<ul className="hidden @2xl:block"> | ||
{links.map((link, index) => ( | ||
<li key={index}> | ||
<SidebarMenuLink href={link.href}>{link.label}</SidebarMenuLink> | ||
</li> | ||
))} | ||
</ul> | ||
<div className="@2xl:hidden"> | ||
<SidebarMenuSelect links={links} /> | ||
</div> | ||
</nav> | ||
); | ||
}} | ||
</Stream> | ||
); | ||
} | ||
|
||
function SidebarMenuSkeleton({ placeholderCount }: { placeholderCount: number }) { | ||
return ( | ||
<> | ||
<div className="hidden [mask-image:linear-gradient(to_bottom,_black_0%,_transparent_90%)] @2xl:block"> | ||
<div className="w-full animate-pulse"> | ||
{Array.from({ length: placeholderCount }).map((_, index) => ( | ||
<div className="flex h-10 items-center px-3" key={index}> | ||
<div className="h-[1lh] flex-1 rounded-lg bg-contrast-100" /> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
<div className="@2xl:hidden"> | ||
<div className="h-[50px] w-full rounded-lg bg-contrast-100" /> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters