108 lines
2.5 KiB
TypeScript
108 lines
2.5 KiB
TypeScript
export interface SavedViewFilter {
|
|
field: string
|
|
operator: string
|
|
value?: any
|
|
values?: any[]
|
|
from?: string
|
|
to?: string
|
|
}
|
|
|
|
export interface SavedViewSort {
|
|
field: string
|
|
direction: 'asc' | 'desc'
|
|
}
|
|
|
|
export interface SavedView {
|
|
id: string
|
|
name: string
|
|
objectApiName: string
|
|
userId: string
|
|
isOwner: boolean
|
|
isShared: boolean
|
|
strategy: 'query'
|
|
filters: SavedViewFilter[]
|
|
sort: SavedViewSort | null
|
|
description: string | null
|
|
createdAt: string
|
|
updatedAt: string
|
|
}
|
|
|
|
export interface CreateSavedViewPayload {
|
|
name: string
|
|
objectApiName: string
|
|
filters: SavedViewFilter[]
|
|
sort?: SavedViewSort | null
|
|
description?: string
|
|
}
|
|
|
|
export interface UpdateSavedViewPayload {
|
|
name?: string
|
|
filters?: SavedViewFilter[]
|
|
sort?: SavedViewSort | null
|
|
description?: string
|
|
}
|
|
|
|
export function useSavedViews() {
|
|
const { api } = useApi()
|
|
|
|
const savedViews = ref<SavedView[]>([])
|
|
const loading = ref(false)
|
|
|
|
async function fetchSavedViews(objectApiName: string) {
|
|
loading.value = true
|
|
try {
|
|
const data = await api.get(`/saved-views/${objectApiName}`)
|
|
savedViews.value = Array.isArray(data) ? data : []
|
|
} catch (e) {
|
|
console.error('Failed to fetch saved views', e)
|
|
savedViews.value = []
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function createSavedView(payload: CreateSavedViewPayload): Promise<SavedView> {
|
|
const created = await api.post('/saved-views', payload)
|
|
savedViews.value = [...savedViews.value, created]
|
|
return created
|
|
}
|
|
|
|
async function updateSavedView(id: string, payload: UpdateSavedViewPayload): Promise<SavedView> {
|
|
const updated = await api.patch(`/saved-views/${id}`, payload)
|
|
savedViews.value = savedViews.value.map(v => (v.id === id ? updated : v))
|
|
return updated
|
|
}
|
|
|
|
async function deleteSavedView(id: string) {
|
|
await api.delete(`/saved-views/${id}`)
|
|
savedViews.value = savedViews.value.filter(v => v.id !== id)
|
|
}
|
|
|
|
async function suggestViewName(
|
|
objectLabel: string,
|
|
filters: SavedViewFilter[],
|
|
explanation?: string,
|
|
): Promise<string> {
|
|
try {
|
|
const result = await api.post('/ai/suggest-view-name', {
|
|
objectLabel,
|
|
filters,
|
|
explanation,
|
|
})
|
|
return result?.suggestedName || `${objectLabel} View`
|
|
} catch {
|
|
return `${objectLabel} View`
|
|
}
|
|
}
|
|
|
|
return {
|
|
savedViews,
|
|
loading,
|
|
fetchSavedViews,
|
|
createSavedView,
|
|
updateSavedView,
|
|
deleteSavedView,
|
|
suggestViewName,
|
|
}
|
|
}
|