1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import { isNull, isString } from "lodash";
import { useMemo } from "react";
export function useIsEntityLoaded(
entity: EntityStruct<any>,
start: number,
end: number
): boolean {
return useMemo(
() => entity.ids.slice(start, end).filter(isNull).length === 0,
[entity.ids, start, end]
);
}
export function useEntityIdByRange(
entity: EntityStruct<any>,
start: number,
end: number
): string[] {
return useMemo(() => {
const ids = entity.ids;
return ids.slice(start, end).flatMap((v) => {
if (isString(v)) {
return [v];
} else {
return [];
}
});
}, [entity.ids, start, end]);
}
export function useEntityByRange<T>(
entity: EntityStruct<T>,
start: number,
end: number
): T[] {
const filteredIds = useEntityIdByRange(entity, start, end);
const content = useMemo<T[]>(() => {
const entities = entity.entities;
return filteredIds.map((v) => entities[v]);
}, [entity.entities, filteredIds]);
return content;
}
export function useEntityToList<T>(entity: EntityStruct<T>): T[] {
return useMemo(
() => entity.ids.filter(isString).map((v) => entity.entities[v]),
[entity]
);
}
export function useEntityToItem<T>(
entity: EntityStruct<T>,
id: string
): T | null {
return useMemo(() => {
if (id in entity.entities) {
return entity.entities[id];
} else {
return null;
}
}, [entity.entities, id]);
}
|