65 lines
4.2 KiB
TypeScript
65 lines
4.2 KiB
TypeScript
'use client'
|
||
|
||
import { useRef } from 'react'
|
||
import Button from '@/components/ui/Button'
|
||
import { FloatingTablePagination } from '@/components/platform/FloatingTablePagination'
|
||
import { structureModelPageSizes, structureModelSelectionLimit } from '@/lib/reference-estimator/standard/structure-model-selection'
|
||
import type { StructureModel } from './structure-client'
|
||
import type { useStructureModelSelection } from './useStructureModelSelection'
|
||
import styles from './skill-maintenance.module.css'
|
||
|
||
/** "SG0164 六格开放书柜" for a model linked to an SPU, otherwise the estimate name. */
|
||
export const structureModelName = (model: Pick<StructureModel, 'title' | 'spu'>) =>
|
||
model.spu ? `${model.spu.code} ${model.spu.name}` : model.title
|
||
|
||
const detail = (model: StructureModel) => model.readable
|
||
? [model.materialType, `修订 ${model.revision}`].filter(Boolean).join(' · ')
|
||
: '这个模型没有可读的结构记录'
|
||
|
||
/** Selects models to preview. The current page and the selection can change independently. */
|
||
export function StructureModelMenu({ picker, busy, onPreview, categoryLabel, id }: {
|
||
picker: ReturnType<typeof useStructureModelSelection>
|
||
busy: boolean
|
||
onPreview: () => void
|
||
categoryLabel: string
|
||
id: string
|
||
}) {
|
||
const list = useRef<HTMLUListElement>(null)
|
||
const { shown, pending, error, chosen, choose } = picker
|
||
const blocked = busy || pending || Boolean(error)
|
||
const remaining = structureModelSelectionLimit - chosen.length
|
||
const available = shown?.models.filter(model => model.readable && !chosen.includes(model.id)).map(model => model.id) ?? []
|
||
const paginate = Boolean(shown && shown.total > structureModelPageSizes[0])
|
||
const go = (change: () => void) => { change(); if (list.current) list.current.scrollTop = 0 }
|
||
return <section id={id} className={styles.menu} aria-label="读取模型">
|
||
<p className={styles.menuTitle}>勾选要读取的{categoryLabel}模型 · 已选 {chosen.length} 个(最多 {structureModelSelectionLimit} 个)</p>
|
||
{pending && <p className={styles.hint} role="status">正在读取模型列表…</p>}
|
||
{error && <p className={styles.hint} role="status">{error} <Button size="small" variant="plain" onClick={picker.retry}>重试</Button></p>}
|
||
{shown && (shown.models.length ? <div className={styles.menuResults}>
|
||
<ul ref={list} className={paginate ? `${styles.menuList} ${styles.menuListPaged}` : styles.menuList} aria-label="测算模型">
|
||
{shown.models.map((model, index) => {
|
||
const checked = chosen.includes(model.id)
|
||
return <li key={model.id}>
|
||
<label className={model.readable ? styles.menuItem : `${styles.menuItem} ${styles.menuItemDisabled}`} title={detail(model)}>
|
||
<input type="checkbox" checked={checked} disabled={blocked || !model.readable || (!checked && !remaining)}
|
||
onChange={() => choose(checked ? chosen.filter(id => id !== model.id) : [...chosen, model.id])} />
|
||
<span className={styles.number} aria-hidden="true">{(shown.page - 1) * shown.pageSize + index + 1}</span>
|
||
<span className={styles.menuName}>{structureModelName(model)}</span>
|
||
</label>
|
||
</li>
|
||
})}
|
||
</ul>
|
||
{paginate && <FloatingTablePagination page={shown.page} pageSize={shown.pageSize} total={shown.total}
|
||
totalPages={shown.totalPages} pageSizes={structureModelPageSizes} loading={blocked}
|
||
onPageChange={page => go(() => picker.changePage(page))} onPageSizeChange={size => go(() => picker.changePageSize(size))} />}
|
||
</div> : <p className={styles.hint}>我保存的这个类目下还没有测算模型。</p>)}
|
||
<div className={styles.menuActions}>
|
||
<Button size="small" variant="plain" disabled={blocked || !available.length || !remaining}
|
||
onClick={() => choose([...chosen, ...available.slice(0, remaining)])}>勾选本页</Button>
|
||
<Button size="small" variant="plain" disabled={busy || !chosen.length} onClick={() => choose([])}>清空勾选</Button>
|
||
<Button size="small" disabled={blocked || !chosen.length} onClick={onPreview}>读出并预览</Button>
|
||
<Button size="small" variant="plain" disabled={busy} onClick={() => picker.setOpen(false)}>收起</Button>
|
||
</div>
|
||
</section>
|
||
}
|