Compare commits

..

No commits in common. "f5a06188a2773cfef4e4ca2876723cf96dd0da31" and "8131e26c778276fd4938e6eaea8f99815e855d20" have entirely different histories.

6 changed files with 31 additions and 171 deletions

View File

@ -1,38 +0,0 @@
import React from "react";
/**
* Accessible iOS-style toggle switch.
* @param {object} props
* @param {boolean} props.checked
* @param {function} props.onChange Called with the next boolean value.
* @param {string} [props.label] Visible text next to the switch.
* @param {string} [props.title] Hover tooltip.
* @param {string} [props.className]
*/
export const Switch = ({ checked = false, onChange, label, title, className = "" }) => {
const id = React.useId();
return (
<label htmlFor={id} className={`inline-flex cursor-pointer select-none items-center gap-2.5 text-sm text-[var(--color-text)] ${className}`} title={title}>
<button
type="button"
id={id}
role="switch"
aria-checked={checked}
onClick={() => onChange && onChange(!checked)}
className={`relative inline-flex h-6 w-11 shrink-0 items-center rounded-full border transition-colors ${
checked
? "border-[var(--color-accent)] bg-[var(--color-accent)]"
: "border-[var(--color-border)] bg-[var(--color-surface-strong)]"
}`}
>
<span
aria-hidden="true"
className={`inline-block h-5 w-5 transform rounded-full bg-white shadow transition-transform ${
checked ? "translate-x-[22px]" : "translate-x-0.5"
}`}
/>
</button>
{label ? <span>{label}</span> : null}
</label>
);
};

View File

