AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

context.tsx 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { createContext, useContext } from 'react';
  2. import type { HeroImageSet, 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. heroImages: HeroImageSet;
  17. };
  18. const StorefrontContext = createContext<Storefront>({
  19. baseUrl: '',
  20. isHome: true,
  21. language: 'en',
  22. products: [],
  23. cart: {
  24. count: 0,
  25. subtotal: 0,
  26. discount: null,
  27. applied: null,
  28. offer_hint: null,
  29. total: 0,
  30. adjusted: false,
  31. lines: [],
  32. },
  33. storeName: '',
  34. shop: {
  35. whatsapp: null,
  36. free_shipping_over: null,
  37. announcements: [],
  38. shipping_from: 0,
  39. offers: [],
  40. },
  41. featuredProducts: {},
  42. heroImages: { refs: {}, fallback: [] },
  43. });
  44. export const StorefrontProvider = StorefrontContext.Provider;
  45. export function useStorefront(): Storefront {
  46. return useContext(StorefrontContext);
  47. }
  48. /**
  49. * Turn a link from the store plan into a real address.
  50. *
  51. * - "#faq" scrolls on the home page, and goes to the home page first from anywhere else.
  52. * - "/shop" becomes an address inside this store.
  53. * - Anything else (https, mailto, tel) is left alone.
  54. */
  55. export function resolveHref(
  56. href: string,
  57. baseUrl: string,
  58. isHome: boolean,
  59. ): string {
  60. // A live store's home page is "/"; in the owner's preview it is the preview address.
  61. const home = baseUrl || '/';
  62. if (href.startsWith('#')) {
  63. if (isHome) {
  64. return href;
  65. }
  66. return href === '#' ? home : home + href;
  67. }
  68. if (href.startsWith('/')) {
  69. return href === '/' ? home : baseUrl + href;
  70. }
  71. return href;
  72. }
  73. export function useHref(href: string): string {
  74. const { baseUrl, isHome } = useStorefront();
  75. return resolveHref(href, baseUrl, isHome);
  76. }