import { nextTick, ref, watch, type Ref } from 'vue';
* 链接选择弹窗列表加载:打开时拉取一次,关闭仅重置表单;避免分页初始事件重复请求
*/
export function useLinkPickerLoad(options: {
loadStamp: Ref<number>;
resetFlag: Ref<boolean>;
resetFields: () => void;
fetchList: (page: number) => void;
}) {
const paginationReady = ref(false);
const filterFetchEnabled = ref(false);
const loadFirstPage = () => {
filterFetchEnabled.value = false;
paginationReady.value = false;
options.resetFields();
options.fetchList(1);
nextTick(() => {
paginationReady.value = true;
window.setTimeout(() => {
filterFetchEnabled.value = true;
}, 100);
});
};
watch(
options.loadStamp,
(val, oldVal) => {
if (!val || val === oldVal) {
return;
}
loadFirstPage();
},
{ immediate: true },
);
watch(options.resetFlag, () => {
options.resetFields();
});
const onPageChange = (page: number) => {
if (!paginationReady.value) {
return;
}
options.fetchList(page);
};
const runFilterFetch = (fn: () => void) => {
if (!filterFetchEnabled.value) {
return;
}
fn();
};
return { onPageChange, filterFetchEnabled, runFilterFetch };
}