Files
co-lite/solvway-admin-portal/app/components/catalog-setup/SkuPicker.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

116 lines
4.8 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import { Button } from "@/app/components/ui/button";
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/app/components/ui/command";
import { Popover, PopoverAnchor, PopoverContent } from "@/app/components/ui/popover";
import { listSkus } from "@/app/lib/services/adminCatalogSetup";
import type { SkuListItem } from "@/app/lib/catalogSetup";
import { qyLinkCode } from "./SkuList";
import { useT } from "@/app/lib/i18n/MessagesProvider";
// Picks the SKU a Single SKU item ships (redesign-product-setup-stepper). Same cmdk-in-popover shape as
// SkuVariationPicker; the server searches SKU code or name and pages at 20. Each search shows "Searching…",
// and a failed one says so with Retry (refine-product-setup-sku-step). Creating a SKU is the page's own choice.
const SEARCH_DELAY_MS = 250;
export interface SkuPickerProps {
onSelect: (sku: SkuListItem) => void;
disabled?: boolean;
}
export function SkuPicker({ onSelect, disabled = false }: SkuPickerProps) {
const t = useT();
const [query, setQuery] = useState("");
const [open, setOpen] = useState(false);
const [options, setOptions] = useState<SkuListItem[] | null>(null);
const [failed, setFailed] = useState(false);
const [attempt, setAttempt] = useState(0);
useEffect(() => {
if (!open) return;
let cancelled = false;
const timer = setTimeout(() => {
setOptions(null);
setFailed(false);
listSkus({ q: query.trim(), pageSize: 20 })
.then((page) => {
if (!cancelled) setOptions(page.items);
})
.catch(() => {
if (!cancelled) setFailed(true);
});
}, SEARCH_DELAY_MS);
return () => {
cancelled = true;
clearTimeout(timer);
};
}, [open, query, attempt]);
function close() {
setOpen(false);
setQuery("");
}
const trimmed = query.trim();
return (
<Popover open={open} onOpenChange={setOpen}>
<Command shouldFilter={false} label={t("Sku")} className="h-auto overflow-visible bg-transparent">
<PopoverAnchor asChild>
<div className="flex h-9 w-full items-center rounded-md border border-input bg-transparent shadow-sm transition-[color,box-shadow] focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50 dark:bg-input/30 [&_[data-slot=command-input-wrapper]]:h-full [&_[data-slot=command-input-wrapper]]:flex-1 [&_[data-slot=command-input-wrapper]]:border-b-0 [&_[data-slot=command-input]]:h-full [&_[data-slot=command-input]]:py-0 [&_[data-slot=command-input]]:outline-none">
<CommandInput
aria-label={t("Sku")}
value={query}
onValueChange={(v) => {
setQuery(v);
setOpen(true);
}}
onFocus={() => setOpen(true)}
placeholder={t("SkuSearchPlaceholder")}
disabled={disabled}
/>
</div>
</PopoverAnchor>
<PopoverContent className="w-[var(--radix-popover-trigger-width)] p-0" align="start" onOpenAutoFocus={(e) => e.preventDefault()}>
<CommandList>
{failed ? (
<div role="alert" className="flex items-center justify-between gap-2 px-3 py-3 text-sm">
<span>{t("SkuPickerLoadFailed")}</span>
<Button type="button" variant="outline" size="sm" onClick={() => setAttempt((n) => n + 1)}>
{t("UiRetry")}
</Button>
</div>
) : options === null ? (
<div className="px-3 py-6 text-center text-sm text-muted-foreground">{t("CatSearching")}</div>
) : options.length === 0 ? (
<CommandEmpty>{trimmed ? t("SkuNoSkusMatch", { query: trimmed }) : t("SkuNoSkusYet")}</CommandEmpty>
) : (
<CommandGroup>
{options.map((sku) => (
<CommandItem
key={sku.id}
value={String(sku.id)}
onSelect={() => {
onSelect(sku);
close();
}}
>
<div className="flex min-w-0 flex-1 flex-col">
<span className="truncate text-sm font-medium">{sku.code}</span>
<span className="truncate text-xs text-muted-foreground">
{t("SkuPickerOptionMeta", { name: sku.name, count: t(sku.variantCount === 1 ? "SkuVariantCount1" : "SkuVariantCountN", { count: sku.variantCount }), link: t(qyLinkCode(sku)) })}
</span>
</div>
</CommandItem>
))}
</CommandGroup>
)}
</CommandList>
</PopoverContent>
</Command>
</Popover>
);
}