AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nav-stores.tsx 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { Link, usePage } from '@inertiajs/react';
  2. import {
  3. ChevronRight,
  4. Package,
  5. Palette,
  6. PlusCircle,
  7. ReceiptText,
  8. Settings,
  9. Tag,
  10. } from 'lucide-react';
  11. import { useEffect, useState } from 'react';
  12. import DiscountCodeController from '@/actions/App/Http/Controllers/DiscountCodeController';
  13. import OrderController from '@/actions/App/Http/Controllers/OrderController';
  14. import ProductController from '@/actions/App/Http/Controllers/ProductController';
  15. import StoreController from '@/actions/App/Http/Controllers/StoreController';
  16. import StoreSettingsController from '@/actions/App/Http/Controllers/StoreSettingsController';
  17. import {
  18. Collapsible,
  19. CollapsibleContent,
  20. CollapsibleTrigger,
  21. } from '@/components/ui/collapsible';
  22. import {
  23. SidebarGroup,
  24. SidebarGroupLabel,
  25. SidebarMenu,
  26. SidebarMenuButton,
  27. SidebarMenuItem,
  28. SidebarMenuSub,
  29. SidebarMenuSubButton,
  30. SidebarMenuSubItem,
  31. useSidebar,
  32. } from '@/components/ui/sidebar';
  33. import { useCurrentUrl } from '@/hooks/use-current-url';
  34. import { cn } from '@/lib/utils';
  35. import { create } from '@/routes/stores';
  36. import type { SidebarStore } from '@/types';
  37. /** Tile colours: our pandan, kunyit and bunga, then three more so neighbouring stores rarely match. */
  38. const tileColours = [
  39. 'bg-[#1D6A4C] text-white',
  40. 'bg-[#F2B33D] text-[#3D2A05]',
  41. 'bg-[#E3573F] text-white',
  42. 'bg-[#2F7F8F] text-white',
  43. 'bg-[#7A5AA6] text-white',
  44. 'bg-[#B4632A] text-white',
  45. ];
  46. /** The same store always gets the same colour, worked out from its web address. */
  47. function tileColour(slug: string): string {
  48. let hash = 0;
  49. for (const char of slug) {
  50. hash = (hash * 31 + char.charCodeAt(0)) >>> 0;
  51. }
  52. return tileColours[hash % tileColours.length];
  53. }
  54. /**
  55. * A store's mark in the sidebar: the first letter of its name on a coloured tile, with a small dot
  56. * in the corner (green live, yellow draft). It stays readable when the sidebar is folded to icons.
  57. */
  58. function StoreTile({ store }: { store: SidebarStore }) {
  59. const letter = (
  60. store.name.match(/[\p{L}\p{N}]/u)?.[0] ?? '?'
  61. ).toUpperCase();
  62. return (
  63. <span
  64. className={cn(
  65. 'relative grid size-5 shrink-0 place-items-center rounded-[5px] text-[11px] leading-none font-bold',
  66. 'group-data-[collapsible=icon]:-m-0.5',
  67. tileColour(store.slug),
  68. )}
  69. aria-hidden="true"
  70. >
  71. {letter}
  72. <span
  73. className={cn(
  74. 'absolute -right-0.5 -bottom-0.5 size-2 rounded-full ring-2 ring-sidebar',
  75. store.is_published ? 'bg-emerald-500' : 'bg-amber-400',
  76. )}
  77. />
  78. </span>
  79. );
  80. }
  81. /** The store the current page belongs to, from an address like /stores/sambal-nenek/orders. */
  82. function currentStoreSlug(path: string): string | null {
  83. const match = path.match(/^\/stores\/([^/]+)/);
  84. return match && match[1] !== 'create' ? match[1] : null;
  85. }
  86. /**
  87. * "Your stores" in the sidebar: Create store first, then one branch per store with its pages.
  88. * The branch of the store being viewed opens by itself.
  89. */
  90. export function NavStores() {
  91. const { sidebar } = usePage().props;
  92. const { currentUrl, isCurrentUrl } = useCurrentUrl();
  93. const current = currentStoreSlug(currentUrl);
  94. const stores = sidebar?.stores ?? [];
  95. return (
  96. <SidebarGroup className="px-2 py-0">
  97. <SidebarGroupLabel>Your stores</SidebarGroupLabel>
  98. <SidebarMenu>
  99. <SidebarMenuItem>
  100. <SidebarMenuButton
  101. asChild
  102. isActive={isCurrentUrl(create())}
  103. tooltip={{ children: 'Create store' }}
  104. className="font-medium text-emerald-700 hover:text-emerald-800 dark:text-emerald-400 dark:hover:text-emerald-300"
  105. >
  106. <Link href={create()} prefetch>
  107. <PlusCircle />
  108. <span>Create store</span>
  109. </Link>
  110. </SidebarMenuButton>
  111. </SidebarMenuItem>
  112. {stores.map((store) => (
  113. <StoreBranch
  114. key={store.slug}
  115. store={store}
  116. isCurrent={store.slug === current}
  117. />
  118. ))}
  119. </SidebarMenu>
  120. </SidebarGroup>
  121. );
  122. }
  123. function StoreBranch({
  124. store,
  125. isCurrent,
  126. }: {
  127. store: SidebarStore;
  128. isCurrent: boolean;
  129. }) {
  130. const [open, setOpen] = useState(isCurrent);
  131. const { state, isMobile } = useSidebar();
  132. const { isCurrentUrl, isCurrentOrParentUrl } = useCurrentUrl();
  133. // The sidebar stays on screen between pages, so open the branch when its store's page is opened.
  134. useEffect(() => {
  135. if (isCurrent) {
  136. setOpen(true);
  137. }
  138. }, [isCurrent]);
  139. const pages = [
  140. {
  141. label: 'Design',
  142. href: StoreController.show.url(store.slug),
  143. icon: Palette,
  144. exact: true,
  145. },
  146. {
  147. label: 'Products',
  148. href: ProductController.index.url(store.slug),
  149. icon: Package,
  150. },
  151. {
  152. label: 'Orders',
  153. href: OrderController.index.url(store.slug),
  154. icon: ReceiptText,
  155. badge: store.to_ship,
  156. },
  157. {
  158. label: 'Discounts',
  159. href: DiscountCodeController.index.url(store.slug),
  160. icon: Tag,
  161. },
  162. {
  163. label: 'Settings',
  164. href: StoreSettingsController.edit.url(store.slug),
  165. icon: Settings,
  166. exact: true,
  167. },
  168. ];
  169. // With the sidebar folded to icons there is no room for the pages, so the store icon opens its design.
  170. if (state === 'collapsed' && !isMobile) {
  171. return (
  172. <SidebarMenuItem>
  173. <SidebarMenuButton
  174. asChild
  175. isActive={isCurrent}
  176. tooltip={{ children: store.name }}
  177. >
  178. <Link href={StoreController.show.url(store.slug)}>
  179. <StoreTile store={store} />
  180. <span>{store.name}</span>
  181. </Link>
  182. </SidebarMenuButton>
  183. </SidebarMenuItem>
  184. );
  185. }
  186. return (
  187. <Collapsible
  188. asChild
  189. open={open}
  190. onOpenChange={setOpen}
  191. className="group/collapsible"
  192. >
  193. <SidebarMenuItem>
  194. <CollapsibleTrigger asChild>
  195. <SidebarMenuButton
  196. tooltip={{ children: store.name }}
  197. className={cn(isCurrent && 'font-medium')}
  198. >
  199. <StoreTile store={store} />
  200. <span className="truncate">{store.name}</span>
  201. <ChevronRight className="ml-auto text-sidebar-foreground/50 transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90" />
  202. </SidebarMenuButton>
  203. </CollapsibleTrigger>
  204. <CollapsibleContent>
  205. <SidebarMenuSub>
  206. {pages.map((page) => (
  207. <SidebarMenuSubItem key={page.label}>
  208. <SidebarMenuSubButton
  209. asChild
  210. isActive={
  211. page.exact
  212. ? isCurrentUrl(page.href)
  213. : isCurrentOrParentUrl(page.href)
  214. }
  215. >
  216. <Link href={page.href} prefetch>
  217. <page.icon />
  218. <span>{page.label}</span>
  219. {page.badge ? (
  220. <span
  221. className="ml-auto rounded-full bg-amber-400 px-1.5 text-xs leading-5 font-semibold text-amber-950 tabular-nums"
  222. aria-label={`${page.badge} to ship`}
  223. >
  224. {page.badge}
  225. </span>
  226. ) : null}
  227. </Link>
  228. </SidebarMenuSubButton>
  229. </SidebarMenuSubItem>
  230. ))}
  231. </SidebarMenuSub>
  232. </CollapsibleContent>
  233. </SidebarMenuItem>
  234. </Collapsible>
  235. );
  236. }