animated-emoji.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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={80} />
  20. </div>
  21. ) : (
  22. <Image alt="Animated Emoji" height={150} src={emoji.url} width={150} />
  23. )}
  24. <div className="w-full space-y-3 flex flex-col items-center justify-center">
  25. <Input
  26. className="w-[95%]"
  27. classNames={{
  28. inputWrapper: "border-midnight dark:border-knight",
  29. label: "text-gray-400",
  30. }}
  31. label="Enter text, get emoji!"
  32. radius="lg"
  33. variant="underlined"
  34. onMouseDown={(e) => {
  35. e.stopPropagation();
  36. }}
  37. onValueChange={(value) => {
  38. setPrompt(value);
  39. }}
  40. />
  41. <Button
  42. className="w-full border-midnight dark:border-knight"
  43. radius="full"
  44. variant="bordered"
  45. onMouseDown={(e) => {
  46. e.stopPropagation();
  47. }}
  48. onPress={async () => {
  49. setLoading(true);
  50. const response = await fetch("/api/emoji", {
  51. method: "POST",
  52. body: JSON.stringify({ prompt }),
  53. headers: {
  54. "Content-Type": "application/json",
  55. },
  56. });
  57. const data = await response.json();
  58. const emojiEntry =
  59. emojiMap[`${data.result}` as keyof typeof emojiMap];
  60. if (emojiEntry) {
  61. const searchedResult = fuzzySearch(emojiEntry.name);
  62. if (searchedResult) {
  63. setEmoji(searchedResult);
  64. }
  65. }
  66. setLoading(false);
  67. }}
  68. >
  69. Generate
  70. </Button>
  71. </div>
  72. </div>
  73. );
  74. };
  75. export default AnimatedEmoji;