@ -302,7 +302,7 @@ const isFilterActive = (filters) => {
return false;
};
export const LogisticsReadinessBoard = ({ orderGroups = [], totalGroups: totalGroupsProp, hideCompleted = true, onToggleHideCompleted, onSelectSet, statusOptions = ORDER_GROUP_DISPLAY_STATUS_OPTIONS, isLoading = false }) => {
export const LogisticsReadinessBoard = ({ orderGroups = [], onSelectSet, statusOptions = ORDER_GROUP_DISPLAY_STATUS_OPTIONS, isLoading = false }) => {
const FILTERS_KEY = 'logistics-board-filters';
const [filters, setFilters] = React.useState(() => {
try {
@ -487,7 +487,7 @@ export const LogisticsReadinessBoard = ({ orderGroups = [], totalGroups: totalGr
Перетаскивайте секции за ручку слева, чтобы изменить порядок отображения.
</p>
</div>
<Badge tone="neutral">{totalGroupsProp ?? totalGroups} групп</Badge>
<Badge tone="neutral">{totalGroups} групп</Badge>
</div>
<OrderFilters
@ -495,8 +495,6 @@ export const LogisticsReadinessBoard = ({ orderGroups = [], totalGroups: totalGr
setFilters={setFilters}
statusOptions={statusOptions}
cities={cities}
hideCompleted={hideCompleted}
onToggleHideCompleted={onToggleHideCompleted}
/>
{!hasActiveFilters && totalGroups > PAGE_SIZE && (

View File

@ -3,7 +3,6 @@ import { Button } from "../UI/Button";
import { Input } from "../UI/Input";
import { Panel } from "../UI/Panel";
import { DatePicker } from "../UI/DatePicker";
import { Switch } from "../UI/Switch";
const DropdownSelect = ({ value, options, onChange, placeholder }) => {
const [isOpen, setIsOpen] = React.useState(false);
@ -47,7 +46,7 @@ const DropdownSelect = ({ value, options, onChange, placeholder }) => {
{isOpen ? (
<div
role="listbox"
className="absolute left-0 top-full z-20 mt-2 w-max min-w-full whitespace-nowrap overflow-hidden rounded-2xl border border-[var(--color-border)] bg-[var(--color-dropdown-surface)] shadow-soft"
className="absolute left-0 right-0 top-full z-20 mt-2 overflow-hidden rounded-2xl border border-[var(--color-border)] bg-[var(--color-dropdown-surface)] shadow-soft"
>
{options.map((option) => {
const isSelected = option.value === value;
@ -78,7 +77,7 @@ const DropdownSelect = ({ value, options, onChange, placeholder }) => {
);
};
export const OrderFilters = ({ filters, setFilters, statusOptions = [], cities = [], hideCompleted = true, onToggleHideCompleted }) => {
export const OrderFilters = ({ filters, setFilters, statusOptions = [], cities = [] }) => {
const statusValue = filters.displayStatus || filters.status || "all";
const selectedStatusLabel = statusOptions.find((option) => option.value === statusValue)?.label || statusValue;
const [isStatusOpen, setIsStatusOpen] = React.useState(false);
@ -130,8 +129,8 @@ export const OrderFilters = ({ filters, setFilters, statusOptions = [], cities =
return (
<Panel className="p-4">
<div className="flex flex-col gap-3">
{/* Row 1: Status + City + DeliveryType + Search (search spans wider) */}
<div className="grid gap-3 md:grid-cols-[minmax(12rem,0.7fr)_minmax(0,0.5fr)_minmax(0,0.5fr)_minmax(0,1.6fr)] md:items-end">
{/* Row 1: Status + City + DeliveryType + Search */}
<div className="grid gap-3 md:grid-cols-[minmax(12rem,0.7fr)_minmax(0,0.5fr)_minmax(0,0.5fr)_minmax(0,1.4fr)] md:items-end">
<div ref={statusMenuRef} className="relative flex min-w-0 flex-col gap-2">
<button
type="button"
@ -197,6 +196,13 @@ export const OrderFilters = ({ filters, setFilters, statusOptions = [], cities =
placeholder="Тип получения"
/>
<DropdownSelect
value={filters.smsStatus || "all"}
options={smsProblemOptions}
onChange={(v) => updateFilter("smsStatus", v)}
placeholder="Проблемы SMS"
/>
<Input
className="h-[46px] py-0"
placeholder="Поиск по группе, клиенту или телефону"
@ -205,10 +211,8 @@ export const OrderFilters = ({ filters, setFilters, statusOptions = [], cities =
/>
</div>
{/* Row 2: Date range + SMS problems (width matches its text) + hide-completed toggle.
Align by the bottom edge of the input fields, not by center — dates have a
label above the field, so items-center would lift SMS/toggle above them. */}
<div className="flex flex-wrap items-end gap-3">
{/* Row 2: Date range */}
<div className="flex items-end gap-3 flex-wrap">
<DatePicker
value={filters.dateFrom || ""}
onChange={(v) => updateFilter("dateFrom", v)}
@ -232,22 +236,6 @@ export const OrderFilters = ({ filters, setFilters, statusOptions = [], cities =
✕ Сбросить
</button>
)}
<DropdownSelect
value={filters.smsStatus || "all"}
options={smsProblemOptions}
onChange={(v) => updateFilter("smsStatus", v)}
placeholder="Проблемы SMS"
/>
{typeof onToggleHideCompleted === "function" && (
<Switch
checked={hideCompleted}
onChange={onToggleHideCompleted}
label="Скрыть завершённые"
title="Доставленные и отгруженные заказы скрыты"
/>
)}
</div>
</div>
</Panel>

View File

@ -1,5 +1,5 @@
import React from "react";
import { assignDriverToOrderGroup, fetchOrderGroupById, fetchOrderGroups, readOrderGroupsCache, saveShipmentData, updateDeliveryStatus, updateOrderGroupDeliveryChoice } from "../services/supabase/orderGroupRepository";
import { assignDriverToOrderGroup, fetchOrderGroupById, fetchOrderGroups, saveShipmentData, updateDeliveryStatus, updateOrderGroupDeliveryChoice } from "../services/supabase/orderGroupRepository";
import {
buildOrderGroupBuckets,
filterOrderGroups,
@ -9,21 +9,8 @@ import {
} from "../services/orderGroupViews";
import { getErrorMessage } from "../utils/deliveryUtils";
export const useOrderGroups = ({
enabled = true,
details = false,
groupId = null,
hideCompleted = true,
} = {}) => {
const [orderGroups, setOrderGroups] = React.useState(() => {
// Instant paint from cache on return-to-list, then background refresh.
const cached = readOrderGroupsCache(details, hideCompleted);
return cached?.data || [];
});
const [totalGroups, setTotalGroups] = React.useState(() => {
const cached = readOrderGroupsCache(details, hideCompleted);
return cached?.total || 0;
});
export const useOrderGroups = ({ enabled = true, details = false, groupId = null } = {}) => {
const [orderGroups, setOrderGroups] = React.useState(() => []);
const FILTERS_STORAGE_KEY = "supersam_order_filters";
const [filters, setFilters] = React.useState(() => {
try {
@ -46,7 +33,7 @@ export const useOrderGroups = ({
const [isSavingDriverAssignment, setIsSavingDriverAssignment] = React.useState(false);
const [isSavingStatusChange, setIsSavingStatusChange] = React.useState(false);
const loadLiveData = React.useCallback(async (showLoading = true, opts = {}) => {
const loadLiveData = React.useCallback(async (showLoading = true) => {
if (showLoading) {
setIsLoading(true);
}
@ -72,7 +59,7 @@ export const useOrderGroups = ({
return;
}
const groupsResult = await fetchOrderGroups({ details, hideCompleted, forceRefresh: opts?.forceRefresh });
const groupsResult = await fetchOrderGroups({ details });
if (!groupsResult) {
return;
@ -85,29 +72,16 @@ export const useOrderGroups = ({
return;
}
const payload = groupsResult.data || {};
setOrderGroups(payload.data || []);
setTotalGroups(payload.total ?? (payload.data?.length || 0));
setOrderGroups(groupsResult.data || []);
setIsLoading(false);
}, [enabled, details, groupId, hideCompleted]);
}, [enabled, details, groupId]);
// Initial load
const hasLoadedRef = React.useRef(false);
React.useEffect(() => {
let cancelled = false;
const doLoad = async () => {
const isFirst = !hasLoadedRef.current;
if (isFirst) {
const hasCache = !!readOrderGroupsCache(details, hideCompleted);
// Full skeleton only on the very first entry; otherwise refresh silently
// so toggling the "hide completed" switch updates the list in place
// without flashing the whole page / jumping to top.
setIsLoading(!hasCache);
await loadLiveData(!hasCache, { forceRefresh: true });
hasLoadedRef.current = true;
} else {
await loadLiveData(false, { forceRefresh: true });
}
setIsLoading(true);
await loadLiveData(true);
if (cancelled) return;
};
if (enabled) doLoad(); else { setOrderGroups([]); setIsLoading(false); }
@ -288,7 +262,6 @@ export const useOrderGroups = ({
return {
orderGroups,
allOrderGroups: orderGroups,
totalGroups,
filteredOrderGroups,
visibleOrderGroups,
selectedOrderGroup,

View File

@ -91,14 +91,6 @@ export const DashboardPage = () => {
const { isInstalled, isInstallAvailable, installApp: onInstallApp } = usePwaStatus();
// ── Order Groups ─────────────────────────────────────────────────────────
const HIDE_COMPLETED_KEY = "supersam-hide-completed";
const [hideCompleted, setHideCompleted] = React.useState(() => {
try { return localStorage.getItem(HIDE_COMPLETED_KEY) !== "false"; } catch { return true; }
});
React.useEffect(() => {
try { localStorage.setItem(HIDE_COMPLETED_KEY, hideCompleted ? "true" : "false"); } catch {}
}, [hideCompleted]);
const {
orderGroups,
allOrderGroups,
@ -110,8 +102,7 @@ export const DashboardPage = () => {
statusOptions,
isLoading,
loadError,
totalGroups,
} = useOrderGroups({ enabled: ["orders", "logistics", "deliveries"].includes(activeSection), details: false, hideCompleted });
} = useOrderGroups({ enabled: ["orders", "logistics", "deliveries"].includes(activeSection), details: false });
// ── Derived City List ─────────────────────────────────────────────────────
const cities = React.useMemo(() => {
@ -209,7 +200,7 @@ const ALLOWED_DASHBOARD_ROLES = ["admin", "mega_admin", "manager", "logistician"
// All roles (logistician, admin, mega_admin, manager) — same grouped board
return (
<div className="space-y-6 xl:space-y-8">
<LogisticsReadinessBoard orderGroups={allOrderGroups} totalGroups={totalGroups} hideCompleted={hideCompleted} onToggleHideCompleted={setHideCompleted} onSelectSet={openGroupPage} statusOptions={statusOptions} isLoading={isLoading} />
<LogisticsReadinessBoard orderGroups={allOrderGroups} onSelectSet={openGroupPage} statusOptions={statusOptions} isLoading={isLoading} />
</div>
);
};

View File

@ -289,10 +289,7 @@ export const mapOrderGroupRowToDeliveryGroup = (row) => {
};
const ORDER_GROUP_SELECT_FIELDS = `id, group_key, order_numbers, status, delivery_status, sms_sent_at, created_at, updated_at, created_from_exchange_at, source_key, town, customer_name, customer_phone, customer_phone_normalized, customer_date, orders_total, orders_ready, orders_not_ready, source_orders, order_list, order_list_structured, delivery_invitation_id, delivery_link, delivery_link_code, notification_status, sms_attempts, first_sms_sent_at, second_sms_sent_at, last_sms_error, next_notification_check_at, delivery_date, delivery_time, delivery_address, customer_address, delivery_date_source, manual_confirmation_at, paid_storage_at, assigned_driver_id, assigned_driver:users!order_groups_assigned_driver_id_fkey(id, name), driver_shipment_data, delivery_type, pickup_date, pickup_time_slot, has_delivery_problem, delivery_problem_note, pickup_code, synced_to_1c_at, manager_name, manager_email, manager_tel, payed_ship, is_payed_ship, delivery_invitation:delivery_invitations!order_groups_delivery_invitation_id_fkey(opened_at, access_count, last_accessed_at), called, called_at, called_comment, order_history(id, action, old_status, new_status, user_id, metadata, created_at)`;
export const ORDER_GROUP_LIST_SELECT_FIELDS = `id, group_key, order_numbers, status, delivery_status, sms_sent_at, created_at, updated_at, created_from_exchange_at, source_key, town, customer_name, customer_phone, customer_phone_normalized, customer_date, orders_total, orders_ready, orders_not_ready, notification_status, sms_attempts, last_sms_error, first_sms_sent_at, second_sms_sent_at, delivery_date, delivery_time, delivery_address, customer_address, delivery_date_source, manual_confirmation_at, paid_storage_at, assigned_driver_id, assigned_driver:users!order_groups_assigned_driver_id_fkey(id, name), driver_shipment_data, delivery_type, pickup_date, pickup_time_slot, has_delivery_problem, delivery_problem_note, pickup_code, synced_to_1c_at, manager_name, manager_email, manager_tel, payed_ship, is_payed_ship, called, called_at, called_comment`;
// Delivery statuses treated as "finished" — hidden by default in the board.
const COMPLETED_DELIVERY_STATUSES = ["delivered", "picked_up", "cancelled"];
const ORDER_GROUP_LIST_SELECT_FIELDS = `id, group_key, order_numbers, status, delivery_status, sms_sent_at, created_at, updated_at, created_from_exchange_at, source_key, town, customer_name, customer_phone, customer_phone_normalized, customer_date, orders_total, orders_ready, orders_not_ready, notification_status, sms_attempts, last_sms_error, first_sms_sent_at, second_sms_sent_at, delivery_date, delivery_time, delivery_address, customer_address, delivery_date_source, manual_confirmation_at, paid_storage_at, assigned_driver_id, assigned_driver:users!order_groups_assigned_driver_id_fkey(id, name), driver_shipment_data, delivery_type, pickup_date, pickup_time_slot, has_delivery_problem, delivery_problem_note, pickup_code, synced_to_1c_at, manager_name, manager_email, manager_tel, payed_ship, is_payed_ship, called, called_at, called_comment`;
export const updateOrderGroupDeliveryChoice = async ({
orderGroupId,
@ -303,7 +300,6 @@ export const updateOrderGroupDeliveryChoice = async ({
pickupDate,
pickupTimeSlot,
}) => {
orderGroupsCache.clear();
return safeSupabaseCall(async () => {
const client = requireSupabase();
const effectiveDeliveryStatus = deliveryType === "pickup" ? "pickup" : "agreed";
@ -358,7 +354,6 @@ export const assignDriverToOrderGroup = async ({
orderGroupId,
driverId,
}) => {
orderGroupsCache.clear();
return safeSupabaseCall(async () => {
const client = requireSupabase();
@ -421,7 +416,6 @@ export const assignDriverToOrderGroup = async ({
};
export const saveShipmentData = async ({ orderGroupId, shipmentData }) => {
orderGroupsCache.clear();
return safeSupabaseCall(async () => {
const client = requireSupabase();
const hasAnyShipped = shipmentData.some((i) => i.shipped);
@ -478,7 +472,6 @@ export const saveShipmentData = async ({ orderGroupId, shipmentData }) => {
};
export const updateDeliveryStatus = async ({ orderGroupId, status, details, shipmentData } = {}) => {
orderGroupsCache.clear();
return safeSupabaseCall(async () => {
const client = requireSupabase();
@ -573,55 +566,14 @@ export const updateDeliveryStatus = async ({ orderGroupId, status, details, ship
}, "Ошибка обновления статуса доставки");
};
// Module-level cache so returning to the list from a card doesn't refetch the
// whole board (the previous "return to list is slow" annoyance). Keyed by details
// + hideCompleted. The hook refetches silently in the background to stay fresh.
const orderGroupsCache = new Map();
const orderGroupsCacheKey = (details, hideCompleted) =>
`v1:${details ? "detail" : "list"}:${hideCompleted ? "hide" : "all"}`;
/** Synchronous read of the cached board list ({ data, total } | null). */
export const readOrderGroupsCache = (details = false, hideCompleted = true) =>
orderGroupsCache.get(orderGroupsCacheKey(details, hideCompleted)) || null;
/**
* Fetch order groups.
*
* Returns { data, total } where total is the full count matching the filter
* (requires `count` support from PostgREST — available on self-hosted).
*
* @param {object} options
* @param {boolean} [options.details=false] Load full detail fields (for detail pages)
* @param {boolean} [options.hideCompleted=true] Exclude delivered/picked_up/cancelled groups
* @param {boolean} [options.forceRefresh=false] Bypass the in-memory cache and refetch
*/
export const fetchOrderGroups = async ({
details = false,
hideCompleted = true,
forceRefresh = false,
} = {}) => {
const cacheKey = orderGroupsCacheKey(details, hideCompleted);
if (!forceRefresh) {
const cached = orderGroupsCache.get(cacheKey);
if (cached) return cached;
}
export const fetchOrderGroups = async ({ details = true } = {}) => {
return safeSupabaseCall(async () => {
const client = requireSupabase();
let query = client
const { data, error } = await client
.from("order_groups")
.select(details ? ORDER_GROUP_SELECT_FIELDS : ORDER_GROUP_LIST_SELECT_FIELDS, { count: "exact" })
.select(details ? ORDER_GROUP_SELECT_FIELDS : ORDER_GROUP_LIST_SELECT_FIELDS)
.order("updated_at", { ascending: false });
if (hideCompleted) {
query = query.not("delivery_status", "in", `(${COMPLETED_DELIVERY_STATUSES.join(",")})`);
}
const { data, error, count } = await query;
if (error) {
throw error;
}
@ -633,17 +585,13 @@ export const fetchOrderGroups = async ({
drivers.forEach((d) => driverMap.set(d.id, d.name || d.email));
}
const groups = (data || []).map((row) => {
return (data || []).map((row) => {
const group = mapOrderGroupRowToDeliveryGroup(row);
if (group && group.assignedDriverId && !group.assignedDriverName) {
group.assignedDriverName = driverMap.get(group.assignedDriverId) || "";
}
return group;
}).filter(Boolean);
const result = { data: groups, total: count ?? groups.length };
orderGroupsCache.set(cacheKey, result);
return result;
}, "Ошибка загрузки групп доставки");
};