el.xwx.moe/components/ModalContent/NewLinkModal.tsx

194 lines
6.0 KiB
TypeScript
Raw Normal View History

2023-11-29 08:41:24 -06:00
import React, { useEffect, useState } from "react";
import CollectionSelection from "@/components/InputSelect/CollectionSelection";
import TagSelection from "@/components/InputSelect/TagSelection";
import TextInput from "@/components/TextInput";
import unescapeString from "@/lib/client/unescapeString";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import { useSession } from "next-auth/react";
import { useRouter } from "next/router";
2023-12-01 16:42:45 -06:00
import Modal from "../Modal";
2024-06-09 08:27:16 -05:00
import { useTranslation } from "next-i18next";
2024-07-30 13:57:09 -05:00
import { useCollections } from "@/hooks/store/collections";
import { useAddLink } from "@/hooks/store/links";
2024-08-14 14:22:28 -05:00
import toast from "react-hot-toast";
2023-11-29 08:41:24 -06:00
type Props = {
onClose: Function;
};
2023-12-01 16:42:45 -06:00
export default function NewLinkModal({ onClose }: Props) {
2024-06-09 08:27:16 -05:00
const { t } = useTranslation();
2023-11-29 08:41:24 -06:00
const { data } = useSession();
const initial = {
name: "",
url: "",
description: "",
2023-12-02 14:23:26 -06:00
type: "url",
2023-11-29 08:41:24 -06:00
tags: [],
preview: "",
image: "",
pdf: "",
readable: "",
2024-06-27 20:58:07 -05:00
monolith: "",
2023-11-29 08:41:24 -06:00
textContent: "",
collection: {
name: "",
ownerId: data?.user.id as number,
},
2023-12-02 14:23:26 -06:00
} as LinkIncludingShortenedCollectionAndTags;
2023-11-29 08:41:24 -06:00
const [link, setLink] =
useState<LinkIncludingShortenedCollectionAndTags>(initial);
const addLink = useAddLink();
2023-11-29 08:41:24 -06:00
const [submitLoader, setSubmitLoader] = useState(false);
const [optionsExpanded, setOptionsExpanded] = useState(false);
const router = useRouter();
const { data: collections = [] } = useCollections();
2023-11-29 08:41:24 -06:00
const setCollection = (e: any) => {
if (e?.__isNew__) e.value = null;
setLink({
...link,
collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId },
});
};
const setTags = (e: any) => {
2024-06-09 08:27:16 -05:00
const tagNames = e.map((e: any) => ({ name: e.label }));
2023-11-29 08:41:24 -06:00
setLink({ ...link, tags: tagNames });
};
useEffect(() => {
2024-08-18 01:55:59 -05:00
if (router.pathname.startsWith("/collections/") && router.query.id) {
2023-11-29 08:41:24 -06:00
const currentCollection = collections.find(
(e) => e.id == Number(router.query.id)
);
if (
currentCollection &&
currentCollection.ownerId &&
router.asPath.startsWith("/collections/")
)
setLink({
...initial,
collection: {
id: currentCollection.id,
name: currentCollection.name,
ownerId: currentCollection.ownerId,
},
});
} else
setLink({
...initial,
2024-06-09 08:27:16 -05:00
collection: { name: "Unorganized", ownerId: data?.user.id as number },
2023-11-29 08:41:24 -06:00
});
2023-12-01 16:42:45 -06:00
}, []);
2023-11-29 08:41:24 -06:00
const submit = async () => {
if (!submitLoader) {
setSubmitLoader(true);
2024-08-14 14:22:28 -05:00
const load = toast.loading(t("creating_link"));
await addLink.mutateAsync(link, {
2024-08-14 14:22:28 -05:00
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
onClose();
toast.success(t("link_created"));
}
},
});
2023-11-29 08:41:24 -06:00
setSubmitLoader(false);
}
};
return (
2023-12-01 16:42:45 -06:00
<Modal toggleModal={onClose}>
2024-06-09 08:27:16 -05:00
<p className="text-xl font-thin">{t("create_new_link")}</p>
2023-12-05 14:17:36 -06:00
<div className="divider mb-3 mt-1"></div>
2023-12-01 16:42:45 -06:00
<div className="grid grid-flow-row-dense sm:grid-cols-5 gap-3">
<div className="sm:col-span-3 col-span-5">
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("link")}</p>
2023-12-01 16:42:45 -06:00
<TextInput
2023-12-02 14:23:26 -06:00
value={link.url || ""}
2023-12-01 16:42:45 -06:00
onChange={(e) => setLink({ ...link, url: e.target.value })}
2024-06-09 08:27:16 -05:00
placeholder={t("link_url_placeholder")}
2023-12-01 16:42:45 -06:00
className="bg-base-200"
/>
</div>
<div className="sm:col-span-2 col-span-5">
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("collection")}</p>
2023-12-01 16:42:45 -06:00
{link.collection.name ? (
<CollectionSelection
onChange={setCollection}
defaultValue={{
label: link.collection.name,
value: link.collection.id,
}}
2023-11-29 08:41:24 -06:00
/>
2023-12-01 16:42:45 -06:00
) : null}
2023-11-29 08:41:24 -06:00
</div>
2023-12-01 16:42:45 -06:00
</div>
<div className={"mt-2"}>
{optionsExpanded ? (
<div className="mt-5">
<div className="grid sm:grid-cols-2 gap-3">
<div>
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("name")}</p>
<TextInput
value={link.name}
onChange={(e) => setLink({ ...link, name: e.target.value })}
2024-06-09 08:27:16 -05:00
placeholder={t("link_name_placeholder")}
className="bg-base-200"
/>
</div>
<div>
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("tags")}</p>
<TagSelection
onChange={setTags}
2024-06-09 08:27:16 -05:00
defaultValue={link.tags.map((e) => ({
label: e.name,
value: e.id,
}))}
/>
</div>
<div className="sm:col-span-2">
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("description")}</p>
<textarea
value={unescapeString(link.description) as string}
onChange={(e) =>
setLink({ ...link, description: e.target.value })
}
2024-06-09 08:27:16 -05:00
placeholder={t("link_description_placeholder")}
2024-02-06 06:44:08 -06:00
className="resize-none w-full rounded-md p-2 border-neutral-content bg-base-200 focus:border-primary border-solid border outline-none duration-100"
/>
</div>
</div>
</div>
) : undefined}
</div>
2023-12-17 22:32:33 -06:00
<div className="flex justify-between items-center mt-5">
<div
onClick={() => setOptionsExpanded(!optionsExpanded)}
className={`rounded-md cursor-pointer btn btn-sm btn-ghost duration-100 flex items-center px-2 w-fit text-sm`}
>
2024-06-09 08:27:16 -05:00
<p>{optionsExpanded ? t("hide_options") : t("more_options")}</p>
<i className={`bi-chevron-${optionsExpanded ? "up" : "down"}`}></i>
2023-12-17 22:32:33 -06:00
</div>
2023-12-07 11:29:45 -06:00
<button
className="btn btn-accent dark:border-violet-400 text-white"
onClick={submit}
>
2024-06-09 08:27:16 -05:00
{t("create_link")}
2023-12-01 16:42:45 -06:00
</button>
2023-11-29 08:41:24 -06:00
</div>
2023-12-01 16:42:45 -06:00
</Modal>
2023-11-29 08:41:24 -06:00
);
}