Spaces:
Sleeping
Sleeping
File size: 9,633 Bytes
05c5ed5 | 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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | "use client";
import EmojiPicker, { Theme } from "emoji-picker-react";
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "ui/dialog";
import { Input } from "ui/input";
import { Textarea } from "ui/textarea";
import { Label } from "ui/label";
import { Button } from "ui/button";
import { useTheme } from "next-themes";
import { useObjectState } from "@/hooks/use-object-state";
import { useState } from "react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuTrigger,
} from "ui/dropdown-menu";
import { Avatar, AvatarFallback, AvatarImage } from "ui/avatar";
import { Loader } from "lucide-react";
import { safe } from "ts-safe";
import { z } from "zod";
import { DBWorkflow, WorkflowIcon } from "app-types/workflow";
import { handleErrorWithToast } from "ui/shared-toast";
import { toast } from "sonner";
import { useRouter } from "next/navigation";
import { cn, createDebounce } from "lib/utils";
import { mutate } from "swr";
import { useTranslations } from "next-intl";
import { BACKGROUND_COLORS } from "lib/const";
const colorUpdateDebounce = createDebounce();
const defaultConfig = {
id: undefined as string | undefined,
icon: {
type: "emoji",
value:
"https://cdn.jsdelivr.net/npm/emoji-datasource-apple/img/apple/64/1f916.png",
style: {
backgroundColor: BACKGROUND_COLORS[0],
},
} as WorkflowIcon,
name: "",
description: "",
};
const zodSchema = z.object({
id: z.string().optional(),
name: z
.string()
.min(1)
.regex(/^[a-zA-Z -]+$/),
description: z.string().max(200).optional(),
icon: z.object({
type: z.enum(["emoji"]),
value: z.string().min(1),
style: z
.object({
backgroundColor: z.string().min(1),
})
.optional(),
}),
});
export function EditWorkflowPopup({
children,
defaultValue,
submitAfterRoute = true,
onSave,
open,
onOpenChange,
}: {
children?: React.ReactNode;
defaultValue?: Pick<DBWorkflow, "id" | "name" | "description" | "icon">;
submitAfterRoute?: boolean;
open?: boolean;
onSave?: (workflow: DBWorkflow) => void;
onOpenChange?: (open: boolean) => void;
}) {
const t = useTranslations();
const { theme } = useTheme();
const getInitialConfig = () => {
return defaultValue
? {
description: defaultValue.description || "",
icon: defaultValue.icon || defaultConfig.icon,
name: defaultValue.name || "",
id: defaultValue.id || "",
}
: { ...defaultConfig };
};
const [config, setConfig] = useObjectState<typeof defaultConfig>(
getInitialConfig(),
);
const router = useRouter();
const [loading, setLoading] = useState(false);
const handleSubmit = async () => {
setLoading(true);
toast.promise(
safe(() => zodSchema.parse(config))
.map(async (body) => {
const response = await fetch("/api/workflow", {
method: "POST",
body: JSON.stringify(body),
});
const data = await response.json();
return data as DBWorkflow;
})
.ifOk((workflow) => {
onOpenChange?.(false);
mutate("/api/workflow");
if (submitAfterRoute) {
router.push(`/workflow/${workflow.id}`);
}
onSave?.(workflow);
})
.ifFail(handleErrorWithToast)
.watch(() => setLoading(false))
.unwrap(),
{
success: t("Common.success"),
loading: t("Common.saving"),
},
);
};
return (
<Dialog
open={open}
onOpenChange={(open) => {
!open && setConfig(getInitialConfig());
onOpenChange?.(open);
}}
>
<DialogTrigger asChild>{children}</DialogTrigger>
<DialogContent className="p-2 md:p-10 pb-0">
<DialogHeader className={cn("mb-4", config.id && "sr-only")}>
<DialogTitle>{t("Workflow.createWorkflow")}</DialogTitle>
<DialogDescription asChild>
<div className="mt-2">
<p>{t("Workflow.createWorkflowDescription")}</p>
<p className="mt-1">{t("Workflow.workflowDescription")}</p>
</div>
</DialogDescription>
</DialogHeader>
<div className="flex w-full h-full gap-10">
{/* Left: Form */}
<div className="gap-6 flex flex-col justify-center w-full">
<div className="flex gap-2">
<div className="flex flex-col gap-2 flex-1">
<Label htmlFor="workflow-name">
{t("Workflow.nameAndIcon")}
</Label>
<Input
value={config.name}
onChange={(e) => setConfig({ name: e.target.value })}
autoFocus
className="bg-input border-transparent"
id="workflow-name"
placeholder={t("Workflow.workflowNamePlaceholder")}
/>
</div>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<div
style={{
backgroundColor: config.icon.style?.backgroundColor,
}}
className="transition-colors hover:bg-secondary! group items-center justify-center flex w-14 h-14 rounded-lg cursor-pointer ring ring-background hover:ring-ring"
>
<Avatar className="size-10">
<AvatarImage
src={config.icon.value}
className="group-hover:scale-110 transition-transform"
/>
<AvatarFallback></AvatarFallback>
</Avatar>
</div>
</DropdownMenuTrigger>
<DropdownMenuContent className="p-0 bg-transparent flex flex-col gap-2 border-none">
<div className="flex gap-2 border rounded-xl p-4 bg-secondary">
{BACKGROUND_COLORS.map((color, index) => (
<div
key={index}
className="w-6 h-6 rounded cursor-pointer"
onClick={() => {
setConfig({
icon: {
...config.icon,
style: { backgroundColor: color },
},
});
}}
style={{ backgroundColor: color }}
></div>
))}
<div className="relative">
<input
type="color"
className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
onChange={(e) => {
colorUpdateDebounce(() => {
setConfig({
icon: {
...config.icon!,
style: { backgroundColor: e.target.value },
},
});
}, 100);
}}
/>
<div className="w-6 h-6 rounded cursor-pointer border-muted-foreground/50 flex items-center justify-center hover:border-muted-foreground transition-colors">
<div
className="w-3 h-3 rounded-full"
style={{
backgroundColor:
config.icon?.style?.backgroundColor,
}}
></div>
</div>
</div>
</div>
<EmojiPicker
lazyLoadEmojis
open
className="fade-300"
theme={theme == "dark" ? Theme.DARK : Theme.LIGHT}
onEmojiClick={(emoji) => {
setConfig({
icon: {
...config.icon,
value: emoji.imageUrl,
},
});
}}
/>
</DropdownMenuContent>
</DropdownMenu>
</div>
<div className="flex flex-col gap-2">
<Label
className="flex items-center gap-1"
htmlFor="workflow-description"
>
{t("Workflow.description")}
<span className="text-xs text-muted-foreground">
{t("Common.optional")}
</span>
</Label>
<Textarea
id="workflow-description"
placeholder={t("Workflow.descriptionPlaceholder")}
className="resize-none min-h-[100px] bg-input border-transparent"
value={config.description}
onChange={(e) => setConfig({ description: e.target.value })}
/>
</div>
</div>
</div>
<DialogFooter>
<DialogClose asChild>
<Button variant="ghost">{t("Common.cancel")}</Button>
</DialogClose>
<Button onClick={handleSubmit} disabled={loading}>
{t("Common.save")}
{loading && <Loader className="size-3.5 animate-spin" />}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
|