feat(webui): enforce reviewer role restrictions in the UI

This commit is contained in:
yiling
2026-06-07 09:51:33 +08:00
parent 9370c6b255
commit d06b523dc7
15 changed files with 449 additions and 303 deletions
@@ -45,6 +45,7 @@ interface FileBrowserContextMenuProps {
shareLinks?: ShareLinkInfo[]
onCreateShareLink?: (items: AssetInfo[]) => void
onAddToShareLink?: (shareId: string, items: AssetInfo[]) => void
canEdit?: boolean
}
export function FileBrowserContextMenu({
@@ -69,6 +70,7 @@ export function FileBrowserContextMenu({
shareLinks = [],
onCreateShareLink,
onAddToShareLink,
canEdit = true,
}: FileBrowserContextMenuProps) {
const allItems = [...folders, ...files]
const selectedItems = allItems.filter((i) => selectedIds.has(i.id!))
@@ -129,6 +131,7 @@ export function FileBrowserContextMenu({
}
if (!item) {
if (!canEdit) return null
// Right-click on empty area
return (
<ContextMenuContent
@@ -167,44 +170,54 @@ export function FileBrowserContextMenu({
<Download className="mr-2 h-4 w-4" />
<span>Download</span>
</ContextMenuItem>
<ContextMenuItem onSelect={() => onMoveTo(itemsToModify)}>
<ArrowRight className="mr-2 h-4 w-4" />
<span>Move to</span>
</ContextMenuItem>
<ContextMenuItem onSelect={() => onCopyTo(itemsToModify)}>
<Copy className="mr-2 h-4 w-4" />
<span>Copy to</span>
</ContextMenuItem>
<ContextMenuItem onSelect={handleDelete} className="text-destructive">
<Trash2 className="mr-2 h-4 w-4" />
<span>Delete</span>
</ContextMenuItem>
{canEdit && (
<ContextMenuItem onSelect={() => onMoveTo(itemsToModify)}>
<ArrowRight className="mr-2 h-4 w-4" />
<span>Move to</span>
</ContextMenuItem>
)}
{canEdit && (
<ContextMenuItem onSelect={() => onCopyTo(itemsToModify)}>
<Copy className="mr-2 h-4 w-4" />
<span>Copy to</span>
</ContextMenuItem>
)}
{canEdit && (
<ContextMenuItem onSelect={handleDelete} className="text-destructive">
<Trash2 className="mr-2 h-4 w-4" />
<span>Delete</span>
</ContextMenuItem>
)}
<ContextMenuSeparator />
<ContextMenuItem onSelect={() => onCreateShareLink?.(itemsToModify)}>
<Plus className="mr-2 h-4 w-4" />
<span>Create Share Link</span>
</ContextMenuItem>
{canEdit && (
<>
<ContextMenuSeparator />
<ContextMenuItem onSelect={() => onCreateShareLink?.(itemsToModify)}>
<Plus className="mr-2 h-4 w-4" />
<span>Create Share Link</span>
</ContextMenuItem>
{shareLinks.length > 0 && (
<ContextMenuSub>
<ContextMenuSubTrigger>
<Link className="mr-2 h-4 w-4" />
<span>Add to Share Links</span>
</ContextMenuSubTrigger>
<ContextMenuPortal>
<ContextMenuSubContent className="w-48">
{shareLinks.map((share) => (
<ContextMenuItem
key={share.id}
onSelect={() => onAddToShareLink?.(share.id, itemsToModify)}
>
<span className="truncate">{share.name}</span>
</ContextMenuItem>
))}
</ContextMenuSubContent>
</ContextMenuPortal>
</ContextMenuSub>
{shareLinks.length > 0 && (
<ContextMenuSub>
<ContextMenuSubTrigger>
<Link className="mr-2 h-4 w-4" />
<span>Add to Share Links</span>
</ContextMenuSubTrigger>
<ContextMenuPortal>
<ContextMenuSubContent className="w-48">
{shareLinks.map((share) => (
<ContextMenuItem
key={share.id}
onSelect={() => onAddToShareLink?.(share.id, itemsToModify)}
>
<span className="truncate">{share.name}</span>
</ContextMenuItem>
))}
</ContextMenuSubContent>
</ContextMenuPortal>
</ContextMenuSub>
)}
</>
)}
</ContextMenuContent>
) : (
@@ -213,29 +226,35 @@ export function FileBrowserContextMenu({
e.preventDefault()
}}
>
<ContextMenuItem
onSelect={() => {
handleRename()
}}
>
<Edit className="mr-2 h-4 w-4" />
<span>Rename</span>
</ContextMenuItem>
{canEdit && (
<ContextMenuItem
onSelect={() => {
handleRename()
}}
>
<Edit className="mr-2 h-4 w-4" />
<span>Rename</span>
</ContextMenuItem>
)}
<ContextMenuItem onSelect={handleDownload}>
<Download className="mr-2 h-4 w-4" />
<span>Download</span>
</ContextMenuItem>
<ContextMenuItem onSelect={() => onMoveTo(itemsToModify)}>
<ArrowRight className="mr-2 h-4 w-4" />
<span>Move to</span>
</ContextMenuItem>
<ContextMenuItem onSelect={() => onCopyTo(itemsToModify)}>
<Copy className="mr-2 h-4 w-4" />
<span>Copy to</span>
</ContextMenuItem>
{canEdit && (
<ContextMenuItem onSelect={() => onMoveTo(itemsToModify)}>
<ArrowRight className="mr-2 h-4 w-4" />
<span>Move to</span>
</ContextMenuItem>
)}
{canEdit && (
<ContextMenuItem onSelect={() => onCopyTo(itemsToModify)}>
<Copy className="mr-2 h-4 w-4" />
<span>Copy to</span>
</ContextMenuItem>
)}
{(item.type === 'file' || item.type === 'version_stack') && (
{canEdit && (item.type === 'file' || item.type === 'version_stack') && (
<ContextMenuItem onSelect={() => onNewVersion(item)}>
<UploadCloud className="mr-2 h-4 w-4" />
<span>Create new version</span>
@@ -249,37 +268,41 @@ export function FileBrowserContextMenu({
</ContextMenuItem>
)}
<ContextMenuSeparator />
<ContextMenuItem onSelect={handleDelete} className="text-destructive">
<Trash2 className="mr-2 h-4 w-4" />
<span>Delete</span>
</ContextMenuItem>
{canEdit && (
<>
<ContextMenuSeparator />
<ContextMenuItem onSelect={handleDelete} className="text-destructive">
<Trash2 className="mr-2 h-4 w-4" />
<span>Delete</span>
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem onSelect={() => onCreateShareLink?.(itemsToModify)}>
<Plus className="mr-2 h-4 w-4" />
<span>Create Share Link</span>
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem onSelect={() => onCreateShareLink?.(itemsToModify)}>
<Plus className="mr-2 h-4 w-4" />
<span>Create Share Link</span>
</ContextMenuItem>
{shareLinks.length > 0 && (
<ContextMenuSub>
<ContextMenuSubTrigger>
<Link className="mr-2 h-4 w-4" />
<span>Add to Share Links</span>
</ContextMenuSubTrigger>
<ContextMenuPortal>
<ContextMenuSubContent className="w-48">
{shareLinks.map((share) => (
<ContextMenuItem
key={share.id}
onSelect={() => onAddToShareLink?.(share.id, itemsToModify)}
>
<span className="truncate">{share.name}</span>
</ContextMenuItem>
))}
</ContextMenuSubContent>
</ContextMenuPortal>
</ContextMenuSub>
{shareLinks.length > 0 && (
<ContextMenuSub>
<ContextMenuSubTrigger>
<Link className="mr-2 h-4 w-4" />
<span>Add to Share Links</span>
</ContextMenuSubTrigger>
<ContextMenuPortal>
<ContextMenuSubContent className="w-48">
{shareLinks.map((share) => (
<ContextMenuItem
key={share.id}
onSelect={() => onAddToShareLink?.(share.id, itemsToModify)}
>
<span className="truncate">{share.name}</span>
</ContextMenuItem>
))}
</ContextMenuSubContent>
</ContextMenuPortal>
</ContextMenuSub>
)}
</>
)}
</ContextMenuContent>
)
@@ -8,6 +8,7 @@ import type { SearchCondition, SearchSort } from '@shumai/dtos'
import type { ShareLinkInfo } from '@shumai/dtos'
import type { CreateUploadTaskRequest } from '@shumai/dtos'
import { formatSize } from '@/ui/lib/format'
import { usePermissions } from '@/ui/hooks/use-permissions'
import { useFieldStore } from '@/ui/stores/fields'
import { useUploadStore } from '@/ui/stores/upload'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
@@ -404,6 +405,7 @@ export function FileBrowser({
const newVersionInputRef = useRef<HTMLInputElement>(null)
const [targetVersionFileId, setTargetVersionFileId] = useState<string | null>(null)
const fileContextMenu = useRef(false)
const { canEdit } = usePermissions()
const scrollContainerRef = useRef<HTMLDivElement>(null)
@@ -513,6 +515,7 @@ export function FileBrowser({
selectedCount: selectedIds.size,
isShareView,
fields: displayedFields,
canEdit,
}
if (displayStyle === 'card') {
if (effectiveType === 'folder') return <FolderCard key={item.id} {...props} />
@@ -706,7 +709,7 @@ export function FileBrowser({
const handlePaste = useCallback(
(e: React.ClipboardEvent | ClipboardEvent) => {
if (isShareView || !!collection) return
if (!canEdit || isShareView || !!collection) return
if (!e.clipboardData) return
const items = e.clipboardData.items
const files: File[] = []
@@ -745,7 +748,7 @@ export function FileBrowser({
// Add global paste listener if browser is active
useEffect(() => {
const onGlobalPaste = (e: ClipboardEvent) => {
if (isShareView || !!collection) return
if (!canEdit || isShareView || !!collection) return
// Don't intercept if user is typing in an input
if (
document.activeElement &&
@@ -925,6 +928,7 @@ export function FileBrowser({
shareLinks={shareLinksData?.data ?? []}
onCreateShareLink={handleCreateShareLink}
onAddToShareLink={handleAddToShareLink}
canEdit={canEdit}
/>
</ContextMenu>
)
@@ -932,28 +936,32 @@ export function FileBrowser({
return (
<>
<input
type="file"
ref={fileInputRef}
multiple
style={{ display: 'none' }}
onChange={handleFileChange}
/>
<input
type="file"
ref={folderInputRef}
multiple
/* @ts-expect-error - webkitdirectory is not in the type definition */
webkitdirectory="true"
style={{ display: 'none' }}
onChange={handleFileChange}
/>
<input
type="file"
ref={newVersionInputRef}
style={{ display: 'none' }}
onChange={handleNewVersionFileChange}
/>
{canEdit && (
<>
<input
type="file"
ref={fileInputRef}
multiple
style={{ display: 'none' }}
onChange={handleFileChange}
/>
<input
type="file"
ref={folderInputRef}
multiple
/* @ts-expect-error - webkitdirectory is not in the type definition */
webkitdirectory="true"
style={{ display: 'none' }}
onChange={handleFileChange}
/>
<input
type="file"
ref={newVersionInputRef}
style={{ display: 'none' }}
onChange={handleNewVersionFileChange}
/>
</>
)}
{renderContent()}
{moveCopyMode && (
@@ -50,6 +50,7 @@ interface FileCardProps {
selectedCount?: number
fields: MetadataFieldInfo[]
isShareView?: boolean
canEdit?: boolean
}
export function FileCard({
@@ -75,6 +76,7 @@ export function FileCard({
selectedCount,
fields,
isShareView,
canEdit = true,
}: FileCardProps) {
const [name, setName] = useState(item.name)
const inputRef = useRef<HTMLInputElement>(null)
@@ -315,7 +317,7 @@ export function FileCard({
</DropdownMenuItem>
) : (
<>
{(!isChecked || (selectedCount || 0) <= 1) && (
{canEdit && (!isChecked || (selectedCount || 0) <= 1) && (
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation()
@@ -335,17 +337,21 @@ export function FileCard({
<Download className="mr-2 h-4 w-4" />
<span>Download</span>
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation()
onAction?.('delete', item)
}}
className="text-destructive"
>
<Trash2 className="mr-2 h-4 w-4" />
<span>Delete</span>
</DropdownMenuItem>
{canEdit && (
<>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation()
onAction?.('delete', item)
}}
className="text-destructive"
>
<Trash2 className="mr-2 h-4 w-4" />
<span>Delete</span>
</DropdownMenuItem>
</>
)}
</>
)}
</DropdownMenuContent>
@@ -362,8 +368,8 @@ export function FileCard({
<FieldRenderer
config={field.config}
value={itemFieldValueMap[field.id!]?.value}
onSave={(val) => onSaveField(field.id!, val)}
readOnly={field.readOnly}
onSave={canEdit ? (val) => onSaveField(field.id!, val) : undefined}
readOnly={field.readOnly || !canEdit}
/>
</div>
</div>
@@ -32,6 +32,7 @@ interface FileListItemProps {
disabled?: boolean
fields: MetadataFieldInfo[]
columnSizing?: Record<string, number>
canEdit?: boolean
}
export function FileListItem({
@@ -54,6 +55,7 @@ export function FileListItem({
disabled,
fields,
columnSizing,
canEdit = true,
}: FileListItemProps) {
const [name, setName] = useState(item.name)
const inputRef = useRef<HTMLInputElement>(null)
@@ -282,8 +284,8 @@ export function FileListItem({
<FieldRenderer
config={field.config}
value={itemFieldValueMap[field.id!]?.value}
onSave={(val) => onSaveField(field.id!, val)}
readOnly={field.readOnly}
onSave={canEdit ? (val) => onSaveField(field.id!, val) : undefined}
readOnly={field.readOnly || !canEdit}
/>
</td>
))}
@@ -33,6 +33,7 @@ interface FolderCardProps {
onAction?: (action: 'rename' | 'delete' | 'download' | 'restore', item: AssetInfo) => void
isRecentlyDeleted?: boolean
selectedCount?: number
canEdit?: boolean
}
const FolderPreviewGrid = ({ items }: { items: ChildPreview[] }) => {
@@ -105,6 +106,7 @@ export function FolderCard({
onAction,
isRecentlyDeleted,
selectedCount,
canEdit = true,
}: FolderCardProps) {
const [name, setName] = useState(item.name || '')
const inputRef = useRef<HTMLInputElement>(null)
@@ -311,7 +313,7 @@ export function FolderCard({
</DropdownMenuItem>
) : (
<>
{(!isChecked || (selectedCount || 0) <= 1) && (
{canEdit && (!isChecked || (selectedCount || 0) <= 1) && (
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation()
@@ -331,17 +333,21 @@ export function FolderCard({
<Download className="mr-2 h-4 w-4" />
<span>Download</span>
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation()
onAction?.('delete', item)
}}
className="text-destructive"
>
<Trash2 className="mr-2 h-4 w-4" />
<span>Delete</span>
</DropdownMenuItem>
{canEdit && (
<>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation()
onAction?.('delete', item)
}}
className="text-destructive"
>
<Trash2 className="mr-2 h-4 w-4" />
<span>Delete</span>
</DropdownMenuItem>
</>
)}
</>
)}
</DropdownMenuContent>
@@ -3,6 +3,7 @@ import type { CollectionInfo } from '@shumai/dtos'
import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos'
import type { SearchCondition, SearchSort } from '@shumai/dtos'
import { client } from '@/ui/api/client'
import { usePermissions } from '@/ui/hooks/use-permissions'
import { useMutation, useQuery } from '@tanstack/react-query'
import type { InferRequestType, InferResponseType } from 'hono/client'
import { useState } from 'react'
@@ -48,6 +49,7 @@ export function FileBrowserToolbar({
onUpdateCollection,
rootFolderId,
}: FileBrowserToolbarProps) {
const { canEdit } = usePermissions()
const [popoverOpen, setPopoverOpen] = useState(false)
const [searchDialogOpen, setSearchDialogOpen] = useState(false)
const [manageDialogOpen, setManageDialogOpen] = useState(false)
@@ -156,7 +158,7 @@ export function FileBrowserToolbar({
<PopoverContent className="w-auto p-0" align="start">
<FieldsManager
projectId={projectId}
onManageFields={handleManageFields}
onManageFields={canEdit ? handleManageFields : undefined}
onSave={() => setPopoverOpen(false)}
/>
</PopoverContent>