File size: 10,410 Bytes
89682f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
"use client";
import { Dice } from "@/components/dice";
import { EmojiSelector } from "@/components/emoji-selector";
import { GithubForkRibbon } from "@/components/github";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Slider } from "@/components/ui/slider";
import { Toaster } from "@/components/ui/toaster";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { useToast } from "@/components/ui/use-toast";
import { presetImage, presetArtStyles } from "@/util/presets";
import { usePrevious } from "@/util/use-previous";
import { useResponse } from "@/util/use-response";
import { getShareUrl, Option, useShare } from "@/util/use-share";
import { clsx } from "clsx";
import { Check, Download, Share2 } from "lucide-react";
import { useEffect, useMemo, useState } from "react";
import {
FacebookIcon,
FacebookShareButton,
LinkedinIcon,
LinkedinShareButton,
TwitterShareButton,
XIcon,
} from "react-share";
import { setEmojiFavicon } from "@/util/set-emoji-favicon";
export default function TryEmoji() {
const { option: presetOption, hasShare } = useShare();
const { toast } = useToast();
const [emoji, setEmoji] = useState({
emoji: presetOption.emoji,
name: presetOption.name,
});
const [preset, setPreset] = useState(
presetArtStyles.find((p) => p.prompt === presetOption.prompt)!,
);
const [strength, setStrength] = useState(presetOption.strength);
const [seed, setSeed] = useState(presetOption.seed);
const shareOption: Option = useMemo(() => {
return {
emoji: emoji.emoji,
name: emoji.name,
prompt: preset.prompt,
seed: seed,
strength: strength,
};
}, [emoji.emoji, emoji.name, preset.prompt, seed, strength]);
const { image, loading } = useResponse(
hasShare,
emoji.emoji,
emoji.name,
preset.prompt,
strength,
seed,
);
const previousImage = usePrevious(image);
const mergedImage = useMemo(
() => image || previousImage || presetImage,
[image, previousImage],
);
useEffect(() => {
setEmojiFavicon(emoji.emoji);
}, [emoji.emoji]);
const shareKey = useMemo(() => {
return getShareUrl(shareOption);
}, [shareOption]);
const shareUrl = useMemo(() => {
return `https://tryemoji.com?share=${shareKey}`;
}, [shareKey]);
const warmOrg: Promise<void> = useMemo(() => {
if (image) {
return fetch("/api/share", {
method: "POST",
body: JSON.stringify({
image: image,
key: shareKey,
}),
}).then();
} else {
return new Promise((resolve) => resolve());
}
}, [image, shareKey]);
return (
<TooltipProvider delayDuration={50}>
<Toaster />
<div className="min-h-screen flex flex-col gap-4 bg-zinc-950 items-center justify-center py-4 md:py-12">
<GithubForkRibbon></GithubForkRibbon>
<div className="text-6xl text-zinc-100">
{emoji.emoji || "🐤"} tryEmoji{" "}
</div>
<div className="text-xl text-zinc-100">
Turn emoji into amazing artwork via AI
</div>
<div className="flex items-center justify-center flex-col md:flex-row gap-2 md:gap-4">
<div className="flex-0 w-full md:w-80">
<EmojiSelector
onSelect={(e) => {
const prefix =
e.keywords.indexOf("animal") > -1 ? "super cute" : "";
const keyword = e.keywords.join(", ");
const emoji = e.native;
const name = `${prefix} ${e.name}, ${keyword}`;
setEmoji({ emoji, name });
}}
></EmojiSelector>
</div>
<div className="flex-1">
<div className="max-w-[100vw] h-auto md:h-[512px] w-[512px] rounded-lg overflow-hidden relative">
<img src={mergedImage} className="h-full w-full object-contain" />
<div
className={clsx("transition absolute inset-0", {
"backdrop-blur-xl": loading,
})}
></div>
<div className="hidden absolute top-2 right-2 md:flex gap-2 items-center">
<FacebookShareButton
beforeOnClick={() => warmOrg}
url={shareUrl}
>
<FacebookIcon className="rounded" size={24}></FacebookIcon>
</FacebookShareButton>
<TwitterShareButton onClick={() => warmOrg} url={shareUrl}>
<XIcon className="rounded" size={24} />
</TwitterShareButton>
<LinkedinShareButton onClick={() => warmOrg} url={shareUrl}>
<LinkedinIcon className="rounded" size={24} />
</LinkedinShareButton>
<Tooltip>
<TooltipTrigger asChild>
<button
onClick={() => {
warmOrg.then(() => {
navigator.clipboard.writeText(shareUrl).then(() => {
toast({
description: (
<div className="flex gap-2 text-sm items-center">
<Check className="text-green-500"></Check>
Copied, paste to share
</div>
),
});
});
});
}}
className="flex-0 rounded bg-amber-600 w-6 flex items-center justify-center h-6 text-sm font-semibold text-white shadow-sm hover:bg-amber-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-amber-600"
>
<Share2 size={16}></Share2>
</button>
</TooltipTrigger>
<TooltipContent>
<p>Share</p>
</TooltipContent>
</Tooltip>
</div>
<div className="absolute bottom-2 left-2 right-2 flex gap-2 flex-wrap">
<div className="flex flex-auto gap-2 w-full md:w-auto">
<div className="text-xl text-zinc-100">AI</div>
<Tooltip>
<TooltipTrigger asChild>
<Slider
className="flex-1"
defaultValue={[strength]}
onValueChange={(v) => setStrength(v[0])}
max={0.7}
min={0.5}
step={0.025}
/>
</TooltipTrigger>
<TooltipContent>
<p>AI strength</p>
</TooltipContent>
</Tooltip>
</div>
<div className="flex flex-auto md:flex-grow-0 gap-2 w-full md:w-auto">
<Select
value={preset.artist}
onValueChange={(value) =>
setPreset(
presetArtStyles.find((p) => p.artist === value)!,
)
}
>
<Tooltip>
<TooltipTrigger asChild>
<SelectTrigger className="flex-1 w-56 border-0 rounded bg-amber-600 px-2 py-1 text-sm font-semibold text-white shadow-sm hover:bg-amber-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-amber-600">
<SelectValue placeholder="Select a fruit" />
</SelectTrigger>
</TooltipTrigger>
<TooltipContent>
<p>Art style</p>
</TooltipContent>
</Tooltip>
<SelectContent>
{presetArtStyles.map((p) => (
<SelectItem key={p.artist} value={p.artist}>
{p.artist}
</SelectItem>
))}
</SelectContent>
</Select>
<Tooltip>
<TooltipTrigger asChild>
<button
onClick={() => {
setSeed(Math.floor(Math.random() * 2159232));
}}
className="flex-0 rounded bg-amber-600 px-0.5 py-0.5 text-sm font-semibold text-white shadow-sm hover:bg-amber-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-amber-600"
>
<Dice></Dice>
</button>
</TooltipTrigger>
<TooltipContent>
<p>Random</p>
</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<a
href={image}
download
className="flex-0 block rounded bg-amber-600 px-0.5 py-0.5 text-sm font-semibold text-white shadow-sm hover:bg-amber-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-amber-600"
>
<Download />
</a>
</TooltipTrigger>
<TooltipContent>
<p>Download</p>
</TooltipContent>
</Tooltip>
</div>
</div>
</div>
</div>
</div>
<div className="text-xs text-zinc-500 font-sans mt-8 flex gap-2">
<a
className="hover:text-zinc-100"
href="https://lepton.ai"
target="_blank"
>
Powered by Lepton AI
</a>
|
<a
className="hover:text-zinc-100"
href="https://github.com/leptonai/tryemoji"
target="_blank"
>
Github
</a>
</div>
</div>
</TooltipProvider>
);
}
|