import { PromptIcon } from '@/components/bs-icons/prompt';
import { Button } from '@/components/bs-ui/button';
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/bs-ui/dialog";
import { useTranslation } from 'react-i18next';
import { CrossCircledIcon } from '@radix-ui/react-icons';
import { cname } from '@/components/bs-ui/utils';
import { useEffect, useState } from 'react';
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
import { updateHomeLabelApi, getAllLabelsApi } from "@/controllers/API/label";
import { captureAndAlertRequestErrorHoc } from '@/controllers/request';
import { useToast } from '@/components/bs-ui/toast/use-toast';
function DragItem({className = '', data, children, onCancel}) {
return
{e.stopPropagation(); onCancel(data.id)}}
className='text-[#999] absolute top-[-6px] right-[-6px] cursor-pointer'/>
{data.index}
{children}
}
export default function MarkLabel({open, home, onClose}) {
const { t } = useTranslation()
const [labels, setLabels] = useState([])
const [selected, setSelected] = useState([])
const { message } = useToast()
useEffect(() => {
async function init() {
const all = await getAllLabelsApi()
const newData = all.data.map(d => {
const res = home.find(h => h.value === d.id)
return res ? {label:d.name, value:d.id, selected:true} : {label:d.name, value:d.id, selected:false}
})
setLabels(newData)
setSelected(home)
}
init()
}, [home])
const handleCancel = () => {
onClose(false)
}
const handleConfirm = async () => {
await captureAndAlertRequestErrorHoc(updateHomeLabelApi(selected.map(s => s.value)))
onClose(false)
}
const handleSelect = (id) => {
setLabels(pre => {
const newData = pre.map(l => l.value === id ? {...l, selected:!l.selected} : l)
if(newData.filter(d => d.selected).length > 10) {
message({
title: t('prompt'),
variant: 'warning',
description: '最多选择10个标签'
})
return pre
}
const select = newData.find(d => d.value === id && d.selected)
setSelected(select ? [...selected, select] : pre => pre.filter(d => d.value !== id))
return newData
})
}
const handleDelete = (id) => {
setSelected(pre => pre.filter(d => d.value !== id))
setLabels(pre => pre.map(d => d.value === id ? {...d, selected:!d.selected} : d))
}
const handleDragEnd = (result) => {
if(!result.destination) return
const newData = selected
const [moveItem] = newData.splice(result.source.index, 1)
newData.splice(result.destination.index, 0, moveItem)
setSelected(newData)
setFlag(false)
}
const [flag, setFlag] = useState(false) // 解决拖拽映射位置错位
return