Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions src/hooks/useHistoryList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ export const useHistoryList = (options: Options) => {
noMore: false,
page: 1,
size: 20,
// 防止旧请求结果覆盖新搜索
fetchId: 0,
queryKey: "",
pendingReload: false,
});

const fetchData = async () => {
try {
if (state.loading) return;
const getQueryKey = () => {
const { group, search } = rootState;

return JSON.stringify([group, search ?? ""]);
};

const fetchData = async (key: string, page: number) => {
try {
state.loading = true;

const { page } = state;
const currentFetchId = ++state.fetchId;

const list = await selectHistory((qb) => {
const { size } = state;
Expand All @@ -55,6 +63,10 @@ export const useHistoryList = (options: Options) => {
.orderBy("createTime", "desc");
});

// 丢弃过期请求的结果,避免旧结果覆盖新搜索
if (currentFetchId !== state.fetchId) return;
if (key !== state.queryKey) return;

for (const item of list) {
const { type, value } = item;

Expand Down Expand Up @@ -91,22 +103,41 @@ export const useHistoryList = (options: Options) => {
rootState.list = unionBy(rootState.list, list, "id");
} finally {
state.loading = false;

if (state.pendingReload) {
state.pendingReload = false;
reload();
}
}
};

const reload = () => {
const key = getQueryKey();

state.queryKey = key;
state.page = 1;
state.noMore = false;

return fetchData();
rootState.list = [];
rootState.activeId = void 0;

if (state.loading) {
state.pendingReload = true;
return;
}

return fetchData(key, 1);
};

const loadMore = () => {
if (state.noMore) return;
if (state.loading) return;

const nextPage = state.page + 1;

state.page += 1;
state.page = nextPage;

fetchData();
fetchData(state.queryKey || getQueryKey(), nextPage);
};

useTauriListen(LISTEN_KEY.REFRESH_CLIPBOARD_LIST, reload);
Expand Down
15 changes: 11 additions & 4 deletions src/hooks/useTauriFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ export const useTauriFocus = (props: Props) => {

const wait = isMac ? 0 : 100;

const debounced = debounce(({ payload }) => {
if (payload) {
// Windows 上偶尔会收到“假失焦”事件,这里用 isFocused 二次确认
const debounced = debounce(async () => {
const focused = await appWindow.isFocused();

if (focused) {
onFocus?.();
} else {
onBlur?.();
}
}, wait);

unlistenRef.current = await appWindow.onFocusChanged(debounced);
unlistenRef.current = await appWindow.onFocusChanged(() => {
void debounced();
});
});

useUnmount(unlistenRef.current);
useUnmount(() => {
unlistenRef.current?.();
});
};
9 changes: 8 additions & 1 deletion src/pages/Main/components/SearchInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ const SearchInput: FC<HTMLAttributes<HTMLDivElement>> = (props) => {
useEffect(() => {
if (isComposition) return;

rootState.search = value;
// 搜索防抖:避免每个按键都触发一次查库
const timer = window.setTimeout(() => {
rootState.search = value;
}, 200);

return () => {
window.clearTimeout(timer);
};
}, [value, isComposition]);

useTauriFocus({
Expand Down