2023-12-01 11:01:56 -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 Link from "next/link";
|
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-08-12 23:08:57 -05:00
|
|
|
import { useUpdateLink } from "@/hooks/store/links";
|
2023-12-01 11:01:56 -06:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onClose: Function;
|
|
|
|
activeLink: LinkIncludingShortenedCollectionAndTags;
|
|
|
|
};
|
|
|
|
|
2023-12-01 16:42:45 -06:00
|
|
|
export default function EditLinkModal({ onClose, activeLink }: Props) {
|
2024-06-09 08:27:16 -05:00
|
|
|
const { t } = useTranslation();
|
2023-12-01 11:01:56 -06:00
|
|
|
const [link, setLink] =
|
|
|
|
useState<LinkIncludingShortenedCollectionAndTags>(activeLink);
|
|
|
|
|
2024-06-09 08:27:16 -05:00
|
|
|
let shortenedURL;
|
2023-12-01 11:01:56 -06:00
|
|
|
try {
|
2024-06-09 08:27:16 -05:00
|
|
|
shortenedURL = new URL(link.url || "").host.toLowerCase();
|
2023-12-01 11:01:56 -06:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
|
|
|
|
2024-08-12 23:08:57 -05:00
|
|
|
const updateLink = useUpdateLink();
|
|
|
|
|
2023-12-01 11:01:56 -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-12-01 11:01:56 -06:00
|
|
|
setLink({ ...link, tags: tagNames });
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setLink(activeLink);
|
2023-12-01 16:42:45 -06:00
|
|
|
}, []);
|
2023-12-01 11:01:56 -06:00
|
|
|
|
|
|
|
const submit = async () => {
|
|
|
|
if (!submitLoader) {
|
|
|
|
setSubmitLoader(true);
|
|
|
|
|
2024-08-12 23:08:57 -05:00
|
|
|
await updateLink.mutateAsync(link, {
|
|
|
|
onSuccess: () => {
|
|
|
|
onClose();
|
|
|
|
},
|
|
|
|
});
|
2023-12-01 11:01:56 -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("edit_link")}</p>
|
2023-12-03 22:52:32 -06:00
|
|
|
|
2023-12-05 14:17:36 -06:00
|
|
|
<div className="divider mb-3 mt-1"></div>
|
2023-12-03 22:52:32 -06:00
|
|
|
|
|
|
|
{link.url ? (
|
|
|
|
<Link
|
|
|
|
href={link.url}
|
|
|
|
className="truncate text-neutral flex gap-2 mb-5 w-fit max-w-full"
|
|
|
|
title={link.url}
|
|
|
|
target="_blank"
|
|
|
|
>
|
2023-12-29 11:21:22 -06:00
|
|
|
<i className="bi-link-45deg text-xl" />
|
2024-06-09 08:27:16 -05:00
|
|
|
<p>{shortenedURL}</p>
|
2023-12-03 22:52:32 -06:00
|
|
|
</Link>
|
|
|
|
) : undefined}
|
2023-12-01 16:42:45 -06:00
|
|
|
|
|
|
|
<div className="w-full">
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("name")}</p>
|
2023-12-01 16:42:45 -06:00
|
|
|
<TextInput
|
|
|
|
value={link.name}
|
|
|
|
onChange={(e) => setLink({ ...link, name: e.target.value })}
|
2024-06-09 08:27:16 -05:00
|
|
|
placeholder={t("placeholder_example_link")}
|
2023-12-01 16:42:45 -06:00
|
|
|
className="bg-base-200"
|
|
|
|
/>
|
|
|
|
</div>
|
2023-12-01 11:01:56 -06:00
|
|
|
|
2023-12-01 16:42:45 -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("collection")}</p>
|
2023-12-01 16:42:45 -06:00
|
|
|
{link.collection.name ? (
|
|
|
|
<CollectionSelection
|
|
|
|
onChange={setCollection}
|
|
|
|
defaultValue={
|
|
|
|
link.collection.id
|
2024-06-09 08:27:16 -05:00
|
|
|
? { value: link.collection.id, label: link.collection.name }
|
|
|
|
: { value: null as unknown as number, label: "Unorganized" }
|
2023-12-01 11:01:56 -06:00
|
|
|
}
|
2024-02-14 07:10:45 -06:00
|
|
|
creatable={false}
|
2023-12-01 11:01:56 -06:00
|
|
|
/>
|
2023-12-01 16:42:45 -06:00
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("tags")}</p>
|
2023-12-01 16:42:45 -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-01 16:42:45 -06:00
|
|
|
/>
|
2023-12-01 11:01:56 -06:00
|
|
|
</div>
|
|
|
|
|
2023-12-01 16:42:45 -06:00
|
|
|
<div className="sm:col-span-2">
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("description")}</p>
|
2023-12-01 16:42:45 -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")}
|
2023-12-01 16:42:45 -06:00
|
|
|
className="resize-none w-full rounded-md p-2 border-neutral-content bg-base-200 focus:border-sky-300 dark:focus:border-sky-600 border-solid border outline-none duration-100"
|
|
|
|
/>
|
|
|
|
</div>
|
2023-12-01 11:01:56 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-12-01 16:42:45 -06:00
|
|
|
|
|
|
|
<div className="flex justify-end items-center mt-5">
|
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("save_changes")}
|
2023-12-01 16:42:45 -06:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
2023-12-01 11:01:56 -06:00
|
|
|
);
|
|
|
|
}
|