animated-emoji.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 className="no-drag 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. onValueChange={(value) => {
  35. setPrompt(value);
  36. }}
  37. />
  38. <Button
  39. className="w-full border-midnight dark:border-knight"
  40. radius="full"
  41. variant="bordered"
  42. onPress={async () => {
  43. setLoading(true);
  44. const response = await fetch("/api/emoji", {
  45. method: "POST",
  46. body: JSON.stringify({ prompt }),
  47. headers: {
  48. "Content-Type": "application/json",
  49. },
  50. });
  51. const data = await response.json();
  52. const emojiEntry =
  53. emojiMap[`${data.result}` as keyof typeof emojiMap];
  54. if (emojiEntry) {
  55. const searchedResult = fuzzySearch(emojiEntry.name);
  56. if (searchedResult) {
  57. setEmoji(searchedResult);
  58. }
  59. }
  60. setLoading(false);
  61. }}
  62. >
  63. Generate
  64. </Button>
  65. </div>
  66. </div>
  67. );
  68. };
  69. export default AnimatedEmoji;