161 lines
4.1 KiB
Vue
161 lines
4.1 KiB
Vue
<script setup lang="ts">
|
||
import { ref, watch } from "vue";
|
||
import { ElMessage } from "element-plus";
|
||
import {
|
||
getSortPreference,
|
||
saveSortPreference,
|
||
type ReviewSortPreset,
|
||
type ReviewSortPreference,
|
||
} from "@/api/review";
|
||
|
||
const props = defineProps<{ open: boolean }>();
|
||
const emit = defineEmits<{ "update:open": [value: boolean] }>();
|
||
|
||
// 智能排序偏好(复习-01):语义档位,不是权重滑块
|
||
const preference = ref<ReviewSortPreference | null>(null);
|
||
const preset = ref<ReviewSortPreset>("TIME_FIRST");
|
||
const loading = ref(false);
|
||
const loadFailed = ref(false);
|
||
const saving = ref(false);
|
||
const presetOptions: { value: ReviewSortPreset; label: string; hint: string }[] = [
|
||
{ value: "TIME_FIRST", label: "时间为主", hint: "久没接触的更容易出现(默认)" },
|
||
{ value: "BALANCED", label: "均衡", hint: "结合接触时间与自己对碎片的明确反馈,不使用回忆评分" },
|
||
];
|
||
|
||
/** 打开时才读偏好:一个只影响首页 Feed 的设置,没必要在页面挂载时就发请求 */
|
||
const loadPreference = async () => {
|
||
if (loading.value) return;
|
||
loading.value = true;
|
||
loadFailed.value = false;
|
||
try {
|
||
const res = await getSortPreference();
|
||
const data = res?.data;
|
||
if (data) {
|
||
preference.value = data;
|
||
preset.value = data.preset === "MASTERY_FIRST" ? "BALANCED" : data.preset;
|
||
}
|
||
} catch {
|
||
loadFailed.value = true;
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
};
|
||
|
||
watch(
|
||
() => props.open,
|
||
(open) => {
|
||
if (open) void loadPreference();
|
||
},
|
||
{ immediate: true },
|
||
);
|
||
|
||
const close = () => {
|
||
emit("update:open", false);
|
||
};
|
||
|
||
const confirm = async () => {
|
||
if (saving.value) return;
|
||
saving.value = true;
|
||
try {
|
||
const res = await saveSortPreference(preset.value);
|
||
if (res?.data) {
|
||
preference.value = res.data;
|
||
preset.value = res.data.preset === "MASTERY_FIRST" ? "BALANCED" : res.data.preset;
|
||
}
|
||
close();
|
||
ElMessage.success("智能排序偏好已保存");
|
||
} catch {
|
||
// 请求层已提示具体错误
|
||
} finally {
|
||
saving.value = false;
|
||
}
|
||
};
|
||
</script>
|
||
|
||
<template>
|
||
<el-dialog :model-value="open" class="sort-dialog" title="智能排序偏好" width="460px" @update:model-value="emit('update:open', $event)">
|
||
<p class="sort-hint">
|
||
决定首页「复习回看」里哪些知识碎片更容易出现,不改变复习总览的回忆记录。
|
||
</p>
|
||
<p v-if="loadFailed" class="sort-error" role="alert">
|
||
当前档位未能读取。<el-button size="small" :loading="loading" @click="loadPreference">重试读取</el-button>
|
||
</p>
|
||
<el-radio-group v-model="preset" class="sort-options">
|
||
<el-radio
|
||
v-for="option in presetOptions"
|
||
:key="option.value"
|
||
:value="option.value"
|
||
class="sort-option"
|
||
>
|
||
<span class="sort-option-label">{{ option.label }}</span>
|
||
<span class="sort-option-hint">{{ option.hint }}</span>
|
||
</el-radio>
|
||
</el-radio-group>
|
||
<p v-if="preference?.description" class="sort-current">
|
||
当前档位说明:{{ preference.description }}
|
||
</p>
|
||
<template #footer>
|
||
<el-button @click="close">取消</el-button>
|
||
<el-button type="success" :loading="saving" @click="confirm">保存</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.sort-hint {
|
||
margin: 0 0 12px;
|
||
font-size: 13px;
|
||
color: var(--text-secondary);
|
||
line-height: 1.6;
|
||
}
|
||
|
||
.sort-error {
|
||
display: flex;
|
||
align-items: center;
|
||
flex-wrap: wrap;
|
||
gap: 8px;
|
||
margin: 0 0 12px;
|
||
font-size: 13px;
|
||
color: var(--accent-danger-text);
|
||
}
|
||
|
||
.sort-options {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: stretch;
|
||
gap: 8px;
|
||
}
|
||
|
||
.sort-option {
|
||
display: flex;
|
||
align-items: flex-start;
|
||
height: auto;
|
||
padding: 8px;
|
||
border: 1px solid var(--border-soft);
|
||
border-radius: 8px;
|
||
margin-right: 0;
|
||
}
|
||
|
||
.sort-option-label {
|
||
font-weight: 600;
|
||
color: var(--green-800);
|
||
}
|
||
|
||
.sort-option-hint {
|
||
margin-left: 8px;
|
||
font-size: 12px;
|
||
color: var(--text-secondary);
|
||
overflow-wrap: anywhere;
|
||
}
|
||
|
||
.sort-option :deep(.el-radio__label) {
|
||
white-space: normal;
|
||
}
|
||
|
||
.sort-current {
|
||
margin: 12px 0 0;
|
||
font-size: 12px;
|
||
color: var(--text-secondary);
|
||
}
|
||
</style>
|