design upgrade
This commit is contained in:
169
apps/suman-bosna/src/lib/configurator/index.ts
Normal file
169
apps/suman-bosna/src/lib/configurator/index.ts
Normal file
@@ -0,0 +1,169 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import configuratorData from "@/config/configurator.json"
|
||||
import {
|
||||
AddonGroup,
|
||||
AddonItem,
|
||||
ConfiguratorConfig,
|
||||
ConfiguratorOverride,
|
||||
ConfiguratorView,
|
||||
LayerDef,
|
||||
LayerImages,
|
||||
OptionDriver,
|
||||
} from "./types"
|
||||
|
||||
export * from "./types"
|
||||
|
||||
/** Selected add-on items per group id */
|
||||
export type AddonSelection = Record<string, string[]>
|
||||
|
||||
export type LayerContext = {
|
||||
options: Partial<Record<OptionDriver, string>>
|
||||
addons: AddonSelection
|
||||
}
|
||||
|
||||
const data = configuratorData as unknown as {
|
||||
default: ConfiguratorConfig
|
||||
products: Record<string, ConfiguratorOverride>
|
||||
}
|
||||
|
||||
function parseMetadataOverride(raw: unknown): ConfiguratorOverride | null {
|
||||
if (!raw) return null
|
||||
if (typeof raw === "object") return raw as ConfiguratorOverride
|
||||
if (typeof raw === "string") {
|
||||
try {
|
||||
return JSON.parse(raw) as ConfiguratorOverride
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function mergeImages(...sources: (LayerImages | undefined)[]): LayerImages {
|
||||
const out: LayerImages = {}
|
||||
for (const src of sources) {
|
||||
if (!src) continue
|
||||
for (const [layerId, views] of Object.entries(src)) {
|
||||
const target = (out[layerId] ??= {})
|
||||
for (const [view, keys] of Object.entries(views ?? {})) {
|
||||
target[view as ConfiguratorView] = {
|
||||
...(target[view as ConfiguratorView] ?? {}),
|
||||
...(keys ?? {}),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* Effective configurator config for a product:
|
||||
* defaults <- products[handle] in configurator.json <- product.metadata.configurator
|
||||
*/
|
||||
export function getConfiguratorConfig(
|
||||
product: HttpTypes.StoreProduct
|
||||
): ConfiguratorConfig {
|
||||
const base = data.default
|
||||
const byHandle = product.handle ? data.products[product.handle] : undefined
|
||||
const fromMetadata = parseMetadataOverride(product.metadata?.configurator)
|
||||
const hidden = new Set([
|
||||
...(byHandle?.hideAddons ?? []),
|
||||
...(fromMetadata?.hideAddons ?? []),
|
||||
])
|
||||
const addons = (fromMetadata?.addons ?? byHandle?.addons ?? base.addons).filter(
|
||||
(g) => !hidden.has(g.id)
|
||||
)
|
||||
|
||||
return {
|
||||
canvas: fromMetadata?.canvas ?? byHandle?.canvas ?? base.canvas,
|
||||
views: fromMetadata?.views ?? byHandle?.views ?? base.views,
|
||||
layers: fromMetadata?.layers ?? byHandle?.layers ?? base.layers,
|
||||
images: mergeImages(base.images, byHandle?.images, fromMetadata?.images),
|
||||
addons,
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether a layer is drawn for the current view and selection */
|
||||
export function isLayerActive(
|
||||
layer: LayerDef,
|
||||
view: ConfiguratorView,
|
||||
ctx: LayerContext,
|
||||
addonGroups: AddonGroup[]
|
||||
) {
|
||||
if (layer.views && !layer.views.includes(view)) return false
|
||||
const driver = layer.driver
|
||||
if (driver && "addon" in driver) {
|
||||
// Layer belongs to an add-on group that this product does not offer
|
||||
if (!addonGroups.some((g) => g.id === driver.addon)) return false
|
||||
if ("item" in driver) return (ctx.addons[driver.addon] ?? []).includes(driver.item)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/** Value that drives a layer, e.g. the selected fabric or base item */
|
||||
export function getLayerValue(layer: LayerDef, ctx: LayerContext) {
|
||||
const driver = layer.driver
|
||||
if (!driver) return undefined
|
||||
if ("option" in driver) return ctx.options[driver.option]
|
||||
if ("item" in driver) return driver.item
|
||||
return ctx.addons[driver.addon]?.[0]
|
||||
}
|
||||
|
||||
/**
|
||||
* Image for a layer, most specific key first:
|
||||
* "<size>|<value>", "<value>", "<size>|*", "*". Undefined means: draw the SVG.
|
||||
*/
|
||||
export function resolveLayerImage(
|
||||
config: ConfiguratorConfig,
|
||||
layer: LayerDef,
|
||||
view: ConfiguratorView,
|
||||
ctx: LayerContext
|
||||
) {
|
||||
const keys = config.images[layer.id]?.[view]
|
||||
if (!keys) return undefined
|
||||
const value = getLayerValue(layer, ctx)
|
||||
const size = ctx.options.size
|
||||
const candidates = [
|
||||
size && value ? `${size}|${value}` : null,
|
||||
value ?? null,
|
||||
size ? `${size}|*` : null,
|
||||
"*",
|
||||
].filter(Boolean) as string[]
|
||||
for (const key of candidates) {
|
||||
if (keys[key]) return keys[key]
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
/** Reason the item cannot be picked with the current options, or null */
|
||||
export function getAddonConflict(item: AddonItem, selectedValues: string[]) {
|
||||
const hit = item.incompatibleWith?.find((needle) =>
|
||||
selectedValues.some((v) => v.toLowerCase().includes(needle.toLowerCase()))
|
||||
)
|
||||
return hit ? item.reason ?? "Nije kompatibilno s odabranom konfiguracijom" : null
|
||||
}
|
||||
|
||||
export function defaultAddonSelection(groups: AddonGroup[]): AddonSelection {
|
||||
return Object.fromEntries(groups.map((g) => [g.id, g.defaults ?? []]))
|
||||
}
|
||||
|
||||
/** "postolje:krizno;oprema:led,grijac" for shareable URLs */
|
||||
export function encodeAddons(selection: AddonSelection) {
|
||||
return Object.entries(selection)
|
||||
.filter(([, items]) => items.length)
|
||||
.map(([group, items]) => `${group}:${items.join(",")}`)
|
||||
.join(";")
|
||||
}
|
||||
|
||||
export function decodeAddons(raw: string | null, groups: AddonGroup[]): AddonSelection | null {
|
||||
if (!raw) return null
|
||||
const out = defaultAddonSelection(groups)
|
||||
for (const part of raw.split(";")) {
|
||||
const [groupId, items] = part.split(":")
|
||||
const group = groups.find((g) => g.id === groupId)
|
||||
if (!group || !items) continue
|
||||
const valid = items.split(",").filter((id) => group.items.some((i) => i.id === id))
|
||||
out[groupId] = group.type === "single" ? valid.slice(0, 1) : valid
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user