animated-emoji.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use client";
  2. import { Button } from "@heroui/button";
  3. import Image from "next/image";
  4. import { Input } from "@heroui/input";
  5. import { useState } from "react";
  6. import { default as emojiMap } from "unicode-emoji-json";
  7. import { HashLoader } from "react-spinners";
  8. import { fuzzySearch } from "@/lib/fuzzySearch";
  9. import { Emoji } from "@/types/emoji";
  10. import { wavingHand } from "@/lib/emojis";
  11. const AnimatedEmoji = () => {
  12. const [prompt, setPrompt] = useState<string>("Hello");
  13. const [loading, setLoading] = useState<boolean>(false);
  14. const [emoji, setEmoji] = useState<Emoji>(wavingHand);
  15. return (
  16. <div className="w-full h-full p-4 flex flex-col justify-between items-center">
  17. {loading ? (
  18. <div className="flex h-full w-full justify-center items-center">
  19. <HashLoader color="#eef0f7" size={50} />
  20. </div>
  21. ) : (
  22. <Image alt="Animated Emoji" height={150} src={emoji.url} width={150} />
  23. )}
  24. <div
  25. className="w-full space-y-3 flex flex-col items-center justify-center"
  26. onMouseDown={(e) => e.stopPropagation()}
  27. onTouchStart={(e) => e.stopPropagation()}
  28. >
  29. <Input
  30. className="w-[95%]"
  31. classNames={{
  32. inputWrapper: "border-midnight dark:border-knight",
  33. label: "text-gray-400",
  34. }}
  35. label="Enter text, get emoji!"
  36. radius="lg"
  37. variant="underlined"
  38. onValueChange={(value) => {
  39. setPrompt(value);
  40. }}
  41. />
  42. <Button
  43. className="w-full border-midnight dark:border-knight"
  44. radius="full"
  45. variant="bordered"
  46. onPress={async () => {
  47. setLoading(true);
  48. const response = await fetch("/api/emoji", {
  49. method: "POST",
  50. body: JSON.stringify({ prompt }),
  51. headers: {
  52. "Content-Type": "application/json",
  53. },
  54. });
  55. const data = await response.json();
  56. const emojiEntry =
  57. emojiMap[`${data.result}` as keyof typeof emojiMap];
  58. if (emojiEntry) {
  59. const searchedResult = fuzzySearch(emojiEntry.name);
  60. if (searchedResult) {
  61. setEmoji(searchedResult);
  62. }
  63. }
  64. setLoading(false);
  65. }}
  66. >
  67. Generate
  68. </Button>
  69. </div>
  70. </div>
  71. );
  72. };
  73. export default AnimatedEmoji;