fix(container-returns): dedupe company names in the return picker

freight.companies allows duplicate names, and Mantine v9 throws on duplicate
Autocomplete option values, taking down the whole Container Returns page with
a render error.

Dedupes the option list by trimmed name. resolveId now returns an id only
when exactly one company carries the name — an ambiguous name resolves to
nothing, so the return keeps the typed company name rather than silently
attaching to whichever duplicate happened to come first.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Hagernesh
2026-08-28 12:10:44 +00:00
parent 9e1e680394
commit a183f00657

View File

@@ -16,11 +16,22 @@ export function useCompanyOptions() {
const companies = data?.items ?? [];
// Company names are not unique — Mantine throws on duplicate option values,
// so the list is deduped by the trimmed name.
const names = [...new Set(companies.map((c) => c.name.trim()).filter(Boolean))];
return {
loading: isLoading,
names: companies.map((c) => c.name),
/** Exact (case-insensitive) name match → company id, else undefined. */
resolveId: (name: string): string | undefined =>
companies.find((c) => c.name.trim().toLowerCase() === name.trim().toLowerCase())?.id,
names,
/**
* Name → company id, only when exactly one company carries that name. An
* ambiguous name resolves to nothing rather than to an arbitrary company:
* the container keeps the typed name and no wrong customer is attached.
*/
resolveId: (name: string): string | undefined => {
const key = name.trim().toLowerCase();
const matches = companies.filter((c) => c.name.trim().toLowerCase() === key);
return matches.length === 1 ? matches[0].id : undefined;
},
};
}