import { Menu, Item, Submenu } from "solid-contextmenu"
import { useCopyLink, useDownload, useLink, useT } from "~/hooks"
import "solid-contextmenu/dist/style.css"
import { HStack, Icon, Text, useColorMode, Image } from "@hope-ui/solid"
import { operations } from "../toolbar/operations"
import {
For,
Show,
createSignal,
onMount,
onCleanup,
lazy,
Suspense,
} from "solid-js"
import { bus, convertURL, notify, joinBase, pathJoin } from "~/utils"
import { ObjType, UserMethods, UserPermissions } from "~/types"
import {
getSettingBool,
haveSelected,
me,
oneChecked,
selectedObjs,
getCurrentPath,
} from "~/store"
import { players } from "../previews/video_box"
import { BsPlayCircleFill } from "solid-icons/bs"
import { isArchive } from "~/store/archive"
import { useLabels } from "~/store/label"
import {
createLabelFileBinding,
createLabelFileBindingBatch,
} from "~/utils/api"
import { usePath } from "~/hooks"
import { AiOutlineTag } from "solid-icons/ai"
// 懒加载对话框组件
const AddLabelDialog = lazy(() => import("~/components/AddLabelDialog"))
const EditLabelDialog = lazy(() => import("~/components/EditLabelDialog"))
interface Label {
id: number
name: string
type: number
description: string
bg_color: string
}
const ItemContent = (props: { name: string }) => {
const t = useT()
return (
{t(`home.toolbar.${props.name}`)}
)
}
export const ContextMenu = () => {
const t = useT()
const { colorMode } = useColorMode()
const { copySelectedRawLink, copySelectedPreviewPage } = useCopyLink()
const { batchDownloadSelected, sendToAria2, playlistDownloadSelected } =
useDownload()
const { refresh } = usePath()
// 标签相关
const { labels, refetch } = useLabels()
const [isAddLabelOpen, setIsAddLabelOpen] = createSignal(false)
const [isEditLabelOpen, setIsEditLabelOpen] = createSignal(false)
const [currentObj, setCurrentObj] = createSignal(null)
onMount(() => {
const refreshHandler = () => {
refetch()
}
bus.on("refresh_labels", refreshHandler)
onCleanup(() => {
bus.off("refresh_labels", refreshHandler)
})
})
const handleAddLabel = async (
name: string,
description: string,
bg_color: string,
) => {
try {
const obj = currentObj()
if (!obj) return
// 获取最新的标签列表
const labelData = await refetch()
if (labelData?.data?.content) {
// 找到刚刚创建的标签
const newLabel = labelData.data.content.find(
(label: Label) =>
label.name === name &&
label.description === description &&
label.bg_color === bg_color,
)
if (newLabel) {
// 创建标签文件绑定
await createLabelFileBindingBatch(newLabel.id.toString(), [obj])
// 强制刷新当前目录
await refresh(false, true)
}
}
} catch (err) {
console.error("Failed to bind label to file:", err)
}
}
const handleEditLabel = (selectedLabels: string[]) => {
// 触发父组件刷新
bus.emit("refresh")
}
const canPackageDownload = () => {
return UserMethods.is_admin(me()) || getSettingBool("package_download")
}
const canS3Transition = () => {
if (!haveSelected()) return false
if (!UserMethods.is_admin(me())) return false
return selectedObjs().every((obj) => !obj.is_dir && !!obj.storage_class)
}
const { rawLink } = useLink()
return (
<>
{/* 标签对话框 */}
}>
setIsAddLabelOpen(false)}
onSubmit={handleAddLabel}
/>
setIsEditLabelOpen(false)}
onSubmit={handleEditLabel}
labels={labels()?.data?.content || []}
obj={currentObj()}
/>
>
)
}