mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 06:24:30 +00:00
feat(fields): improve fields popup options, group filter, and dropdown clipping
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
import { FieldType, type FieldInfo, type FieldInfo as MetadataFieldInfo } from '@shumai/dtos'
|
import {
|
||||||
|
FieldType,
|
||||||
|
type FieldInfo,
|
||||||
|
type FieldInfo as MetadataFieldInfo,
|
||||||
|
type SelectOption,
|
||||||
|
} from '@shumai/dtos'
|
||||||
import { client } from '@/ui/api/client'
|
import { client } from '@/ui/api/client'
|
||||||
import { useFieldStore } from '@/ui/stores/fields'
|
import { useFieldStore } from '@/ui/stores/fields'
|
||||||
import { DragDropProvider, KeyboardSensor, PointerSensor, type DragEndEvent } from '@dnd-kit/react'
|
import { DragDropProvider, KeyboardSensor, PointerSensor, type DragEndEvent } from '@dnd-kit/react'
|
||||||
import { useSortable } from '@dnd-kit/react/sortable'
|
import { useSortable } from '@dnd-kit/react/sortable'
|
||||||
import { useMutation } from '@tanstack/react-query'
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||||
import { InferRequestType, InferResponseType } from 'hono/client'
|
import { InferRequestType, InferResponseType } from 'hono/client'
|
||||||
|
|
||||||
import { Button } from '@/ui/components/ui/button'
|
import { Button } from '@/ui/components/ui/button'
|
||||||
@@ -18,8 +23,35 @@ import { Label } from '@/ui/components/ui/label'
|
|||||||
import { Switch } from '@/ui/components/ui/switch'
|
import { Switch } from '@/ui/components/ui/switch'
|
||||||
import { arrayMove } from '@/ui/lib/dnd-utils'
|
import { arrayMove } from '@/ui/lib/dnd-utils'
|
||||||
import { PointerActivationConstraints } from '@dnd-kit/dom'
|
import { PointerActivationConstraints } from '@dnd-kit/dom'
|
||||||
import { ChevronDown, GripVertical, Plus, Search, Settings, Sparkles } from 'lucide-react'
|
import {
|
||||||
import { useMemo, useState } from 'react'
|
AlignLeft,
|
||||||
|
Calendar,
|
||||||
|
ChevronDown,
|
||||||
|
GripVertical,
|
||||||
|
Hash,
|
||||||
|
List,
|
||||||
|
Plus,
|
||||||
|
Search,
|
||||||
|
Settings,
|
||||||
|
Sparkles,
|
||||||
|
Star,
|
||||||
|
Tags,
|
||||||
|
ToggleLeft,
|
||||||
|
Type,
|
||||||
|
X,
|
||||||
|
} from 'lucide-react'
|
||||||
|
import { useMemo, useState, useEffect } from 'react'
|
||||||
|
|
||||||
|
export const FIELD_TYPE_ICONS: Record<FieldType, React.ComponentType<{ className?: string }>> = {
|
||||||
|
[FieldType.text]: Type,
|
||||||
|
[FieldType.longText]: AlignLeft,
|
||||||
|
[FieldType.select]: List,
|
||||||
|
[FieldType.selectMulti]: Tags,
|
||||||
|
[FieldType.rating]: Star,
|
||||||
|
[FieldType.number]: Hash,
|
||||||
|
[FieldType.toggle]: ToggleLeft,
|
||||||
|
[FieldType.date]: Calendar,
|
||||||
|
}
|
||||||
|
|
||||||
type SortableFieldItemProps = {
|
type SortableFieldItemProps = {
|
||||||
field: FieldInfo
|
field: FieldInfo
|
||||||
@@ -33,14 +65,17 @@ function SortableFieldItem({ field, index, onVisibilityChange }: SortableFieldIt
|
|||||||
index,
|
index,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const Icon = field.config?.type ? FIELD_TYPE_ICONS[field.config.type as FieldType] : null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={ref} className="flex items-center justify-between p-2 rounded-md hover:bg-muted">
|
<div ref={ref} className="flex items-center justify-between p-2 rounded-md hover:bg-muted">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<div ref={handleRef} className="cursor-grab">
|
<div ref={handleRef} className="cursor-grab">
|
||||||
<GripVertical className="h-4 w-4 text-muted-foreground" />
|
<GripVertical className="h-4 w-4 text-muted-foreground" />
|
||||||
</div>
|
</div>
|
||||||
{field.aiAutofill && <Sparkles className="h-4 w-4 text-muted-foreground" />}
|
{Icon && <Icon className="h-4 w-4 text-muted-foreground shrink-0" />}
|
||||||
<span className="text-sm">{field.config?.name}</span>
|
{field.aiAutofill && <Sparkles className="h-4 w-4 text-muted-foreground shrink-0" />}
|
||||||
|
<span className="text-sm truncate">{field.config?.name}</span>
|
||||||
</div>
|
</div>
|
||||||
<Switch
|
<Switch
|
||||||
checked={field.visible}
|
checked={field.visible}
|
||||||
@@ -64,6 +99,12 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
|
|||||||
const [newFieldType, setNewFieldType] = useState<FieldType | ''>('')
|
const [newFieldType, setNewFieldType] = useState<FieldType | ''>('')
|
||||||
const [newAiAutofill, setNewAiAutofill] = useState(false)
|
const [newAiAutofill, setNewAiAutofill] = useState(false)
|
||||||
const [newDescription, setNewDescription] = useState('')
|
const [newDescription, setNewDescription] = useState('')
|
||||||
|
const [newOptions, setNewOptions] = useState<SelectOption[]>([])
|
||||||
|
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
useEffect(() => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['fields', projectId] })
|
||||||
|
}, [projectId, queryClient])
|
||||||
|
|
||||||
const $patchOrder = client.api.projects[':projectId'].fields.order.$patch
|
const $patchOrder = client.api.projects[':projectId'].fields.order.$patch
|
||||||
const { mutate: updateFieldsOrder } = useMutation<
|
const { mutate: updateFieldsOrder } = useMutation<
|
||||||
@@ -96,6 +137,7 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
|
|||||||
const newFieldsList = [...fields, newField]
|
const newFieldsList = [...fields, newField]
|
||||||
updateFields(newFieldsList)
|
updateFields(newFieldsList)
|
||||||
setNewFieldName('')
|
setNewFieldName('')
|
||||||
|
setNewOptions([])
|
||||||
|
|
||||||
updateFieldsOrder({
|
updateFieldsOrder({
|
||||||
param: { projectId: projectId },
|
param: { projectId: projectId },
|
||||||
@@ -147,12 +189,19 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
|
|||||||
|
|
||||||
const handleCreateField = () => {
|
const handleCreateField = () => {
|
||||||
if (!newFieldName || !newFieldType) return
|
if (!newFieldName || !newFieldType) return
|
||||||
|
const typeConfig =
|
||||||
|
newFieldType === FieldType.select
|
||||||
|
? { select: { options: newOptions } }
|
||||||
|
: newFieldType === FieldType.selectMulti
|
||||||
|
? { selectMulti: { options: newOptions } }
|
||||||
|
: {}
|
||||||
createField({
|
createField({
|
||||||
param: { projectId: projectId },
|
param: { projectId: projectId },
|
||||||
json: {
|
json: {
|
||||||
config: {
|
config: {
|
||||||
name: newFieldName,
|
name: newFieldName,
|
||||||
type: newFieldType,
|
type: newFieldType,
|
||||||
|
...typeConfig,
|
||||||
},
|
},
|
||||||
label: newFieldName,
|
label: newFieldName,
|
||||||
aiAutofill: newAiAutofill,
|
aiAutofill: newAiAutofill,
|
||||||
@@ -195,7 +244,10 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
|
|||||||
{Object.values(FieldType).map((type) => (
|
{Object.values(FieldType).map((type) => (
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
key={type}
|
key={type}
|
||||||
onClick={() => setNewFieldType(type)}
|
onClick={() => {
|
||||||
|
setNewFieldType(type)
|
||||||
|
setNewOptions([])
|
||||||
|
}}
|
||||||
className="capitalize"
|
className="capitalize"
|
||||||
>
|
>
|
||||||
{type.replace(/_/g, ' ')}
|
{type.replace(/_/g, ' ')}
|
||||||
@@ -220,6 +272,59 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
|
|||||||
<Label htmlFor="ai-autofill">AI Autofill</Label>
|
<Label htmlFor="ai-autofill">AI Autofill</Label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{(newFieldType === FieldType.select || newFieldType === FieldType.selectMulti) && (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<Label className="text-sm font-medium">Options</Label>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
className="h-8 px-2"
|
||||||
|
onClick={() => {
|
||||||
|
const newOption: SelectOption = {
|
||||||
|
id: crypto.randomUUID(),
|
||||||
|
displayName: 'New Option',
|
||||||
|
color: '#808080',
|
||||||
|
}
|
||||||
|
setNewOptions([...newOptions, newOption])
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Plus className="h-4 w-4 mr-1" /> Add Option
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2 max-h-40 overflow-y-auto pr-1">
|
||||||
|
{newOptions.map((opt, idx) => (
|
||||||
|
<div key={opt.id} className="flex items-center gap-2">
|
||||||
|
<Input
|
||||||
|
value={opt.displayName}
|
||||||
|
onChange={(e) => {
|
||||||
|
const updated = [...newOptions]
|
||||||
|
updated[idx] = { ...opt, displayName: e.target.value }
|
||||||
|
setNewOptions(updated)
|
||||||
|
}}
|
||||||
|
placeholder="Option name"
|
||||||
|
className="h-8 flex-1"
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
className="w-6 h-6 rounded-full border shrink-0"
|
||||||
|
style={{ backgroundColor: opt.color }}
|
||||||
|
></div>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
className="h-8 w-8 shrink-0 text-destructive hover:text-destructive hover:bg-destructive/10"
|
||||||
|
onClick={() => {
|
||||||
|
setNewOptions(newOptions.filter((_, i) => i !== idx))
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<X className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="flex gap-2 pt-2">
|
<div className="flex gap-2 pt-2">
|
||||||
<Button
|
<Button
|
||||||
variant="secondary"
|
variant="secondary"
|
||||||
@@ -230,6 +335,7 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
|
|||||||
setNewFieldType('')
|
setNewFieldType('')
|
||||||
setNewAiAutofill(false)
|
setNewAiAutofill(false)
|
||||||
setNewDescription('')
|
setNewDescription('')
|
||||||
|
setNewOptions([])
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
|
|||||||
@@ -1,28 +1,15 @@
|
|||||||
import type { SelectOption } from '@shumai/dtos'
|
import type { SelectOption } from '@shumai/dtos'
|
||||||
import { ChevronDown, X } from 'lucide-react'
|
import { ChevronDown, X } from 'lucide-react'
|
||||||
import React, { useEffect, useRef, useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
import type { FieldProps } from './field-types'
|
import type { FieldProps } from './field-types'
|
||||||
|
import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popover'
|
||||||
|
|
||||||
const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, readOnly }) => {
|
const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, readOnly }) => {
|
||||||
const selectConfig = config?.select
|
const selectConfig = config?.select
|
||||||
const [isEditing, setIsEditing] = useState(false)
|
const [isEditing, setIsEditing] = useState(false)
|
||||||
const containerRef = useRef<HTMLDivElement>(null)
|
|
||||||
|
|
||||||
const selectedOption = selectConfig?.options?.find((opt: SelectOption) => opt.id === value)
|
const selectedOption = selectConfig?.options?.find((opt: SelectOption) => opt.id === value)
|
||||||
|
|
||||||
// Click outside handler
|
|
||||||
useEffect(() => {
|
|
||||||
const handleClickOutside = (event: MouseEvent) => {
|
|
||||||
if (containerRef.current && !containerRef.current.contains(event.target as Node)) {
|
|
||||||
if (isEditing) {
|
|
||||||
setIsEditing(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
document.addEventListener('mousedown', handleClickOutside)
|
|
||||||
return () => document.removeEventListener('mousedown', handleClickOutside)
|
|
||||||
}, [isEditing])
|
|
||||||
|
|
||||||
const handleStartEdit = () => {
|
const handleStartEdit = () => {
|
||||||
if (!readOnly) {
|
if (!readOnly) {
|
||||||
setIsEditing(true)
|
setIsEditing(true)
|
||||||
@@ -50,46 +37,53 @@ const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, read
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="relative w-full" ref={containerRef}>
|
<div className="relative w-full">
|
||||||
<div
|
<Popover open={isEditing} onOpenChange={setIsEditing}>
|
||||||
onClick={handleStartEdit}
|
<PopoverTrigger asChild>
|
||||||
className={`flex items-center justify-between w-full min-h-[28px] px-2 py-1 rounded border border-transparent transition-colors ${
|
|
||||||
!readOnly ? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200 group' : ''
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{renderOptionBadge(selectedOption)}
|
|
||||||
{!readOnly && (
|
|
||||||
<ChevronDown className="w-4 h-4 text-gray-400 opacity-0 group-hover:opacity-100" />
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{isEditing && (
|
|
||||||
<div className="absolute z-50 top-full left-0 mt-1 w-full min-w-[150px] bg-white border border-gray-200 rounded-lg shadow-lg py-1 max-h-60 overflow-auto">
|
|
||||||
<div
|
<div
|
||||||
onClick={() => handleSelect('')}
|
onClick={handleStartEdit}
|
||||||
className="px-3 py-2 text-sm text-gray-500 hover:bg-gray-50 cursor-pointer flex items-center gap-2"
|
className={`flex items-center justify-between w-full min-h-[28px] px-2 py-1 rounded border border-transparent transition-colors ${
|
||||||
|
!readOnly ? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200 group' : ''
|
||||||
|
}`}
|
||||||
>
|
>
|
||||||
<X className="w-3 h-3" /> Clear
|
{renderOptionBadge(selectedOption)}
|
||||||
|
{!readOnly && (
|
||||||
|
<ChevronDown className="w-4 h-4 text-gray-400 opacity-0 group-hover:opacity-100" />
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
{selectConfig?.options?.map((option: SelectOption) => (
|
</PopoverTrigger>
|
||||||
|
|
||||||
|
{isEditing && (
|
||||||
|
<PopoverContent
|
||||||
|
className="p-1 w-[--radix-popover-trigger-width] min-w-[150px] max-h-60 overflow-auto bg-white border border-gray-200 rounded-lg shadow-lg"
|
||||||
|
align="start"
|
||||||
|
>
|
||||||
<div
|
<div
|
||||||
key={option.id}
|
onClick={() => handleSelect('')}
|
||||||
onClick={() => handleSelect(option.id)}
|
className="px-3 py-2 text-sm text-gray-500 hover:bg-gray-50 cursor-pointer flex items-center gap-2"
|
||||||
className="px-3 py-2 hover:bg-gray-50 cursor-pointer flex items-center"
|
|
||||||
>
|
>
|
||||||
<span
|
<X className="w-3 h-3" /> Clear
|
||||||
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
|
|
||||||
style={{
|
|
||||||
backgroundColor: `${option.color}33`,
|
|
||||||
color: option.color || undefined,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{option.displayName}
|
|
||||||
</span>
|
|
||||||
</div>
|
</div>
|
||||||
))}
|
{selectConfig?.options?.map((option: SelectOption) => (
|
||||||
</div>
|
<div
|
||||||
)}
|
key={option.id}
|
||||||
|
onClick={() => handleSelect(option.id)}
|
||||||
|
className="px-3 py-2 hover:bg-gray-50 cursor-pointer flex items-center"
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
|
||||||
|
style={{
|
||||||
|
backgroundColor: `${option.color}33`,
|
||||||
|
color: option.color || undefined,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{option.displayName}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</PopoverContent>
|
||||||
|
)}
|
||||||
|
</Popover>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { Check } from 'lucide-react'
|
import { Check } from 'lucide-react'
|
||||||
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'
|
import React, { useLayoutEffect, useRef, useState } from 'react'
|
||||||
import type { FieldProps } from './field-types'
|
import type { FieldProps } from './field-types'
|
||||||
import type { SelectOption } from '@shumai/dtos'
|
import type { SelectOption } from '@shumai/dtos'
|
||||||
|
import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popover'
|
||||||
|
|
||||||
const SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
const SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
||||||
value = [],
|
value = [],
|
||||||
@@ -11,7 +12,6 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const selectConfig = config?.selectMulti
|
const selectConfig = config?.selectMulti
|
||||||
const containerRef = useRef<HTMLDivElement>(null)
|
const containerRef = useRef<HTMLDivElement>(null)
|
||||||
const dropdownRef = useRef<HTMLDivElement>(null)
|
|
||||||
const [isEditing, setIsEditing] = useState(false)
|
const [isEditing, setIsEditing] = useState(false)
|
||||||
const [expanded, setExpanded] = useState(false)
|
const [expanded, setExpanded] = useState(false)
|
||||||
const [visibleCount, setVisibleCount] = useState(value.length)
|
const [visibleCount, setVisibleCount] = useState(value.length)
|
||||||
@@ -63,28 +63,6 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
|||||||
}
|
}
|
||||||
}, [selectedOptions, isEditing, expanded, value])
|
}, [selectedOptions, isEditing, expanded, value])
|
||||||
|
|
||||||
// Click outside to close dropdown or expanded view
|
|
||||||
useEffect(() => {
|
|
||||||
const handleClickOutside = (event: MouseEvent) => {
|
|
||||||
// Check if click is outside both the main container and the dropdown
|
|
||||||
const isOutsideDropdown =
|
|
||||||
!dropdownRef.current || !dropdownRef.current.contains(event.target as Node)
|
|
||||||
const isOutsideContainer =
|
|
||||||
!containerRef.current || !containerRef.current.contains(event.target as Node)
|
|
||||||
|
|
||||||
if (isOutsideDropdown && isOutsideContainer) {
|
|
||||||
if (isEditing) {
|
|
||||||
setIsEditing(false)
|
|
||||||
}
|
|
||||||
if (expanded) {
|
|
||||||
setExpanded(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
document.addEventListener('mousedown', handleClickOutside)
|
|
||||||
return () => document.removeEventListener('mousedown', handleClickOutside)
|
|
||||||
}, [isEditing, expanded])
|
|
||||||
|
|
||||||
const toggleOption = (optionId: string) => {
|
const toggleOption = (optionId: string) => {
|
||||||
const currentVal = value || []
|
const currentVal = value || []
|
||||||
let newVal
|
let newVal
|
||||||
@@ -121,99 +99,108 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const hiddenCount = selectedOptions.length - visibleCount
|
const hiddenCount = selectedOptions.length - visibleCount
|
||||||
|
const isOpen = (expanded && !isEditing) || isEditing
|
||||||
|
|
||||||
|
const handleOpenChange = (open: boolean) => {
|
||||||
|
if (!open) {
|
||||||
|
setExpanded(false)
|
||||||
|
setIsEditing(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="relative w-full">
|
<div className="relative w-full">
|
||||||
{/* Placeholder Display Mode */}
|
<Popover open={isOpen} onOpenChange={handleOpenChange}>
|
||||||
<div
|
<PopoverTrigger asChild>
|
||||||
ref={containerRef}
|
{/* Placeholder Display Mode */}
|
||||||
onClick={handlePlaceholderClick}
|
<div
|
||||||
className={`w-full px-1 py-1 flex flex-wrap gap-1 rounded border border-transparent transition-all box-border h-[32px] overflow-hidden ${
|
ref={containerRef}
|
||||||
!readOnly || hiddenCount > 0
|
onClick={handlePlaceholderClick}
|
||||||
? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200'
|
className={`w-full px-1 py-1 flex flex-wrap gap-1 rounded border border-transparent transition-all box-border h-[32px] overflow-hidden ${
|
||||||
: ''
|
!readOnly || hiddenCount > 0
|
||||||
}`}
|
? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200'
|
||||||
>
|
: ''
|
||||||
{selectedOptions.length === 0 && (
|
}`}
|
||||||
<span className="text-gray-400 text-sm italic px-1 pt-0.5">Empty</span>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Render visible items */}
|
|
||||||
{selectedOptions.slice(0, visibleCount).map((option: SelectOption) => (
|
|
||||||
<span
|
|
||||||
key={option.id}
|
|
||||||
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium border border-transparent whitespace-nowrap h-[22px]"
|
|
||||||
style={{
|
|
||||||
backgroundColor: `${option.color}33`,
|
|
||||||
color: option.color || undefined,
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{option.displayName}
|
{selectedOptions.length === 0 && (
|
||||||
</span>
|
<span className="text-gray-400 text-sm italic px-1 pt-0.5">Empty</span>
|
||||||
))}
|
)}
|
||||||
|
|
||||||
{hiddenCount > 0 && (
|
{/* Render visible items */}
|
||||||
<span className="inline-flex items-center px-1.5 py-0.5 rounded-full text-xs font-medium bg-gray-200 text-gray-600 h-[22px]">
|
{selectedOptions.slice(0, visibleCount).map((option: SelectOption) => (
|
||||||
+{hiddenCount}
|
<span
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Expanded Overlay Mode */}
|
|
||||||
{expanded && !isEditing && (
|
|
||||||
<div
|
|
||||||
onClick={handleOverlayClick}
|
|
||||||
className={`absolute top-0 left-0 w-full px-1 py-1 flex flex-wrap gap-1 bg-white rounded border border-blue-500 shadow-lg z-50 min-h-[32px] h-auto ${
|
|
||||||
!readOnly ? 'cursor-pointer' : ''
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{selectedOptions.length === 0 && (
|
|
||||||
<span className="text-gray-400 text-sm italic px-1 pt-0.5">Empty</span>
|
|
||||||
)}
|
|
||||||
{selectedOptions.map((option: SelectOption) => (
|
|
||||||
<span
|
|
||||||
key={option.id}
|
|
||||||
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium border border-transparent whitespace-nowrap h-[22px]"
|
|
||||||
style={{
|
|
||||||
backgroundColor: `${option.color}33`,
|
|
||||||
color: option.color || undefined,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{option.displayName}
|
|
||||||
</span>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Edit Mode Dropdown */}
|
|
||||||
{isEditing && (
|
|
||||||
<div
|
|
||||||
ref={dropdownRef}
|
|
||||||
className="absolute z-50 top-full left-0 mt-1 w-64 bg-white border border-gray-200 rounded-lg shadow-xl max-h-60 overflow-auto"
|
|
||||||
>
|
|
||||||
{selectConfig?.options?.map((option: SelectOption) => {
|
|
||||||
const isSelected = (value || []).includes(option.id)
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
key={option.id}
|
key={option.id}
|
||||||
onClick={() => toggleOption(option.id)}
|
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium border border-transparent whitespace-nowrap h-[22px]"
|
||||||
className="px-3 py-2 hover:bg-gray-50 cursor-pointer flex items-center justify-between"
|
style={{
|
||||||
|
backgroundColor: `${option.color}33`,
|
||||||
|
color: option.color || undefined,
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<span
|
{option.displayName}
|
||||||
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
|
</span>
|
||||||
style={{
|
))}
|
||||||
backgroundColor: `${option.color}33`,
|
|
||||||
color: option.color || undefined,
|
{hiddenCount > 0 && (
|
||||||
}}
|
<span className="inline-flex items-center px-1.5 py-0.5 rounded-full text-xs font-medium bg-gray-200 text-gray-600 h-[22px]">
|
||||||
|
+{hiddenCount}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</PopoverTrigger>
|
||||||
|
|
||||||
|
{expanded && !isEditing && (
|
||||||
|
<PopoverContent
|
||||||
|
onClick={handleOverlayClick}
|
||||||
|
className="p-1 w-[--radix-popover-trigger-width] min-w-[200px] flex flex-wrap gap-1 bg-white rounded border border-blue-500 shadow-lg min-h-[32px] h-auto cursor-pointer"
|
||||||
|
align="start"
|
||||||
|
>
|
||||||
|
{selectedOptions.length === 0 && (
|
||||||
|
<span className="text-gray-400 text-sm italic px-1 pt-0.5">Empty</span>
|
||||||
|
)}
|
||||||
|
{selectedOptions.map((option: SelectOption) => (
|
||||||
|
<span
|
||||||
|
key={option.id}
|
||||||
|
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium border border-transparent whitespace-nowrap h-[22px]"
|
||||||
|
style={{
|
||||||
|
backgroundColor: `${option.color}33`,
|
||||||
|
color: option.color || undefined,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{option.displayName}
|
||||||
|
</span>
|
||||||
|
))}
|
||||||
|
</PopoverContent>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{isEditing && (
|
||||||
|
<PopoverContent
|
||||||
|
className="p-1 w-64 bg-white border border-gray-200 rounded-lg shadow-xl max-h-60 overflow-auto"
|
||||||
|
align="start"
|
||||||
|
>
|
||||||
|
{selectConfig?.options?.map((option: SelectOption) => {
|
||||||
|
const isSelected = (value || []).includes(option.id)
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={option.id}
|
||||||
|
onClick={() => toggleOption(option.id)}
|
||||||
|
className="px-3 py-2 hover:bg-gray-50 cursor-pointer flex items-center justify-between"
|
||||||
>
|
>
|
||||||
{option.displayName}
|
<span
|
||||||
</span>
|
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
|
||||||
{isSelected && <Check className="w-4 h-4 text-blue-500" />}
|
style={{
|
||||||
</div>
|
backgroundColor: `${option.color}33`,
|
||||||
)
|
color: option.color || undefined,
|
||||||
})}
|
}}
|
||||||
</div>
|
>
|
||||||
)}
|
{option.displayName}
|
||||||
|
</span>
|
||||||
|
{isSelected && <Check className="w-4 h-4 text-blue-500" />}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</PopoverContent>
|
||||||
|
)}
|
||||||
|
</Popover>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ export default function FileSystemManager({
|
|||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const { canEdit } = usePermissions(projectId)
|
const { canEdit } = usePermissions(projectId)
|
||||||
const { loadedProjectId, setFields } = useFieldStore()
|
const { setFields } = useFieldStore()
|
||||||
const $patchMetadata = client.api.files[':fileId'].metadata.$patch
|
const $patchMetadata = client.api.files[':fileId'].metadata.$patch
|
||||||
const { mutate: patchMetadata } = useMutation<
|
const { mutate: patchMetadata } = useMutation<
|
||||||
InferResponseType<typeof $patchMetadata>,
|
InferResponseType<typeof $patchMetadata>,
|
||||||
@@ -75,10 +75,10 @@ export default function FileSystemManager({
|
|||||||
})
|
})
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (fields && projectId !== loadedProjectId) {
|
if (fields) {
|
||||||
setFields(fields as unknown as MetadataFieldInfo[], projectId)
|
setFields(fields as unknown as MetadataFieldInfo[], projectId)
|
||||||
}
|
}
|
||||||
}, [fields, projectId, loadedProjectId, setFields])
|
}, [fields, projectId, setFields])
|
||||||
|
|
||||||
const isRecentlyDeleted = assetId === 'recently-deleted'
|
const isRecentlyDeleted = assetId === 'recently-deleted'
|
||||||
const [selectedItem, setSelectedItem] = useState<AssetInfo | null>(null)
|
const [selectedItem, setSelectedItem] = useState<AssetInfo | null>(null)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {
|
|||||||
type SelectOption,
|
type SelectOption,
|
||||||
} from '@shumai/dtos'
|
} from '@shumai/dtos'
|
||||||
import { client } from '@/ui/api/client'
|
import { client } from '@/ui/api/client'
|
||||||
import { useMutation } from '@tanstack/react-query'
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||||
import { InferRequestType, InferResponseType } from 'hono/client'
|
import { InferRequestType, InferResponseType } from 'hono/client'
|
||||||
import { useFieldStore } from '@/ui/stores/fields'
|
import { useFieldStore } from '@/ui/stores/fields'
|
||||||
import { DragDropProvider, KeyboardSensor, PointerSensor, type DragEndEvent } from '@dnd-kit/react'
|
import { DragDropProvider, KeyboardSensor, PointerSensor, type DragEndEvent } from '@dnd-kit/react'
|
||||||
@@ -22,7 +22,8 @@ import {
|
|||||||
Sparkles,
|
Sparkles,
|
||||||
X,
|
X,
|
||||||
} from 'lucide-react'
|
} from 'lucide-react'
|
||||||
import { useMemo, useState } from 'react'
|
import { useMemo, useState, useEffect } from 'react'
|
||||||
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
import { Button } from '@/ui/components/ui/button'
|
import { Button } from '@/ui/components/ui/button'
|
||||||
import { Dialog, DialogContent, DialogTitle } from '@/ui/components/ui/dialog'
|
import { Dialog, DialogContent, DialogTitle } from '@/ui/components/ui/dialog'
|
||||||
@@ -117,6 +118,13 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
|
|||||||
const [selectedFieldId, setSelectedFieldId] = useState<string | null>(null)
|
const [selectedFieldId, setSelectedFieldId] = useState<string | null>(null)
|
||||||
const [isCreating, setIsCreating] = useState(false)
|
const [isCreating, setIsCreating] = useState(false)
|
||||||
|
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
useEffect(() => {
|
||||||
|
if (open) {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['fields', projectId] })
|
||||||
|
}
|
||||||
|
}, [open, projectId, queryClient])
|
||||||
|
|
||||||
// Editor State
|
// Editor State
|
||||||
const [editLabel, setEditLabel] = useState('')
|
const [editLabel, setEditLabel] = useState('')
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
@@ -159,6 +167,7 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
|
|||||||
setIsCreating(false)
|
setIsCreating(false)
|
||||||
setSelectedFieldId(data.id!)
|
setSelectedFieldId(data.id!)
|
||||||
resetEditor(data)
|
resetEditor(data)
|
||||||
|
toast.success('Field created successfully')
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const $put = client.api.fields[':fieldId'].$put
|
const $put = client.api.fields[':fieldId'].$put
|
||||||
@@ -177,6 +186,7 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
|
|||||||
f.id === data.id ? { ...f, ...(data as MetadataFieldInfo) } : f,
|
f.id === data.id ? { ...f, ...(data as MetadataFieldInfo) } : f,
|
||||||
)
|
)
|
||||||
updateFields(newFields)
|
updateFields(newFields)
|
||||||
|
toast.success('Field updated successfully')
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const $delete = client.api.fields[':fieldId'].$delete
|
const $delete = client.api.fields[':fieldId'].$delete
|
||||||
@@ -194,6 +204,7 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
|
|||||||
const newFields = fields.filter((f) => f.id !== variables.param.fieldId)
|
const newFields = fields.filter((f) => f.id !== variables.param.fieldId)
|
||||||
updateFields(newFields)
|
updateFields(newFields)
|
||||||
setSelectedFieldId(null)
|
setSelectedFieldId(null)
|
||||||
|
toast.success('Field deleted successfully')
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -211,6 +222,10 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
|
|||||||
}))
|
}))
|
||||||
}, [fields])
|
}, [fields])
|
||||||
|
|
||||||
|
const filteredFields = useMemo(() => {
|
||||||
|
return fields.filter((f) => (f.scope || SCOPE_GROUPS.PROJECT) === selectedGroup)
|
||||||
|
}, [fields, selectedGroup])
|
||||||
|
|
||||||
const selectedField = useMemo(
|
const selectedField = useMemo(
|
||||||
() => fields.find((f) => f.id === selectedFieldId),
|
() => fields.find((f) => f.id === selectedFieldId),
|
||||||
[fields, selectedFieldId],
|
[fields, selectedFieldId],
|
||||||
@@ -565,7 +580,7 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
|
|||||||
]}
|
]}
|
||||||
onDragEnd={handleDragEnd}
|
onDragEnd={handleDragEnd}
|
||||||
>
|
>
|
||||||
{fields.map((field, index) => (
|
{filteredFields.map((field, index) => (
|
||||||
<SortableFieldRow
|
<SortableFieldRow
|
||||||
key={field.id}
|
key={field.id}
|
||||||
field={field}
|
field={field}
|
||||||
@@ -576,7 +591,7 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
|
|||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</DragDropProvider>
|
</DragDropProvider>
|
||||||
{fields.length === 0 && (
|
{filteredFields.length === 0 && (
|
||||||
<div className="text-center text-muted-foreground text-sm py-8">
|
<div className="text-center text-muted-foreground text-sm py-8">
|
||||||
No fields found
|
No fields found
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user