emoji.tsx 4.4 KB

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