311 lines
15 KiB
Vue
311 lines
15 KiB
Vue
<script setup lang="ts">
|
||
import { computed, onBeforeUnmount, ref, watch } from 'vue'
|
||
import { onShow } from '@dcloudio/uni-app'
|
||
import { Box, Check, ChevronDown, ChevronLeft, ChevronRight, Folder, House, LogOut, MapPin, Plus, Search, Star, UserRound, X } from '@lucide/vue'
|
||
import { ApiError, api, login, logout, message, signedIn } from '../api'
|
||
import { family, type Item, type Place, type Space } from '../types'
|
||
import { activeSpace, canWrite, loadPlaces, loadSpaces, openIntake, openItem, places, revision, selectSpace, spaces } from '../workspace'
|
||
|
||
const username = ref('')
|
||
const password = ref('')
|
||
const signingIn = ref(false)
|
||
const spaceBusy = ref(false)
|
||
const creating = ref(false)
|
||
const newName = ref('')
|
||
const addingPlace = ref(false)
|
||
const placeName = ref('')
|
||
const placeBusy = ref(false)
|
||
const topError = ref('')
|
||
const placesError = ref('')
|
||
const readingPlaces = ref(false)
|
||
const tab = ref<'items' | 'places' | 'profile'>('items')
|
||
const query = ref('')
|
||
const favorites = ref(false)
|
||
const parentId = ref('')
|
||
const subtree = ref(true)
|
||
const rows = ref<Item[]>([])
|
||
const loadedKey = ref('')
|
||
const nextOffset = ref(0)
|
||
const loading = ref(false)
|
||
const more = ref(false)
|
||
const listError = ref('')
|
||
const place = computed(() => places.value.find((entry) => entry.id === parentId.value))
|
||
const children = computed(() => places.value
|
||
.filter((entry) => (entry.parentId ?? '') === parentId.value)
|
||
.sort((left, right) => (left.sortRank ?? Number.MAX_SAFE_INTEGER) - (right.sortRank ?? Number.MAX_SAFE_INTEGER)))
|
||
const scope = computed(() => tab.value === 'places' ? parentId.value : '')
|
||
const endpoint = computed(() => {
|
||
if (!activeSpace.value || tab.value === 'profile' || (tab.value === 'places' && !scope.value)) return ''
|
||
const params = new URLSearchParams({ limit: '30', q: tab.value === 'items' ? query.value.trim() : '',
|
||
favorites: String(tab.value === 'items' && favorites.value) })
|
||
if (scope.value) {
|
||
params.set('location', scope.value)
|
||
if (subtree.value) params.set('includeDescendants', 'true')
|
||
}
|
||
return family(activeSpace.value.id) + '/items?' + params.toString()
|
||
})
|
||
let listRequest: AbortController | undefined
|
||
let timer: number | undefined
|
||
let seenRevision = -1
|
||
const previews = new Map<string, { rows: Item[]; revision: number }>()
|
||
|
||
async function bootstrap() {
|
||
if (!signedIn.value || spaceBusy.value) return
|
||
spaceBusy.value = true
|
||
topError.value = ''
|
||
try { await loadSpaces() }
|
||
catch (error) { if (signedIn.value) topError.value = message(error) }
|
||
finally { spaceBusy.value = false }
|
||
}
|
||
|
||
onShow(() => {
|
||
if (!signedIn.value) return
|
||
if (!activeSpace.value) void bootstrap()
|
||
if (seenRevision !== revision.value) {
|
||
seenRevision = revision.value
|
||
restart(false)
|
||
void readPlaces()
|
||
}
|
||
})
|
||
|
||
async function signIn() {
|
||
if (signingIn.value) return
|
||
signingIn.value = true
|
||
topError.value = ''
|
||
try {
|
||
await login(username.value.trim(), password.value)
|
||
password.value = ''
|
||
await bootstrap()
|
||
} catch (error) { topError.value = message(error) }
|
||
finally { signingIn.value = false }
|
||
}
|
||
|
||
async function readPlaces() {
|
||
placesError.value = ''
|
||
readingPlaces.value = true
|
||
try {
|
||
await loadPlaces()
|
||
if (parentId.value && !places.value.some((entry) => entry.id === parentId.value)) parentId.value = ''
|
||
} catch (error) { placesError.value = message(error) }
|
||
finally { readingPlaces.value = false }
|
||
}
|
||
|
||
function switchSpace(event: Event) {
|
||
const chosen = spaces.value.find((space) => space.id === (event.target as HTMLSelectElement).value)
|
||
if (chosen) selectSpace(chosen)
|
||
}
|
||
|
||
function signedOut() {
|
||
previews.clear()
|
||
query.value = ''
|
||
favorites.value = false
|
||
parentId.value = ''
|
||
tab.value = 'items'
|
||
topError.value = ''
|
||
void logout()
|
||
}
|
||
|
||
async function createSpace() {
|
||
if (spaceBusy.value || !newName.value.trim()) return
|
||
spaceBusy.value = true
|
||
topError.value = ''
|
||
try {
|
||
const space = await api<Space>('/api/households', { method: 'POST', body: { name: newName.value.trim() } })
|
||
spaces.value = [...spaces.value, space]
|
||
selectSpace(space)
|
||
creating.value = false
|
||
newName.value = ''
|
||
} catch (error) { topError.value = message(error) }
|
||
finally { spaceBusy.value = false }
|
||
}
|
||
|
||
watch(() => activeSpace.value?.id, () => {
|
||
previews.clear()
|
||
parentId.value = ''
|
||
favorites.value = false
|
||
addingPlace.value = false
|
||
if (activeSpace.value) void readPlaces()
|
||
})
|
||
|
||
watch([endpoint, revision], () => {
|
||
seenRevision = revision.value
|
||
restart(tab.value === 'items' && !!query.value.trim())
|
||
})
|
||
|
||
function restart(debounce: boolean) {
|
||
clearTimeout(timer)
|
||
listRequest?.abort()
|
||
const current = endpoint.value
|
||
if (loadedKey.value !== current) {
|
||
const cached = previews.get(current)
|
||
rows.value = cached?.revision === revision.value ? cached.rows : []
|
||
more.value = rows.value.length === 30
|
||
nextOffset.value = rows.value.length
|
||
loadedKey.value = current
|
||
}
|
||
listError.value = ''
|
||
loading.value = !!current
|
||
if (current) timer = window.setTimeout(() => void loadRows(), debounce ? 170 : 0)
|
||
}
|
||
|
||
async function loadRows(append = false) {
|
||
const current = endpoint.value
|
||
if (!current) return
|
||
listRequest?.abort()
|
||
const request = new AbortController()
|
||
listRequest = request
|
||
loading.value = true
|
||
listError.value = ''
|
||
try {
|
||
const page = await api<Item[]>(current + (append ? '&offset=' + nextOffset.value : ''), { signal: request.signal })
|
||
if (request.signal.aborted || current !== endpoint.value) return
|
||
const seen = new Set(append ? rows.value.map((item) => item.id) : [])
|
||
rows.value = append ? [...rows.value, ...page.filter((item) => !seen.has(item.id))] : page
|
||
nextOffset.value = (append ? nextOffset.value : 0) + page.length
|
||
loadedKey.value = current
|
||
more.value = page.length === 30 && nextOffset.value <= 10000
|
||
if (!append) {
|
||
previews.delete(current)
|
||
previews.set(current, { rows: page, revision: revision.value })
|
||
if (previews.size > 10) previews.delete(previews.keys().next().value as string)
|
||
}
|
||
} catch (error) {
|
||
if (!request.signal.aborted) {
|
||
if (error instanceof ApiError && [403, 404].includes(error.status)) {
|
||
previews.delete(current)
|
||
rows.value = []
|
||
more.value = false
|
||
}
|
||
listError.value = message(error)
|
||
}
|
||
}
|
||
finally { if (!request.signal.aborted) loading.value = false }
|
||
}
|
||
|
||
const roleLabel = (space: Space) => ({ OWNER: '所有者', EDITOR: '可编辑', VIEWER: '仅查看' })[space.role]
|
||
function openPlace(entry: Place) { parentId.value = entry.id; subtree.value = true; addingPlace.value = false }
|
||
function showFavorites() { favorites.value = true; query.value = ''; tab.value = 'items' }
|
||
|
||
async function createPlace() {
|
||
const space = activeSpace.value
|
||
if (!space || !canWrite.value || placeBusy.value || !placeName.value.trim()) return
|
||
placeBusy.value = true
|
||
placesError.value = ''
|
||
try {
|
||
const child = await api<Place>(family(space.id) + '/locations',
|
||
{ method: 'POST', body: { name: placeName.value.trim(), parentId: parentId.value || null } })
|
||
if (activeSpace.value?.id !== space.id) return
|
||
places.value = [...places.value, child]
|
||
addingPlace.value = false
|
||
placeName.value = ''
|
||
revision.value++
|
||
void readPlaces()
|
||
} catch (error) { if (activeSpace.value?.id === space.id) placesError.value = message(error) }
|
||
finally { placeBusy.value = false }
|
||
}
|
||
onBeforeUnmount(() => { clearTimeout(timer); listRequest?.abort() })
|
||
</script>
|
||
|
||
<template>
|
||
<main v-if="!signedIn" class="phone welcome">
|
||
<div class="welcome-brand"><img src="/static/brand.png" alt="" /><span>见物</span></div>
|
||
<div class="welcome-copy">
|
||
<h1>于寻常处,<br />见生活。</h1>
|
||
<p>记下家里的东西,下次找它,知道从哪里找起。</p>
|
||
</div>
|
||
<form class="form sign-form" @submit="signIn">
|
||
<label>见物账号<input v-model="username" aria-label="见物账号" required autocomplete="username" maxlength="100" /></label>
|
||
<label>密码<input v-model="password" aria-label="密码" required type="password" autocomplete="current-password" /></label>
|
||
<p v-if="topError" class="notice error" role="alert">{{ topError }}</p>
|
||
<button class="primary" form-type="submit" :disabled="signingIn">{{ signingIn ? '登录中…' : '登录' }}</button>
|
||
</form>
|
||
</main>
|
||
<main v-else class="phone workbench">
|
||
<header class="work-top">
|
||
<div class="brand-small"><img src="/static/brand.png" alt="" /><span>见物</span></div>
|
||
<div v-if="spaces.length" class="space-select">
|
||
<House :size="15" /><select aria-label="当前空间" :value="activeSpace?.id" @change="switchSpace">
|
||
<option v-for="space in spaces" :key="space.id" :value="space.id">{{ space.name }}</option>
|
||
</select><ChevronDown :size="13" />
|
||
</div>
|
||
</header>
|
||
<p v-if="topError" class="notice error" role="alert">{{ topError }} <button class="inline" @click="bootstrap">重试</button></p>
|
||
<div v-if="!activeSpace && !spaceBusy" class="blank">
|
||
<House :size="27" /><h1>先建一个空间</h1>
|
||
<button class="primary" @click="creating = true">新建空间</button>
|
||
</div>
|
||
<p v-else-if="!activeSpace" class="center secondary" role="status">正在读取空间…</p>
|
||
<template v-if="activeSpace">
|
||
<section v-if="tab === 'items'">
|
||
<div class="section-top">
|
||
<h1>{{ favorites ? '我的收藏' : '找物' }}</h1>
|
||
<button v-if="favorites" class="quiet" @click="favorites = false">全部物品</button>
|
||
<button v-else class="square" aria-label="我的收藏" @click="showFavorites"><Star :size="19" /></button>
|
||
</div>
|
||
<form class="search" @submit="loadRows()">
|
||
<Search :size="19" /><input v-model="query" type="search" maxlength="150" aria-label="找物" placeholder="搜名称、别名或位置" />
|
||
<button v-if="query" class="square quiet" aria-label="清除搜索" @click="query = ''"><X :size="15" /></button>
|
||
</form>
|
||
</section>
|
||
<section v-if="tab === 'places'">
|
||
<div class="section-top"><h1>位置</h1><button v-if="parentId" class="quiet" @click="parentId = place?.parentId || ''; addingPlace = false"><ChevronLeft :size="16" />上一级</button></div>
|
||
<p v-if="place" class="location-path"><MapPin :size="17" />{{ place.path }}</p>
|
||
<p v-if="placesError" class="notice error" role="alert">{{ placesError }} <button class="inline" @click="readPlaces">重试</button></p>
|
||
<div v-if="children.length" class="card row-list">
|
||
<button v-for="child in children" :key="child.id" class="result-row" @click="openPlace(child)">
|
||
<span class="tile amber"><Folder :size="20" /></span><span class="row-info"><strong>{{ child.name }}</strong><span>{{ child.path }}</span></span><ChevronRight :size="17" />
|
||
</button>
|
||
</div>
|
||
<p v-else-if="readingPlaces" class="center secondary" role="status">正在读取位置…</p>
|
||
<div v-else-if="!place && !placesError && !addingPlace" class="blank"><Folder :size="29" /><h2>这里还没有位置</h2></div>
|
||
<button v-if="canWrite && !addingPlace" class="add-place" @click="addingPlace = true"><Plus :size="16" />{{ parentId ? '新建子位置' : '新建位置' }}</button>
|
||
<form v-if="addingPlace" class="form card create-form" @submit="createPlace">
|
||
<label>位置名称<input v-model="placeName" aria-label="位置名称" required maxlength="100" /></label>
|
||
<div class="form-actions"><button @click="addingPlace = false">取消</button><button class="primary" form-type="submit" :disabled="placeBusy">保存位置</button></div>
|
||
</form>
|
||
<div v-if="place" class="subhead">
|
||
<h2>这里的物品</h2>
|
||
<button v-if="children.length" class="quiet" :aria-pressed="subtree" @click="subtree = !subtree"><Check v-if="subtree" :size="15" />含下级位置</button>
|
||
<button v-if="canWrite" class="quiet" @click="openIntake(place.id)"><Plus :size="16" />在这里录入</button>
|
||
</div>
|
||
</section>
|
||
<section v-if="endpoint" aria-label="物品结果">
|
||
<p v-if="listError" class="notice error" role="alert">{{ listError }} <button class="inline" @click="loadRows()">重试</button></p>
|
||
<div v-if="rows.length" class="card row-list">
|
||
<button v-for="item in rows" :key="item.id" class="result-row" @click="openItem(item.id)">
|
||
<span class="tile"><Box :size="20" /></span>
|
||
<span class="row-info"><strong>{{ item.name }}</strong><span>{{ item.locationPath || '未设置位置' }}</span></span>
|
||
<small v-if="item.quantity > 1">×{{ item.quantity }}</small><ChevronRight :size="17" />
|
||
</button>
|
||
</div>
|
||
<p v-if="loading && !rows.length" class="center secondary" role="status">正在读取物品…</p>
|
||
<div v-else-if="!rows.length && !listError" class="blank">
|
||
<Box :size="30" /><h2>{{ query.trim() && tab === 'items' ? '没有找到物品' : favorites && tab === 'items' ? '还没有收藏' : '这里还没有物品' }}</h2>
|
||
<button v-if="canWrite && !favorites && !query.trim()" @click="openIntake(scope)"><Plus :size="16" />录入物品</button>
|
||
</div>
|
||
<div v-if="more" class="center"><button :disabled="loading" @click="loadRows(true)">加载更多</button></div>
|
||
</section>
|
||
<section v-if="tab === 'profile'">
|
||
<div class="section-top"><h1>我的</h1></div>
|
||
<div class="card profile-card"><span class="tile"><UserRound :size="23" /></span><div><strong>{{ username }}</strong><p>{{ activeSpace.name }} · {{ roleLabel(activeSpace) }}</p></div></div>
|
||
<div class="card row-list">
|
||
<button class="result-row" @click="showFavorites"><Star :size="19" /><span class="row-info"><strong>我的收藏</strong></span><ChevronRight :size="17" /></button>
|
||
<button class="result-row" @click="creating = true"><Plus :size="20" /><span class="row-info"><strong>新建空间</strong></span><ChevronRight :size="17" /></button>
|
||
<button class="result-row" @click="signedOut"><LogOut :size="18" /><span class="row-info"><strong>退出登录</strong></span></button>
|
||
</div>
|
||
</section>
|
||
</template>
|
||
<form v-if="creating" class="form card create-form" @submit="createSpace">
|
||
<h2>新建空间</h2>
|
||
<label>空间名称<input v-model="newName" required maxlength="100" /></label>
|
||
<div class="form-actions"><button @click="creating = false">取消</button><button class="primary" form-type="submit" :disabled="spaceBusy">创建</button></div>
|
||
</form>
|
||
<nav v-if="activeSpace" class="dock" aria-label="主导航">
|
||
<button :aria-current="tab === 'items' ? 'page' : undefined" @click="tab = 'items'"><Search :size="20" />找物</button>
|
||
<button :aria-current="tab === 'places' ? 'page' : undefined" @click="tab = 'places'"><Folder :size="20" />位置</button>
|
||
<button v-if="canWrite" class="dock-add" @click="openIntake(scope)"><span><Plus :size="24" /></span>录入</button>
|
||
<button :aria-current="tab === 'profile' ? 'page' : undefined" @click="tab = 'profile'"><UserRound :size="20" />我的</button>
|
||
</nav>
|
||
</main>
|
||
</template>
|