From fe585f421c4c9dda1e41c10d32c6bb8c3892adb9 Mon Sep 17 00:00:00 2001 From: romanskijdev Date: Mon, 27 Jul 2026 12:36:44 +0300 Subject: [PATCH] Fix users page crash --- src/components/UserEditModal.tsx | 45 ++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/components/UserEditModal.tsx b/src/components/UserEditModal.tsx index 1fe1d8f..1f744dd 100644 --- a/src/components/UserEditModal.tsx +++ b/src/components/UserEditModal.tsx @@ -61,13 +61,25 @@ export default function UserEditModal({ userId, opened, onClose }: UserEditModal department_id: user.department_id || null, is_blocked: user.is_blocked || false, is_fired: user.is_fired || false, - role_ids: user.user_roles?.map((ur) => ur.role_id!) || [], + // 🩹 ФИКС: user.user_roles?.map(...) защищал только от + // null/undefined, но не от случая, когда пришёл не массив, + // а объект-обёртка — именно это и роняло страницу с + // "l?.map is not a function" (см. componentStack — крэш + // внутри дочернего компонента UsersPage, коим и является + // этот модал, он рендерится всегда, не только при opened). + role_ids: Array.isArray(user.user_roles) + ? user.user_roles.map((ur) => ur.role_id!) + : [], }) } }, [user]) useEffect(() => { - if (userRoles && userRoles.length > 0) { + // 🩹 ФИКС: было `userRoles && userRoles.length > 0` — не роняло + // страницу (не вызывает .map при не-массиве), но и не защищало + // корректно от объекта-обёртки с полем length. Array.isArray + // явный и однозначный. + if (Array.isArray(userRoles) && userRoles.length > 0) { const roleIds = userRoles .map((ur) => ur.role?.id || ur.role_id) .filter((id): id is number => !!id) @@ -103,16 +115,27 @@ export default function UserEditModal({ userId, opened, onClose }: UserEditModal setCopyRolesOpened(false) } - const departmentOptions = departments?.map((d) => ({ - value: String(d.id), - label: d.name || '', - })) || [] + // 🩹 ГЛАВНЫЙ ФИКС: было `departments?.map(...) || []` — optional + // chaining защищает только от null/undefined, а не от случая, когда + // departments пришёл не массивом (например, объектом-обёрткой вида + // {data: [...], total_count} — судя по такой же явной проверке в + // UsersPage.tsx для тех же хуков, это реально случающийся кейс). + // Array.isArray — единственная надёжная защита от "map is not a function". + const departmentOptions = Array.isArray(departments) + ? departments.map((d) => ({ + value: String(d.id), + label: d.name || '', + })) + : [] - const roleOptions = allRoles?.map((r) => ({ - value: String(r.id), - label: r.name || '', - description: r.description, - })) || [] + // 🩹 То же самое для ролей. + const roleOptions = Array.isArray(allRoles) + ? allRoles.map((r) => ({ + value: String(r.id), + label: r.name || '', + description: r.description, + })) + : [] const isLoading = userLoading || rolesLoading const isSubmitting = updateUser.isPending || assignRoles.isPending