import DeleteIcon from '@mui/icons-material/Delete'
import EditIcon from '@mui/icons-material/Edit'
import {
Avatar,
Box,
Chip,
CircularProgress,
Container,
IconButton,
Stack,
Typography,
} from '@mui/joy'
import { useEffect, useState } from 'react'
import LabelModal from '../Modals/Inputs/LabelModal'
import { Add } from '@mui/icons-material'
import { useQueryClient } from '@tanstack/react-query'
import {
Type as ListType,
SwipeableList,
SwipeableListItem,
SwipeAction,
TrailingActions,
} from 'react-swipeable-list'
import 'react-swipeable-list/dist/styles.css'
import { useUserProfile } from '../../queries/UserQueries'
import { getTextColorFromBackgroundColor } from '../../utils/Colors'
import { DeleteLabel } from '../../utils/Fetcher'
import { getSafeBottomStyles } from '../../utils/SafeAreaUtils'
import ConfirmationModal from '../Modals/Inputs/ConfirmationModal'
import { useLabels } from './LabelQueries'
const LabelCardContent = ({ label, currentUserId }) => {
// Check if current user owns this label
const isOwnedByCurrentUser = label.created_by === currentUserId
return (
{/* Color Avatar */}
{label.name.charAt(0).toUpperCase()}
{/* Content - Center */}
{/* Label Name */}
{label.name}
{/* Color Info */}
{!isOwnedByCurrentUser && (
Shared
)}
)
}
const LabelView = () => {
const { data: labels, isLabelsLoading, isError } = useLabels()
const { data: userProfile } = useUserProfile()
const [userLabels, setUserLabels] = useState([])
const [modalOpen, setModalOpen] = useState(false)
const [currentLabel, setCurrentLabel] = useState(null)
const queryClient = useQueryClient()
const [confirmationModel, setConfirmationModel] = useState({})
const handleAddLabel = () => {
setCurrentLabel(null)
setModalOpen(true)
}
const handleEditLabel = label => {
setCurrentLabel(label)
setModalOpen(true)
}
const handleDeleteClicked = id => {
setConfirmationModel({
isOpen: true,
title: 'Delete Label',
message:
'Are you sure you want to delete this label? This will remove the label from all tasks.',
confirmText: 'Delete',
color: 'danger',
cancelText: 'Cancel',
onClose: confirmed => {
if (confirmed === true) {
handleDeleteLabel(id)
}
setConfirmationModel({})
},
})
}
const handleDeleteLabel = id => {
DeleteLabel(id).then(() => {
const updatedLabels = userLabels.filter(label => label.id !== id)
setUserLabels(updatedLabels)
queryClient.invalidateQueries('labels')
})
}
const handleSaveLabel = newOrUpdatedLabel => {
queryClient.invalidateQueries('labels')
setModalOpen(false)
const updatedLabels = userLabels.map(label =>
label.id === newOrUpdatedLabel.id ? newOrUpdatedLabel : label,
)
setUserLabels(updatedLabels)
}
useEffect(() => {
if (labels) {
setUserLabels(labels)
}
}, [labels])
if (isLabelsLoading) {
return (
)
}
if (isError) {
return (
Failed to load labels. Please try again.
)
}
return (
{/* */}
Labels
Manage your labels and organize your tasks effectively. Labels will
be automatically shared with your circle if they are used on a
shared task.
{userLabels.length === 0 && (
No labels available. Add a new label to get started.
)}
{userLabels.map(label => (
handleEditLabel(label)}>
Edit
handleDeleteClicked(label.id)}>
Delete
}
>
))}
{modalOpen && (
setModalOpen(false)}
onSave={handleSaveLabel}
label={currentLabel}
/>
)}
)
}
export default LabelView