fix: align company data with machines
This commit is contained in:
parent
40e92cf2b9
commit
5de8b2bf7f
3 changed files with 124 additions and 22 deletions
|
|
@ -119,22 +119,59 @@ export function AdminCompaniesManager({ initialCompanies }: { initialCompanies:
|
|||
const machinesByCompanyId = useMemo(() => {
|
||||
const map = new Map<string, MachineSummary[]>()
|
||||
;(machinesQuery ?? []).forEach((machine) => {
|
||||
if (!machine.companyId) return
|
||||
const list = map.get(machine.companyId) ?? []
|
||||
list.push(machine)
|
||||
map.set(machine.companyId, list)
|
||||
const keys = [
|
||||
machine.companyId ?? undefined,
|
||||
machine.companySlug ?? undefined,
|
||||
].filter((key): key is string => Boolean(key))
|
||||
if (keys.length === 0) {
|
||||
return
|
||||
}
|
||||
keys.forEach((key) => {
|
||||
const list = map.get(key)
|
||||
if (list) {
|
||||
list.push(machine)
|
||||
} else {
|
||||
map.set(key, [machine])
|
||||
}
|
||||
})
|
||||
})
|
||||
return map
|
||||
}, [machinesQuery])
|
||||
|
||||
const editingCompanyMachines = useMemo(() => {
|
||||
if (!editingId) return []
|
||||
return machinesByCompanyId.get(editingId) ?? []
|
||||
}, [machinesByCompanyId, editingId])
|
||||
const machinesDialogList = useMemo(() => {
|
||||
if (!machinesDialog) return []
|
||||
return machinesByCompanyId.get(machinesDialog.companyId) ?? []
|
||||
}, [machinesByCompanyId, machinesDialog])
|
||||
const getMachinesForCompany = useCallback(
|
||||
(company: Company | null | undefined) => {
|
||||
if (!company) return []
|
||||
const keys = [company.id, company.slug].filter(Boolean)
|
||||
for (const key of keys) {
|
||||
const list = machinesByCompanyId.get(key)
|
||||
if (list && list.length > 0) {
|
||||
return list
|
||||
}
|
||||
}
|
||||
return []
|
||||
},
|
||||
[machinesByCompanyId]
|
||||
)
|
||||
|
||||
const editingCompany = useMemo(
|
||||
() => (editingId ? companies.find((company) => company.id === editingId) ?? null : null),
|
||||
[companies, editingId]
|
||||
)
|
||||
|
||||
const editingCompanyMachines = useMemo(
|
||||
() => getMachinesForCompany(editingCompany),
|
||||
[getMachinesForCompany, editingCompany]
|
||||
)
|
||||
|
||||
const machinesDialogCompany = useMemo(
|
||||
() => (machinesDialog ? companies.find((company) => company.id === machinesDialog.companyId) ?? null : null),
|
||||
[companies, machinesDialog]
|
||||
)
|
||||
|
||||
const machinesDialogList = useMemo(
|
||||
() => getMachinesForCompany(machinesDialogCompany),
|
||||
[getMachinesForCompany, machinesDialogCompany]
|
||||
)
|
||||
|
||||
const resetForm = () => setForm({})
|
||||
|
||||
|
|
@ -525,7 +562,7 @@ export function AdminCompaniesManager({ initialCompanies }: { initialCompanies:
|
|||
<div className="space-y-4 rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
|
||||
{hasCompanies ? (
|
||||
filteredCompanies.map((company) => {
|
||||
const companyMachines = machinesByCompanyId.get(company.id) ?? []
|
||||
const companyMachines = getMachinesForCompany(company)
|
||||
const formattedPhone = formatPhoneDisplay(company.phone)
|
||||
const alertInfo = lastAlerts[company.slug] ?? null
|
||||
const usagePct = alertInfo?.usagePct ?? 0
|
||||
|
|
@ -714,7 +751,7 @@ export function AdminCompaniesManager({ initialCompanies }: { initialCompanies:
|
|||
? formatDistanceToNow(alertInfo.createdAt, { addSuffix: true, locale: ptBR })
|
||||
: null
|
||||
const formattedPhone = formatPhoneDisplay(company.phone)
|
||||
const companyMachines = machinesByCompanyId.get(company.id) ?? []
|
||||
const companyMachines = getMachinesForCompany(company)
|
||||
const machineCount = companyMachines.length
|
||||
return (
|
||||
<TableRow
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue