Skip to content

Commit 7a371a1

Browse files
committed
added centrifuge app updater
1 parent fd1136f commit 7a371a1

File tree

3 files changed

+117
-32
lines changed

3 files changed

+117
-32
lines changed

src/hooks/useCentrifuge.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@ import { useAppStore } from '../store/useAppStore';import { refreshToken } from
44

55
const VITE_APP_CENTRIFUGE_SERVICE = import.meta.env.VITE_APP_CENTRIFUGE_SERVICE;
66

7+
type CounterType =
8+
| 'counter_chats'
9+
| 'counter_api_calls'
10+
| 'counter_aitokens'
11+
| 'counter_files'
12+
| 'counter_transactions'
13+
| 'counter_sessions'
14+
| 'counter_registered';
15+
16+
interface CentrifugeData {
17+
type: CounterType;
18+
appId: string;
19+
}
20+
721
export function useCentrifugeChannel() {
822
const currentUser = useAppStore((s) => s.currentUser);
923

10-
const [data, setData] = useState<Record<string, string | number>>();
24+
const [data, setData] = useState<CentrifugeData>();
1125
const [connected, setConnected] = useState(false);
1226

1327
const getToken = async () => {
28+
console.log('[centrifuge] getToken CALLED!');
1429
const newTokens = await refreshToken();
1530

1631
return newTokens.wsToken;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { useEffect } from 'react';
2+
import { useCentrifugeChannel } from './useCentrifuge';
3+
import { ModelApp } from '../models';
4+
5+
type CounterType =
6+
| 'counter_chats'
7+
| 'counter_api_calls'
8+
| 'counter_aitokens'
9+
| 'counter_files'
10+
| 'counter_transactions'
11+
| 'counter_sessions'
12+
| 'counter_registered';
13+
14+
interface CentrifugeData {
15+
type: CounterType;
16+
appId: string;
17+
}
18+
19+
function capitalize(word: string): string {
20+
return word.charAt(0).toUpperCase() + word.slice(1);
21+
}
22+
23+
function updateStatsField(
24+
setAppState: React.Dispatch<React.SetStateAction<ModelApp[]>>,
25+
appId: string,
26+
fields: Partial<Record<keyof ModelApp['stats'], number>>
27+
) {
28+
setAppState((prev) =>
29+
prev.map((app) =>
30+
app._id === appId
31+
? {
32+
...app,
33+
stats: Object.entries(fields).reduce((acc, [key, value]) => {
34+
acc[key as keyof typeof acc] += value!;
35+
return acc;
36+
}, { ...app.stats }),
37+
}
38+
: app
39+
)
40+
);
41+
}
42+
43+
export function useCentrifugeAppUpdater(
44+
setAppState: React.Dispatch<React.SetStateAction<ModelApp[]>>
45+
) {
46+
const { data } = useCentrifugeChannel();
47+
48+
useEffect(() => {
49+
if (!data) return;
50+
51+
const { type, appId } = data as CentrifugeData;
52+
53+
if (!type.startsWith('counter_')) return;
54+
55+
const statBase = type.replace('counter_', '');
56+
const statName = statBase
57+
.split('_')
58+
.map((part, i) => (i === 0 ? part : capitalize(part)))
59+
.join('');
60+
61+
const totalKey = `total${capitalize(statName)}` as keyof ModelApp['stats'];
62+
const recentlyKey = `recently${capitalize(statName)}` as keyof ModelApp['stats'];
63+
64+
updateStatsField(setAppState, appId, {
65+
[totalKey]: 1,
66+
[recentlyKey]: 1,
67+
});
68+
}, [data, setAppState]);
69+
}

src/pages/AdminApps.tsx

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { NewAppModal } from '../components/modal/NewAppModal';
99
import { Sorting } from '../components/Sorting';
1010
import CsvButton from '../components/UI/Buttons/CSVButton.tsx';
1111
import { Pagination } from '../components/UI/Pagination/Pagination.tsx';
12-
import { useCentrifugeChannel } from '../hooks/useCentrifuge.ts';
12+
import { useCentrifugeAppUpdater } from '../hooks/useCentrifugeAppUpdater.ts';
1313
import { getExportAppsCsv, httpGetApps } from '../http';
1414
import { ModelApp, OrderByType } from '../models';
1515
import { useAppStore } from '../store/useAppStore';
@@ -28,8 +28,6 @@ export default function AdminApps() {
2828
const doSetApps = useAppStore((s) => s.doSetApps);
2929
const currentApp = useAppStore((s) => s.currentApp as ModelApp);
3030

31-
const { data } = useCentrifugeChannel();
32-
3331
const limit = useMemo(
3432
() => Number(searchParams.get('limit')) || 5,
3533
[searchParams]
@@ -175,26 +173,28 @@ export default function AdminApps() {
175173
setAppState(apps);
176174
}, [apps]);
177175

178-
useEffect(() => {
179-
if (!data) return;
180-
181-
if (data.type === 'counter_new_chats') {
182-
setAppState((prev) =>
183-
prev.map((app) =>
184-
app._id === data.appId
185-
? {
186-
...app,
187-
stats: {
188-
...app.stats,
189-
totalChats: app.stats.totalChats + 1,
190-
recentlyChats: app.stats.recentlyChats + 1,
191-
},
192-
}
193-
: app
194-
)
195-
);
196-
}
197-
}, [data]);
176+
useCentrifugeAppUpdater(setAppState);
177+
178+
// useEffect(() => {
179+
// if (!data) return;
180+
181+
// if (data.type === 'counter_chats') {
182+
// setAppState((prev) =>
183+
// prev.map((app) =>
184+
// app._id === data.appId
185+
// ? {
186+
// ...app,
187+
// stats: {
188+
// ...app.stats,
189+
// totalChats: app.stats.totalChats + 1,
190+
// recentlyChats: app.stats.recentlyChats + 1,
191+
// },
192+
// }
193+
// : app
194+
// )
195+
// );
196+
// }
197+
// }, [data]);
198198

199199
return (
200200
<>
@@ -218,7 +218,7 @@ export default function AdminApps() {
218218
)}
219219
<button
220220
onClick={() => setShowModal(true)}
221-
className="flex items-center justify-center sm:w-[40px] h-[40px] w-[60px] bg-brand-500 rounded-xl hover:bg-brand-darker text-white text-sm font-varela"
221+
className="flex items-center justify-center sm:w-full h-[40px] w-[60px] bg-brand-500 rounded-xl hover:bg-brand-darker text-white text-sm font-varela"
222222
>
223223
<IconAdd color="white" className="md:mr-2" />
224224
<span className="hidden md:block">Create App</span>
@@ -234,13 +234,14 @@ export default function AdminApps() {
234234
<ApplicationStarterInf onClose={() => setShowStarterInf(false)} />
235235
)}
236236

237-
{appsState.map((app) => (
238-
<ApplicationPreview
239-
key={app._id}
240-
app={app}
241-
primaryColor={currentApp.primaryColor}
242-
/>
243-
))}
237+
{appsState &&
238+
appsState.map((app) => (
239+
<ApplicationPreview
240+
key={app._id}
241+
app={app}
242+
primaryColor={currentApp.primaryColor}
243+
/>
244+
))}
244245

245246
{currentUser?.isSuperAdmin && (
246247
<Pagination

0 commit comments

Comments
 (0)