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";
|
2024-08-12 23:08:57 -05:00
|
|
|
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: [],
|
2023-12-22 12:13:43 -06:00
|
|
|
preview: "",
|
|
|
|
image: "",
|
|
|
|
pdf: "",
|
|
|
|
readable: "",
|
2024-06-27 20:58:07 -05:00
|
|
|
monolith: "",
|
2023-11-29 08:41:24 -06:00
|
|
|
textContent: "",
|
2024-08-19 17:14:09 -05:00
|
|
|
icon: "",
|
|
|
|
iconWeight: "",
|
|
|
|
color: "",
|
2023-11-29 08:41:24 -06:00
|
|
|
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);
|
2024-08-12 23:08:57 -05:00
|
|
|
|
|
|
|
const addLink = useAddLink();
|
|
|
|
|
2023-11-29 08:41:24 -06:00
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
|
|
|
const [optionsExpanded, setOptionsExpanded] = useState(false);
|
|
|
|
const router = useRouter();
|
2024-08-12 23:08:57 -05:00
|
|
|
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-12 23:08:57 -05:00
|
|
|
|
2024-06-09 08:27:16 -05:00
|
|
|
const load = toast.loading(t("creating_link"));
|
2024-08-14 14:22:28 -05:00
|
|
|
|
2024-08-12 23:08:57 -05:00
|
|
|
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"));
|
|
|
|
}
|
2024-08-12 23:08:57 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
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>
|
2024-07-27 16:17:38 -05:00
|
|
|
{link.collection.name && (
|
2023-12-01 16:42:45 -06:00
|
|
|
<CollectionSelection
|
|
|
|
onChange={setCollection}
|
|
|
|
defaultValue={{
|
|
|
|
label: link.collection.name,
|
|
|
|
value: link.collection.id,
|
|
|
|
}}
|
2023-11-29 08:41:24 -06:00
|
|
|
/>
|
2024-07-27 16:17:38 -05:00
|
|
|
)}
|
2023-11-29 08:41:24 -06:00
|
|
|
</div>
|
2023-12-01 16:42:45 -06:00
|
|
|
</div>
|
2023-12-17 18:30:14 -06:00
|
|
|
<div className={"mt-2"}>
|
2024-07-22 22:34:36 -05:00
|
|
|
{optionsExpanded && (
|
2023-12-17 18:30:14 -06:00
|
|
|
<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>
|
2023-12-17 18:30:14 -06:00
|
|
|
<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")}
|
2023-12-17 18:30:14 -06:00
|
|
|
className="bg-base-200"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("tags")}</p>
|
2023-12-17 18:30:14 -06:00
|
|
|
<TagSelection
|
|
|
|
onChange={setTags}
|
2024-06-09 08:27:16 -05:00
|
|
|
defaultValue={link.tags.map((e) => ({
|
|
|
|
label: e.name,
|
|
|
|
value: e.id,
|
|
|
|
}))}
|
2023-12-17 18:30:14 -06:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="sm:col-span-2">
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("description")}</p>
|
2023-12-17 18:30:14 -06:00
|
|
|
<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-08-26 20:04:52 -05:00
|
|
|
className="resize-none w-full h-32 rounded-md p-2 border-neutral-content bg-base-200 focus:border-primary border-solid border outline-none duration-100"
|
2023-12-17 18:30:14 -06:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-07-22 22:34:36 -05:00
|
|
|
)}
|
2023-12-17 18:30:14 -06:00
|
|
|
</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
|
|
|
);
|
|
|
|
}
|