mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 22:14:50 +00:00
refactor(monorepo): decouple api and webui into workspaces packages
This commit is contained in:
@@ -0,0 +1,406 @@
|
||||
import type { SearchCondition, SearchConditionOperator } from '@shumai/dtos'
|
||||
import { type FieldInfo } from '@shumai/dtos'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import { Input } from '@/ui/components/ui/input'
|
||||
import { DebouncedInput } from '@/ui/components/ui/debounced-input'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/ui/components/ui/select'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { Trash2 } from 'lucide-react'
|
||||
|
||||
interface FilterPanelProps {
|
||||
fields: FieldInfo[]
|
||||
conditions: SearchCondition[]
|
||||
onChange: (conditions: SearchCondition[]) => void
|
||||
className?: string
|
||||
excludeFields?: string[]
|
||||
hidePrefix?: boolean
|
||||
}
|
||||
|
||||
const SYSTEM_FIELDS = [
|
||||
{ id: 'name', label: 'Name', type: 'text' },
|
||||
{ id: 'createdAt', label: 'Created At', type: 'date' },
|
||||
{ id: 'updatedAt', label: 'Updated At', type: 'date' },
|
||||
{ id: 'sizeByte', label: 'Size', type: 'number' },
|
||||
]
|
||||
|
||||
export function FilterPanel({
|
||||
fields,
|
||||
conditions,
|
||||
onChange,
|
||||
className,
|
||||
excludeFields,
|
||||
hidePrefix,
|
||||
}: FilterPanelProps) {
|
||||
const allFields = [
|
||||
...SYSTEM_FIELDS,
|
||||
...fields.map((f) => ({
|
||||
id: f.id!,
|
||||
label: f.config?.name || f.description || 'Unknown',
|
||||
type: getFieldType(f),
|
||||
options: f.config?.select?.options,
|
||||
})),
|
||||
].filter((f) => !excludeFields?.includes(f.id))
|
||||
|
||||
const handleAddCondition = () => {
|
||||
onChange([
|
||||
...conditions,
|
||||
{
|
||||
field: allFields[0]?.id || 'name',
|
||||
operator: 'eq',
|
||||
value: '',
|
||||
},
|
||||
])
|
||||
}
|
||||
|
||||
const handleRemoveCondition = (index: number) => {
|
||||
const newConditions = [...conditions]
|
||||
newConditions.splice(index, 1)
|
||||
onChange(newConditions)
|
||||
}
|
||||
|
||||
const handleConditionChange = (index: number, key: keyof SearchCondition, value: unknown) => {
|
||||
const newConditions = [...conditions]
|
||||
newConditions[index] = { ...newConditions[index], [key]: value } as SearchCondition
|
||||
|
||||
// Reset operator/value if field changes
|
||||
if (key === 'field') {
|
||||
const field = allFields.find((f) => f.id === value)
|
||||
if (field) {
|
||||
newConditions[index].operator = getDefaultOperator(field.type)
|
||||
newConditions[index].value = ''
|
||||
}
|
||||
}
|
||||
|
||||
onChange(newConditions)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn('p-2 space-y-4 min-w-[100px]', className)}>
|
||||
{conditions.map((condition, index) => (
|
||||
<div key={index} className="flex items-center gap-2">
|
||||
{!hidePrefix && (
|
||||
<div className="w-16 text-sm text-muted-foreground font-medium text-right">
|
||||
{index === 0 ? 'Where' : 'and'}
|
||||
</div>
|
||||
)}
|
||||
<div className="flex-1 flex items-center gap-2">
|
||||
<Select
|
||||
value={condition.field}
|
||||
onValueChange={(val) => handleConditionChange(index, 'field', val)}
|
||||
>
|
||||
<SelectTrigger className="w-[180px]">
|
||||
<SelectValue placeholder="Select field" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{allFields.map((field) => (
|
||||
<SelectItem key={field.id} value={field.id}>
|
||||
{field.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
|
||||
<Select
|
||||
value={condition.operator}
|
||||
onValueChange={(val) => handleConditionChange(index, 'operator', val)}
|
||||
>
|
||||
<SelectTrigger className="w-[140px]">
|
||||
<SelectValue placeholder="Operator" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{getOperators(
|
||||
allFields.find((f) => f.id === condition.field) || {
|
||||
id: '',
|
||||
label: '',
|
||||
type: 'text',
|
||||
},
|
||||
).map((op) => (
|
||||
<SelectItem key={op.value} value={op.value}>
|
||||
{op.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
|
||||
<div className="flex-1">
|
||||
{condition.operator !== 'isEmpty' && condition.operator !== 'isNotEmpty' && (
|
||||
<ConditionValueInput
|
||||
field={allFields.find((f) => f.id === condition.field)}
|
||||
value={condition.value}
|
||||
onChange={(val) => handleConditionChange(index, 'value', val)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Button variant="ghost" size="icon" onClick={() => handleRemoveCondition(index)}>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="text-muted-foreground hover:text-foreground"
|
||||
onClick={handleAddCondition}
|
||||
>
|
||||
+ Add condition
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function getFieldType(field: FieldInfo): string {
|
||||
if (field.config?.type === 'select') return 'select'
|
||||
if (field.config?.type === 'selectMulti') return 'selectMulti'
|
||||
if (field.config?.type === 'number') return 'number'
|
||||
if (field.config?.type === 'date') return 'date'
|
||||
if (field.config?.type === 'rating') return 'number'
|
||||
if (field.config?.type === 'toggle') return 'toggle'
|
||||
return 'text'
|
||||
}
|
||||
|
||||
function getDefaultOperator(type: string): SearchConditionOperator {
|
||||
switch (type) {
|
||||
case 'selectMulti':
|
||||
return 'hasAny' as SearchConditionOperator
|
||||
case 'number':
|
||||
case 'date':
|
||||
return 'eq'
|
||||
default:
|
||||
return 'eq' as SearchConditionOperator
|
||||
}
|
||||
}
|
||||
|
||||
function getOperators(field: { id: string; label: string; type: string }): {
|
||||
value: SearchConditionOperator
|
||||
label: string
|
||||
}[] {
|
||||
const type = field.type
|
||||
const id = field.id
|
||||
|
||||
if (id === 'name') {
|
||||
return [
|
||||
{ value: 'eq' as SearchConditionOperator, label: 'is' },
|
||||
{ value: 'neq' as SearchConditionOperator, label: 'is not' },
|
||||
{ value: 'contains' as SearchConditionOperator, label: 'contains' },
|
||||
{ value: 'notContains' as SearchConditionOperator, label: 'does not contain' },
|
||||
{ value: 'isEmpty' as SearchConditionOperator, label: 'is empty' },
|
||||
{ value: 'isNotEmpty' as SearchConditionOperator, label: 'is not empty' },
|
||||
]
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case 'text':
|
||||
return [
|
||||
{ value: 'eq' as SearchConditionOperator, label: 'is' },
|
||||
{ value: 'neq' as SearchConditionOperator, label: 'is not' },
|
||||
{ value: 'isEmpty' as SearchConditionOperator, label: 'is empty' },
|
||||
{ value: 'isNotEmpty' as SearchConditionOperator, label: 'is not empty' },
|
||||
]
|
||||
case 'number':
|
||||
return [
|
||||
{ value: 'eq' as SearchConditionOperator, label: '=' },
|
||||
{ value: 'neq' as SearchConditionOperator, label: '≠' },
|
||||
{ value: 'gt' as SearchConditionOperator, label: '>' },
|
||||
{ value: 'lt' as SearchConditionOperator, label: '<' },
|
||||
{ value: 'lte' as SearchConditionOperator, label: '≤' },
|
||||
{ value: 'gte' as SearchConditionOperator, label: '≥' },
|
||||
{ value: 'isEmpty' as SearchConditionOperator, label: 'is empty' },
|
||||
{ value: 'isNotEmpty' as SearchConditionOperator, label: 'is not empty' },
|
||||
]
|
||||
case 'select':
|
||||
return [
|
||||
{ value: 'eq' as SearchConditionOperator, label: 'is' },
|
||||
{ value: 'neq' as SearchConditionOperator, label: 'is not' },
|
||||
{ value: 'in' as SearchConditionOperator, label: 'is any of' },
|
||||
{ value: 'notIn' as SearchConditionOperator, label: 'is none of' },
|
||||
{ value: 'isEmpty' as SearchConditionOperator, label: 'is empty' },
|
||||
{ value: 'isNotEmpty' as SearchConditionOperator, label: 'is not empty' },
|
||||
]
|
||||
case 'selectMulti':
|
||||
return [
|
||||
{ value: 'hasAny' as SearchConditionOperator, label: 'has any of' },
|
||||
{ value: 'hasAll' as SearchConditionOperator, label: 'has all of' },
|
||||
{ value: 'eq' as SearchConditionOperator, label: 'is exactly' },
|
||||
{ value: 'hasNone' as SearchConditionOperator, label: 'has none of' },
|
||||
{ value: 'isEmpty' as SearchConditionOperator, label: 'is empty' },
|
||||
{ value: 'isNotEmpty' as SearchConditionOperator, label: 'is not empty' },
|
||||
]
|
||||
case 'date':
|
||||
return [
|
||||
{ value: 'eq' as SearchConditionOperator, label: 'is' },
|
||||
{ value: 'isWithin' as SearchConditionOperator, label: 'is within' },
|
||||
{ value: 'lt' as SearchConditionOperator, label: 'is before' },
|
||||
{ value: 'gt' as SearchConditionOperator, label: 'is after' },
|
||||
{ value: 'lte' as SearchConditionOperator, label: 'is on or before' },
|
||||
{ value: 'gte' as SearchConditionOperator, label: 'is on or after' },
|
||||
{ value: 'neq' as SearchConditionOperator, label: 'is not' },
|
||||
{ value: 'isEmpty' as SearchConditionOperator, label: 'is empty' },
|
||||
{ value: 'isNotEmpty' as SearchConditionOperator, label: 'is not empty' },
|
||||
]
|
||||
case 'toggle':
|
||||
return [{ value: 'eq' as SearchConditionOperator, label: 'is' }]
|
||||
default:
|
||||
return [{ value: 'eq' as SearchConditionOperator, label: 'is' }]
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type FieldType = any
|
||||
|
||||
function ConditionValueInput({
|
||||
field,
|
||||
value,
|
||||
onChange,
|
||||
}: {
|
||||
field: FieldType
|
||||
value: unknown
|
||||
onChange: (val: unknown) => void
|
||||
}) {
|
||||
if (!field) return <Input disabled />
|
||||
|
||||
if (field.type === 'select' && field.options) {
|
||||
return (
|
||||
<Select value={value as string} onValueChange={onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Select option" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{field.options.map((opt: FieldType) => (
|
||||
<SelectItem key={opt.id} value={opt.id}>
|
||||
{opt.displayName}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
)
|
||||
}
|
||||
|
||||
if (field.type === 'selectMulti' && field.options) {
|
||||
const selected = Array.isArray(value) ? value : []
|
||||
return (
|
||||
<Select
|
||||
value={selected[0] || ''}
|
||||
onValueChange={(val) => {
|
||||
if (selected.includes(val)) {
|
||||
onChange(selected.filter((v) => v !== val))
|
||||
} else {
|
||||
onChange([...selected, val])
|
||||
}
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue
|
||||
placeholder={selected.length > 0 ? `${selected.length} selected` : 'Select options'}
|
||||
/>
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{field.options.map((opt: FieldType) => (
|
||||
<SelectItem key={opt.id} value={opt.id}>
|
||||
<div className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selected.includes(opt.id)}
|
||||
readOnly
|
||||
className="pointer-events-none"
|
||||
/>
|
||||
{opt.displayName}
|
||||
</div>
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
)
|
||||
}
|
||||
|
||||
if (field.type === 'date') {
|
||||
const relativeDates = [
|
||||
{ value: 'today', label: 'Today' },
|
||||
{ value: 'yesterday', label: 'Yesterday' },
|
||||
{ value: 'tomorrow', label: 'Tomorrow' },
|
||||
{ value: 'one week ago', label: 'One week ago' },
|
||||
{ value: 'one week from now', label: 'One week from now' },
|
||||
{ value: 'one month ago', label: 'One month ago' },
|
||||
{ value: 'one month from now', label: 'One month from now' },
|
||||
]
|
||||
|
||||
const isRelative = relativeDates.some((rd) => rd.value === value)
|
||||
|
||||
return (
|
||||
<div className="flex gap-2">
|
||||
<Select
|
||||
value={isRelative ? (value as string) : 'exact'}
|
||||
onValueChange={(val) => {
|
||||
if (val === 'exact') onChange('')
|
||||
else onChange(val)
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className="w-[140px]">
|
||||
<SelectValue placeholder="Date type" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{relativeDates.map((rd) => (
|
||||
<SelectItem key={rd.value} value={rd.value}>
|
||||
{rd.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
<SelectItem value="exact">Exact date...</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{!isRelative && (
|
||||
<Input
|
||||
type="date"
|
||||
className="flex-1"
|
||||
value={value as string}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (field.type === 'number') {
|
||||
return (
|
||||
<DebouncedInput
|
||||
type="number"
|
||||
value={value as string}
|
||||
onChange={(val) => onChange(Number(val))}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
if (field.type === 'toggle') {
|
||||
return (
|
||||
<Select
|
||||
value={value === true ? 'true' : 'false'}
|
||||
onValueChange={(val) => onChange(val === 'true')}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="true">Checked</SelectItem>
|
||||
<SelectItem value="false">Unchecked</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<DebouncedInput
|
||||
type="text"
|
||||
placeholder="Enter a value"
|
||||
value={value as string}
|
||||
onChange={onChange}
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,511 @@
|
||||
import { type AssetInfo } from '@shumai/dtos'
|
||||
import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos'
|
||||
import type { SearchCondition } from '@shumai/dtos'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/ui/components/ui/dialog'
|
||||
import { Input } from '@/ui/components/ui/input'
|
||||
import { Label } from '@/ui/components/ui/label'
|
||||
import { Switch } from '@/ui/components/ui/switch'
|
||||
import { formatSize } from '@/ui/lib/format'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useNavigate } from '@tanstack/react-router'
|
||||
import {
|
||||
AlertCircle,
|
||||
ChevronDown,
|
||||
ChevronUp,
|
||||
FileIcon,
|
||||
FolderIcon,
|
||||
Loader2,
|
||||
Search,
|
||||
X,
|
||||
} from 'lucide-react'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
import { FilterPanel } from './filter-panel'
|
||||
|
||||
interface SearchFilterDialogProps {
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
teamId: string
|
||||
projectId: string
|
||||
assetId: string
|
||||
fields: MetadataFieldInfo[]
|
||||
initialConditions: SearchCondition[]
|
||||
onApply: (conditions: SearchCondition[]) => void
|
||||
}
|
||||
|
||||
export function SearchFilterDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
teamId,
|
||||
projectId,
|
||||
assetId,
|
||||
fields,
|
||||
initialConditions,
|
||||
onApply,
|
||||
}: SearchFilterDialogProps) {
|
||||
const navigate = useNavigate()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const initialKeyword = useMemo(() => {
|
||||
const cond = initialConditions.find((c) => c.field === 'name' && c.operator === 'contains')
|
||||
return cond ? String(cond.value) : ''
|
||||
}, [initialConditions])
|
||||
|
||||
const initialOtherConditions = useMemo(() => {
|
||||
return initialConditions.filter((c) => !(c.field === 'name' && c.operator === 'contains'))
|
||||
}, [initialConditions])
|
||||
|
||||
const [conditions, setConditions] = useState<SearchCondition[]>(initialOtherConditions)
|
||||
const [isFiltersExpanded, setIsFiltersExpanded] = useState(true)
|
||||
const [searchInput, setSearchInput] = useState(initialKeyword)
|
||||
const [triggerQuery, setTriggerQuery] = useState(initialKeyword)
|
||||
const [isSemantic, setIsSemantic] = useState<boolean>(false)
|
||||
|
||||
const { data: teamSettings } = useQuery({
|
||||
queryKey: ['team-settings', teamId],
|
||||
queryFn: async () => {
|
||||
const res = await client.api.teams[':teamId'].settings.$get({
|
||||
param: { teamId },
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to fetch team settings')
|
||||
return (await res.json()) as { semanticSearchEnabled: boolean }
|
||||
},
|
||||
enabled: open,
|
||||
})
|
||||
|
||||
const semanticSearchEnabled = teamSettings?.semanticSearchEnabled ?? false
|
||||
|
||||
// Sync back if changed from outside, clear on close
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setSearchInput(initialKeyword)
|
||||
setTriggerQuery(initialKeyword)
|
||||
setConditions(initialOtherConditions)
|
||||
setIsSemantic(false)
|
||||
} else {
|
||||
setSearchInput('')
|
||||
setTriggerQuery('')
|
||||
setConditions([])
|
||||
setIsSemantic(false)
|
||||
}
|
||||
}, [open, initialKeyword, initialOtherConditions])
|
||||
|
||||
const combinedConditions = useMemo(() => {
|
||||
const result: SearchCondition[] = [...conditions]
|
||||
const activeText = triggerQuery
|
||||
if (activeText.trim() && !isSemantic) {
|
||||
result.push({
|
||||
field: 'name',
|
||||
operator: 'contains',
|
||||
value: activeText.trim(),
|
||||
})
|
||||
}
|
||||
return result
|
||||
}, [isSemantic, triggerQuery, conditions])
|
||||
|
||||
const hasActiveCriteria = useMemo(() => {
|
||||
const activeText = triggerQuery
|
||||
return activeText.trim() !== '' || conditions.some((c) => String(c.value || '').trim() !== '')
|
||||
}, [triggerQuery, conditions])
|
||||
|
||||
const { mutate: saveAsCollection, isPending: isSaving } = useMutation({
|
||||
mutationFn: async () => {
|
||||
const activeText = triggerQuery
|
||||
let name = activeText.trim()
|
||||
if (!name && conditions.length > 0) {
|
||||
const first = conditions[0]
|
||||
const field = fields.find((f) => f.id === first.field)
|
||||
const fieldName = field?.config?.name || first.field
|
||||
name = `${fieldName} ${first.operator} ${first.value}`
|
||||
}
|
||||
if (!name) name = 'Untitled Collection'
|
||||
|
||||
const res = await client.api.projects[':projectId'].collections.$post({
|
||||
param: { projectId },
|
||||
json: {
|
||||
name,
|
||||
filter: {
|
||||
sourceFolderId: assetId,
|
||||
searchFilter: {
|
||||
conditions: combinedConditions,
|
||||
recursively: true,
|
||||
query: activeText.trim(),
|
||||
isSemantic: isSemantic,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to save collection')
|
||||
return await res.json()
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['collections', projectId] })
|
||||
toast.success('Collection saved')
|
||||
onOpenChange(false)
|
||||
navigate({
|
||||
to: '/projects/$projectId/collections/$collectionId',
|
||||
params: { projectId, collectionId: data.id },
|
||||
})
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(`Error: ${err.message}`)
|
||||
},
|
||||
})
|
||||
|
||||
const {
|
||||
data: searchResults,
|
||||
isLoading,
|
||||
error: searchError,
|
||||
} = useQuery({
|
||||
queryKey: ['search-preview', teamId, assetId, combinedConditions, isSemantic, triggerQuery],
|
||||
queryFn: async () => {
|
||||
const activeText = triggerQuery
|
||||
const res = await client.api.folders[':folderId'].search.$post({
|
||||
param: { folderId: assetId },
|
||||
json: {
|
||||
assetType: undefined,
|
||||
conditions: combinedConditions,
|
||||
recursively: true,
|
||||
query: activeText.trim(),
|
||||
isSemantic: isSemantic,
|
||||
first: 20,
|
||||
},
|
||||
})
|
||||
if (!res.ok) {
|
||||
const errorData = (await res.json().catch(() => ({}))) as Record<string, unknown>
|
||||
throw new Error((errorData.message as string) || 'Search failed')
|
||||
}
|
||||
return (await res.json()) as { data: AssetInfo[] }
|
||||
},
|
||||
enabled: open && hasActiveCriteria,
|
||||
staleTime: 500,
|
||||
retry: false,
|
||||
})
|
||||
|
||||
const handleApply = () => {
|
||||
onApply(combinedConditions)
|
||||
onOpenChange(false)
|
||||
}
|
||||
|
||||
const handleClearFilters = () => {
|
||||
setConditions([])
|
||||
setSearchInput('')
|
||||
setTriggerQuery('')
|
||||
setIsSemantic(false)
|
||||
}
|
||||
|
||||
const formatTimestamp = (seconds: number) => {
|
||||
const m = Math.floor(seconds / 60)
|
||||
const s = Math.floor(seconds % 60)
|
||||
return `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`
|
||||
}
|
||||
|
||||
const handleResultClick = (item: AssetInfo) => {
|
||||
if (item.type === 'folder') {
|
||||
navigate({
|
||||
to: '/projects/$projectId/folders/$folderId',
|
||||
params: { projectId, folderId: item.id! },
|
||||
})
|
||||
} else {
|
||||
navigate({
|
||||
to: '/projects/$projectId/files/$fileId',
|
||||
params: { projectId, fileId: item.versionStack ? item.versionStack.id : item.id! },
|
||||
search: { version: undefined },
|
||||
})
|
||||
}
|
||||
onOpenChange(false)
|
||||
}
|
||||
|
||||
const activeFiltersCount = conditions.filter((c) => c.value !== '').length
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="sm:max-w-[900px] p-0 gap-0 overflow-hidden bg-background border-border rounded-2xl shadow-2xl flex flex-col max-h-[90vh]">
|
||||
<DialogHeader className="px-6 pt-3 pb-0 text-left">
|
||||
<DialogTitle className="text-lg font-bold">Search</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="px-6 pb-6 pt-4 border-b border-border bg-muted/30 flex flex-col gap-3">
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault()
|
||||
setTriggerQuery(searchInput.trim())
|
||||
}}
|
||||
className="flex flex-col gap-3 mt-2"
|
||||
>
|
||||
<div className="relative">
|
||||
<Input
|
||||
value={searchInput}
|
||||
onChange={(e) => setSearchInput(e.target.value)}
|
||||
placeholder={
|
||||
isSemantic ? 'Search records by content...' : 'Search records by name...'
|
||||
}
|
||||
className="w-full pl-4 pr-24 py-3 bg-background border-border rounded-xl text-sm h-12 focus-visible:ring-2 focus-visible:ring-primary/20 shadow-sm"
|
||||
/>
|
||||
{searchInput && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setSearchInput('')
|
||||
setTriggerQuery('')
|
||||
}}
|
||||
className="absolute right-20 top-1/2 -translate-y-1/2 p-1 rounded-md text-muted-foreground hover:text-foreground transition-colors"
|
||||
>
|
||||
<X className="w-4 h-4" />
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
type="submit"
|
||||
className="absolute right-1.5 top-1.5 bottom-1.5 px-4 bg-primary hover:bg-primary/90 text-primary-foreground text-xs font-semibold rounded-lg flex items-center justify-center transition-colors shadow-sm"
|
||||
>
|
||||
Search
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-3 py-1">
|
||||
<div className="flex items-center gap-2 bg-background border border-border px-3 py-1.5 rounded-lg shadow-sm">
|
||||
<Switch
|
||||
id="semantic-search-switch"
|
||||
checked={isSemantic}
|
||||
onCheckedChange={(checked) => {
|
||||
setIsSemantic(checked)
|
||||
setSearchInput('')
|
||||
setTriggerQuery('')
|
||||
setConditions([])
|
||||
}}
|
||||
disabled={!semanticSearchEnabled}
|
||||
/>
|
||||
<Label
|
||||
htmlFor="semantic-search-switch"
|
||||
className={cn(
|
||||
'text-xs font-semibold select-none cursor-pointer',
|
||||
!semanticSearchEnabled && 'opacity-50 cursor-not-allowed',
|
||||
)}
|
||||
>
|
||||
Semantic Search
|
||||
</Label>
|
||||
</div>
|
||||
{!semanticSearchEnabled && (
|
||||
<span className="text-[10px] text-muted-foreground italic">
|
||||
* Semantic search requires an enabled embedding agent.
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div
|
||||
onClick={() => setIsFiltersExpanded(!isFiltersExpanded)}
|
||||
className="flex items-center justify-between py-1.5 px-1 mt-1 rounded-lg hover:bg-accent/50 cursor-pointer select-none transition-colors group"
|
||||
>
|
||||
<div className="flex items-center gap-2">
|
||||
{isFiltersExpanded ? (
|
||||
<ChevronUp className="w-4 h-4 text-muted-foreground group-hover:text-foreground" />
|
||||
) : (
|
||||
<ChevronDown className="w-4 h-4 text-muted-foreground group-hover:text-foreground" />
|
||||
)}
|
||||
<span className="text-xs font-semibold text-foreground/80">Filters</span>
|
||||
|
||||
{activeFiltersCount > 0 && (
|
||||
<span className="px-1.5 py-0.5 rounded-full text-[10px] font-mono font-bold bg-primary/10 text-primary border border-primary/20">
|
||||
{activeFiltersCount} active
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{(activeFiltersCount > 0 || searchInput) && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
handleClearFilters()
|
||||
}}
|
||||
className="text-[11px] font-medium text-destructive hover:text-destructive/80 underline underline-offset-2 transition-colors"
|
||||
>
|
||||
Reset
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{isFiltersExpanded && (
|
||||
<div className="mt-1 p-4 bg-muted/40 border border-border rounded-xl flex flex-col gap-3 animate-in slide-in-from-top-1 duration-200">
|
||||
<FilterPanel
|
||||
fields={fields}
|
||||
conditions={conditions}
|
||||
onChange={setConditions}
|
||||
excludeFields={['name']}
|
||||
hidePrefix={true}
|
||||
className="p-0 bg-transparent space-y-2"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Results Body ported from Demo */}
|
||||
<div className="flex-1 overflow-y-auto p-4 md:p-5 bg-background">
|
||||
<div className="flex items-center justify-between mb-3 px-1">
|
||||
<span className="text-[11px] font-bold uppercase tracking-wider text-muted-foreground/60 font-mono">
|
||||
Search Results
|
||||
</span>
|
||||
{isLoading && (
|
||||
<span className="inline-flex items-center gap-1.5 text-xs text-primary">
|
||||
<Loader2 className="h-3 w-3 animate-spin" />
|
||||
{isSemantic
|
||||
? 'Semantic search using media intelligence might be slow...'
|
||||
: 'Querying database...'}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col border border-border rounded-xl divide-y divide-border overflow-hidden">
|
||||
{!hasActiveCriteria ? (
|
||||
<div className="py-20 text-center flex flex-col items-center justify-center bg-muted/20 px-4">
|
||||
<Search className="w-10 h-10 text-muted-foreground/30 mb-2" />
|
||||
<h3 className="text-sm font-semibold text-foreground/80">Ready to Search</h3>
|
||||
<p className="text-xs text-muted-foreground max-w-sm mt-1 mx-auto">
|
||||
Type your query above and click the Search button to display results.
|
||||
</p>
|
||||
{isSemantic && (
|
||||
<p className="text-xs text-muted-foreground/80 max-w-md mt-4 mx-auto border-t border-border/60 pt-3">
|
||||
Semantic search results will be ordered by their relevance to the search query
|
||||
text, but won't be strictly filtered. If you need accurate filtering, please add
|
||||
filter conditions to control that.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
) : searchError ? (
|
||||
<div className="py-12 px-4 text-center flex flex-col items-center justify-center bg-muted/20">
|
||||
<AlertCircle className="w-10 h-10 text-destructive/50 mb-2" />
|
||||
<h3 className="text-sm font-semibold text-foreground/80">Search failed</h3>
|
||||
<p className="text-xs text-muted-foreground max-w-sm mt-1 mx-auto">
|
||||
{searchError.message.includes('Embedding agent not configured')
|
||||
? 'Please create and enable an embedding agent in the team settings to use semantic search.'
|
||||
: searchError.message}
|
||||
</p>
|
||||
</div>
|
||||
) : isLoading ? (
|
||||
Array.from({ length: 3 }).map((_, idx) => (
|
||||
<div key={idx} className="p-3.5 flex items-center gap-3 animate-pulse bg-muted/10">
|
||||
<div className="w-11 h-11 rounded-xl bg-muted shrink-0" />
|
||||
<div className="flex-1 space-y-2">
|
||||
<div className="h-4 bg-muted rounded w-1/3" />
|
||||
<div className="h-3 bg-muted/50 rounded w-1/4" />
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
) : searchResults?.data?.length === 0 ? (
|
||||
<div className="py-12 px-4 text-center flex flex-col items-center justify-center bg-muted/20">
|
||||
<AlertCircle className="w-10 h-10 text-muted-foreground/30 mb-2" />
|
||||
<h3 className="text-sm font-semibold text-foreground/80">
|
||||
No records matched your criteria
|
||||
</h3>
|
||||
<p className="text-xs text-muted-foreground max-w-sm mt-1 mx-auto">
|
||||
Try adjusting spelling or removing filter rows.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
searchResults?.data?.map((record) => (
|
||||
<div
|
||||
key={record.id}
|
||||
onClick={() => handleResultClick(record)}
|
||||
className="p-3.5 flex items-center gap-3 hover:bg-muted/40 transition-colors group/record cursor-pointer"
|
||||
>
|
||||
<div
|
||||
className={cn(
|
||||
'shrink-0 w-11 h-11 rounded-xl shadow-xs flex items-center justify-center text-white font-bold relative group-hover/record:scale-105 transition-transform bg-muted overflow-hidden',
|
||||
record.type === 'folder' ? 'bg-primary/5' : '',
|
||||
)}
|
||||
>
|
||||
{record.preview?.thumbnailUrl ? (
|
||||
<img
|
||||
src={record.preview.thumbnailUrl}
|
||||
alt=""
|
||||
className="w-full h-full object-cover rounded-xl"
|
||||
/>
|
||||
) : record.type === 'folder' ? (
|
||||
<FolderIcon className="h-6 w-6 text-primary/70" />
|
||||
) : (
|
||||
<FileIcon className="h-6 w-6 text-muted-foreground" />
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="truncate">
|
||||
<span className="font-medium text-sm text-foreground group-hover/record:text-primary transition-colors block truncate">
|
||||
{record.name}
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground block mt-0.5 truncate">
|
||||
{record.type === 'folder'
|
||||
? 'Folder'
|
||||
: `Size: ${formatSize(record.sizeByte || 0)}`}
|
||||
</span>
|
||||
{record.startTime !== undefined &&
|
||||
record.startTime !== null &&
|
||||
record.endTime !== undefined &&
|
||||
record.endTime !== null && (
|
||||
<span className="text-[10px] font-semibold text-primary bg-primary/10 border border-primary/20 px-1.5 py-0.5 rounded mt-1 inline-block">
|
||||
Match found at {formatTimestamp(record.startTime)} -{' '}
|
||||
{formatTimestamp(record.endTime)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<span className="text-xs text-muted-foreground font-mono shrink-0">
|
||||
{new Date(record.createdAt).toLocaleDateString(undefined, {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Footer ported from Demo */}
|
||||
<div className="p-4 border-t border-border bg-muted/30 flex items-center justify-between shrink-0">
|
||||
<div className="text-xs text-muted-foreground flex items-center gap-2">
|
||||
<span>
|
||||
Matched{' '}
|
||||
<strong className="font-mono font-bold text-foreground">
|
||||
{searchResults?.data?.length || 0}
|
||||
</strong>{' '}
|
||||
items
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="xs"
|
||||
className="h-8 text-[10px] font-semibold uppercase tracking-wider mr-1"
|
||||
disabled={!hasActiveCriteria || isSaving || isSemantic}
|
||||
onClick={() => saveAsCollection()}
|
||||
>
|
||||
{isSaving ? (
|
||||
<>
|
||||
<Loader2 className="mr-2 h-3 w-3 animate-spin" />
|
||||
Saving...
|
||||
</>
|
||||
) : (
|
||||
'Save as collection'
|
||||
)}
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
size="xs"
|
||||
className="h-8 text-[10px] font-semibold uppercase tracking-wider"
|
||||
onClick={isSemantic ? () => onOpenChange(false) : handleApply}
|
||||
>
|
||||
{isSemantic ? 'Close' : 'Apply and Close'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos'
|
||||
import type { SearchSort } from '@shumai/dtos'
|
||||
import { ArrowDownUp } from 'lucide-react'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popover'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/ui/components/ui/select'
|
||||
|
||||
interface SortControlProps {
|
||||
fields: MetadataFieldInfo[]
|
||||
sort?: SearchSort
|
||||
onSortChange: (sort?: SearchSort) => void
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
const SYSTEM_FIELDS = [
|
||||
{ id: 'custom', label: 'Custom' },
|
||||
{ id: 'name', label: 'Name' },
|
||||
{ id: 'createdAt', label: 'Date Created' },
|
||||
{ id: 'updatedAt', label: 'Date Modified' },
|
||||
{ id: 'sizeByte', label: 'Size' },
|
||||
]
|
||||
|
||||
export function SortControl({ fields, sort, onSortChange, disabled }: SortControlProps) {
|
||||
const allFields = [
|
||||
...SYSTEM_FIELDS,
|
||||
...fields.map((f) => ({
|
||||
id: f.id!,
|
||||
label: f.config?.name || f.description || 'Unknown',
|
||||
})),
|
||||
]
|
||||
|
||||
// Default to Custom if no sort selected
|
||||
const currentFieldId = sort?.field || 'custom'
|
||||
const currentField = allFields.find((f) => f.id === currentFieldId) || allFields[0]
|
||||
|
||||
const isAsc = sort?.order === 'asc'
|
||||
|
||||
const getDirectionOptions = (fieldId: string) => {
|
||||
if (fieldId === 'name') {
|
||||
return { asc: 'A → Z', desc: 'Z → A' }
|
||||
}
|
||||
if (['createdAt', 'updatedAt'].includes(fieldId)) {
|
||||
return { asc: 'Oldest → Newest', desc: 'Newest → Oldest' }
|
||||
}
|
||||
if (fieldId === 'sizeByte') {
|
||||
return { asc: 'Smallest → Largest', desc: 'Largest → Smallest' }
|
||||
}
|
||||
return { asc: 'Ascending', desc: 'Descending' }
|
||||
}
|
||||
|
||||
const dirOptions = getDirectionOptions(currentFieldId)
|
||||
|
||||
const handleFieldChange = (fieldId: string) => {
|
||||
onSortChange({
|
||||
field: fieldId,
|
||||
order: sort?.order || 'desc',
|
||||
})
|
||||
}
|
||||
|
||||
const handleOrderChange = (val: string) => {
|
||||
onSortChange({
|
||||
field: currentFieldId,
|
||||
order: val === 'asc' ? 'asc' : 'desc',
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
variant={sort ? 'secondary' : 'ghost'}
|
||||
size="sm"
|
||||
className="h-8"
|
||||
disabled={disabled}
|
||||
>
|
||||
{sort ? `Sorted by ${currentField.label}` : 'Sort by'}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-[280px] p-3" align="end">
|
||||
<div className="grid gap-4">
|
||||
<div className="grid gap-2">
|
||||
<span className="text-sm text-muted-foreground">Sort by</span>
|
||||
<Select value={currentFieldId} onValueChange={handleFieldChange}>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent position="popper">
|
||||
{allFields.map((f) => (
|
||||
<SelectItem key={f.id} value={f.id}>
|
||||
{f.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{currentFieldId !== 'custom' && (
|
||||
<div className="grid gap-2">
|
||||
<span className="text-sm text-muted-foreground">Order</span>
|
||||
<Button
|
||||
variant="outline"
|
||||
className="w-full justify-between font-normal"
|
||||
onClick={() => handleOrderChange(isAsc ? 'desc' : 'asc')}
|
||||
>
|
||||
<span>{isAsc ? dirOptions.asc : dirOptions.desc}</span>
|
||||
<ArrowDownUp className="size-4 opacity-50" />
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user