emoji.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use client";
  2. import { useEffect, useRef, useState, type FormEvent } from "react";
  3. import { useT } from "@/components/locale-provider";
  4. import { cn } from "@/lib/utils";
  5. type Result = { url: string; name: string };
  6. /** Type a feeling, get an animated emoji (OpenAI picks it, server-side). */
  7. export function EmojiCard() {
  8. const { emoji: t } = useT();
  9. const [text, setText] = useState("");
  10. const [state, setState] = useState<"idle" | "loading" | "error">("idle");
  11. const [result, setResult] = useState<Result | null>(null);
  12. const [animate, setAnimate] = useState(false);
  13. const [loaded, setLoaded] = useState(false);
  14. const rootRef = useRef<HTMLFormElement>(null);
  15. // Swap the still frame for the animated wave once the card has been on screen a moment.
  16. useEffect(() => {
  17. const el = rootRef.current;
  18. if (!el || window.matchMedia("(prefers-reduced-motion: reduce)").matches)
  19. return;
  20. let timer = 0;
  21. const io = new IntersectionObserver(([e]) => {
  22. window.clearTimeout(timer);
  23. if (e.isIntersecting)
  24. timer = window.setTimeout(() => setAnimate(true), 1200);
  25. });
  26. io.observe(el);
  27. return () => {
  28. io.disconnect();
  29. window.clearTimeout(timer);
  30. };
  31. }, []);
  32. const submit = async (e: FormEvent) => {
  33. e.preventDefault();
  34. const prompt = text.trim() || "hello";
  35. setState("loading");
  36. setLoaded(false);
  37. try {
  38. const res = await fetch("/api/emoji", {
  39. method: "POST",
  40. headers: { "Content-Type": "application/json" },
  41. body: JSON.stringify({ prompt }),
  42. });
  43. const data = (await res.json()) as Partial<Result>;
  44. if (!res.ok || !data.url) throw new Error("no emoji");
  45. setResult({ url: data.url, name: data.name ?? "emoji" });
  46. setState("idle");
  47. } catch {
  48. setState("error");
  49. }
  50. };
  51. return (
  52. <form
  53. ref={rootRef}
  54. className="flex h-full flex-col gap-2 p-3 sm:gap-3 sm:p-6"
  55. onSubmit={submit}
  56. >
  57. <div className="relative grid min-h-0 flex-1 place-items-center">
  58. <span className="absolute inset-[12%] rounded-full bg-[radial-gradient(closest-side,color-mix(in_oklab,var(--ember)_22%,transparent),transparent)]" />
  59. {state === "loading" ? (
  60. <span className="size-16 animate-shimmer sm:size-[88px] rounded-full bg-[linear-gradient(90deg,var(--card-2),color-mix(in_oklab,var(--glacier)_20%,transparent),var(--card-2))] bg-[length:200%_100%]" />
  61. ) : result ? (
  62. // eslint-disable-next-line @next/next/no-img-element -- animated APNG, can't be optimized
  63. <img
  64. key={result.url}
  65. alt={result.name}
  66. className={cn(
  67. "relative size-16 transition-[opacity,scale] duration-500 ease-[cubic-bezier(.3,1.5,.5,1)] sm:size-[104px]",
  68. loaded ? "scale-100 opacity-100" : "scale-50 opacity-0",
  69. )}
  70. height={104}
  71. src={result.url}
  72. width={104}
  73. onLoad={() => setLoaded(true)}
  74. />
  75. ) : (
  76. <picture className="relative">
  77. {animate && <source srcSet="/emoji/wave.avif" type="image/avif" />}
  78. <img
  79. alt={t.wave}
  80. className="size-16 sm:size-[104px]"
  81. height={104}
  82. src={animate ? "/emoji/wave.webp" : "/emoji/wave-poster.webp"}
  83. width={104}
  84. />
  85. </picture>
  86. )}
  87. </div>
  88. <div className="flex min-w-0 flex-col gap-2">
  89. <p className="text-[13px] font-semibold text-muted max-sm:hidden">
  90. {t.title}
  91. </p>
  92. <div className="flex items-center gap-1 rounded-full border border-line bg-card-2/60 p-0.5 pl-2.5 focus-within:border-glacier sm:gap-1.5 sm:p-1 sm:pl-3">
  93. <input
  94. aria-label={t.aria}
  95. className="min-w-0 flex-1 bg-transparent text-[12.5px] outline-none placeholder:text-muted sm:text-[13.5px]"
  96. maxLength={80}
  97. placeholder={state === "error" ? t.retry : t.placeholder}
  98. value={text}
  99. onChange={(e) => setText(e.target.value)}
  100. />
  101. <button
  102. className="h-7 rounded-full bg-ink px-2.5 text-[12px] font-semibold text-bg transition-transform hover:scale-[1.04] active:scale-95 disabled:opacity-60 sm:h-8 sm:px-3"
  103. data-cursor={t.cursor}
  104. disabled={state === "loading"}
  105. type="submit"
  106. >
  107. {t.go}
  108. </button>
  109. </div>
  110. </div>
  111. </form>
  112. );
  113. }