@sudobility/testomniac_lib 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/business/hooks/index.d.ts +2 -0
- package/dist/business/hooks/index.d.ts.map +1 -0
- package/dist/business/hooks/index.js +2 -0
- package/dist/business/hooks/index.js.map +1 -0
- package/dist/business/hooks/useHistoriesManager.d.ts +24 -0
- package/dist/business/hooks/useHistoriesManager.d.ts.map +1 -0
- package/dist/business/hooks/useHistoriesManager.js +96 -0
- package/dist/business/hooks/useHistoriesManager.js.map +1 -0
- package/dist/business/index.d.ts +4 -0
- package/dist/business/index.d.ts.map +1 -0
- package/dist/business/index.js +4 -0
- package/dist/business/index.js.map +1 -0
- package/dist/business/stores/historiesStore.d.ts +19 -0
- package/dist/business/stores/historiesStore.d.ts.map +1 -0
- package/dist/business/stores/historiesStore.js +96 -0
- package/dist/business/stores/historiesStore.js.map +1 -0
- package/dist/business/stores/index.d.ts +2 -0
- package/dist/business/stores/index.d.ts.map +1 -0
- package/dist/business/stores/index.js +2 -0
- package/dist/business/stores/index.js.map +1 -0
- package/dist/business/utils/calculations.d.ts +4 -0
- package/dist/business/utils/calculations.d.ts.map +1 -0
- package/dist/business/utils/calculations.js +10 -0
- package/dist/business/utils/calculations.js.map +1 -0
- package/dist/business/utils/index.d.ts +2 -0
- package/dist/business/utils/index.d.ts.map +1 -0
- package/dist/business/utils/index.js +2 -0
- package/dist/business/utils/index.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/business/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,KAAK,yBAAyB,EAC9B,KAAK,yBAAyB,GAC/B,MAAM,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/business/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,GAGpB,MAAM,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { History, HistoryCreateRequest, HistoryUpdateRequest, NetworkClient, Optional } from '@sudobility/testomniac_types';
|
|
2
|
+
import type { FirebaseIdToken } from '@sudobility/testomniac_client';
|
|
3
|
+
export interface UseHistoriesManagerConfig {
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
networkClient: NetworkClient;
|
|
6
|
+
userId: Optional<string>;
|
|
7
|
+
token: Optional<FirebaseIdToken>;
|
|
8
|
+
autoFetch?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface UseHistoriesManagerReturn {
|
|
11
|
+
histories: History[];
|
|
12
|
+
total: number;
|
|
13
|
+
percentage: number;
|
|
14
|
+
isLoading: boolean;
|
|
15
|
+
error: Optional<string>;
|
|
16
|
+
isCached: boolean;
|
|
17
|
+
cachedAt: Optional<number>;
|
|
18
|
+
createHistory: (data: HistoryCreateRequest) => Promise<void>;
|
|
19
|
+
updateHistory: (historyId: string, data: HistoryUpdateRequest) => Promise<void>;
|
|
20
|
+
deleteHistory: (historyId: string) => Promise<void>;
|
|
21
|
+
refresh: () => void;
|
|
22
|
+
}
|
|
23
|
+
export declare const useHistoriesManager: ({ baseUrl, networkClient, userId, token, autoFetch, }: UseHistoriesManagerConfig) => UseHistoriesManagerReturn;
|
|
24
|
+
//# sourceMappingURL=useHistoriesManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHistoriesManager.d.ts","sourceRoot":"","sources":["../../../src/business/hooks/useHistoriesManager.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,OAAO,EACP,oBAAoB,EACpB,oBAAoB,EACpB,aAAa,EACb,QAAQ,EACT,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAmBrE,MAAM,WAAW,yBAAyB;IAExC,OAAO,EAAE,MAAM,CAAC;IAMhB,aAAa,EAAE,aAAa,CAAC;IAM7B,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAOzB,KAAK,EAAE,QAAQ,CAAC,eAAe,CAAC,CAAC;IAajC,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAQD,MAAM,WAAW,yBAAyB;IAMxC,SAAS,EAAE,OAAO,EAAE,CAAC;IAMrB,KAAK,EAAE,MAAM,CAAC;IASd,UAAU,EAAE,MAAM,CAAC;IAMnB,SAAS,EAAE,OAAO,CAAC;IAUnB,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IASxB,QAAQ,EAAE,OAAO,CAAC;IAMlB,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAS3B,aAAa,EAAE,CAAC,IAAI,EAAE,oBAAoB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAU7D,aAAa,EAAE,CACb,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,oBAAoB,KACvB,OAAO,CAAC,IAAI,CAAC,CAAC;IASnB,aAAa,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAMpD,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAmDD,eAAO,MAAM,mBAAmB,GAAI,uDAMjC,yBAAyB,KAAG,yBAwJ9B,CAAC"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import { useHistories, useHistoriesTotal } from '@sudobility/testomniac_client';
|
|
3
|
+
import { useHistoriesStore } from '../stores/historiesStore';
|
|
4
|
+
import { calculatePercentage } from '../utils/calculations';
|
|
5
|
+
export const useHistoriesManager = ({ baseUrl, networkClient, userId, token, autoFetch = true, }) => {
|
|
6
|
+
const { histories: clientHistories, isLoading: historiesLoading, error: historiesError, update, createHistory: clientCreate, updateHistory: clientUpdate, deleteHistory: clientDelete, isCreating, isUpdating, isDeleting, } = useHistories(networkClient, baseUrl, userId ?? null, token ?? null);
|
|
7
|
+
const { total, isLoading: totalLoading, error: totalError, } = useHistoriesTotal(networkClient, baseUrl);
|
|
8
|
+
const cacheEntry = useHistoriesStore(useCallback(state => (userId ? state.cache[userId] : undefined), [userId]));
|
|
9
|
+
const setHistories = useHistoriesStore(state => state.setHistories);
|
|
10
|
+
const addHistoryToStore = useHistoriesStore(state => state.addHistory);
|
|
11
|
+
const updateHistoryInStore = useHistoriesStore(state => state.updateHistory);
|
|
12
|
+
const removeHistoryFromStore = useHistoriesStore(state => state.removeHistory);
|
|
13
|
+
const cachedHistories = cacheEntry?.histories;
|
|
14
|
+
const cachedAt = cacheEntry?.cachedAt;
|
|
15
|
+
const histories = useMemo(() => clientHistories.length > 0 ? clientHistories : (cachedHistories ?? []), [clientHistories, cachedHistories]);
|
|
16
|
+
const isCached = clientHistories.length === 0 && (cachedHistories?.length ?? 0) > 0;
|
|
17
|
+
const prevClientHistoriesRef = useRef(clientHistories);
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
if (clientHistories.length > 0 &&
|
|
20
|
+
userId &&
|
|
21
|
+
clientHistories !== prevClientHistoriesRef.current) {
|
|
22
|
+
prevClientHistoriesRef.current = clientHistories;
|
|
23
|
+
setHistories(userId, clientHistories);
|
|
24
|
+
}
|
|
25
|
+
}, [clientHistories, userId, setHistories]);
|
|
26
|
+
const percentage = useMemo(() => calculatePercentage(histories, total), [histories, total]);
|
|
27
|
+
const createHistory = useCallback(async (data) => {
|
|
28
|
+
const response = await clientCreate(data);
|
|
29
|
+
if (response.success && response.data && userId) {
|
|
30
|
+
addHistoryToStore(userId, response.data);
|
|
31
|
+
}
|
|
32
|
+
if (!response.success) {
|
|
33
|
+
throw new Error(response.error || 'Failed to create history');
|
|
34
|
+
}
|
|
35
|
+
}, [clientCreate, userId, addHistoryToStore]);
|
|
36
|
+
const updateHistory = useCallback(async (historyId, data) => {
|
|
37
|
+
const response = await clientUpdate(historyId, data);
|
|
38
|
+
if (response.success && response.data && userId) {
|
|
39
|
+
updateHistoryInStore(userId, historyId, response.data);
|
|
40
|
+
}
|
|
41
|
+
if (!response.success) {
|
|
42
|
+
throw new Error(response.error || 'Failed to update history');
|
|
43
|
+
}
|
|
44
|
+
}, [clientUpdate, userId, updateHistoryInStore]);
|
|
45
|
+
const deleteHistory = useCallback(async (historyId) => {
|
|
46
|
+
const response = await clientDelete(historyId);
|
|
47
|
+
if (response.success && userId) {
|
|
48
|
+
removeHistoryFromStore(userId, historyId);
|
|
49
|
+
}
|
|
50
|
+
if (!response.success) {
|
|
51
|
+
throw new Error(response.error || 'Failed to delete history');
|
|
52
|
+
}
|
|
53
|
+
}, [clientDelete, userId, removeHistoryFromStore]);
|
|
54
|
+
const isLoading = historiesLoading || totalLoading || isCreating || isUpdating || isDeleting;
|
|
55
|
+
const error = historiesError ?? totalError ?? null;
|
|
56
|
+
const hasAttemptedFetchRef = useRef(false);
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (autoFetch &&
|
|
59
|
+
token &&
|
|
60
|
+
userId &&
|
|
61
|
+
histories.length === 0 &&
|
|
62
|
+
!hasAttemptedFetchRef.current) {
|
|
63
|
+
hasAttemptedFetchRef.current = true;
|
|
64
|
+
update();
|
|
65
|
+
}
|
|
66
|
+
}, [autoFetch, token, userId, histories.length, update]);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
hasAttemptedFetchRef.current = false;
|
|
69
|
+
}, [token]);
|
|
70
|
+
return useMemo(() => ({
|
|
71
|
+
histories,
|
|
72
|
+
total,
|
|
73
|
+
percentage,
|
|
74
|
+
isLoading,
|
|
75
|
+
error,
|
|
76
|
+
isCached,
|
|
77
|
+
cachedAt: cachedAt ?? null,
|
|
78
|
+
createHistory,
|
|
79
|
+
updateHistory,
|
|
80
|
+
deleteHistory,
|
|
81
|
+
refresh: update,
|
|
82
|
+
}), [
|
|
83
|
+
histories,
|
|
84
|
+
total,
|
|
85
|
+
percentage,
|
|
86
|
+
isLoading,
|
|
87
|
+
error,
|
|
88
|
+
isCached,
|
|
89
|
+
cachedAt,
|
|
90
|
+
createHistory,
|
|
91
|
+
updateHistory,
|
|
92
|
+
deleteHistory,
|
|
93
|
+
update,
|
|
94
|
+
]);
|
|
95
|
+
};
|
|
96
|
+
//# sourceMappingURL=useHistoriesManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHistoriesManager.js","sourceRoot":"","sources":["../../../src/business/hooks/useHistoriesManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAShE,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAChF,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAwM5D,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,EAClC,OAAO,EACP,aAAa,EACb,MAAM,EACN,KAAK,EACL,SAAS,GAAG,IAAI,GACU,EAA6B,EAAE;IACzD,MAAM,EACJ,SAAS,EAAE,eAAe,EAC1B,SAAS,EAAE,gBAAgB,EAC3B,KAAK,EAAE,cAAc,EACrB,MAAM,EACN,aAAa,EAAE,YAAY,EAC3B,aAAa,EAAE,YAAY,EAC3B,aAAa,EAAE,YAAY,EAC3B,UAAU,EACV,UAAU,EACV,UAAU,GACX,GAAG,YAAY,CAAC,aAAa,EAAE,OAAO,EAAE,MAAM,IAAI,IAAI,EAAE,KAAK,IAAI,IAAI,CAAC,CAAC;IAExE,MAAM,EACJ,KAAK,EACL,SAAS,EAAE,YAAY,EACvB,KAAK,EAAE,UAAU,GAClB,GAAG,iBAAiB,CAAC,aAAa,EAAE,OAA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AAC;IAEZ,OAAO,OAAO,CACZ,GAAG,EAAE,CAAC,CAAC;QACL,SAAS;QACT,KAAK;QACL,UAAU;QACV,SAAS;QACT,KAAK;QACL,QAAQ;QACR,QAAQ,EAAE,QAAQ,IAAI,IAAI;QAC1B,aAAa;QACb,aAAa;QACb,aAAa;QACb,OAAO,EAAE,MAAM;KAChB,CAAC,EACF;QACE,SAAS;QACT,KAAK;QACL,UAAU;QACV,SAAS;QACT,KAAK;QACL,QAAQ;QACR,QAAQ;QACR,aAAa;QACb,aAAa;QACb,aAAa;QACb,MAAM;KACP,CACF,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/business/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/business/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { History } from '@sudobility/testomniac_types';
|
|
2
|
+
export declare const DEFAULT_CACHE_EXPIRATION_MS: number;
|
|
3
|
+
export interface HistoriesCacheEntry {
|
|
4
|
+
histories: History[];
|
|
5
|
+
cachedAt: number;
|
|
6
|
+
}
|
|
7
|
+
export interface HistoriesStoreState {
|
|
8
|
+
cache: Record<string, HistoriesCacheEntry>;
|
|
9
|
+
setHistories: (userId: string, histories: History[]) => void;
|
|
10
|
+
getHistories: (userId: string, maxAge?: number) => History[] | undefined;
|
|
11
|
+
getCacheEntry: (userId: string, maxAge?: number) => HistoriesCacheEntry | undefined;
|
|
12
|
+
addHistory: (userId: string, history: History) => void;
|
|
13
|
+
updateHistory: (userId: string, historyId: string, history: History) => void;
|
|
14
|
+
removeHistory: (userId: string, historyId: string) => void;
|
|
15
|
+
purgeExpired: (maxAge?: number) => void;
|
|
16
|
+
clearAll: () => void;
|
|
17
|
+
}
|
|
18
|
+
export declare const useHistoriesStore: import("zustand").UseBoundStore<import("zustand").StoreApi<HistoriesStoreState>>;
|
|
19
|
+
//# sourceMappingURL=historiesStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"historiesStore.d.ts","sourceRoot":"","sources":["../../../src/business/stores/historiesStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAS5D,eAAO,MAAM,2BAA2B,QAAiB,CAAC;AAQ1D,MAAM,WAAW,mBAAmB;IAElC,SAAS,EAAE,OAAO,EAAE,CAAC;IAErB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAqBD,MAAM,WAAW,mBAAmB;IAElC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;IAS3C,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;IAU7D,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,OAAO,EAAE,GAAG,SAAS,CAAC;IAUzE,aAAa,EAAE,CACb,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,MAAM,KACZ,mBAAmB,GAAG,SAAS,CAAC;IASrC,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IAUvD,aAAa,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IAS7E,aAAa,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,IAAI,CAAC;IAO3D,YAAY,EAAE,CAAC,MAAM,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAKxC,QAAQ,EAAE,MAAM,IAAI,CAAC;CACtB;AAqCD,eAAO,MAAM,iBAAiB,kFAwG3B,CAAC"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { create } from 'zustand';
|
|
2
|
+
export const DEFAULT_CACHE_EXPIRATION_MS = 10 * 60 * 1000;
|
|
3
|
+
const isCacheExpired = (entry, maxAge) => {
|
|
4
|
+
return Date.now() - entry.cachedAt > maxAge;
|
|
5
|
+
};
|
|
6
|
+
export const useHistoriesStore = create((set, get) => ({
|
|
7
|
+
cache: {},
|
|
8
|
+
setHistories: (userId, histories) => set(state => ({
|
|
9
|
+
cache: {
|
|
10
|
+
...state.cache,
|
|
11
|
+
[userId]: {
|
|
12
|
+
histories,
|
|
13
|
+
cachedAt: Date.now(),
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
})),
|
|
17
|
+
getHistories: (userId, maxAge = DEFAULT_CACHE_EXPIRATION_MS) => {
|
|
18
|
+
const entry = get().cache[userId];
|
|
19
|
+
if (!entry)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (isCacheExpired(entry, maxAge))
|
|
22
|
+
return undefined;
|
|
23
|
+
return entry.histories;
|
|
24
|
+
},
|
|
25
|
+
getCacheEntry: (userId, maxAge = DEFAULT_CACHE_EXPIRATION_MS) => {
|
|
26
|
+
const entry = get().cache[userId];
|
|
27
|
+
if (!entry)
|
|
28
|
+
return undefined;
|
|
29
|
+
if (isCacheExpired(entry, maxAge))
|
|
30
|
+
return undefined;
|
|
31
|
+
return entry;
|
|
32
|
+
},
|
|
33
|
+
addHistory: (userId, history) => set(state => {
|
|
34
|
+
const existing = state.cache[userId];
|
|
35
|
+
if (!existing) {
|
|
36
|
+
return {
|
|
37
|
+
cache: {
|
|
38
|
+
...state.cache,
|
|
39
|
+
[userId]: {
|
|
40
|
+
histories: [history],
|
|
41
|
+
cachedAt: Date.now(),
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
cache: {
|
|
48
|
+
...state.cache,
|
|
49
|
+
[userId]: {
|
|
50
|
+
histories: [...existing.histories, history],
|
|
51
|
+
cachedAt: Date.now(),
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}),
|
|
56
|
+
updateHistory: (userId, historyId, history) => set(state => {
|
|
57
|
+
const existing = state.cache[userId];
|
|
58
|
+
if (!existing)
|
|
59
|
+
return state;
|
|
60
|
+
return {
|
|
61
|
+
cache: {
|
|
62
|
+
...state.cache,
|
|
63
|
+
[userId]: {
|
|
64
|
+
histories: existing.histories.map(h => h.id === historyId ? history : h),
|
|
65
|
+
cachedAt: Date.now(),
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}),
|
|
70
|
+
removeHistory: (userId, historyId) => set(state => {
|
|
71
|
+
const existing = state.cache[userId];
|
|
72
|
+
if (!existing)
|
|
73
|
+
return state;
|
|
74
|
+
return {
|
|
75
|
+
cache: {
|
|
76
|
+
...state.cache,
|
|
77
|
+
[userId]: {
|
|
78
|
+
histories: existing.histories.filter(h => h.id !== historyId),
|
|
79
|
+
cachedAt: Date.now(),
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}),
|
|
84
|
+
purgeExpired: (maxAge = DEFAULT_CACHE_EXPIRATION_MS) => set(state => {
|
|
85
|
+
const now = Date.now();
|
|
86
|
+
const newCache = {};
|
|
87
|
+
for (const [userId, entry] of Object.entries(state.cache)) {
|
|
88
|
+
if (now - entry.cachedAt <= maxAge) {
|
|
89
|
+
newCache[userId] = entry;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return { cache: newCache };
|
|
93
|
+
}),
|
|
94
|
+
clearAll: () => set({ cache: {} }),
|
|
95
|
+
}));
|
|
96
|
+
//# sourceMappingURL=historiesStore.js.map
|
|
@@ -0,0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,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/business/stores/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,2BAA2B,EAC3B,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,GACzB,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/business/stores/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,2BAA2B,GAG5B,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { History } from '@sudobility/testomniac_types';
|
|
2
|
+
export declare const calculateSum: (histories: History[]) => number;
|
|
3
|
+
export declare const calculatePercentage: (histories: History[], globalTotal: number) => number;
|
|
4
|
+
//# sourceMappingURL=calculations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculations.d.ts","sourceRoot":"","sources":["../../../src/business/utils/calculations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAiB5D,eAAO,MAAM,YAAY,GAAI,WAAW,OAAO,EAAE,KAAG,MAEnD,CAAC;AA2BF,eAAO,MAAM,mBAAmB,GAC9B,WAAW,OAAO,EAAE,EACpB,aAAa,MAAM,KAClB,MAIF,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const calculateSum = (histories) => {
|
|
2
|
+
return histories.reduce((sum, h) => sum + h.value, 0);
|
|
3
|
+
};
|
|
4
|
+
export const calculatePercentage = (histories, globalTotal) => {
|
|
5
|
+
if (globalTotal <= 0)
|
|
6
|
+
return 0;
|
|
7
|
+
const userSum = calculateSum(histories);
|
|
8
|
+
return (userSum / globalTotal) * 100;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=calculations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculations.js","sourceRoot":"","sources":["../../../src/business/utils/calculations.ts"],"names":[],"mappings":"AAiBA,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,SAAoB,EAAU,EAAE;IAC3D,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;AACxD,CAAC,CAAC;AA2BF,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,SAAoB,EACpB,WAAmB,EACX,EAAE;IACV,IAAI,WAAW,IAAI,CAAC;QAAE,OAAO,CAAC,CAAC;IAC/B,MAAM,OAAO,GAAG,YAAY,CAAC,SAAS,CAAC,CAAC;IACxC,OAAO,CAAC,OAAO,GAAG,WAAW,CAAC,GAAG,GAAG,CAAC;AACvC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/business/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/business/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sudobility/testomniac_lib",
|
|
3
|
+
"version": "0.0.11",
|
|
4
|
+
"description": "Business logic library for Starter with Zustand stores",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "restricted"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist/**/*"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "bunx tsc -p tsconfig.build.json",
|
|
16
|
+
"build:watch": "bunx tsc --watch",
|
|
17
|
+
"clean": "rm -rf dist",
|
|
18
|
+
"prepublishOnly": "bun run clean && bun run build",
|
|
19
|
+
"typecheck": "bunx tsc --noEmit",
|
|
20
|
+
"lint": "bunx eslint src/",
|
|
21
|
+
"lint:fix": "bunx eslint src/ --fix",
|
|
22
|
+
"format": "bunx prettier --write \"src/**/*.ts\"",
|
|
23
|
+
"format:check": "bunx prettier --check \"src/**/*.ts\"",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"test:watch": "vitest",
|
|
26
|
+
"verify": "bun run typecheck && bun run lint && bun run test && bun run build"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"testomniac",
|
|
30
|
+
"business-logic",
|
|
31
|
+
"zustand",
|
|
32
|
+
"react",
|
|
33
|
+
"typescript"
|
|
34
|
+
],
|
|
35
|
+
"author": "Sudobility",
|
|
36
|
+
"license": "BUSL-1.1",
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@sudobility/types": "^1.9.55",
|
|
39
|
+
"@tanstack/react-query": ">=5.0.0",
|
|
40
|
+
"react": ">=18.0.0",
|
|
41
|
+
"zustand": ">=5.0.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@eslint/js": "^9.38.0",
|
|
45
|
+
"@sudobility/testomniac_client": "^0.0.13",
|
|
46
|
+
"@sudobility/testomniac_types": "^0.0.8",
|
|
47
|
+
"@sudobility/types": "^1.9.55",
|
|
48
|
+
"@tanstack/react-query": "^5.90.5",
|
|
49
|
+
"@testing-library/react": "^16.3.2",
|
|
50
|
+
"@testing-library/react-hooks": "^8.0.1",
|
|
51
|
+
"@types/react": "^19.2.2",
|
|
52
|
+
"@typescript-eslint/eslint-plugin": "^8.46.2",
|
|
53
|
+
"@typescript-eslint/parser": "^8.46.2",
|
|
54
|
+
"eslint": "^9.39.2",
|
|
55
|
+
"eslint-config-prettier": "^10.1.5",
|
|
56
|
+
"eslint-plugin-prettier": "^5.4.1",
|
|
57
|
+
"eslint-plugin-react-hooks": "^5.2.0",
|
|
58
|
+
"globals": "^16.4.0",
|
|
59
|
+
"jsdom": "^28.1.0",
|
|
60
|
+
"prettier": "^3.7.4",
|
|
61
|
+
"react": "^19.2.1",
|
|
62
|
+
"react-dom": "^19.2.4",
|
|
63
|
+
"typescript": "~5.9.3",
|
|
64
|
+
"vitest": "^4.0.15",
|
|
65
|
+
"zustand": "^5.0.8"
|
|
66
|
+
}
|
|
67
|
+
}
|