AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

context.tsx 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { createContext, useContext } from 'react';
  2. import type { ShopInfo, StoreCart, StoreProduct } from './types';
  3. export type Storefront = {
  4. /** Address of the store's home page. Every other storefront address is built on it. */
  5. baseUrl: string;
  6. /** True when the home page is showing, where "#section" links can simply scroll. */
  7. isHome: boolean;
  8. language: string;
  9. /** The merchant's real products that shoppers may see. */
  10. products: StoreProduct[];
  11. cart: StoreCart;
  12. storeName: string;
  13. shop: ShopInfo;
  14. /** Real products that featured product sections point to, by web-address name. */
  15. featuredProducts: Record<string, StoreProduct>;
  16. };
  17. const StorefrontContext = createContext<Storefront>({
  18. baseUrl: '',
  19. isHome: true,
  20. language: 'en',
  21. products: [],
  22. cart: {
  23. count: 0,
  24. subtotal: 0,
  25. discount: null,
  26. total: 0,
  27. adjusted: false,
  28. lines: [],
  29. },
  30. storeName: '',
  31. shop: { whatsapp: null, free_shipping_over: null },
  32. featuredProducts: {},
  33. });
  34. export const StorefrontProvider = StorefrontContext.Provider;
  35. export function useStorefront(): Storefront {
  36. return useContext(StorefrontContext);
  37. }
  38. /**
  39. * Turn a link from the store plan into a real address.
  40. *
  41. * - "#faq" scrolls on the home page, and goes to the home page first from anywhere else.
  42. * - "/shop" becomes an address inside this store.
  43. * - Anything else (https, mailto, tel) is left alone.
  44. */
  45. export function resolveHref(
  46. href: string,
  47. baseUrl: string,
  48. isHome: boolean,
  49. ): string {
  50. // A live store's home page is "/"; in the owner's preview it is the preview address.
  51. const home = baseUrl || '/';
  52. if (href.startsWith('#')) {
  53. if (isHome) {
  54. return href;
  55. }
  56. return href === '#' ? home : home + href;
  57. }
  58. if (href.startsWith('/')) {
  59. return href === '/' ? home : baseUrl + href;
  60. }
  61. return href;
  62. }
  63. export function useHref(href: string): string {
  64. const { baseUrl, isHome } = useStorefront();
  65. return resolveHref(href, baseUrl, isHome);
  66. }