-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Adjust navigation #79
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
117 changes: 97 additions & 20 deletions
117
apps/storefront/src/app/[locale]/(main)/_components/navigation.tsx
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import type { Menu } from "@nimara/domain/objects/Menu"; | ||
|
||
import { Link } from "@/i18n/routing"; | ||
import { generateLinkUrl, isInternalUrl } from "@/lib/helpers"; | ||
import { generateLinkUrl, isInternalUrl } from "@/lib/cms"; | ||
import { paths } from "@/lib/paths"; | ||
import type { Maybe } from "@/lib/types"; | ||
|
||
|
@@ -23,33 +23,48 @@ export const MobileNavigation = ({ | |
return ( | ||
<ul className="grid py-4"> | ||
{menu.items.map((item) => ( | ||
<li key={item.id} className="p-2 text-stone-500" onClick={handleClick}> | ||
<li key={item.id} className="p-2 text-stone-500"> | ||
{isInternalUrl(item.url) ? ( | ||
<Link href={generateLinkUrl(item, paths)}> | ||
<Link href={generateLinkUrl(item, paths)} onClick={handleClick}> | ||
{item.name || item.category?.name || item.collection?.name} | ||
{item.children?.length ? ( | ||
<ul> | ||
{item.children.map((child) => ( | ||
<li | ||
key={child.id} | ||
className="py-2 text-stone-900" | ||
onClick={handleClick} | ||
> | ||
<Link href={generateLinkUrl(child, paths)}> | ||
{child.name || | ||
child.collection?.name || | ||
child.category?.name} | ||
</Link> | ||
</li> | ||
))} | ||
</ul> | ||
) : null} | ||
</Link> | ||
) : ( | ||
<a href={item.url as string} target="_blank"> | ||
<a | ||
href={item.url as string} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
onClick={handleClick} | ||
> | ||
{item.name} | ||
</a> | ||
)} | ||
{item.children?.length ? ( | ||
<ul className="mt-2 pl-6"> | ||
{item.children.map((child) => ( | ||
<li key={child.id} className="py-1 pl-2 text-stone-700"> | ||
{isInternalUrl(child.url) ? ( | ||
<Link | ||
href={generateLinkUrl(child, paths)} | ||
onClick={handleClick} | ||
> | ||
{child.name || | ||
child.collection?.name || | ||
child.category?.name} | ||
</Link> | ||
) : ( | ||
<a | ||
href={child.url as string} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
onClick={handleClick} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handleMenuItemClick ? to be more specific |
||
> | ||
{child.name} | ||
</a> | ||
)} | ||
</li> | ||
))} | ||
</ul> | ||
) : null} | ||
</li> | ||
))} | ||
</ul> | ||
|
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,89 @@ | ||
import type { MenuItem } from "@nimara/domain/objects/Menu"; | ||
import { loggingService } from "@nimara/infrastructure/logging/service"; | ||
|
||
export const getQueryParams = ( | ||
item: MenuItem, | ||
): { queryKey: string | null; queryValue: string | null } => { | ||
if (item.url) { | ||
const url = new URL(item.url); | ||
const queryKey = Array.from(url.searchParams.keys())[0] || null; | ||
const queryValue = queryKey ? url.searchParams.get(queryKey) : null; | ||
|
||
if (queryKey && queryValue) { | ||
return { queryKey, queryValue }; | ||
} | ||
} | ||
|
||
if (item.category?.slug) { | ||
return { queryKey: "category", queryValue: item.category.slug }; | ||
} | ||
|
||
if (item.collection?.slug) { | ||
return { queryKey: "collection", queryValue: item.collection.slug }; | ||
} | ||
|
||
return { queryKey: null, queryValue: null }; | ||
}; | ||
|
||
interface Paths { | ||
page: { | ||
asPath: (params: { slug: string }) => string; | ||
}; | ||
search: { | ||
asPath: (params: { query: Record<string, string> }) => string; | ||
}; | ||
} | ||
//TO DO - handle validating internal url | ||
export const generateLinkUrl = (item: MenuItem, paths: Paths): string => { | ||
if (item.collection) { | ||
return paths.search.asPath({ | ||
query: { collection: item.collection.slug }, | ||
}); | ||
} | ||
if (item.category) { | ||
return paths.search.asPath({ | ||
query: { category: item.category.slug }, | ||
}); | ||
} | ||
if (item.page) { | ||
return paths.page.asPath({ slug: item.page.slug }); | ||
} | ||
if (item.url) { | ||
return item.url; | ||
} | ||
|
||
return "#"; | ||
}; | ||
|
||
const internalUrls = [ | ||
"https://nimara-dev.vercel.app", | ||
"https://nimara-stage.vercel.app", | ||
"https://nimara-prod.vercel.app", | ||
"http://localhost", | ||
"https://localhost", | ||
]; | ||
|
||
export const isInternalUrl = (url: string | null): boolean => { | ||
if (url === null) { | ||
return true; | ||
} | ||
if (url) { | ||
try { | ||
const parsedUrl = new URL(url); | ||
|
||
return internalUrls.some((internalUrl) => { | ||
const internalParsedUrl = new URL(internalUrl); | ||
|
||
return parsedUrl.hostname === internalParsedUrl.hostname; | ||
}); | ||
} catch (e) { | ||
loggingService.error("Given URL is not internal", { | ||
error: e, | ||
}); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
return false; | ||
}; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
value sound too generic. Is it possible to have some more speciifc name? smthng like currentMenuItem ?