AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

context.tsx 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. applied: null,
  27. offer_hint: null,
  28. total: 0,
  29. adjusted: false,
  30. lines: [],
  31. },
  32. storeName: '',
  33. shop: { whatsapp: null, free_shipping_over: null, offers: [] },
  34. featuredProducts: {},
  35. });
  36. export const StorefrontProvider = StorefrontContext.Provider;
  37. export function useStorefront(): Storefront {
  38. return useContext(StorefrontContext);
  39. }
  40. /**
  41. * Turn a link from the store plan into a real address.
  42. *
  43. * - "#faq" scrolls on the home page, and goes to the home page first from anywhere else.
  44. * - "/shop" becomes an address inside this store.
  45. * - Anything else (https, mailto, tel) is left alone.
  46. */
  47. export function resolveHref(
  48. href: string,
  49. baseUrl: string,
  50. isHome: boolean,
  51. ): string {
  52. // A live store's home page is "/"; in the owner's preview it is the preview address.
  53. const home = baseUrl || '/';
  54. if (href.startsWith('#')) {
  55. if (isHome) {
  56. return href;
  57. }
  58. return href === '#' ? home : home + href;
  59. }
  60. if (href.startsWith('/')) {
  61. return href === '/' ? home : baseUrl + href;
  62. }
  63. return href;
  64. }
  65. export function useHref(href: string): string {
  66. const { baseUrl, isHome } = useStorefront();
  67. return resolveHref(href, baseUrl, isHome);
  68. }