feat(collection): support source folder and selector in collections

This commit is contained in:
yiling
2026-05-25 20:26:03 +08:00
parent 00dc7f7bda
commit 5037b27d81
10 changed files with 238 additions and 131 deletions
+24 -9
View File
@@ -24,8 +24,11 @@ describe('Collection API', () => {
const mockCreate = vi.spyOn(collectionService, 'createCollection').mockResolvedValue({
id: collectionId,
name: 'New Collection',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
filter: { conditions: [], operator: 'AND', recursively: true } as any,
filter: {
sourceFolderId: 'root-1',
searchFilter: { conditions: [], operator: 'AND', recursively: true },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
projectId,
createdAt: new Date(),
updatedAt: new Date(),
@@ -40,7 +43,10 @@ describe('Collection API', () => {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'New Collection',
filter: { conditions: [], operator: 'AND', recursively: true },
filter: {
sourceFolderId: 'root-1',
searchFilter: { conditions: [], operator: 'AND', recursively: true },
},
}),
},
{
@@ -60,8 +66,11 @@ describe('Collection API', () => {
{
id: collectionId,
name: 'Col 1',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
filter: { conditions: [], operator: 'AND', recursively: true } as any,
filter: {
sourceFolderId: 'root-1',
searchFilter: { conditions: [], operator: 'AND', recursively: true },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
projectId,
createdAt: new Date(),
updatedAt: new Date(),
@@ -85,8 +94,11 @@ describe('Collection API', () => {
const mockGet = vi.spyOn(collectionService, 'getCollection').mockResolvedValue({
id: collectionId,
name: 'Col 1',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
filter: { conditions: [], operator: 'AND', recursively: true } as any,
filter: {
sourceFolderId: 'root-1',
searchFilter: { conditions: [], operator: 'AND', recursively: true },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
projectId,
createdAt: new Date(),
updatedAt: new Date(),
@@ -107,8 +119,11 @@ describe('Collection API', () => {
const mockUpdate = vi.spyOn(collectionService, 'updateCollection').mockResolvedValue({
id: collectionId,
name: 'Updated Name',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
filter: { conditions: [], operator: 'AND', recursively: true } as any,
filter: {
sourceFolderId: 'root-1',
searchFilter: { conditions: [], operator: 'AND', recursively: true },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
projectId,
createdAt: new Date(),
updatedAt: new Date(),
+2 -2
View File
@@ -7,9 +7,9 @@ import {
updateCollectionRequestSchema,
listCollectionsRequestSchema,
CollectionInfo,
CollectionFilter,
} from '@/dtos/collection'
import type { Prisma } from '@/generated/prisma/client'
import type { SearchFilter } from '@/dtos/search'
type User = Prisma.UserGetPayload<Record<string, never>>
@@ -17,7 +17,7 @@ function toCollectionInfo(c: Prisma.CollectionGetPayload<Record<string, never>>)
return {
id: c.id,
name: c.name,
filter: c.filter as unknown as SearchFilter,
filter: c.filter as unknown as CollectionFilter,
projectId: c.projectId,
createdAt: c.createdAt.toISOString(),
updatedAt: c.updatedAt.toISOString(),
+10 -3
View File
@@ -2,10 +2,17 @@ import { z } from 'zod'
import { searchFilterSchema } from './search'
import { paginationParamsSchema } from './pagination'
export const collectionFilterSchema = z.object({
sourceFolderId: z.string(),
searchFilter: searchFilterSchema,
})
export type CollectionFilter = z.infer<typeof collectionFilterSchema>
export const collectionSchema = z.object({
id: z.string(),
name: z.string(),
filter: searchFilterSchema,
filter: collectionFilterSchema,
projectId: z.string(),
createdAt: z.string(),
updatedAt: z.string(),
@@ -15,14 +22,14 @@ export type CollectionInfo = z.infer<typeof collectionSchema>
export const createCollectionRequestSchema = z.object({
name: z.string(),
filter: searchFilterSchema,
filter: collectionFilterSchema,
})
export type CreateCollectionRequest = z.infer<typeof createCollectionRequestSchema>
export const updateCollectionRequestSchema = z.object({
name: z.string().optional(),
filter: searchFilterSchema.optional(),
filter: collectionFilterSchema.optional(),
})
export type UpdateCollectionRequest = z.infer<typeof updateCollectionRequestSchema>
+1 -1
View File
@@ -52,7 +52,7 @@ declare global {
// ----------------------------------------------------------------------
// Collection Filter
// ----------------------------------------------------------------------
export type CollectionFilter = import('./dtos/search').SearchFilter
export type CollectionFilter = import('./dtos/collection').CollectionFilter
export type ImageTranscodeStrategy = 'disable' | 'single'
export interface TranscodeSettings {
+22 -10
View File
@@ -43,14 +43,17 @@ describe('CollectionService', () => {
const collection = await collectionService.createCollection(project.id, {
name: 'Test Collection',
filter: {
operator: 'AND',
conditions: [{ field: 'name', operator: 'contains', value: 'test' }],
recursively: true,
sourceFolderId: project.rootFolder!,
searchFilter: {
operator: 'AND',
conditions: [{ field: 'name', operator: 'contains', value: 'test' }],
recursively: true,
},
},
})
expect(collection.name).toBe('Test Collection')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((collection.filter as any).conditions[0].value).toBe('test')
expect((collection.filter as any).searchFilter.conditions[0].value).toBe('test')
// Get
const fetched = await collectionService.getCollection(collection.id)
@@ -60,14 +63,17 @@ describe('CollectionService', () => {
const updated = await collectionService.updateCollection(collection.id, {
name: 'Updated Name',
filter: {
operator: 'AND',
conditions: [{ field: 'rating', operator: 'eq', value: 5 }],
recursively: true,
sourceFolderId: project.rootFolder!,
searchFilter: {
operator: 'AND',
conditions: [{ field: 'rating', operator: 'eq', value: 5 }],
recursively: true,
},
},
})
expect(updated.name).toBe('Updated Name')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((updated.filter as any).conditions[0].field).toBe('rating')
expect((updated.filter as any).searchFilter.conditions[0].field).toBe('rating')
// Delete
await collectionService.deleteCollection(collection.id)
@@ -82,7 +88,10 @@ describe('CollectionService', () => {
for (let i = 0; i < 5; i++) {
await collectionService.createCollection(project.id, {
name: `Collection ${i}`,
filter: { operator: 'AND', conditions: [], recursively: true },
filter: {
sourceFolderId: project.rootFolder!,
searchFilter: { operator: 'AND', conditions: [], recursively: true },
},
})
}
@@ -117,7 +126,10 @@ describe('CollectionService', () => {
await collectionService.createCollection(project.id, {
name: 'To be deleted',
filter: { operator: 'AND', conditions: [], recursively: true },
filter: {
sourceFolderId: project.rootFolder!,
searchFilter: { operator: 'AND', conditions: [], recursively: true },
},
})
const countBefore = await prisma.collection.count({
+96 -35
View File
@@ -1,6 +1,7 @@
import { type FieldInfo as MetadataFieldInfo } from '@/dtos/metadata'
import type { SearchCondition, SearchSort } from '@/dtos/search'
import type { CollectionInfo } from '@/dtos/collection'
import type { AssetInfo } from '@/dtos/asset'
import { client } from '@/ui/api/client'
import { useMutation, useQuery } from '@tanstack/react-query'
import type { InferRequestType, InferResponseType } from 'hono/client'
@@ -11,6 +12,7 @@ import { MembersDialog } from '../members-dialog'
import { SearchFilterDialog } from '../search/search-filter-dialog'
import { FilterPanel } from '../search/filter-panel'
import { SortControl } from '../search/sort-control'
import { FolderTree } from '../folder-tree'
import { Avatar, AvatarFallback } from '../ui/avatar'
import { Button } from '../ui/button'
import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover'
@@ -48,10 +50,23 @@ export function FileBrowserToolbar({
const [searchDialogOpen, setSearchDialogOpen] = useState(false)
const [manageDialogOpen, setManageDialogOpen] = useState(false)
const [isMembersDialogOpen, setIsMembersDialogOpen] = useState(false)
const [isFolderSelectorOpen, setIsFolderSelectorOpen] = useState(false)
const activeFiltersCount = filterConditions.length
const isCollection = !!collection
const { data: folderInfo } = useQuery({
queryKey: ['folders', teamId, assetId],
queryFn: async () => {
const res = await client.api.teams[':teamId'].folders[':folderId'].$get({
param: { teamId: teamId, folderId: assetId },
})
if (!res.ok) throw new Error('failed to fetch folder')
return (await res.json()) as unknown as AssetInfo
},
enabled: isCollection && !!teamId && !!assetId,
})
const { data: members } = useQuery({
queryKey: ['projects', projectId, 'members'],
queryFn: async () => {
@@ -156,41 +171,87 @@ export function FileBrowserToolbar({
<Separator orientation="vertical" />
{isCollection ? (
<Popover>
<PopoverTrigger asChild>
<Button
variant={activeFiltersCount > 0 ? 'secondary' : 'ghost'}
size="sm"
className="inline-flex items-center gap-2 px-4 py-2 hover:bg-primary/10 font-semibold rounded-xl cursor-pointer h-8"
>
<span>Filter</span>
{activeFiltersCount > 0 && (
<span className="ml-1 px-1.5 py-0.5 rounded-full text-[10px] font-mono font-bold bg-primary/50 border border-primary-foreground/20">
{activeFiltersCount}
</span>
)}
</Button>
</PopoverTrigger>
<PopoverContent className="w-[500px] p-0 overflow-hidden" align="start">
<div className="max-h-[80vh] overflow-y-auto">
<FilterPanel
fields={fields}
conditions={filterConditions}
onChange={(newConditions) => {
onFilterChange(newConditions)
onUpdateCollection?.({
filter: {
...collection.filter,
conditions: newConditions,
},
})
}}
excludeFields={[]}
hidePrefix
/>
</div>
</PopoverContent>
</Popover>
<>
<Popover open={isFolderSelectorOpen} onOpenChange={setIsFolderSelectorOpen}>
<PopoverTrigger asChild>
<Button
variant="ghost"
size="sm"
className="inline-flex items-center gap-2 px-3 py-2 hover:bg-primary/10 font-semibold rounded-xl cursor-pointer h-8 text-muted-foreground"
>
<span className="truncate max-w-[150px]">{folderInfo?.name || 'Loading...'}</span>
</Button>
</PopoverTrigger>
<PopoverContent className="w-[300px] p-0" align="start">
<div className="h-[400px] flex flex-col">
<div className="p-2 border-b text-xs font-semibold text-muted-foreground uppercase tracking-wider">
Select Source Folder
</div>
<div className="flex-1 overflow-y-auto">
<FolderTree
teamId={teamId}
projectId={projectId}
projectName=""
rootFolderId={collection.filter.sourceFolderId} // This is not ideal but FolderTree handles subtrees
onSelect={(folder: AssetInfo) => {
onUpdateCollection?.({
filter: {
...collection.filter,
sourceFolderId: folder.id,
},
})
setIsFolderSelectorOpen(false)
}}
selectedFolderId={assetId}
hideCollections
hideShares
/>
</div>
</div>
</PopoverContent>
</Popover>
<Separator orientation="vertical" />
<Popover>
<PopoverTrigger asChild>
<Button
variant={activeFiltersCount > 0 ? 'secondary' : 'ghost'}
size="sm"
className="inline-flex items-center gap-2 px-4 py-2 hover:bg-primary/10 font-semibold rounded-xl cursor-pointer h-8"
>
<span>Filter</span>
{activeFiltersCount > 0 && (
<span className="ml-1 px-1.5 py-0.5 rounded-full text-[10px] font-mono font-bold bg-primary/50 border border-primary-foreground/20">
{activeFiltersCount}
</span>
)}
</Button>
</PopoverTrigger>
<PopoverContent className="w-[500px] p-0 overflow-hidden" align="start">
<div className="max-h-[80vh] overflow-y-auto">
<FilterPanel
fields={fields}
conditions={filterConditions}
onChange={(newConditions) => {
onFilterChange(newConditions)
onUpdateCollection?.({
filter: {
...collection.filter,
searchFilter: {
...collection.filter.searchFilter,
conditions: newConditions,
},
},
})
}}
excludeFields={[]}
hidePrefix
/>
</div>
</PopoverContent>
</Popover>
</>
) : (
<Button
onClick={() => setSearchDialogOpen(true)}
+1 -1
View File
@@ -96,7 +96,7 @@ export default function FileSystemManager({
}, [teamId, fetchMembers, fetchMetadata])
const initialFilterConditions = useMemo(() => {
return collection?.filter?.conditions || []
return collection?.filter?.searchFilter?.conditions || []
}, [collection])
const [filterConditions, setFilterConditions] =
+74 -67
View File
@@ -11,7 +11,6 @@ import {
Loader2,
Trash2,
Plus,
MoreHorizontal,
Share2,
LayoutGrid,
Bookmark,
@@ -32,6 +31,8 @@ interface FolderTreeProps {
dragState?: DragState
onSelect?: (folder: AssetInfo) => void
selectedFolderId?: string
hideCollections?: boolean
hideShares?: boolean
}
export function FolderTree({
@@ -42,6 +43,8 @@ export function FolderTree({
dragState,
onSelect,
selectedFolderId,
hideCollections,
hideShares,
}: FolderTreeProps) {
const navigate = useNavigate()
const queryClient = useQueryClient()
@@ -83,8 +86,11 @@ export function FolderTree({
json: {
name: 'Untitled Collection',
filter: {
conditions: [],
recursively: true,
sourceFolderId: rootFolderId,
searchFilter: {
conditions: [],
recursively: true,
},
},
},
})
@@ -186,78 +192,79 @@ export function FolderTree({
</div>
</div>
<div>
<header className="flex items-center justify-between px-2 mb-1">
<h3 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider">
Collections
</h3>
<button
onClick={() => createCollection()}
className="text-muted-foreground hover:text-foreground"
>
<Plus className="h-3.5 w-3.5" />
</button>
</header>
<div className="space-y-0.5">
{collectionsData?.data.map((collection) => (
<div
key={collection.id}
className="group flex cursor-pointer items-center gap-2 rounded-md px-2 py-1.5 text-sm transition-colors hover:bg-sidebar-accent hover:text-sidebar-accent-foreground"
onClick={() =>
navigate({
to: '/projects/$projectId/collections/$collectionId',
params: { projectId, collectionId: collection.id },
})
}
>
<div className="flex h-4 w-4 items-center justify-center">
<Bookmark className="h-4 w-4 text-sidebar-primary" />
</div>
<span className="flex-1 truncate text-sidebar-foreground">{collection.name}</span>
</div>
))}
</div>
</div>
<div>
<header className="flex items-center justify-between px-2 mb-1">
<h3 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider">
Share Links
</h3>
<div className="flex items-center gap-1">
<button className="text-muted-foreground hover:text-foreground">
<MoreHorizontal className="h-3.5 w-3.5" />
</button>
{!hideCollections && (
<div>
<header className="flex items-center justify-between px-2 mb-1">
<h3 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider">
Collections
</h3>
<button
onClick={() => createShareLink()}
onClick={() => createCollection()}
className="text-muted-foreground hover:text-foreground"
>
<Plus className="h-3.5 w-3.5" />
</button>
</div>
</header>
</header>
<div className="space-y-0.5">
<div className="group flex cursor-pointer items-center gap-2 rounded-md px-2 py-1.5 text-sm transition-colors hover:bg-sidebar-accent hover:text-sidebar-accent-foreground">
<div className="flex h-4 w-4 items-center justify-center">
<LayoutGrid className="h-4 w-4 text-sidebar-primary" />
</div>
<span className="flex-1 truncate text-sidebar-foreground">
All Share Links ({shareLinks.length})
</span>
<div className="space-y-0.5">
{collectionsData?.data.map((collection) => (
<div
key={collection.id}
className="group flex cursor-pointer items-center gap-2 rounded-md px-2 py-1.5 text-sm transition-colors hover:bg-sidebar-accent hover:text-sidebar-accent-foreground"
onClick={() =>
navigate({
to: '/projects/$projectId/collections/$collectionId',
params: { projectId, collectionId: collection.id },
})
}
>
<div className="flex h-4 w-4 items-center justify-center">
<Bookmark className="h-4 w-4 text-sidebar-primary" />
</div>
<span className="flex-1 truncate text-sidebar-foreground">{collection.name}</span>
</div>
))}
</div>
{shareLinks.map((link) => (
<ShareLinkItem
key={link.id}
link={link}
projectId={projectId}
dragState={dragState}
/>
))}
</div>
</div>
)}
{!hideShares && (
<div>
<header className="flex items-center justify-between px-2 mb-1">
<h3 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider">
Share Links
</h3>
<div className="flex items-center gap-1">
<button
onClick={() => createShareLink()}
className="text-muted-foreground hover:text-foreground"
>
<Plus className="h-3.5 w-3.5" />
</button>
</div>
</header>
<div className="space-y-0.5">
<div className="group flex cursor-pointer items-center gap-2 rounded-md px-2 py-1.5 text-sm transition-colors hover:bg-sidebar-accent hover:text-sidebar-accent-foreground">
<div className="flex h-4 w-4 items-center justify-center">
<LayoutGrid className="h-4 w-4 text-sidebar-primary" />
</div>
<span className="flex-1 truncate text-sidebar-foreground">
All Share Links ({shareLinks.length})
</span>
</div>
{shareLinks.map((link) => (
<ShareLinkItem
key={link.id}
link={link}
projectId={projectId}
dragState={dragState}
/>
))}
</div>
</div>
)}
</div>
</div>
)
@@ -113,8 +113,11 @@ export function SearchFilterDialog({
json: {
name,
filter: {
conditions: combinedConditions,
recursively: true,
sourceFolderId: assetId,
searchFilter: {
conditions: combinedConditions,
recursively: true,
},
},
},
})
@@ -79,12 +79,14 @@ function CollectionPage() {
return <div>Loading collection...</div>
}
const sourceFolderId = collection.filter.sourceFolderId || rootFolderId
return (
<FileSystemManager
teamId={teamId}
projectId={projectId}
projectName={projectInfo.name ?? ''}
assetId={rootFolderId}
assetId={sourceFolderId}
rootFolderId={rootFolderId}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
collection={collection as any}