| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- import { Link, usePage } from '@inertiajs/react';
- import {
- ChevronRight,
- Package,
- Palette,
- PlusCircle,
- ReceiptText,
- Settings,
- Tag,
- } from 'lucide-react';
- import { useEffect, useState } from 'react';
- import DiscountCodeController from '@/actions/App/Http/Controllers/DiscountCodeController';
- import OrderController from '@/actions/App/Http/Controllers/OrderController';
- import ProductController from '@/actions/App/Http/Controllers/ProductController';
- import StoreController from '@/actions/App/Http/Controllers/StoreController';
- import StoreSettingsController from '@/actions/App/Http/Controllers/StoreSettingsController';
- import {
- Collapsible,
- CollapsibleContent,
- CollapsibleTrigger,
- } from '@/components/ui/collapsible';
- import {
- SidebarGroup,
- SidebarGroupLabel,
- SidebarMenu,
- SidebarMenuButton,
- SidebarMenuItem,
- SidebarMenuSub,
- SidebarMenuSubButton,
- SidebarMenuSubItem,
- useSidebar,
- } from '@/components/ui/sidebar';
- import { useCurrentUrl } from '@/hooks/use-current-url';
- import { cn } from '@/lib/utils';
- import { create } from '@/routes/stores';
- import type { SidebarStore } from '@/types';
-
- /** Tile colours: our pandan, kunyit and bunga, then three more so neighbouring stores rarely match. */
- const tileColours = [
- 'bg-[#1D6A4C] text-white',
- 'bg-[#F2B33D] text-[#3D2A05]',
- 'bg-[#E3573F] text-white',
- 'bg-[#2F7F8F] text-white',
- 'bg-[#7A5AA6] text-white',
- 'bg-[#B4632A] text-white',
- ];
-
- /** The same store always gets the same colour, worked out from its web address. */
- function tileColour(slug: string): string {
- let hash = 0;
-
- for (const char of slug) {
- hash = (hash * 31 + char.charCodeAt(0)) >>> 0;
- }
-
- return tileColours[hash % tileColours.length];
- }
-
- /**
- * A store's mark in the sidebar: the first letter of its name on a coloured tile, with a small dot
- * in the corner (green live, yellow draft). It stays readable when the sidebar is folded to icons.
- */
- function StoreTile({ store }: { store: SidebarStore }) {
- const letter = (
- store.name.match(/[\p{L}\p{N}]/u)?.[0] ?? '?'
- ).toUpperCase();
-
- return (
- <span
- className={cn(
- 'relative grid size-5 shrink-0 place-items-center rounded-[5px] text-[11px] leading-none font-bold',
- 'group-data-[collapsible=icon]:-m-0.5',
- tileColour(store.slug),
- )}
- aria-hidden="true"
- >
- {letter}
- <span
- className={cn(
- 'absolute -right-0.5 -bottom-0.5 size-2 rounded-full ring-2 ring-sidebar',
- store.is_published ? 'bg-emerald-500' : 'bg-amber-400',
- )}
- />
- </span>
- );
- }
-
- /** The store the current page belongs to, from an address like /stores/sambal-nenek/orders. */
- function currentStoreSlug(path: string): string | null {
- const match = path.match(/^\/stores\/([^/]+)/);
-
- return match && match[1] !== 'create' ? match[1] : null;
- }
-
- /**
- * "Your stores" in the sidebar: Create store first, then one branch per store with its pages.
- * The branch of the store being viewed opens by itself.
- */
- export function NavStores() {
- const { sidebar } = usePage().props;
- const { currentUrl, isCurrentUrl } = useCurrentUrl();
- const current = currentStoreSlug(currentUrl);
- const stores = sidebar?.stores ?? [];
-
- return (
- <SidebarGroup className="px-2 py-0">
- <SidebarGroupLabel>Your stores</SidebarGroupLabel>
- <SidebarMenu>
- <SidebarMenuItem>
- <SidebarMenuButton
- asChild
- isActive={isCurrentUrl(create())}
- tooltip={{ children: 'Create store' }}
- className="font-medium text-emerald-700 hover:text-emerald-800 dark:text-emerald-400 dark:hover:text-emerald-300"
- >
- <Link href={create()} prefetch>
- <PlusCircle />
- <span>Create store</span>
- </Link>
- </SidebarMenuButton>
- </SidebarMenuItem>
-
- {stores.map((store) => (
- <StoreBranch
- key={store.slug}
- store={store}
- isCurrent={store.slug === current}
- />
- ))}
- </SidebarMenu>
- </SidebarGroup>
- );
- }
-
- function StoreBranch({
- store,
- isCurrent,
- }: {
- store: SidebarStore;
- isCurrent: boolean;
- }) {
- const [open, setOpen] = useState(isCurrent);
- const { state, isMobile } = useSidebar();
- const { isCurrentUrl, isCurrentOrParentUrl } = useCurrentUrl();
-
- // The sidebar stays on screen between pages, so open the branch when its store's page is opened.
- useEffect(() => {
- if (isCurrent) {
- setOpen(true);
- }
- }, [isCurrent]);
-
- const pages = [
- {
- label: 'Design',
- href: StoreController.show.url(store.slug),
- icon: Palette,
- exact: true,
- },
- {
- label: 'Products',
- href: ProductController.index.url(store.slug),
- icon: Package,
- },
- {
- label: 'Orders',
- href: OrderController.index.url(store.slug),
- icon: ReceiptText,
- badge: store.to_ship,
- },
- {
- label: 'Discounts',
- href: DiscountCodeController.index.url(store.slug),
- icon: Tag,
- },
- {
- label: 'Settings',
- href: StoreSettingsController.edit.url(store.slug),
- icon: Settings,
- exact: true,
- },
- ];
-
- // With the sidebar folded to icons there is no room for the pages, so the store icon opens its design.
- if (state === 'collapsed' && !isMobile) {
- return (
- <SidebarMenuItem>
- <SidebarMenuButton
- asChild
- isActive={isCurrent}
- tooltip={{ children: store.name }}
- >
- <Link href={StoreController.show.url(store.slug)}>
- <StoreTile store={store} />
- <span>{store.name}</span>
- </Link>
- </SidebarMenuButton>
- </SidebarMenuItem>
- );
- }
-
- return (
- <Collapsible
- asChild
- open={open}
- onOpenChange={setOpen}
- className="group/collapsible"
- >
- <SidebarMenuItem>
- <CollapsibleTrigger asChild>
- <SidebarMenuButton
- tooltip={{ children: store.name }}
- className={cn(isCurrent && 'font-medium')}
- >
- <StoreTile store={store} />
- <span className="truncate">{store.name}</span>
- <ChevronRight className="ml-auto text-sidebar-foreground/50 transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90" />
- </SidebarMenuButton>
- </CollapsibleTrigger>
- <CollapsibleContent>
- <SidebarMenuSub>
- {pages.map((page) => (
- <SidebarMenuSubItem key={page.label}>
- <SidebarMenuSubButton
- asChild
- isActive={
- page.exact
- ? isCurrentUrl(page.href)
- : isCurrentOrParentUrl(page.href)
- }
- >
- <Link href={page.href} prefetch>
- <page.icon />
- <span>{page.label}</span>
- {page.badge ? (
- <span
- className="ml-auto rounded-full bg-amber-400 px-1.5 text-xs leading-5 font-semibold text-amber-950 tabular-nums"
- aria-label={`${page.badge} to ship`}
- >
- {page.badge}
- </span>
- ) : null}
- </Link>
- </SidebarMenuSubButton>
- </SidebarMenuSubItem>
- ))}
- </SidebarMenuSub>
- </CollapsibleContent>
- </SidebarMenuItem>
- </Collapsible>
- );
- }
|