AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

pulse-card.tsx 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { Link, router } from '@inertiajs/react';
  2. import { Package, RefreshCw, Sparkles, Truck, UploadCloud } from 'lucide-react';
  3. import { useEffect, useState } from 'react';
  4. import DashboardController from '@/actions/App/Http/Controllers/DashboardController';
  5. import { actionHref, prefersReducedMotion } from '@/components/overview/format';
  6. import { cn } from '@/lib/utils';
  7. import type { StorePulse } from '@/types';
  8. const icons = { ship: Truck, restock: Package, publish: UploadCloud };
  9. /** The note types itself out once, the first time it appears. */
  10. function useTyped(text: string): string {
  11. const [shown, setShown] = useState(() =>
  12. prefersReducedMotion() ? text : '',
  13. );
  14. useEffect(() => {
  15. if (prefersReducedMotion()) {
  16. setShown(text);
  17. return;
  18. }
  19. let count = 0;
  20. setShown('');
  21. const timer = window.setInterval(() => {
  22. count = Math.min(text.length, count + 2);
  23. setShown(text.slice(0, count));
  24. if (count >= text.length) {
  25. window.clearInterval(timer);
  26. }
  27. }, 16);
  28. return () => window.clearInterval(timer);
  29. }, [text]);
  30. return shown;
  31. }
  32. function Orb() {
  33. return (
  34. <div
  35. className="relative grid size-11 shrink-0 place-items-center rounded-full bg-[#F2B33D]/15 text-[#F2B33D]"
  36. aria-hidden="true"
  37. >
  38. <Sparkles className="size-5" />
  39. <span className="ov-spin absolute -inset-1 rounded-full border-[2.5px] border-[#F2B33D] border-r-transparent border-b-transparent" />
  40. </div>
  41. );
  42. }
  43. /**
  44. * The "Store pulse" card: a short AI note on how the stores are doing, with buttons for what to do first.
  45. * The note is saved for the day, so refreshing the page does not ask the AI again.
  46. */
  47. export default function PulseCard({
  48. pulse,
  49. store,
  50. }: {
  51. pulse: StorePulse;
  52. store: string | null;
  53. }) {
  54. const typed = useTyped(pulse.summary);
  55. const [writing, setWriting] = useState(false);
  56. const done = typed.length >= pulse.summary.length;
  57. const time = pulse.written_at
  58. ? new Date(pulse.written_at).toLocaleTimeString('en-MY', {
  59. hour: 'numeric',
  60. minute: '2-digit',
  61. })
  62. : null;
  63. const writeAgain = () => {
  64. router.post(
  65. DashboardController.rewritePulse.url(),
  66. { store },
  67. {
  68. preserveScroll: true,
  69. onStart: () => setWriting(true),
  70. onFinish: () => setWriting(false),
  71. },
  72. );
  73. };
  74. return (
  75. <section
  76. aria-label="Store pulse"
  77. className="ov-rise grid grid-cols-[auto_minmax(0,1fr)] gap-x-4 gap-y-2 rounded-[20px] bg-(--ov-pulse) p-5 text-[#E7F0EA] sm:p-6"
  78. style={{ animationDelay: '80ms' }}
  79. >
  80. <div className="row-span-3 max-sm:col-span-2 max-sm:row-span-1">
  81. <Orb />
  82. </div>
  83. <div className="flex flex-wrap items-start gap-x-4 gap-y-1 max-sm:col-span-2">
  84. <div className="min-w-0">
  85. <h2 className="ov-heading text-lg font-bold">
  86. Store pulse
  87. </h2>
  88. <p className="text-xs text-[#E7F0EA]/65">
  89. {pulse.source === 'ai'
  90. ? `Written by AI${time ? ` today at ${time}` : ''}`
  91. : 'Written from your numbers. The AI was not available.'}
  92. </p>
  93. </div>
  94. <div className="ml-auto text-right">
  95. {pulse.rewrites_left > 0 ? (
  96. <button
  97. type="button"
  98. onClick={writeAgain}
  99. disabled={writing}
  100. className="inline-flex items-center gap-1.5 rounded-full px-2 py-1 text-sm font-medium text-[#E7F0EA]/85 hover:bg-white/10 hover:text-white disabled:opacity-60"
  101. >
  102. <RefreshCw
  103. className={cn(
  104. 'size-4',
  105. writing && 'animate-spin',
  106. )}
  107. aria-hidden="true"
  108. />
  109. {writing ? 'Writing…' : 'Write again'}
  110. </button>
  111. ) : (
  112. <span className="inline-flex items-center gap-1.5 px-2 py-1 text-sm text-[#E7F0EA]/55">
  113. <RefreshCw className="size-4" aria-hidden="true" />
  114. Write again
  115. </span>
  116. )}
  117. <p
  118. className="px-2 text-xs text-[#E7F0EA]/60 tabular-nums"
  119. data-test="pulse-rewrites-left"
  120. >
  121. {pulse.rewrites_left > 0
  122. ? `${pulse.rewrites_left} of ${pulse.rewrites_limit} left today`
  123. : 'None left today. New note tomorrow.'}
  124. </p>
  125. </div>
  126. </div>
  127. <p
  128. className="min-h-[3.2em] max-w-[70ch] text-base leading-relaxed max-sm:col-span-2"
  129. aria-live="polite"
  130. >
  131. {typed}
  132. {!done && (
  133. <span
  134. className="ov-caret ml-0.5 inline-block h-[1.05em] w-0.5 translate-y-0.5 bg-[#F2B33D]"
  135. aria-hidden="true"
  136. />
  137. )}
  138. </p>
  139. {pulse.actions.length > 0 && (
  140. <div className="flex flex-wrap gap-2 max-sm:col-span-2">
  141. {pulse.actions.map((action) => {
  142. const Icon = icons[action.kind];
  143. return (
  144. <Link
  145. key={action.label}
  146. href={actionHref(
  147. action.kind,
  148. action.store_slug,
  149. action.product_id,
  150. )}
  151. className="inline-flex items-center gap-1.5 rounded-full bg-white/12 px-3.5 py-2 text-sm font-semibold transition hover:-translate-y-px hover:bg-[#F2B33D] hover:text-[#3D2A05]"
  152. >
  153. <Icon className="size-4" aria-hidden="true" />
  154. {action.label}
  155. </Link>
  156. );
  157. })}
  158. </div>
  159. )}
  160. </section>
  161. );
  162. }
  163. /** Shown for a moment while today's note loads (or is written for the first time). */
  164. export function PulseLoading() {
  165. return (
  166. <section
  167. aria-label="Store pulse"
  168. className="grid grid-cols-[auto_minmax(0,1fr)] gap-x-4 gap-y-3 rounded-[20px] bg-(--ov-pulse) p-5 text-[#E7F0EA] sm:p-6"
  169. >
  170. <Orb />
  171. <div className="grid gap-2.5">
  172. <p className="ov-heading text-lg font-bold">Store pulse</p>
  173. <p className="text-sm text-[#E7F0EA]/70">
  174. Writing today’s note…
  175. </p>
  176. <div className="h-3 w-4/5 animate-pulse rounded-full bg-white/10" />
  177. <div className="h-3 w-3/5 animate-pulse rounded-full bg-white/10" />
  178. </div>
  179. </section>
  180. );
  181. }