feat(redis): improve key browser toolbar and infinite scroll loading
This commit is contained in:
14 files changed
+254
-40
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { shouldLoadMoreRedisKeys } from "@/lib/redis/redisKeyInfiniteScroll";
|
||||
|
||||
const baseState = {
|
||||
enabled: true,
|
||||
hasMore: true,
|
||||
busy: false,
|
||||
loadedKeys: 1000,
|
||||
maxKeys: 5000,
|
||||
scrollTop: 800,
|
||||
clientHeight: 200,
|
||||
scrollHeight: 1050,
|
||||
};
|
||||
|
||||
describe("shouldLoadMoreRedisKeys", () => {
|
||||
it("loads the next scan page near the bottom", () => {
|
||||
expect(shouldLoadMoreRedisKeys(baseState)).toBe(true);
|
||||
});
|
||||
|
||||
it("does not load while disabled, busy, or exhausted", () => {
|
||||
expect(shouldLoadMoreRedisKeys({ ...baseState, enabled: false })).toBe(false);
|
||||
expect(shouldLoadMoreRedisKeys({ ...baseState, busy: true })).toBe(false);
|
||||
expect(shouldLoadMoreRedisKeys({ ...baseState, hasMore: false })).toBe(false);
|
||||
});
|
||||
|
||||
it("stops automatic loading at the configured maximum", () => {
|
||||
expect(shouldLoadMoreRedisKeys({ ...baseState, loadedKeys: 5000 })).toBe(false);
|
||||
});
|
||||
|
||||
it("does not load before reaching the bottom threshold", () => {
|
||||
expect(shouldLoadMoreRedisKeys({ ...baseState, scrollTop: 700 })).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
export interface RedisKeyInfiniteScrollState {
|
||||
enabled: boolean;
|
||||
hasMore: boolean;
|
||||
busy: boolean;
|
||||
loadedKeys: number;
|
||||
maxKeys: number;
|
||||
scrollTop: number;
|
||||
clientHeight: number;
|
||||
scrollHeight: number;
|
||||
}
|
||||
|
||||
export function shouldLoadMoreRedisKeys(state: RedisKeyInfiniteScrollState, threshold = 100): boolean {
|
||||
if (!state.enabled || !state.hasMore || state.busy) return false;
|
||||
if (state.loadedKeys >= state.maxKeys || state.clientHeight <= 0) return false;
|
||||
return state.scrollTop + state.clientHeight >= state.scrollHeight - Math.max(0, threshold);
|
||||
}
|
||||
Reference in new issue
Block a user