2023-04-23 08:26:39 -05:00
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-02-24 11:32:28 -06:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" ;
2023-05-25 13:44:08 -05:00
import {
faUser ,
faPenToSquare ,
faTrashCan ,
faEllipsis ,
} from "@fortawesome/free-solid-svg-icons" ;
2023-03-05 15:03:20 -06:00
import Link from "next/link" ;
2023-05-14 10:41:08 -05:00
import { ExtendedCollection } from "@/types/global" ;
import useLinkStore from "@/store/links" ;
2023-05-25 09:17:20 -05:00
import ImageWithFallback from "./ImageWithFallback" ;
2023-05-25 13:44:08 -05:00
import Dropdown from "./Dropdown" ;
import { useState } from "react" ;
import Modal from "@/components/Modal" ;
import EditCollection from "@/components/Modal/EditCollection" ;
import DeleteCollection from "@/components/Modal/DeleteCollection" ;
2023-02-24 11:32:28 -06:00
2023-05-14 10:41:08 -05:00
export default function ( { collection } : { collection : ExtendedCollection } ) {
const { links } = useLinkStore ( ) ;
2023-02-24 11:32:28 -06:00
const formattedDate = new Date ( collection . createdAt ) . toLocaleString ( "en-US" , {
year : "numeric" ,
month : "short" ,
day : "numeric" ,
} ) ;
2023-05-25 13:44:08 -05:00
const [ expandDropdown , setExpandDropdown ] = useState ( false ) ;
const [ editCollectionModal , setEditCollectionModal ] = useState ( false ) ;
const [ deleteCollectionModal , setDeleteCollectionModal ] = useState ( false ) ;
const toggleEditCollectionModal = ( ) = > {
setEditCollectionModal ( ! editCollectionModal ) ;
} ;
const toggleDeleteCollectionModal = ( ) = > {
setDeleteCollectionModal ( ! deleteCollectionModal ) ;
} ;
2023-02-24 11:32:28 -06:00
return (
2023-05-25 13:44:08 -05:00
< div className = "p-5 bg-gradient-to-tr from-sky-100 from-10% via-gray-100 via-20% self-stretch min-h-[12rem] rounded-md cursor-pointer shadow duration-100 hover:shadow-none group relative" >
< div
onClick = { ( ) = > setExpandDropdown ( ! expandDropdown ) }
id = "edit-dropdown"
className = "inline-flex absolute top-5 right-5 rounded-md cursor-pointer hover:bg-white hover:border-sky-500 border-sky-100 border duration-100 p-1"
>
< FontAwesomeIcon
icon = { faEllipsis }
id = "edit-dropdown"
className = "w-5 h-5 text-gray-500"
/ >
< / div >
2023-05-25 09:17:20 -05:00
< Link href = { ` /collections/ ${ collection . id } ` } >
2023-05-25 13:44:08 -05:00
< div className = "flex flex-col gap-2 justify-between h-full select-none" >
2023-05-25 09:17:20 -05:00
< div >
2023-05-25 13:44:08 -05:00
< p className = "text-2xl w-max font-bold capitalize text-sky-600" >
{ collection . name }
< / p >
2023-04-25 07:39:46 -05:00
< / div >
2023-05-25 09:17:20 -05:00
< div className = "flex justify-between items-center" >
< div className = "text-sky-400 flex items-center w-full" >
{ collection . members
2023-05-25 13:44:08 -05:00
. sort ( ( a , b ) = > a . userId - b . userId )
2023-05-25 09:17:20 -05:00
. map ( ( e , i ) = > {
return (
< ImageWithFallback
key = { i }
src = { ` /api/avatar/ ${ e . userId } ` }
className = "h-10 w-10 shadow rounded-full border-[3px] border-sky-100 -mr-3"
>
< div className = "text-white bg-sky-500 h-10 w-10 shadow rounded-full border-[3px] border-sky-100 -mr-3 flex items-center justify-center" >
< FontAwesomeIcon icon = { faUser } className = "w-5 h-5" / >
< / div >
< / ImageWithFallback >
) ;
} )
2023-05-25 13:44:08 -05:00
. slice ( 0 , 4 ) }
{ collection . members . length - 4 > 0 ? (
2023-05-25 09:17:20 -05:00
< div className = "h-10 w-10 text-white flex items-center justify-center rounded-full border-[3px] bg-sky-500 border-sky-100 -mr-3" >
+ { collection . members . length - 3 }
< / div >
) : null }
< / div >
2023-05-25 13:44:08 -05:00
< div className = "text-right w-40" >
2023-05-25 09:17:20 -05:00
< p className = "text-sky-500 font-bold text-sm" >
{ links . filter ( ( e ) = > e . collectionId === collection . id ) . length } { " " }
Links
< / p >
< p className = "text-gray-500 font-bold text-xs" > { formattedDate } < / p >
< / div >
2023-05-24 20:47:18 -05:00
< / div >
2023-03-05 15:03:20 -06:00
< / div >
2023-05-25 09:17:20 -05:00
< / Link >
2023-05-25 13:44:08 -05:00
{ expandDropdown ? (
< Dropdown
items = { [
{
name : "Edit Collection" ,
icon : < FontAwesomeIcon icon = { faPenToSquare } / > ,
onClick : ( ) = > {
toggleEditCollectionModal ( ) ;
setExpandDropdown ( false ) ;
} ,
} ,
{
name : "Delete Collection" ,
icon : < FontAwesomeIcon icon = { faTrashCan } / > ,
onClick : ( ) = > {
toggleDeleteCollectionModal ( ) ;
setExpandDropdown ( false ) ;
} ,
} ,
] }
onClickOutside = { ( e : Event ) = > {
const target = e . target as HTMLInputElement ;
if ( target . id !== "edit-dropdown" ) setExpandDropdown ( false ) ;
} }
className = "absolute top-[3.2rem] right-5 z-10 w-44"
/ >
) : null }
{ editCollectionModal ? (
< Modal toggleModal = { toggleEditCollectionModal } >
< EditCollection
toggleCollectionModal = { toggleEditCollectionModal }
collection = { collection }
/ >
< / Modal >
) : null }
{ deleteCollectionModal ? (
< Modal toggleModal = { toggleDeleteCollectionModal } >
< DeleteCollection
collection = { collection }
toggleDeleteCollectionModal = { toggleDeleteCollectionModal }
/ >
< / Modal >
) : null }
2023-05-25 09:17:20 -05:00
< / div >
2023-02-24 11:32:28 -06:00
) ;
}