218 lines
11 KiB
Vue
218 lines
11 KiB
Vue
<script setup lang="ts">
|
|
import { computed, ref, watch } from 'vue'
|
|
import { Archive, ArrowLeftRight, ArrowRight, Box, ChevronRight, Folder, House, Map, Plus, Search, Star } from '@lucide/vue'
|
|
import { useRoute, useRouter } from 'vue-router'
|
|
import { api, message } from '../api'
|
|
import { family, type Item, type ItemDetail, type Place, type Space } from '../types'
|
|
import IntakeDialog from './IntakeDialog.vue'
|
|
|
|
const props = defineProps<{ space: Space }>()
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
const root = family(props.space.id)
|
|
const canWrite = computed(() => props.space.role !== 'VIEWER')
|
|
const query = ref('')
|
|
const location = computed(() => typeof route.query.location === 'string' ? route.query.location : '')
|
|
const subtree = ref(true)
|
|
const onlyFavorites = ref(false)
|
|
const places = ref<Place[]>([])
|
|
const items = ref<Item[]>([])
|
|
const detail = ref<ItemDetail>()
|
|
const selected = ref('')
|
|
const detailError = ref('')
|
|
const listError = ref('')
|
|
const locationError = ref('')
|
|
const loading = ref(true)
|
|
const more = ref(false)
|
|
const next = ref(0)
|
|
const revision = ref(0)
|
|
const intake = ref<'add' | 'edit'>()
|
|
const moving = ref(false)
|
|
const moveTo = ref('')
|
|
const busy = ref(false)
|
|
const writeError = ref('')
|
|
const folder = computed(() => places.value.find((place) => place.id === location.value))
|
|
const hasChildren = computed(() => places.value.some((place) => place.parentId === folder.value?.id))
|
|
const filters = computed(() => {
|
|
const value = new URLSearchParams({ limit: '50', q: query.value.trim(), favorites: String(onlyFavorites.value) })
|
|
if (location.value) {
|
|
value.set('location', location.value)
|
|
if (subtree.value) value.set('includeDescendants', 'true')
|
|
}
|
|
return value.toString()
|
|
})
|
|
const path = computed(() => root + '/items?' + filters.value)
|
|
|
|
async function fetchPlaces(signal?: AbortSignal) {
|
|
try { places.value = await api<Place[]>(root + '/locations', { signal }); locationError.value = '' }
|
|
catch (cause) { if (!signal?.aborted) locationError.value = message(cause) }
|
|
}
|
|
watch(() => props.space.id, (_, __, clean) => {
|
|
const controller = new AbortController()
|
|
clean(() => controller.abort())
|
|
void fetchPlaces(controller.signal)
|
|
}, { immediate: true })
|
|
|
|
watch([path, revision], (_, __, clean) => {
|
|
const controller = new AbortController()
|
|
const endpoint = path.value
|
|
loading.value = true
|
|
listError.value = ''
|
|
items.value = []
|
|
more.value = false
|
|
next.value = 0
|
|
const timer = window.setTimeout(async () => {
|
|
try {
|
|
const page = await api<Item[]>(endpoint, { signal: controller.signal })
|
|
if (!controller.signal.aborted) { items.value = page; more.value = page.length === 50; next.value = page.length }
|
|
} catch (cause) { if (!controller.signal.aborted) listError.value = message(cause) }
|
|
finally { if (!controller.signal.aborted) loading.value = false }
|
|
}, query.value.trim() ? 180 : 0)
|
|
clean(() => { clearTimeout(timer); controller.abort() })
|
|
}, { immediate: true })
|
|
|
|
watch([selected, revision], async ([id], _, clean) => {
|
|
detail.value = undefined
|
|
detailError.value = ''
|
|
if (!id) return
|
|
const controller = new AbortController()
|
|
clean(() => controller.abort())
|
|
try { detail.value = await api<ItemDetail>(root + '/items/' + id, { signal: controller.signal }) }
|
|
catch (cause) { if (!controller.signal.aborted) detailError.value = message(cause) }
|
|
}, { immediate: true })
|
|
|
|
async function loadMore() {
|
|
const endpoint = path.value
|
|
const version = revision.value
|
|
const offset = next.value
|
|
loading.value = true
|
|
listError.value = ''
|
|
try {
|
|
const page = await api<Item[]>(endpoint + '&offset=' + offset)
|
|
if (path.value !== endpoint || revision.value !== version) return
|
|
const existing = new Set(items.value.map((item) => item.id))
|
|
items.value = [...items.value, ...page.filter((item) => !existing.has(item.id))]
|
|
next.value += page.length
|
|
more.value = page.length === 50 && next.value <= 10000
|
|
} catch (cause) { if (path.value === endpoint && revision.value === version) listError.value = message(cause) }
|
|
finally { if (path.value === endpoint && revision.value === version) loading.value = false }
|
|
}
|
|
|
|
function chooseLocation(id: string) {
|
|
selected.value = ''
|
|
void router.replace({ query: id ? { location: id } : {} })
|
|
}
|
|
|
|
function openItem(id: string) {
|
|
if (busy.value) return
|
|
moving.value = false
|
|
writeError.value = ''
|
|
selected.value = id
|
|
}
|
|
|
|
function saved(id?: string) {
|
|
intake.value = undefined
|
|
if (id) selected.value = id
|
|
revision.value++
|
|
}
|
|
|
|
async function update(method: string, suffix = '', body?: unknown) {
|
|
const item = detail.value?.item
|
|
if (!item || busy.value) return
|
|
busy.value = true
|
|
writeError.value = ''
|
|
try {
|
|
await api(root + '/items/' + item.id + suffix, { method, body })
|
|
if (method === 'DELETE') selected.value = ''
|
|
moving.value = false
|
|
revision.value++
|
|
} catch (cause) { writeError.value = message(cause) }
|
|
finally { busy.value = false }
|
|
}
|
|
|
|
function archiveItem(item: Item) {
|
|
if (window.confirm('删除「' + item.name + '」和它的照片?')) void update('DELETE', '?version=' + item.version)
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="inventory-page">
|
|
<header class="page-header">
|
|
<div><div class="crumb">{{ space.name }}<ChevronRight :size="14" />{{ folder?.path || '物品' }}</div><h1>物品</h1></div>
|
|
<button v-if="canWrite" class="primary" @click="intake = 'add'"><Plus :size="18" />录入物品</button>
|
|
</header>
|
|
<div class="inventory-grid" :class="{ 'has-detail': selected }">
|
|
<aside class="panel location-tree" aria-label="位置">
|
|
<h2>位置</h2>
|
|
<button class="tree-row" :class="{ current: !location && !onlyFavorites }" @click="onlyFavorites = false; chooseLocation('')"><House :size="16" />全部物品</button>
|
|
<button class="tree-row" :class="{ current: onlyFavorites }" @click="onlyFavorites = true; chooseLocation('')"><Star :size="16" />我的收藏</button>
|
|
<div class="tree-divider"></div>
|
|
<p v-if="locationError" class="notice error">{{ locationError }} <button @click="fetchPlaces()">重试</button></p>
|
|
<p v-else-if="!places.length" class="secondary small">空间还没有位置</p>
|
|
<button v-for="place in places" :key="place.id" class="tree-row" :class="{ current: location === place.id }"
|
|
:style="{ paddingInlineStart: (10 + Math.min(5, Math.max(0, (place.depth || 1) - 1)) * 13) + 'px' }"
|
|
:title="place.path" @click="onlyFavorites = false; chooseLocation(place.id)">
|
|
<Folder :size="15" /><span>{{ place.name }}</span>
|
|
</button>
|
|
<RouterLink class="tree-row tree-link" :to="{ name: 'plan', query: folder ? { position: folder.id } : {} }"><Map :size="16" />打开平面图</RouterLink>
|
|
</aside>
|
|
<section class="panel item-list" aria-label="物品列表">
|
|
<div class="list-toolbar">
|
|
<label class="search-field"><Search :size="17" /><input v-model="query" aria-label="找物" placeholder="搜名称、别名或位置" maxlength="150" /></label>
|
|
<label v-if="folder && hasChildren" class="check-field"><input v-model="subtree" type="checkbox" />含下级位置</label>
|
|
</div>
|
|
<p v-if="listError" class="notice error" role="alert">{{ listError }} <button @click="revision++">重试</button></p>
|
|
<div v-if="items.length" class="table-wrap">
|
|
<table>
|
|
<thead><tr><th>物品</th><th>存放位置</th><th>数量</th></tr></thead>
|
|
<tbody>
|
|
<tr v-for="item in items" :key="item.id" :class="{ 'active-row': selected === item.id }">
|
|
<td><button class="item-cell" @click="openItem(item.id)"><span class="item-mark"><Box :size="17" /></span><span>{{ item.name }}</span></button></td>
|
|
<td><button class="table-path" @click="openItem(item.id)">{{ item.locationPath || '未设置' }}</button></td>
|
|
<td>{{ item.quantity }}</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<p v-if="loading" class="list-status" role="status">正在读取物品…</p>
|
|
<div v-else-if="!items.length && !listError" class="empty">
|
|
<Box :size="31" /><h2>{{ query.trim() ? '没有找到物品' : '这里还没有物品' }}</h2>
|
|
<button v-if="canWrite && !query.trim() && !onlyFavorites" @click="intake = 'add'"><Plus :size="16" />添加物品</button>
|
|
</div>
|
|
<div v-if="more" class="list-status"><button :disabled="loading" @click="loadMore">加载更多</button></div>
|
|
</section>
|
|
<aside v-if="selected" class="panel item-detail" aria-label="物品详情">
|
|
<div class="detail-header"><h2>物品详情</h2><button class="quiet" :disabled="busy" @click="openItem('')">关闭</button></div>
|
|
<p v-if="detailError" class="notice error">{{ detailError }} <button @click="revision++">重试</button></p>
|
|
<p v-else-if="!detail" class="secondary" role="status">正在读取详情…</p>
|
|
<template v-else>
|
|
<div class="detail-hero"><span class="item-mark"><Box :size="22" /></span><h3>{{ detail.item.name }}</h3></div>
|
|
<dl>
|
|
<dt>存放位置</dt><dd>{{ detail.item.locationPath || '未设置' }}</dd>
|
|
<dt>数量</dt><dd>{{ detail.item.quantity }}</dd>
|
|
<template v-if="detail.item.aliases.length"><dt>别名</dt><dd>{{ detail.item.aliases.join('、') }}</dd></template>
|
|
<template v-if="detail.item.notes"><dt>备注</dt><dd class="plain-text">{{ detail.item.notes }}</dd></template>
|
|
</dl>
|
|
<div class="detail-actions">
|
|
<RouterLink v-if="detail.item.locationId" class="action" :to="{ name: 'plan', query: { position: detail.item.locationId } }"><Map :size="16" />在图中查看</RouterLink>
|
|
<button :disabled="busy" @click="update('PUT', '/preference', { ...detail.preference, favorite: !detail.preference.favorite })"><Star :size="16" />{{ detail.preference.favorite ? '取消收藏' : '收藏' }}</button>
|
|
<button v-if="canWrite" :disabled="busy" @click="intake = 'edit'">编辑</button>
|
|
<button v-if="canWrite" :disabled="busy" @click="moving = !moving; moveTo = detail.item.locationId ?? ''"><ArrowLeftRight :size="16" />移位</button>
|
|
</div>
|
|
<form v-if="moving && canWrite" class="fields move-form" @submit.prevent="update('POST', '/moves', { locationId: moveTo || null, version: detail.item.version })">
|
|
<label>新位置<select v-model="moveTo"><option value="">暂不设置</option><option v-for="place in places" :key="place.id" :value="place.id">{{ place.path }}</option></select></label>
|
|
<button class="primary" :disabled="busy || (moveTo || null) === detail.item.locationId">确认移位</button>
|
|
</form>
|
|
<p v-if="writeError" class="notice error" role="alert">{{ writeError }}</p>
|
|
<div v-if="detail.moves.length" class="move-history">
|
|
<h3>移位记录</h3>
|
|
<p v-for="(entry, index) in detail.moves" :key="index"><span>{{ entry.fromPath || '未设置' }} <ArrowRight :size="13" /> {{ entry.toPath || '未设置' }}</span><time>{{ entry.movedAt.slice(0, 10) }}</time></p>
|
|
</div>
|
|
<button v-if="canWrite" class="quiet danger" :disabled="busy" @click="archiveItem(detail.item)"><Archive :size="16" />删除物品</button>
|
|
</template>
|
|
</aside>
|
|
</div>
|
|
<IntakeDialog v-if="intake" :space="space.id" :places="places" :location="location" :item="intake === 'edit' ? detail?.item : undefined" @close="intake = undefined" @saved="saved" />
|
|
</div>
|
|
</template>
|