Files
lpt-fe/src/components/ReviewHistoryList.vue
T

205 lines
6.1 KiB
Vue

<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { listRecalls, type RecallSummary } from "@/api/recalls";
import { useDelayedVisible } from "./composables/useDelayedVisible";
import ReviewRecallRow from "./ReviewRecallRow.vue";
const props = withDefaults(defineProps<{ excludeRecallNum?: string | null }>(), { excludeRecallNum: null });
/** 首屏只放最近三条:再长的历史进「查看全部」弹窗,总览页不铺长列表 */
const RECENT_SIZE = 3;
/** 多取一条:顶部草稿可能也在最近列表里,过滤后仍能保留 3 条 */
const RECENT_FETCH_SIZE = RECENT_SIZE + 1;
/** 弹窗内分页沿用列表页大小 */
const DIALOG_PAGE_SIZE = 20;
const records = ref<RecallSummary[]>([]);
const recent = computed(() => records.value.filter(record => record.recallNum !== props.excludeRecallNum).slice(0, RECENT_SIZE));
const total = ref(0);
const loaded = ref(false);
const loading = ref(false);
const failed = ref(false);
const loadingVisible = useDelayedVisible(loading);
const dialogOpen = ref(false);
const dialogRecords = ref<RecallSummary[]>([]);
const dialogPage = ref(1);
const dialogLoaded = ref(false);
const dialogLoading = ref(false);
const dialogFailed = ref(false);
const dialogLoadingVisible = useDelayedVisible(dialogLoading);
const dialogPages = computed(() => Math.max(1, Math.ceil(total.value / DIALOG_PAGE_SIZE)));
const loadRecent = async () => {
if (loading.value) return;
loading.value = true;
failed.value = false;
try {
const page = (await listRecalls({ pageNum: 1, pageSize: RECENT_FETCH_SIZE }, { silent: true })).data;
records.value = page.records;
total.value = page.total;
loaded.value = true;
} catch {
failed.value = true;
} finally {
loading.value = false;
}
};
const loadPage = async (pageNum = dialogPage.value) => {
if (dialogLoading.value) return;
dialogLoading.value = true;
dialogFailed.value = false;
try {
const page = (await listRecalls({ pageNum, pageSize: DIALOG_PAGE_SIZE }, { silent: true })).data;
dialogRecords.value = page.records;
dialogPage.value = page.current;
total.value = page.total;
dialogLoaded.value = true;
} catch {
dialogFailed.value = true;
} finally {
dialogLoading.value = false;
}
};
const openAll = () => {
dialogOpen.value = true;
void loadPage(1);
};
onMounted(() => {
void loadRecent();
});
</script>
<template>
<section class="module review-history">
<div class="module-head">
<h2 class="module-title">最近回忆</h2>
<span v-if="loaded && total > 0" class="history-total">{{ total }} 条</span>
<el-button v-if="total > RECENT_SIZE" class="history-more" text size="small" @click="openAll">
查看全部
</el-button>
</div>
<p v-if="loadingVisible" role="status">正在读取回忆记录…</p>
<div v-if="failed" class="history-error" role="alert">
回忆记录未能读取。<el-button size="small" :loading="loading" @click="loadRecent">重试读取</el-button>
</div>
<template v-else-if="loaded">
<p v-if="!recent.length" class="history-empty">{{ total > 0 ? '最近的记录已在上面显示。' : '还没有自由回忆记录,选一个标签范围开始第一次回忆。' }}</p>
<div v-else class="history-rows">
<ReviewRecallRow v-for="record in recent" :key="record.recallNum" :record="record" />
</div>
</template>
<el-dialog v-model="dialogOpen" class="history-dialog" title="全部回忆记录" width="640px">
<p v-if="dialogLoadingVisible" role="status">正在读取回忆记录…</p>
<div v-if="dialogFailed" role="alert">
回忆记录未能读取。<el-button size="small" :loading="dialogLoading" @click="loadPage()">重试读取</el-button>
</div>
<template v-else>
<div v-if="dialogPages > 1" class="history-pager">
<el-button size="small" :disabled="dialogPage <= 1 || dialogLoading" @click="loadPage(dialogPage - 1)">上一页</el-button>
<span>第 {{ dialogPage }} / {{ dialogPages }} 页</span>
<el-button size="small" :disabled="dialogPage >= dialogPages || dialogLoading" @click="loadPage(dialogPage + 1)">下一页</el-button>
</div>
<p v-if="dialogLoaded && !dialogRecords.length" class="history-empty">还没有自由回忆记录。</p>
<div class="history-rows">
<ReviewRecallRow v-for="record in dialogRecords" :key="record.recallNum" :record="record" />
</div>
<el-pagination
v-if="dialogPages > 1"
class="history-pagination"
layout="prev, pager, next"
:total="total"
:page-size="DIALOG_PAGE_SIZE"
:current-page="dialogPage"
:disabled="dialogLoading"
@current-change="loadPage"
/>
</template>
<template #footer>
<el-button @click="dialogOpen = false">关闭</el-button>
</template>
</el-dialog>
</section>
</template>
<style scoped>
.module {
display: flex;
flex-direction: column;
gap: 12px;
}
.module-head {
display: flex;
align-items: baseline;
flex-wrap: wrap;
gap: 8px 12px;
}
.module-title {
margin: 0;
font-size: 17px;
font-weight: 600;
color: var(--green-900);
}
.history-total {
font-size: 13px;
color: var(--text-secondary);
font-variant-numeric: tabular-nums;
}
.history-more {
margin-left: auto;
}
.history-empty {
margin: 0;
font-size: 13px;
color: var(--text-secondary);
}
.history-error {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
font-size: 13px;
color: var(--accent-danger-text);
}
.history-rows {
display: flex;
flex-direction: column;
gap: 8px;
}
.history-pager {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 8px;
font-size: 13px;
color: var(--text-secondary);
font-variant-numeric: tabular-nums;
}
.history-pagination {
margin-top: 12px;
}
</style>
<style>
/* 20 条一页的弹窗列表在桌面上会超出视口,只让弹窗正文滚动 */
.history-dialog .el-dialog__body {
max-height: min(60vh, calc(100dvh - 200px));
overflow-y: auto;
}
</style>