-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
454 additions
and
283 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
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 { PlatformImage, PlatformMenu, PlatformProduct } from "@enterprise-commerce/core/platform/types" | ||
import { replicate } from "clients/replicate" | ||
|
||
/* | ||
* Enrich product by attaching hierarchical categories to it | ||
* Takes in all tags, and tries to find hierarchy against it | ||
* Currently just shopify focused | ||
*/ | ||
|
||
export const enrichProduct = async (product: PlatformProduct, collections: PlatformMenu["items"]) => { | ||
const categoryMap = buildCategoryMap(collections) | ||
const images = await generateProductAltTags(product) | ||
const hierarchicalCategories = generateHierarchicalCategories(product.tags, categoryMap) | ||
|
||
return { | ||
...product, | ||
images: images.filter(Boolean), | ||
hierarchicalCategories, | ||
} | ||
} | ||
|
||
function buildCategoryMap(categories: PlatformMenu["items"]) { | ||
const categoryMap = new Map<string, string[]>() | ||
|
||
function traverse(items: PlatformMenu["items"], path: string[]) { | ||
for (const item of items) { | ||
const newPath = [...path, item.resource!.handle] | ||
categoryMap.set(item.resource!.handle, newPath) | ||
if (item.items && item.items.length > 0) { | ||
traverse(item.items, newPath) | ||
} | ||
} | ||
} | ||
|
||
traverse(categories, []) | ||
|
||
return categoryMap | ||
} | ||
|
||
function generateHierarchicalCategories(tags: PlatformProduct["tags"], categoryMap: Map<string, string[]>) { | ||
const hierarchicalCategories: Record<"lvl0" | "lvl1" | "lvl2", string[]> = { lvl0: [], lvl1: [], lvl2: [] } | ||
|
||
if (!tags.length || !categoryMap.size) return hierarchicalCategories | ||
|
||
tags.forEach((tag) => { | ||
const path = categoryMap.get(tag) | ||
if (path) { | ||
if (path.length > 0 && !hierarchicalCategories.lvl0.includes(path[0])) { | ||
hierarchicalCategories.lvl0.push(path[0]) | ||
} | ||
if (path.length > 1) { | ||
const lvl1Path = path.slice(0, 2).join(" > ") | ||
if (!hierarchicalCategories.lvl1.includes(lvl1Path)) { | ||
hierarchicalCategories.lvl1.push(lvl1Path) | ||
} | ||
} | ||
if (path.length > 2) { | ||
const lvl2Path = path.slice(0, 3).join(" > ") | ||
if (!hierarchicalCategories.lvl2.includes(lvl2Path)) { | ||
hierarchicalCategories.lvl2.push(lvl2Path) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
return hierarchicalCategories | ||
} | ||
|
||
async function generateProductAltTags(product: PlatformProduct): Promise<(PlatformImage | undefined)[]> { | ||
try { | ||
const altTagAwareImages = await Promise.all(product?.images?.slice(0, 1).map(mapper).filter(Boolean)) | ||
return [...altTagAwareImages, ...product?.images?.slice(1)?.filter(Boolean)] || [] | ||
} catch (e) { | ||
console.error(e) | ||
return product.images // graceful exit | ||
} | ||
|
||
async function mapper(image: PlatformImage) { | ||
if (!replicate) return | ||
const output = (await replicate.run("salesforce/blip:2e1dddc8621f72155f24cf2e0adbde548458d3cab9f00c0139eea840d0ac4746", { | ||
input: { | ||
task: "image_captioning", | ||
image: image.url, | ||
}, | ||
})) as unknown as string | ||
|
||
return { ...image, altText: output?.replace("Caption:", "") || "" } | ||
} | ||
} |
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,60 @@ | ||
import type { CategoriesDistribution } from "meilisearch" | ||
import { useParams, useSearchParams } from "next/navigation" | ||
|
||
interface HierarchicalMenuOptions { | ||
attributes: readonly string[] | ||
distribution: Record<string, CategoriesDistribution> | ||
separator: string | ||
} | ||
|
||
type HierarchicalMenuItem = Record<string, number> | ||
|
||
export const useHierarchicalMenu = ({ attributes, distribution, separator }: HierarchicalMenuOptions) => { | ||
const { slug } = useParams() | ||
const searchParams = useSearchParams() | ||
const normalizedSlug = Array.isArray(slug) ? slug.join(separator) : slug || searchParams.get("categories")?.split(",").join(separator) || "" | ||
|
||
const initialPath = findInitialPath(separator, normalizedSlug, attributes, distribution) || [] | ||
|
||
const getCategoryChildren = (path: string[]) => { | ||
const level = path.length | ||
const key = attributes[level] | ||
const prefix = path.join(separator) | ||
|
||
return Object.keys(distribution[key] || {}) | ||
.filter((item) => item.startsWith(prefix)) | ||
.reduce((acc: HierarchicalMenuItem, item) => { | ||
const category = item.split(separator)[level] | ||
if (category && !acc[category]) { | ||
acc[category] = distribution[key][item] | ||
} | ||
return acc | ||
}, {}) | ||
} | ||
|
||
const items: HierarchicalMenuItem = initialPath.length === 0 ? distribution[attributes[0]] : getCategoryChildren(initialPath) | ||
const parent = initialPath.length > 0 ? initialPath.slice(0, -1).pop() : null | ||
const current = initialPath.length > 0 ? initialPath.pop() : null | ||
|
||
return { | ||
items, | ||
current, | ||
parent, | ||
} | ||
} | ||
|
||
const findInitialPath = (separator: string, activeCategory: string, attributes: readonly string[], distribution: Record<string, CategoriesDistribution>): string[] => { | ||
if (activeCategory === "") { | ||
return [] | ||
} | ||
|
||
for (let level = 0; level < attributes.length; level++) { | ||
const key = attributes[level] | ||
const categoriesAtLevel = Object.keys(distribution[key] || {}) | ||
const path = categoriesAtLevel.find((item) => item.endsWith(activeCategory)) | ||
if (path) { | ||
return path.split(separator) | ||
} | ||
} | ||
return [] | ||
} |
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
Oops, something went wrong.