2023-12-01 11:01:56 -06:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import TextInput from "@/components/TextInput";
|
|
|
|
import useCollectionStore from "@/store/collections";
|
2023-12-17 15:59:52 -06:00
|
|
|
import toast from "react-hot-toast";
|
2023-12-01 11:01:56 -06:00
|
|
|
import { CollectionIncludingMembersAndLinkCount, Member } from "@/types/global";
|
|
|
|
import getPublicUserData from "@/lib/client/getPublicUserData";
|
|
|
|
import useAccountStore from "@/store/account";
|
|
|
|
import usePermissions from "@/hooks/usePermissions";
|
|
|
|
import ProfilePhoto from "../ProfilePhoto";
|
|
|
|
import addMemberToCollection from "@/lib/client/addMemberToCollection";
|
2023-12-01 16:42:45 -06:00
|
|
|
import Modal from "../Modal";
|
2023-12-01 11:01:56 -06:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onClose: Function;
|
|
|
|
activeCollection: CollectionIncludingMembersAndLinkCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function EditCollectionSharingModal({
|
|
|
|
onClose,
|
|
|
|
activeCollection,
|
|
|
|
}: Props) {
|
|
|
|
const [collection, setCollection] =
|
|
|
|
useState<CollectionIncludingMembersAndLinkCount>(activeCollection);
|
|
|
|
|
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
|
|
|
const { updateCollection } = useCollectionStore();
|
|
|
|
|
|
|
|
const submit = async () => {
|
|
|
|
if (!submitLoader) {
|
|
|
|
setSubmitLoader(true);
|
|
|
|
if (!collection) return null;
|
|
|
|
|
|
|
|
setSubmitLoader(true);
|
|
|
|
|
|
|
|
const load = toast.loading("Updating...");
|
|
|
|
|
|
|
|
let response;
|
|
|
|
|
|
|
|
response = await updateCollection(collection as any);
|
|
|
|
|
|
|
|
toast.dismiss(load);
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
toast.success(`Updated!`);
|
2023-12-01 16:42:45 -06:00
|
|
|
onClose();
|
2023-12-01 11:01:56 -06:00
|
|
|
} else toast.error(response.data as string);
|
|
|
|
|
|
|
|
setSubmitLoader(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const { account } = useAccountStore();
|
|
|
|
const permissions = usePermissions(collection.id as number);
|
|
|
|
|
|
|
|
const currentURL = new URL(document.URL);
|
|
|
|
|
|
|
|
const publicCollectionURL = `${currentURL.origin}/public/collections/${collection.id}`;
|
|
|
|
|
|
|
|
const [memberUsername, setMemberUsername] = useState("");
|
|
|
|
|
|
|
|
const [collectionOwner, setCollectionOwner] = useState({
|
2023-12-19 10:50:43 -06:00
|
|
|
id: null as unknown as number,
|
2023-12-01 11:01:56 -06:00
|
|
|
name: "",
|
|
|
|
username: "",
|
|
|
|
image: "",
|
2023-12-19 10:50:43 -06:00
|
|
|
archiveAsScreenshot: undefined as unknown as boolean,
|
|
|
|
archiveAsPDF: undefined as unknown as boolean,
|
2023-12-01 11:01:56 -06:00
|
|
|
});
|
|
|
|
|
2023-12-19 10:50:43 -06:00
|
|
|
useEffect(() => {
|
|
|
|
const fetchOwner = async () => {
|
|
|
|
const owner = await getPublicUserData(collection.ownerId as number);
|
|
|
|
setCollectionOwner(owner);
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchOwner();
|
|
|
|
|
|
|
|
setCollection(activeCollection);
|
|
|
|
}, []);
|
|
|
|
|
2023-12-01 11:01:56 -06:00
|
|
|
const setMemberState = (newMember: Member) => {
|
|
|
|
if (!collection) return null;
|
|
|
|
|
|
|
|
setCollection({
|
|
|
|
...collection,
|
|
|
|
members: [...collection.members, newMember],
|
|
|
|
});
|
|
|
|
|
|
|
|
setMemberUsername("");
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-12-01 16:42:45 -06:00
|
|
|
<Modal toggleModal={onClose}>
|
2023-12-03 22:52:32 -06:00
|
|
|
<p className="text-xl font-thin">
|
2023-12-01 16:42:45 -06:00
|
|
|
{permissions === true ? "Share and Collaborate" : "Team"}
|
|
|
|
</p>
|
|
|
|
|
2023-12-05 14:17:36 -06:00
|
|
|
<div className="divider mb-3 mt-1"></div>
|
2023-12-03 22:52:32 -06:00
|
|
|
|
2023-12-01 16:42:45 -06:00
|
|
|
<div className="flex flex-col gap-3">
|
|
|
|
{permissions === true && (
|
|
|
|
<div>
|
|
|
|
<p>Make Public</p>
|
|
|
|
|
|
|
|
<label className="label cursor-pointer justify-start gap-2">
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
checked={collection.isPublic}
|
|
|
|
onChange={() =>
|
|
|
|
setCollection({
|
|
|
|
...collection,
|
|
|
|
isPublic: !collection.isPublic,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
className="checkbox checkbox-primary"
|
|
|
|
/>
|
|
|
|
<span className="label-text">Make this a public collection</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<p className="text-neutral text-sm">
|
2023-12-08 10:01:47 -06:00
|
|
|
This will let <b>Anyone</b> to view this collection and it's
|
2023-12-01 16:42:45 -06:00
|
|
|
users.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{collection.isPublic ? (
|
|
|
|
<div className={permissions === true ? "pl-5" : ""}>
|
|
|
|
<p className="mb-2">Sharable Link (Click to copy)</p>
|
|
|
|
<div
|
|
|
|
onClick={() => {
|
|
|
|
try {
|
|
|
|
navigator.clipboard
|
|
|
|
.writeText(publicCollectionURL)
|
|
|
|
.then(() => toast.success("Copied!"));
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
className="w-full hide-scrollbar overflow-x-auto whitespace-nowrap rounded-md p-2 bg-base-200 border-neutral-content border-solid border outline-none hover:border-primary dark:hover:border-primary duration-100 cursor-text"
|
|
|
|
>
|
|
|
|
{publicCollectionURL}
|
2023-12-01 11:01:56 -06:00
|
|
|
</div>
|
2023-12-01 16:42:45 -06:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
{permissions === true && <div className="divider my-3"></div>}
|
|
|
|
|
|
|
|
{permissions === true && (
|
|
|
|
<>
|
2023-12-18 00:55:39 -06:00
|
|
|
<p>Members</p>
|
2023-12-01 16:42:45 -06:00
|
|
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
<TextInput
|
|
|
|
value={memberUsername || ""}
|
|
|
|
className="bg-base-200"
|
|
|
|
placeholder="Username (without the '@')"
|
|
|
|
onChange={(e) => setMemberUsername(e.target.value)}
|
|
|
|
onKeyDown={(e) =>
|
|
|
|
e.key === "Enter" &&
|
|
|
|
addMemberToCollection(
|
|
|
|
account.username as string,
|
|
|
|
memberUsername || "",
|
|
|
|
collection,
|
|
|
|
setMemberState
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
2023-12-01 11:01:56 -06:00
|
|
|
|
|
|
|
<div
|
2023-12-01 16:42:45 -06:00
|
|
|
onClick={() =>
|
|
|
|
addMemberToCollection(
|
|
|
|
account.username as string,
|
|
|
|
memberUsername || "",
|
|
|
|
collection,
|
|
|
|
setMemberState
|
|
|
|
)
|
|
|
|
}
|
2023-12-19 10:50:43 -06:00
|
|
|
className="btn btn-accent dark:border-violet-400 text-white btn-square btn-sm h-10 w-10"
|
2023-12-01 11:01:56 -06:00
|
|
|
>
|
2023-12-18 00:55:39 -06:00
|
|
|
<i className="bi-person-add text-xl"></i>
|
2023-12-01 11:01:56 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-12-01 16:42:45 -06:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{collection?.members[0]?.user && (
|
|
|
|
<>
|
2023-12-19 17:28:48 -06:00
|
|
|
<div className="flex flex-col divide-y divide-neutral-content border border-neutral-content rounded-xl bg-base-200">
|
2023-12-01 16:42:45 -06:00
|
|
|
<div
|
2023-12-19 17:28:48 -06:00
|
|
|
className="relative p-3 bg-base-200 rounded-xl flex gap-2 justify-between"
|
|
|
|
title={`@${collectionOwner.username} is the owner of this collection`}
|
2023-12-01 16:42:45 -06:00
|
|
|
>
|
2023-12-18 00:55:39 -06:00
|
|
|
<div className={"flex items-center justify-between w-full"}>
|
|
|
|
<div className={"flex items-center"}>
|
|
|
|
<div className={"shrink-0"}>
|
|
|
|
<ProfilePhoto
|
2023-12-19 17:28:48 -06:00
|
|
|
src={
|
|
|
|
collectionOwner.image
|
|
|
|
? collectionOwner.image
|
|
|
|
: undefined
|
|
|
|
}
|
2023-12-18 00:55:39 -06:00
|
|
|
name={collectionOwner.name}
|
|
|
|
/>
|
2023-12-01 11:01:56 -06:00
|
|
|
</div>
|
2023-12-18 00:55:39 -06:00
|
|
|
<div className={"grow ml-2"}>
|
2023-12-19 17:28:48 -06:00
|
|
|
<p className="text-sm font-semibold">
|
|
|
|
{collectionOwner.name}
|
|
|
|
</p>
|
|
|
|
<p className="text-xs text-neutral">
|
|
|
|
@{collectionOwner.username}
|
|
|
|
</p>
|
2023-12-18 00:55:39 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div>
|
2023-12-19 17:28:48 -06:00
|
|
|
<p className="text-sm font-bold">Owner</p>
|
2023-12-01 11:01:56 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-12-01 16:42:45 -06:00
|
|
|
</div>
|
2023-12-01 11:01:56 -06:00
|
|
|
|
2023-12-19 17:28:48 -06:00
|
|
|
<div className="divider my-0 last:hidden h-[3px]"></div>
|
|
|
|
|
2023-12-01 16:42:45 -06:00
|
|
|
{collection.members
|
|
|
|
.sort((a, b) => (a.userId as number) - (b.userId as number))
|
|
|
|
.map((e, i) => {
|
2023-12-19 17:28:48 -06:00
|
|
|
const roleLabel =
|
|
|
|
e.canCreate && e.canUpdate && e.canDelete
|
|
|
|
? "Admin"
|
|
|
|
: e.canCreate && !e.canUpdate && !e.canDelete
|
2023-12-29 11:21:22 -06:00
|
|
|
? "Contributor"
|
|
|
|
: !e.canCreate && !e.canUpdate && !e.canDelete
|
|
|
|
? "Viewer"
|
|
|
|
: undefined;
|
2023-12-19 17:28:48 -06:00
|
|
|
|
2023-12-01 16:42:45 -06:00
|
|
|
return (
|
2023-12-19 17:28:48 -06:00
|
|
|
<>
|
|
|
|
<div
|
|
|
|
key={i}
|
|
|
|
className="relative p-3 bg-base-200 rounded-xl flex gap-2 justify-between border-none"
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className={"flex items-center justify-between w-full"}
|
|
|
|
>
|
|
|
|
<div className={"flex items-center"}>
|
|
|
|
<div className={"shrink-0"}>
|
|
|
|
<ProfilePhoto
|
|
|
|
src={e.user.image ? e.user.image : undefined}
|
|
|
|
name={e.user.name}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={"grow ml-2"}>
|
|
|
|
<p className="text-sm font-semibold">
|
|
|
|
{e.user.name}
|
|
|
|
</p>
|
|
|
|
<p className="text-xs text-neutral">
|
|
|
|
@{e.user.username}
|
|
|
|
</p>
|
|
|
|
</div>
|
2023-12-18 00:55:39 -06:00
|
|
|
</div>
|
|
|
|
|
2023-12-19 17:28:48 -06:00
|
|
|
<div className={"flex items-center gap-2"}>
|
2023-12-18 00:55:39 -06:00
|
|
|
{permissions === true ? (
|
2023-12-19 17:28:48 -06:00
|
|
|
<div className="dropdown dropdown-bottom dropdown-end">
|
|
|
|
<div
|
|
|
|
tabIndex={0}
|
|
|
|
role="button"
|
|
|
|
className="btn btn-sm btn-primary font-normal"
|
|
|
|
>
|
|
|
|
{roleLabel}
|
|
|
|
<i className="bi-chevron-down"></i>
|
|
|
|
</div>
|
|
|
|
<ul className="dropdown-content z-[30] menu shadow bg-base-200 border border-neutral-content rounded-xl w-64 mt-1">
|
|
|
|
<li>
|
|
|
|
<label
|
|
|
|
className="label cursor-pointer flex justify-start"
|
|
|
|
tabIndex={0}
|
|
|
|
role="button"
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name={`role-radio-${e.userId}`}
|
|
|
|
className="radio checked:bg-primary"
|
|
|
|
checked={
|
|
|
|
!e.canCreate &&
|
|
|
|
!e.canUpdate &&
|
|
|
|
!e.canDelete
|
|
|
|
}
|
|
|
|
onChange={() => {
|
|
|
|
const updatedMember = {
|
|
|
|
...e,
|
|
|
|
canCreate: false,
|
|
|
|
canUpdate: false,
|
|
|
|
canDelete: false,
|
|
|
|
};
|
|
|
|
const updatedMembers =
|
|
|
|
collection.members.map((member) =>
|
|
|
|
member.userId === e.userId
|
|
|
|
? updatedMember
|
|
|
|
: member
|
|
|
|
);
|
|
|
|
setCollection({
|
|
|
|
...collection,
|
|
|
|
members: updatedMembers,
|
|
|
|
});
|
|
|
|
(
|
|
|
|
document?.activeElement as HTMLElement
|
|
|
|
)?.blur();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<div>
|
|
|
|
<p className="font-bold">Viewer</p>
|
|
|
|
<p>Read-only access</p>
|
|
|
|
</div>
|
|
|
|
</label>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<label
|
|
|
|
className="label cursor-pointer flex justify-start"
|
|
|
|
tabIndex={0}
|
|
|
|
role="button"
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name={`role-radio-${e.userId}`}
|
|
|
|
className="radio checked:bg-primary"
|
|
|
|
checked={
|
|
|
|
e.canCreate &&
|
|
|
|
!e.canUpdate &&
|
|
|
|
!e.canDelete
|
|
|
|
}
|
|
|
|
onChange={() => {
|
|
|
|
const updatedMember = {
|
|
|
|
...e,
|
|
|
|
canCreate: true,
|
|
|
|
canUpdate: false,
|
|
|
|
canDelete: false,
|
|
|
|
};
|
|
|
|
const updatedMembers =
|
|
|
|
collection.members.map((member) =>
|
|
|
|
member.userId === e.userId
|
|
|
|
? updatedMember
|
|
|
|
: member
|
|
|
|
);
|
|
|
|
setCollection({
|
|
|
|
...collection,
|
|
|
|
members: updatedMembers,
|
|
|
|
});
|
|
|
|
(
|
|
|
|
document?.activeElement as HTMLElement
|
|
|
|
)?.blur();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<div>
|
|
|
|
<p className="font-bold">Contributor</p>
|
|
|
|
<p>Can view and create Links</p>
|
|
|
|
</div>
|
|
|
|
</label>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<label
|
|
|
|
className="label cursor-pointer flex justify-start"
|
|
|
|
tabIndex={0}
|
|
|
|
role="button"
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name={`role-radio-${e.userId}`}
|
|
|
|
className="radio checked:bg-primary"
|
|
|
|
checked={
|
|
|
|
e.canCreate &&
|
|
|
|
e.canUpdate &&
|
|
|
|
e.canDelete
|
|
|
|
}
|
|
|
|
onChange={() => {
|
|
|
|
const updatedMember = {
|
|
|
|
...e,
|
|
|
|
canCreate: true,
|
|
|
|
canUpdate: true,
|
|
|
|
canDelete: true,
|
|
|
|
};
|
|
|
|
const updatedMembers =
|
|
|
|
collection.members.map((member) =>
|
|
|
|
member.userId === e.userId
|
|
|
|
? updatedMember
|
|
|
|
: member
|
|
|
|
);
|
|
|
|
setCollection({
|
|
|
|
...collection,
|
|
|
|
members: updatedMembers,
|
|
|
|
});
|
|
|
|
(
|
|
|
|
document?.activeElement as HTMLElement
|
|
|
|
)?.blur();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<div>
|
|
|
|
<p className="font-bold">Admin</p>
|
|
|
|
<p>Full access to all Links</p>
|
|
|
|
</div>
|
|
|
|
</label>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
2023-12-18 00:55:39 -06:00
|
|
|
) : (
|
|
|
|
<p className="text-sm text-neutral">
|
2023-12-19 17:28:48 -06:00
|
|
|
{roleLabel}
|
2023-12-18 00:55:39 -06:00
|
|
|
</p>
|
|
|
|
)}
|
2023-12-19 17:28:48 -06:00
|
|
|
|
|
|
|
{permissions === true && (
|
|
|
|
<i
|
|
|
|
className={
|
|
|
|
"bi-x text-xl btn btn-sm btn-square btn-ghost text-neutral hover:text-red-500 dark:hover:text-red-500 duration-100 cursor-pointer"
|
|
|
|
}
|
|
|
|
title="Remove Member"
|
|
|
|
onClick={() => {
|
|
|
|
const updatedMembers =
|
|
|
|
collection.members.filter((member) => {
|
|
|
|
return (
|
|
|
|
member.user.username !== e.user.username
|
|
|
|
);
|
|
|
|
});
|
|
|
|
setCollection({
|
|
|
|
...collection,
|
|
|
|
members: updatedMembers,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
2023-12-01 16:42:45 -06:00
|
|
|
</div>
|
2023-12-18 00:55:39 -06:00
|
|
|
</div>
|
2023-12-01 11:01:56 -06:00
|
|
|
</div>
|
2023-12-19 17:28:48 -06:00
|
|
|
<div className="divider my-0 last:hidden h-[3px]"></div>
|
|
|
|
</>
|
2023-12-01 16:42:45 -06:00
|
|
|
);
|
2023-12-19 17:28:48 -06:00
|
|
|
})}
|
2023-12-01 16:42:45 -06:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{permissions === true && (
|
|
|
|
<button
|
2023-12-07 11:29:45 -06:00
|
|
|
className="btn btn-accent dark:border-violet-400 text-white w-fit ml-auto mt-3"
|
2023-12-01 16:42:45 -06:00
|
|
|
onClick={submit}
|
|
|
|
>
|
|
|
|
Save
|
|
|
|
</button>
|
|
|
|
)}
|
2023-12-01 11:01:56 -06:00
|
|
|
</div>
|
2023-12-01 16:42:45 -06:00
|
|
|
</Modal>
|
2023-12-01 11:01:56 -06:00
|
|
|
);
|
|
|
|
}
|