Files
klz-cables.com/.pnpm-store/v10/files/24/1fe2f193891a8045b9320db97d1e7eb47516f79e503e22a90b46d9c099ac22320db9fb98ff64aad6ed46da517117a1c7f8881e6c983a601531600e55cbea05
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

44 lines
2.1 KiB
Plaintext

/**
* Transforms various forms of columns into `ColumnPreference[]` which is what is stored in the user's preferences table
* In React state, for example, columns are stored in in their entirety, including React components: `[{ accessor: 'title', active: true, Label: React.ReactNode, ... }]`
* In the URL, they are stored as an array of strings: `['title', '-slug']`, where the `-` prefix is used to indicate that the column is inactive
* However in the database, columns must be in this exact shape: `[{ accessor: 'title', active: true }, { accessor: 'slug', active: false }]`
* This means that when handling columns, they need to be consistently transformed back and forth
*/ export const transformColumnsToPreferences = (columns)=>{
if (!columns) {
return undefined;
}
let columnsToTransform = columns;
// Columns that originate from the URL are a stringified JSON array and need to be parsed first
if (typeof columns === 'string') {
try {
columnsToTransform = JSON.parse(columns);
} catch (e) {
console.error('Error parsing columns', columns, e); // eslint-disable-line no-console
}
}
if (columnsToTransform && Array.isArray(columnsToTransform)) {
return columnsToTransform.map((col)=>{
if (typeof col === 'string') {
const active = col[0] !== '-';
return {
accessor: active ? col : col.slice(1),
active
};
}
return {
accessor: col.accessor,
active: col.active
};
});
}
};
/**
* Does the opposite of `transformColumnsToPreferences`, where `ColumnPreference[]` and `Column[]` are transformed into `ColumnsFromURL`
* This is useful for storing the columns in the URL, where it appears as a simple comma delimited array of strings
* The `-` prefix is used to indicate that the column is inactive
*/ export const transformColumnsToSearchParams = (columns)=>{
return columns?.map((col)=>col.active ? col.accessor : `-${col.accessor}`);
};
//# sourceMappingURL=transformColumnPreferences.js.map