50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
import visualsData from "@/config/product-visuals.json"
|
|
|
|
/** Fixed steps of the quote configurator that have their own image */
|
|
export type VisualStep = "uvod" | "postupak" | "specifikacije" | "sazetak"
|
|
|
|
type ProductVisuals = {
|
|
steps?: Partial<Record<VisualStep, string>>
|
|
/** option title -> value -> url */
|
|
options?: Record<string, Record<string, string>>
|
|
}
|
|
|
|
const data = visualsData as unknown as { products: Record<string, ProductVisuals> }
|
|
|
|
const STEP_TEXT: Record<VisualStep, string> = {
|
|
uvod: "",
|
|
postupak: "Mjerenje i montaža",
|
|
specifikacije: "Detalji izrade",
|
|
sazetak: "",
|
|
}
|
|
|
|
function placeholder(text: string, bg = "d4d8de", fg = "6b7280") {
|
|
const t = encodeURIComponent(text).replace(/%20/g, "+")
|
|
return `https://placehold.co/1600x1200/${bg}/${fg}?text=${t}`
|
|
}
|
|
|
|
/** Image for a fixed step, or a placeholder named after the step */
|
|
export function getStepVisual(handle: string | null | undefined, title: string, step: VisualStep) {
|
|
const custom = handle ? data.products[handle]?.steps?.[step] : undefined
|
|
return custom ?? placeholder(STEP_TEXT[step] || title)
|
|
}
|
|
|
|
/**
|
|
* Image for an option value. Colour-like values get a placeholder in that
|
|
* colour (from `colorHex`), so changing e.g. the fabric visibly swaps it.
|
|
*/
|
|
export function getOptionVisual(
|
|
handle: string | null | undefined,
|
|
optionTitle: string,
|
|
value: string,
|
|
colorHex?: string,
|
|
isDark?: boolean
|
|
) {
|
|
const custom = handle ? data.products[handle]?.options?.[optionTitle]?.[value] : undefined
|
|
if (custom) return custom
|
|
if (colorHex?.startsWith("#")) {
|
|
return placeholder(`${optionTitle}: ${value}`, colorHex.slice(1), isDark ? "ffffff" : "1f2937")
|
|
}
|
|
return placeholder(`${optionTitle}: ${value}`)
|
|
}
|