import { getBtnAuth } from '@/utils/utils'
import { FILE } from '@/db/const'
import { reactive, watch } from 'vue'
import { useShareStore, useGlobalStore } from '@/store'
import { FileInfo } from '@/types/share'
interface UseAuthData {
canIShare: boolean
canIDownload: boolean
canIAddFileCopy: boolean
canIMove: boolean
canIDelete: boolean
canIRename: boolean
canIEdit: boolean
canIReviewHistory: boolean
canIManageCooper: boolean
canIShareOutWay: boolean
isOwner: boolean
canIUploadVersion: boolean
canIRead: boolean
}
const useAuth = (file?: FileInfo) => {
const shareStore = useShareStore()
const globalStore = useGlobalStore()
let useAuthData = reactive({}) as UseAuthData
const handleValue = (fileInfo) => {
const FILE_ACTION_BITS = FILE.ACTION_BITS
const actions = fileInfo?.actions
const canIShare: boolean = getBtnAuth(actions, FILE_ACTION_BITS.SHARE)
const canIDownload: boolean = getBtnAuth(actions, FILE_ACTION_BITS.DOWNLOAD)
const canIAddFileCopy: boolean = getBtnAuth(actions, FILE_ACTION_BITS.COPY)
const canIMove: boolean = getBtnAuth(actions, FILE_ACTION_BITS.MOVE)
const canIDelete: boolean = getBtnAuth(actions, FILE_ACTION_BITS.DELETE)
const canIRename: boolean = getBtnAuth(actions, FILE_ACTION_BITS.RENAME)
const canIEdit: boolean = getBtnAuth(actions, FILE_ACTION_BITS.UPLOADVERSION)
const canIReviewHistory: boolean = getBtnAuth(actions, FILE_ACTION_BITS.LIST_ALL_VERSION)
const canIManageCooper: boolean = getBtnAuth(actions, FILE_ACTION_BITS.RESHARE)
const canIShareOutWay: boolean = !globalStore.query('enterprise').forbidShareOut
const isOwner: boolean = fileInfo?.ownerId === globalStore.query('user').userId
const canIUploadVersion: boolean = getBtnAuth(actions, FILE_ACTION_BITS.UPLOADVERSION)
const canIRead: boolean = getBtnAuth(actions, FILE_ACTION_BITS.READ)
useAuthData = Object.assign(useAuthData, {
canIShare,
canIDownload,
canIAddFileCopy,
canIMove,
canIDelete,
canIRename,
canIEdit,
canIReviewHistory,
canIManageCooper,
canIShareOutWay,
isOwner,
canIUploadVersion,
canIRead,
})
}
watch(
() => shareStore.fileInfo,
(fileInfo) => {
handleValue(fileInfo)
},
{
immediate: true,
deep: true,
}
)
if (file) {
handleValue(file)
}
return useAuthData
}
export default useAuth