Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
👻 add support for multiple sorts on the table
Brad Barbin18f076effa8a
diff --git a/apps/carbon/app/components/Data/Table/Table.tsx b/apps/carbon/app/components/Data/Table/Table.tsxindex 66b431a979..d2dc9954ec 100644--- a/apps/carbon/app/components/Data/Table/Table.tsx+++ b/apps/carbon/app/components/Data/Table/Table.tsx@@ -92,22 +92,28 @@ const Table = <T extends object>({onSelectedRowsChange,}: TableProps<T>) => {const [params, setParams] = useUrlParams();+const toggleSortBy = (columnId: string) => {- const sort = params.getAll("sort");+ const existingSort = params.getAll("sort");const sortAsc = `${columnId}:asc`;const sortDesc = `${columnId}:desc`;- if (sort.includes(sortAsc)) {- setParams({ ...Object.fromEntries(params), sort: sortDesc });- } else if (sort.includes(sortDesc)) {- setParams({ ...Object.fromEntries(params), sort: undefined });+ if (existingSort.includes(sortAsc)) {+ setParams({+ sort: existingSort.filter((s) => s !== sortAsc).concat(sortDesc),+ });+ } else if (existingSort.includes(sortDesc)) {+ setParams({ sort: existingSort.filter((s) => s !== sortDesc) });} else {- setParams({ ...Object.fromEntries(params), sort: sortAsc });+ setParams({ sort: existingSort.concat(sortAsc) });}};+const isSorted = (columnId: string): -1 | null | 1 => {- if (params.get("sort") === `${columnId}:asc`) return 1;- if (params.get("sort") === `${columnId}:desc`) return -1;+ const existingSort = params.getAll("sort");++ if (existingSort.includes(`${columnId}:asc`)) return 1;+ if (existingSort.includes(`${columnId}:desc`)) return -1;return null;};diff --git a/apps/carbon/app/hooks/useUrlParams.tsx b/apps/carbon/app/hooks/useUrlParams.tsxindex 2641649506..df815dfcaf 100644--- a/apps/carbon/app/hooks/useUrlParams.tsx+++ b/apps/carbon/app/hooks/useUrlParams.tsx@@ -3,16 +3,32 @@ import { useCallback } from "react";export function useUrlParams(): [URLSearchParams,- (params: Record<string, string | number | undefined | null>) => void+ (+ params: Record<string, string | string[] | number | undefined | null>+ ) => void] {const submit = useSubmit();const [searchParams] = useSearchParams();const setSearchParams = useCallback(- (params: Record<string, string | number | undefined | null>) => {+ (params: Record<string, string | string[] | number | undefined | null>) => {Object.entries(params).forEach(([name, value]) => {if (value) {- searchParams.set(name, value.toString());+ if (Array.isArray(value)) {+ if (value.length === 0) {+ searchParams.delete(name);+ } else {+ value.forEach((v, i) => {+ if (i === 0) {+ searchParams.set(name, v.toString());+ } else {+ searchParams.append(name, v.toString());+ }+ });+ }+ } else {+ searchParams.set(name, value.toString());+ }} else {searchParams.delete(name);}diff --git a/apps/carbon/app/routes/app.users.employee-types.tsx b/apps/carbon/app/routes/app.users.employee-types.tsxindex 945bbdab9b..961282cda7 100644--- a/apps/carbon/app/routes/app.users.employee-types.tsx+++ b/apps/carbon/app/routes/app.users.employee-types.tsx@@ -7,7 +7,7 @@ import {} from "~/modules/Users/EmployeeTypes";import { requirePermissions } from "~/services/auth";import { getEmployeeTypes } from "~/services/users";-import { getPaginationParams } from "~/utils/http";+import { getQueryFilters } from "~/utils/http";export async function loader({ request }: LoaderArgs) {const { client } = await requirePermissions(request, {@@ -17,7 +17,7 @@ export async function loader({ request }: LoaderArgs) {const url = new URL(request.url);const searchParams = new URLSearchParams(url.search);const name = searchParams.get("name");- const { limit, offset } = getPaginationParams(searchParams);+ const { limit, offset } = getQueryFilters(searchParams);return json(await getEmployeeTypes(client, { name, limit, offset }));}diff --git a/apps/carbon/app/routes/app.users.employees.tsx b/apps/carbon/app/routes/app.users.employees.tsxindex 13201d5cd4..856e1cf8cb 100644--- a/apps/carbon/app/routes/app.users.employees.tsx+++ b/apps/carbon/app/routes/app.users.employees.tsx@@ -8,7 +8,7 @@ import {} from "~/modules/Users/Employees";import { requirePermissions } from "~/services/auth";import { getEmployees, getEmployeeTypes } from "~/services/users";-import { getPaginationParams } from "~/utils/http";+import { getQueryFilters } from "~/utils/http";export async function loader({ request }: LoaderArgs) {const { client } = await requirePermissions(request, {@@ -19,10 +19,10 @@ export async function loader({ request }: LoaderArgs) {const searchParams = new URLSearchParams(url.search);const name = searchParams.get("name");const type = searchParams.get("type");- const { limit, offset, sortBy, sortAsc } = getPaginationParams(searchParams);+ const { limit, offset, sorts } = getQueryFilters(searchParams);const [employees, employeeTypes] = await Promise.all([- getEmployees(client, { name, type, limit, offset, sortBy, sortAsc }),+ getEmployees(client, { name, type, limit, offset, sorts }),getEmployeeTypes(client),]);diff --git a/apps/carbon/app/routes/app.users.groups.tsx b/apps/carbon/app/routes/app.users.groups.tsxindex 13d26148bd..d5e3856e9e 100644--- a/apps/carbon/app/routes/app.users.groups.tsx+++ b/apps/carbon/app/routes/app.users.groups.tsx@@ -7,7 +7,7 @@ import type { Group } from "~/modules/Users/types";import { requirePermissions } from "~/services/auth";import { flash } from "~/services/session";import { getGroups } from "~/services/users";-import { getPaginationParams } from "~/utils/http";+import { getQueryFilters } from "~/utils/http";import { error } from "~/utils/result";export async function loader({ request }: LoaderArgs) {@@ -19,7 +19,7 @@ export async function loader({ request }: LoaderArgs) {const searchParams = new URLSearchParams(url.search);const name = searchParams.get("name");const uid = searchParams.get("uid");- const { limit, offset } = getPaginationParams(searchParams);+ const { limit, offset } = getQueryFilters(searchParams);const groups = await getGroups(client, { name, uid, limit, offset });diff --git a/apps/carbon/app/services/users/users.server.ts b/apps/carbon/app/services/users/users.server.tsindex c096962cfb..1e3ff6354e 100644--- a/apps/carbon/app/services/users/users.server.ts+++ b/apps/carbon/app/services/users/users.server.ts@@ -14,7 +14,7 @@ import { deleteAuthAccount, sendInviteByEmail } from "~/services/auth";import { requireAuthSession, flash } from "~/services/session";import type { Result } from "~/types";import type { PaginationParams } from "~/utils/http";-import { setPaginationParams } from "~/utils/http";+import { setQueryFilters } from "~/utils/http";import { error, success } from "~/utils/result";export async function createEmployeeAccount(@@ -161,7 +161,7 @@ export async function getEmployees(query = query.eq("employeeTypeId", args.type);}- query = setPaginationParams(query, args, "user(lastName)");+ query = setQueryFilters(query, args, "user(lastName)");return query;}@@ -189,7 +189,7 @@ export async function getEmployeeTypes(}if (args) {- query = setPaginationParams(query, args, "name");+ query = setQueryFilters(query, args, "name");}return query;diff --git a/apps/carbon/app/utils/http.ts b/apps/carbon/app/utils/http.tsindex 43511b6ccb..0eaaca8595 100644--- a/apps/carbon/app/utils/http.ts+++ b/apps/carbon/app/utils/http.ts@@ -107,30 +107,42 @@ export function parseNumberFromUrlParam(return parsed;}+export type Sort = {+ sortBy: string;+ sortAsc: boolean;+};+export interface PaginationParams {limit: number;offset: number;- sortBy: string | null;- sortAsc: boolean;+ sorts?: Sort[];}-export function getPaginationParams(params: URLSearchParams): PaginationParams {+export function getQueryFilters(params: URLSearchParams): PaginationParams {const limit = parseNumberFromUrlParam(params, "limit", 15);const offset = parseNumberFromUrlParam(params, "offset", 0);- let sortBy = null;- let sortDirection = "asc";-- const sort = params.get("sort");- if (sort) {- [sortBy, sortDirection] = sort.split(":");- }- const sortAsc = sortDirection === "asc";-- return { limit, offset, sortBy, sortAsc };-}--export function setPaginationParams<+ const sortParams = params.getAll("sort");+ const sorts: Sort[] =+ sortParams.length > 0+ ? (sortParams+ .map((sort) => {+ const [sortBy, sortDirection] = sort.split(":");+ if (+ !sortBy ||+ !sortDirection ||+ !["asc", "desc"].includes(sortDirection)+ )+ return undefined;+ return { sortBy, sortAsc: sortDirection === "asc" };+ })+ .filter((sort) => sort !== undefined) as Sort[])+ : [];++ return { limit, offset, sorts };+}++export function setQueryFilters<T extends GenericSchema,U extends Record<string, unknown>,V@@ -139,15 +151,17 @@ export function setPaginationParams<args: PaginationParams,defaultSort?: string): PostgrestFilterBuilder<T, U, V> {- if (args.sortBy) {- if (args.sortBy.includes(".")) {- const [table, column] = args.sortBy.split(".");- query = query.order(`${table}(${column})`, {- ascending: args.sortAsc,- });- } else {- query = query.order(args.sortBy, { ascending: args.sortAsc });- }+ if (args.sorts && args.sorts.length > 0) {+ args.sorts.forEach((sort) => {+ if (sort.sortBy.includes(".")) {+ const [table, column] = sort.sortBy.split(".");+ query = query.order(`${table}(${column})`, {+ ascending: sort.sortAsc,+ });+ } else {+ query = query.order(sort.sortBy, { ascending: sort.sortAsc });+ }+ });} else if (defaultSort) {query = query.order(defaultSort, {ascending: true,