Merge pull request #265 from shumaiOne/fix/custom-field-options-and-placeholder

feat(webui): render select options in field creation mode and pick random option color
This commit is contained in:
Yiling-J
2026-08-05 15:28:56 +08:00
committed by GitHub
10 changed files with 129 additions and 34 deletions
@@ -2,10 +2,7 @@ import { expect, test } from '../../fixtures'
test.use({ fileOptions: { mediaType: 'audio' } })
test('upload an audio file, create a comment at a non-zero timestamp', async ({
file,
prisma,
}) => {
test('upload an audio file, create a comment at a non-zero timestamp', async ({ file, prisma }) => {
const { page, projectId, fileId } = file
const commentText = `Audio timestamp comment ${Date.now()}`
@@ -20,11 +20,11 @@ test('upload a txt file, create a comment with draw', async ({ file, prisma }) =
await page.goto(`/projects/${projectId}/files/${fileId}`)
// Verify PDF viewer container or page is loaded
await expect(
page.locator('.pdf-viewer, canvas, [data-testid="pdf-viewer"]').first(),
).toBeVisible({
timeout: 30_000,
})
await expect(page.locator('.pdf-viewer, canvas, [data-testid="pdf-viewer"]').first()).toBeVisible(
{
timeout: 30_000,
},
)
// Toggle drawing mode
const drawToggleBtn = page.getByTitle('Toggle Annotation')
+1 -3
View File
@@ -1731,9 +1731,7 @@ describe('Agent Database Activities Integration', () => {
expect(source?.type).toBe('select')
expect(source?.description).toBe('Generation source')
expect(source?.options).toEqual([{ displayName: 'Gemini' }, { displayName: 'Seedance' }])
expect(
res.fields.find((f: { name: string }) => f.name === 'Manual Notes'),
).toBeUndefined()
expect(res.fields.find((f: { name: string }) => f.name === 'Manual Notes')).toBeUndefined()
})
it('should resolve the project through the ancestor chain', async () => {
+7 -7
View File
@@ -1,7 +1,7 @@
import {
type AgentMessage,
type AgentTool,
type SessionTreeEntry,
type AgentMessage,
type AgentTool,
type SessionTreeEntry,
} from '@earendil-works/pi-agent-core'
import { type ImageContent } from '@earendil-works/pi-ai'
import { assetService } from '@shumai/core/src/asset/asset'
@@ -19,10 +19,10 @@ import { generateKeyBetween } from 'jittered-fractional-indexing'
import { ulid } from 'ulid'
import { DatabaseSessionStorage } from '../database-session-storage'
import {
createAgentSession,
fieldsToTypeBoxSchema,
type AutofillField,
type DbProviderInfo,
createAgentSession,
fieldsToTypeBoxSchema,
type AutofillField,
type DbProviderInfo,
} from '../index'
import { aiUsageService } from '@shumai/core/src/ai-usage/ai-usage'
+10 -1
View File
@@ -71,6 +71,15 @@ export function getOptionStyle(color?: string) {
color: color,
}
}
export function getRandomUnusedColor(existingOptions: SelectOption[] = []): string {
const usedColors = new Set(existingOptions.map((o) => o.color?.toLowerCase()).filter(Boolean))
const available = PREDEFINED_COLORS.filter((color) => !usedColors.has(color.toLowerCase()))
if (available.length > 0) {
return available[Math.floor(Math.random() * available.length)]
}
return PREDEFINED_COLORS[Math.floor(Math.random() * PREDEFINED_COLORS.length)]
}
import {
AlignLeft,
Calendar,
@@ -338,7 +347,7 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
const newOption: SelectOption = {
id: ulid(),
displayName: m.new_option(),
color: '#808080',
color: getRandomUnusedColor(newOptions),
}
setNewOptions([...newOptions, newOption])
}}
@@ -27,7 +27,12 @@ import { useMemo, useState, useEffect } from 'react'
import { toast } from 'sonner'
import { Popover, PopoverTrigger, PopoverContent } from '@/ui/components/ui/popover'
import { m } from '@/ui/paraglide/messages.js'
import { FIELD_TYPE_ICONS, PREDEFINED_COLORS, COLOR_MAP } from './fields-manager'
import {
FIELD_TYPE_ICONS,
PREDEFINED_COLORS,
COLOR_MAP,
getRandomUnusedColor,
} from './fields-manager'
import { Button } from '@/ui/components/ui/button'
import { Dialog, DialogContent, DialogTitle } from '@/ui/components/ui/dialog'
@@ -137,6 +142,7 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
const [newType, setNewType] = useState<FieldType | ''>('')
const [newAiAutofill, setNewAiAutofill] = useState(false)
const [newDescription, setNewDescription] = useState('')
const [newOptions, setNewOptions] = useState<SelectOption[]>([])
const $patchOrder = client.api.projects[':projectId'].fields.order.$patch
const { mutate: updateFieldsOrder } = useMutation<
@@ -268,6 +274,7 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
setNewType('')
setNewAiAutofill(false)
setNewDescription('')
setNewOptions([])
}
const handleDragEnd = (event: DragEndEvent) => {
@@ -294,12 +301,19 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
const handleSaveCreation = () => {
if (!newName || !newType) return
const typeConfig =
newType === FieldType.select
? { select: { options: newOptions } }
: newType === FieldType.selectMulti
? { selectMulti: { options: newOptions } }
: {}
createField({
param: { projectId: projectId },
json: {
config: {
name: newName,
type: newType,
...typeConfig,
},
label: newName,
scope: selectedGroup === SCOPE_GROUPS.PROJECT ? undefined : selectedGroup,
@@ -363,7 +377,10 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
{Object.values(FieldType).map((type) => (
<DropdownMenuItem
key={type}
onClick={() => setNewType(type)}
onClick={() => {
setNewType(type)
setNewOptions([])
}}
className="capitalize"
>
{type.replace(/_/g, ' ')}
@@ -384,6 +401,81 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
<Switch checked={newAiAutofill} onCheckedChange={setNewAiAutofill} />
<Label>{m.ai_autofill()}</Label>
</div>
{(newType === FieldType.select || newType === FieldType.selectMulti) && (
<div className="space-y-2">
<div className="flex items-center justify-between">
<Label className="text-sm font-medium">{m.options()}</Label>
<Button
variant="ghost"
size="sm"
onClick={() => {
const newOption: SelectOption = {
id: ulid(),
displayName: m.new_option(),
color: getRandomUnusedColor(newOptions),
}
setNewOptions([...newOptions, newOption])
}}
>
<Plus className="h-4 w-4 mr-2" /> {m.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 || idx} 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={m.option_name_placeholder()}
/>
<Popover>
<PopoverTrigger asChild>
<button
type="button"
className="w-6 h-6 rounded-full border shrink-0 cursor-pointer focus:outline-none hover:scale-105 transition-transform"
style={{
backgroundColor:
COLOR_MAP[opt.color || '']?.hex || opt.color || '#808080',
}}
/>
</PopoverTrigger>
<PopoverContent className="p-2 w-auto" align="end">
<div className="flex gap-1">
{PREDEFINED_COLORS.map((color) => (
<button
key={color}
type="button"
className="w-5 h-5 rounded-full border cursor-pointer hover:scale-110 transition-transform"
style={{ backgroundColor: COLOR_MAP[color]?.hex || color }}
onClick={() => {
const updated = [...newOptions]
updated[idx] = { ...opt, color }
setNewOptions(updated)
}}
/>
))}
</div>
</PopoverContent>
</Popover>
<Button
variant="ghost"
size="icon"
onClick={() => {
setNewOptions(newOptions.filter((_, i) => i !== idx))
}}
>
<X className="h-4 w-4" />
</Button>
</div>
))}
</div>
</div>
)}
</div>
)
}
@@ -447,16 +539,15 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
variant="ghost"
size="sm"
onClick={() => {
// Logic to add option
const newOption = {
id: ulid(),
displayName: m.new_option(),
color: '#808080',
}
const currentOptions =
(selectedField.config?.type === 'select'
? editConfig.select?.options
: editConfig.selectMulti?.options) || []
const newOption = {
id: ulid(),
displayName: m.new_option(),
color: getRandomUnusedColor(currentOptions),
}
const newOptions = [...currentOptions, newOption]
setEditConfig({
+1 -1
View File
@@ -641,7 +641,7 @@
"field_name_placeholder": "e.g. Status",
"field_type": "Field Type",
"select_field_type": "Select field type",
"description_optional_placeholder": "Description (Optional)",
"description_optional_placeholder": "Optional, used for AI autofill",
"ai_autofill": "AI Autofill",
"select_field_to_edit": "Select a field to edit options",
"type_label": "Type:",
+1 -1
View File
@@ -641,7 +641,7 @@
"field_name_placeholder": "例如:状态",
"field_type": "字段类型",
"select_field_type": "选择字段类型",
"description_optional_placeholder": "描述(可选)",
"description_optional_placeholder": "可选,用于 AI 自动填充",
"ai_autofill": "AI 自动填充",
"select_field_to_edit": "选择一个字段以编辑选项",
"type_label": "类型:",
+1 -1
View File
@@ -1,7 +1,7 @@
/**
* | output |
* | --- |
* | "Description (Optional)" |
* | "Optional, used for AI autofill" |
*
* @param {Description_Optional_PlaceholderInputs} inputs
* @param {{ locale?: "en" | "zh" }} options
@@ -6,17 +6,17 @@ import { getLocale, experimentalStaticLocale } from '../runtime.js';
/** @typedef {{}} Description_Optional_PlaceholderInputs */
const en_description_optional_placeholder = /** @type {(inputs: Description_Optional_PlaceholderInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`Description (Optional)`)
return /** @type {LocalizedString} */ (`Optional, used for AI autofill`)
};
const zh_description_optional_placeholder = /** @type {(inputs: Description_Optional_PlaceholderInputs) => LocalizedString} */ () => {
return /** @type {LocalizedString} */ (`描述(可选)`)
return /** @type {LocalizedString} */ (`可选,用于 AI 自动填充`)
};
/**
* | output |
* | --- |
* | "Description (Optional)" |
* | "Optional, used for AI autofill" |
*
* @param {Description_Optional_PlaceholderInputs} inputs
* @param {{ locale?: "en" | "zh" }} options