import Area from '@components/common/Area.js'; import { useAppDispatch } from '@components/common/context/app.js'; import { WidgetSettingsScope } from '@components/common/page-builder/WidgetSettingsScope.js'; import { Button } from '@components/common/ui/Button.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { Check, Pin, PinOff, Share2, X } from 'lucide-react'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useQuery } from 'urql'; import { v4 as uuidv4 } from 'uuid'; // Per-widget default drawer widths. Spec § 7.5 — wider widgets get more // breathing room. Falls back to DEFAULT for unmapped types. const DRAWER_WIDTHS: Record = { product_grid: 540, form_builder: 580, collection_products: 540 }; const DRAWER_WIDTH_DEFAULT = 400; const DRAWER_WIDTH_MIN = 320; const DRAWER_WIDTH_MAX = 900; const WIDTH_STORAGE_PREFIX = 'pb_drawer_width_'; function readPersistedWidth(widgetType: string): number | null { if (typeof window === 'undefined') return null; try { const raw = window.localStorage.getItem(WIDTH_STORAGE_PREFIX + widgetType); if (!raw) return null; const n = Number(raw); if (!Number.isFinite(n)) return null; return Math.min(DRAWER_WIDTH_MAX, Math.max(DRAWER_WIDTH_MIN, n)); } catch { return null; } } function persistWidth(widgetType: string, width: number): void { if (typeof window === 'undefined') return; try { window.localStorage.setItem( WIDTH_STORAGE_PREFIX + widgetType, String(width) ); } catch { // localStorage unavailable / quota — non-fatal. } } const WIDGET_PLACEMENTS_QUERY = ` query WidgetPlacements($uuid: String!, $changeset: String) { widgetByUuid(uuid: $uuid, changeset: $changeset) { placements { uuid route area sortOrder } } } `; interface SelectedWidget { uid: string; type: string; settings: Record; } interface SharableRoute { id: string; name: string; path?: string; } interface PlacementRow { uuid: string; route: string; area: string; sortOrder: number; } interface SettingsDrawerProps { widget: SelectedWidget; currentRouteId: string; /** * The editor's active draft changeset token. Passed to `widgetByUuid` so the * Share panel reads DRAFT-applied placements (staged route toggles included), * keeping the checkboxes in sync with the iframe rather than the published * `widget_placement` table. */ changesetToken?: string; shareableRoutes: SharableRoute[]; pinned: boolean; onTogglePin: () => void; onAddPlacement: ( route: string, placementUuid: string, area: string, sortOrder?: number ) => Promise; onRemovePlacement: (placementUuid: string) => Promise; onClose: () => void; /** * Optional initial placements seed for INSTANT feedback before the * (overlay-aware) `widgetByUuid` query resolves. The Editor passes the * overlay-applied placements it already has. Also covers a freshly-dropped * widget, which isn't in the published table yet, so `widgetByUuid` returns * null for it until publish. */ initialPlacements?: PlacementRow[]; /** * The widget type's registered display name (e.g. "Collection products"), * looked up from `widgetTypes` by the Editor. Used (translated via `_()`) as * the drawer title; when absent we fall back to a title-cased form of the * type code. */ widgetTypeName?: string; /** * Optional ref forwarded to the drawer's outer `