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

context.tsx 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { createContext, useContext } from 'react';
  2. import type { 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. };
  12. const StorefrontContext = createContext<Storefront>({
  13. baseUrl: '',
  14. isHome: true,
  15. language: 'en',
  16. products: [],
  17. });
  18. export const StorefrontProvider = StorefrontContext.Provider;
  19. export function useStorefront(): Storefront {
  20. return useContext(StorefrontContext);
  21. }
  22. /**
  23. * Turn a link from the store plan into a real address.
  24. *
  25. * - "#faq" scrolls on the home page, and goes to the home page first from anywhere else.
  26. * - "/shop" becomes an address inside this store.
  27. * - Anything else (https, mailto, tel) is left alone.
  28. */
  29. export function resolveHref(
  30. href: string,
  31. baseUrl: string,
  32. isHome: boolean,
  33. ): string {
  34. if (href.startsWith('#')) {
  35. if (isHome) {
  36. return href;
  37. }
  38. return href === '#' ? baseUrl : baseUrl + href;
  39. }
  40. if (href.startsWith('/')) {
  41. return href === '/' ? baseUrl : baseUrl + href;
  42. }
  43. return href;
  44. }
  45. export function useHref(href: string): string {
  46. const { baseUrl, isHome } = useStorefront();
  47. return resolveHref(href, baseUrl, isHome);
  48. }