112 lines
3.3 KiB
TypeScript
112 lines
3.3 KiB
TypeScript
import { reactive, watch, onScopeDispose } from 'vue'
|
|
import { stripSelfColumnParams } from '../../../../composables/columnFacetContext'
|
|
import type { ColumnFilterDef } from '../../../../composables/useClientColumnFilters'
|
|
|
|
/** Mobile picker owns a temporary selection until confirmation. */
|
|
export function useMobileCertFacet(config: {
|
|
defs: ColumnFilterDef[]
|
|
context: () => Record<string, unknown>
|
|
account: () => string
|
|
fetch: (params: {
|
|
field: string
|
|
keyword?: string
|
|
limit?: number
|
|
[key: string]: unknown
|
|
}) => Promise<unknown>
|
|
}) {
|
|
const state = reactive({
|
|
visible: false,
|
|
field: '',
|
|
keyword: '',
|
|
selected: [] as string[],
|
|
options: [] as { value: string; label: string; count: number }[],
|
|
loading: false,
|
|
error: ''
|
|
})
|
|
let generation = 0
|
|
let disposed = false
|
|
let openedContext = ''
|
|
const identity = () => JSON.stringify([config.context(), config.account()])
|
|
function close() {
|
|
generation++
|
|
state.visible = false
|
|
state.loading = false
|
|
state.error = ''
|
|
state.options = []
|
|
state.selected = []
|
|
}
|
|
watch(identity, close, { flush: 'sync' })
|
|
onScopeDispose(() => {
|
|
disposed = true
|
|
close()
|
|
})
|
|
async function retry() {
|
|
if (disposed || !state.visible || openedContext !== identity()) return
|
|
const version = ++generation
|
|
const field = state.field
|
|
const keyword = state.keyword
|
|
const contextKey = identity()
|
|
const current = () =>
|
|
!disposed &&
|
|
state.visible &&
|
|
version === generation &&
|
|
field === state.field &&
|
|
keyword === state.keyword &&
|
|
contextKey === identity()
|
|
state.loading = true
|
|
state.error = ''
|
|
state.options = []
|
|
const context = stripSelfColumnParams(config.context(), field, config.defs)
|
|
for (const key of [
|
|
'sortField',
|
|
'sortOrder',
|
|
'includeDocumentAttachments',
|
|
'includeArrangeCopyFlags',
|
|
'exportFormat',
|
|
'page',
|
|
'pageSize'
|
|
])
|
|
delete context[key]
|
|
try {
|
|
const result: any = await config.fetch({ ...context, field, keyword, limit: 500 })
|
|
if (!current()) return
|
|
const data = result?.list ? result : result?.data || result || {}
|
|
state.options = (Array.isArray(data.list) ? data.list : []).map((item: any) => ({
|
|
value: String(item?.value ?? ''),
|
|
label: String(item?.label ?? item?.value ?? ''),
|
|
count: Number(item?.count ?? 0)
|
|
}))
|
|
} catch (error: any) {
|
|
if (current()) state.error = error?.message || '加载可选值失败'
|
|
} finally {
|
|
if (current()) state.loading = false
|
|
}
|
|
}
|
|
function open(field: string, selected: string[]) {
|
|
if (disposed) return
|
|
close()
|
|
state.field = field
|
|
state.keyword = ''
|
|
state.selected = selected.map(String)
|
|
openedContext = identity()
|
|
state.visible = true
|
|
void retry()
|
|
}
|
|
function search(keyword: string) {
|
|
state.keyword = keyword
|
|
void retry()
|
|
}
|
|
function toggle(value: string) {
|
|
state.selected = state.selected.includes(value)
|
|
? state.selected.filter((item) => item !== value)
|
|
: [...state.selected, value]
|
|
}
|
|
function confirm() {
|
|
if (disposed || !state.visible || openedContext !== identity()) return null
|
|
const result = { field: state.field, selected: [...state.selected] }
|
|
close()
|
|
return result
|
|
}
|
|
return { state, open, close, search, retry, toggle, confirm }
|
|
}
|