AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

context.tsx 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. collections: [],
  38. announcements: [],
  39. shipping_from: 0,
  40. shipping_west: 0,
  41. shipping_east: 0,
  42. offers: [],
  43. },
  44. featuredProducts: {},
  45. heroImages: { refs: {}, fallback: [] },
  46. });
  47. export const StorefrontProvider = StorefrontContext.Provider;
  48. export function useStorefront(): Storefront {
  49. return useContext(StorefrontContext);
  50. }
  51. /**
  52. * Turn a link from the store plan into a real address.
  53. *
  54. * - "#faq" scrolls on the home page, and goes to the home page first from anywhere else.
  55. * - "/shop" becomes an address inside this store.
  56. * - Anything else (https, mailto, tel) is left alone.
  57. */
  58. export function resolveHref(
  59. href: string,
  60. baseUrl: string,
  61. isHome: boolean,
  62. ): string {
  63. // A live store's home page is "/"; in the owner's preview it is the preview address.
  64. const home = baseUrl || '/';
  65. if (href.startsWith('#')) {
  66. if (isHome) {
  67. return href;
  68. }
  69. return href === '#' ? home : home + href;
  70. }
  71. if (href.startsWith('/')) {
  72. return href === '/' ? home : baseUrl + href;
  73. }
  74. return href;
  75. }
  76. export function useHref(href: string): string {
  77. const { baseUrl, isHome } = useStorefront();
  78. return resolveHref(href, baseUrl, isHome);
  79. }