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

context.tsx 2.0KB

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