Implement search functionality for models by label in SpotlightContext and update list data handling to include model actions. Enhance model retrieval logic for improved user interaction in the dashboard.
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good
This commit is contained in:
parent
2df25364a0
commit
a896cdf223
@ -30,9 +30,11 @@ import IdDisplay from '../common/IdDisplay'
|
||||
import config from '../../../config'
|
||||
import {
|
||||
getModelByName,
|
||||
getModelByPrefix
|
||||
getModelByPrefix,
|
||||
searchModelsByLabel
|
||||
} from '../../../database/ObjectModels'
|
||||
import InfoCircleIcon from '../../Icons/InfoCircleIcon'
|
||||
import MenuIcon from '../../Icons/MenuIcon'
|
||||
import { ApiServerContext } from './ApiServerContext'
|
||||
import { AuthContext } from './AuthContext'
|
||||
import { ElectronContext } from './ElectronContext'
|
||||
@ -128,6 +130,22 @@ const SpotlightContent = ({
|
||||
}
|
||||
|
||||
setLoading(false)
|
||||
|
||||
const models = searchModelsByLabel(searchQuery.trim()).map((model) => ({
|
||||
type: model.name,
|
||||
name: model.labelPlural,
|
||||
icon: model.icon,
|
||||
actions: [
|
||||
{
|
||||
default: true,
|
||||
row: true,
|
||||
icon: MenuIcon,
|
||||
url: () => {
|
||||
return model.url
|
||||
}
|
||||
}
|
||||
]
|
||||
}))
|
||||
// If the query contains a prefix mode character, and the response is an object, wrap it in an array
|
||||
if (
|
||||
/[:?^]/.test(searchQuery) &&
|
||||
@ -135,9 +153,9 @@ const SpotlightContent = ({
|
||||
!Array.isArray(data) &&
|
||||
typeof data === 'object'
|
||||
) {
|
||||
setListData([data])
|
||||
setListData([...models, data])
|
||||
} else {
|
||||
setListData(data)
|
||||
setListData([...models, ...data])
|
||||
}
|
||||
|
||||
// Check if there's a pending query after this fetch completes
|
||||
@ -290,7 +308,11 @@ const SpotlightContent = ({
|
||||
const item = listData[0]
|
||||
let type = item.type || item.objectType || inputPrefix?.type
|
||||
const model = getModelByName(type)
|
||||
const defaultAction = model ? getDefaultRowAction(model) : null
|
||||
const defaultAction = item?.type
|
||||
? getDefaultRowAction(item)
|
||||
: model
|
||||
? getDefaultRowAction(model)
|
||||
: null
|
||||
if (defaultAction) {
|
||||
triggerRowAction(defaultAction, item)
|
||||
}
|
||||
@ -304,7 +326,11 @@ const SpotlightContent = ({
|
||||
const item = listData[index]
|
||||
let type = item.type || item.objectType || inputPrefix?.type
|
||||
const model = getModelByName(type)
|
||||
const defaultAction = model ? getDefaultRowAction(model) : null
|
||||
const defaultAction = item?.type
|
||||
? getDefaultRowAction(item)
|
||||
: model
|
||||
? getDefaultRowAction(model)
|
||||
: null
|
||||
if (defaultAction) {
|
||||
triggerRowAction(defaultAction, item)
|
||||
}
|
||||
@ -447,10 +473,12 @@ const SpotlightContent = ({
|
||||
<List
|
||||
dataSource={listData}
|
||||
renderItem={(item, index) => {
|
||||
let type = item.objectType || inputPrefix?.type
|
||||
let type = item.objectType || inputPrefix?.type || item?.type
|
||||
const model = getModelByName(type)
|
||||
const Icon = model?.icon
|
||||
const rowActions = getRowActions(model)
|
||||
const rowActions = item?.type
|
||||
? item.actions
|
||||
: getRowActions(model)
|
||||
let shortcutText = ''
|
||||
if (index === 0) {
|
||||
shortcutText = 'ENTER'
|
||||
@ -492,7 +520,9 @@ const SpotlightContent = ({
|
||||
showId={false}
|
||||
/>
|
||||
) : null}
|
||||
<IdDisplay id={item._id} type={type} longId={false} />
|
||||
{item?._id ? (
|
||||
<IdDisplay id={item._id} type={type} longId={false} />
|
||||
) : null}
|
||||
</Flex>
|
||||
<Flex gap={'small'}>
|
||||
{rowActions
|
||||
|
||||
@ -213,6 +213,14 @@ export function getModelByPrefix(prefix) {
|
||||
)
|
||||
}
|
||||
|
||||
export function searchModelsByLabel(label) {
|
||||
return objectModels.filter(
|
||||
(meta) =>
|
||||
meta.label.toLowerCase().includes(label.toLowerCase()) ||
|
||||
meta.labelPlural.toLowerCase().includes(label.toLowerCase())
|
||||
)
|
||||
}
|
||||
|
||||
// Utility function to get nested object values
|
||||
export const getPropertyValue = (obj, path) => {
|
||||
if (!obj || !path) return undefined
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user