mirror of
https://github.com/vercel/commerce.git
synced 2025-07-27 20:21:22 +00:00
.github
.vscode
packages
site
assets
components
config
lib
api
click-outside
hooks
colors.ts
focus-trap.tsx
get-slug.ts
range-map.ts
search-props.tsx
search.tsx
to-pixels.ts
usage-warns.ts
pages
public
.env.template
.eslintrc
.gitignore
.npmrc
.prettierignore
.prettierrc
commerce-config.js
commerce.config.json
global.d.ts
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
.editorconfig
.gitignore
.prettierignore
.prettierrc
README.md
license.md
package.json
pnpm-lock.yaml
pnpm-workspace.yaml
turbo.json
* Update product types * Cart types progress, add zod & initial schema validator * Update normalize.ts * Update with-schema-parser.ts * Updated types, schemas & providers * Fix providers after schema parse errors * Fix paths * More provider fixes * Fix kibocommerce & commercejs * Add customer updated types & fixes * Add checkout & customer types * Import core types only from commerce * Update tsconfig.json * Convert hooks interfaces to types * Requested changes * Change to relative paths * Move Zod dependency
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { useEffect, useState } from 'react'
|
|
import getSlug from './get-slug'
|
|
|
|
export function useSearchMeta(asPath: string) {
|
|
const [pathname, setPathname] = useState<string>('/search')
|
|
const [category, setCategory] = useState<string | undefined>()
|
|
const [brand, setBrand] = useState<string | undefined>()
|
|
|
|
useEffect(() => {
|
|
// Only access asPath after hydration to avoid a server mismatch
|
|
const path = asPath.split('?')[0]
|
|
const parts = path.split('/')
|
|
|
|
let c = parts[2]
|
|
let b = parts[3]
|
|
|
|
if (c === 'designers') {
|
|
c = parts[4]
|
|
}
|
|
|
|
if (path !== pathname) setPathname(path)
|
|
if (c !== category) setCategory(c)
|
|
if (b !== brand) setBrand(b)
|
|
}, [asPath, pathname, category, brand])
|
|
|
|
return { pathname, category, brand }
|
|
}
|
|
|
|
// Removes empty query parameters from the query object
|
|
export const filterQuery = (query: any) =>
|
|
Object.keys(query).reduce<any>((obj, key) => {
|
|
if (query[key]?.length) {
|
|
obj[key] = query[key]
|
|
}
|
|
return obj
|
|
}, {})
|
|
|
|
export const getCategoryPath = (path: string, brand?: string) => {
|
|
const category = getSlug(path)
|
|
|
|
return `/search${brand ? `/designers/${brand}` : ''}${
|
|
category ? `/${category}` : ''
|
|
}`
|
|
}
|
|
|
|
export const getDesignerPath = (path: string, category?: string) => {
|
|
return `/search${path ? `/designers${path}` : ''}${
|
|
category ? `/${category}` : ''
|
|
}`
|
|
}
|