Skip to content

Commit f6ac16d

Browse files
committed
feat(react-client): migrate from recoil to zustand
- Migrate from recoil to zustand - Bump react to v19
1 parent dd4beb5 commit f6ac16d

21 files changed

+563
-1072
lines changed

libs/react-client/package.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "@chainlit/react-client",
33
"description": "Websocket client to connect to your chainlit app.",
4-
"version": "0.2.3",
4+
"version": "0.3.0",
55
"scripts": {
6-
"build": "tsup src/index.ts --tsconfig tsconfig.build.json --clean --format esm,cjs --dts --external react --external recoil --minify --sourcemap --treeshake",
7-
"dev": "tsup src/index.ts --clean --format esm,cjs --dts --external react --external recoil --minify --sourcemap --treeshake",
6+
"build": "tsup src/index.ts --tsconfig tsconfig.build.json --clean --format esm,cjs --dts --external react --external zustand --minify --sourcemap --treeshake",
7+
"dev": "tsup src/index.ts --clean --format esm,cjs --dts --external react --external zustand --minify --sourcemap --treeshake",
88
"lint": "eslint ./src --ext ts,tsx --report-unused-disable-directives --max-warnings 0 && tsc --noemit",
99
"format": "prettier '**/*.{ts,tsx}' --write",
1010
"test": "echo no tests yet",
@@ -31,8 +31,9 @@
3131
"types": "dist/index.d.ts",
3232
"devDependencies": {
3333
"@swc/core": "^1.3.86",
34-
"@testing-library/jest-dom": "^5.17.0",
35-
"@testing-library/react": "^14.0.0",
34+
"@testing-library/dom": "^10.4.0",
35+
"@testing-library/jest-dom": "^6.6.3",
36+
"@testing-library/react": "^16.2.0",
3637
"@types/lodash": "^4.14.199",
3738
"@types/uuid": "^9.0.3",
3839
"@vitejs/plugin-react": "^4.0.4",
@@ -46,18 +47,19 @@
4647
"vitest": "^0.34.4"
4748
},
4849
"peerDependencies": {
49-
"@types/react": "^18.3.1",
50-
"react": "^18.3.1",
51-
"react-dom": "^18.3.1",
52-
"recoil": "^0.7.7"
50+
"@types/react": "^19.0.0",
51+
"react": "^19.0.0",
52+
"react-dom": "^19.0.0",
53+
"zustand": "^5.0.3"
5354
},
5455
"dependencies": {
5556
"jwt-decode": "^3.1.2",
5657
"lodash": "^4.17.21",
5758
"socket.io-client": "^4.7.2",
5859
"sonner": "^1.7.1",
59-
"swr": "^2.2.2",
60-
"uuid": "^9.0.0"
60+
"swr": "^2.3.0",
61+
"uuid": "^9.0.0",
62+
"zustand": "^5.0.3"
6163
},
6264
"pnpm": {
6365
"overrides": {

libs/react-client/pnpm-lock.yaml

Lines changed: 109 additions & 692 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/react-client/src/api/hooks/auth/state.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { useRecoilState, useSetRecoilState } from 'recoil';
2-
import { authState, threadHistoryState, userState } from 'src/state';
1+
import { useAuthStore } from 'src/store/auth';
32

43
export const useAuthState = () => {
5-
const [authConfig, setAuthConfig] = useRecoilState(authState);
6-
const [user, setUser] = useRecoilState(userState);
7-
const setThreadHistory = useSetRecoilState(threadHistoryState);
4+
const authConfig = useAuthStore((s) => s.authConfig);
5+
const setAuthConfig = useAuthStore((s) => s.setAuthConfig);
6+
const user = useAuthStore((s) => s.user);
7+
const setUser = useAuthStore((s) => s.setUser);
8+
const setThreadHistory = useAuthStore((s) => s.setThreadHistory);
89

910
return {
1011
authConfig,

libs/react-client/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export * from './useConfig';
77
export * from './api';
88
export * from './types';
99
export * from './context';
10-
export * from './state';
1110
export * from './utils/message';
1211

1312
export { Socket } from 'socket.io-client';

libs/react-client/src/state.ts

Lines changed: 0 additions & 215 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { isEqual } from 'lodash';
2+
import { groupByDate } from 'src/utils/group';
3+
import { create } from 'zustand';
4+
5+
import { IAuthConfig, IUser, ThreadHistory } from '..';
6+
7+
interface AuthState {
8+
authConfig?: IAuthConfig;
9+
user?: IUser | null;
10+
11+
threadHistory?: ThreadHistory;
12+
13+
setAuthConfig: (authConfig?: IAuthConfig) => void;
14+
setUser: (user?: IUser | null) => void;
15+
setThreadHistory: (threadHistory?: ThreadHistory) => void;
16+
}
17+
18+
export const useAuthStore = create<AuthState>((set, get) => ({
19+
threadHistory: {
20+
threads: undefined,
21+
currentThreadId: undefined,
22+
timeGroupedThreads: undefined,
23+
pageInfo: undefined
24+
},
25+
26+
setAuthConfig: (authConfig?: IAuthConfig) => {
27+
set({ authConfig });
28+
},
29+
30+
setUser: (user?: IUser | null) => {
31+
set({ user });
32+
},
33+
34+
setThreadHistory: (threadHistory?: ThreadHistory) => {
35+
const oldValue = get().threadHistory;
36+
37+
let timeGroupedThreads = threadHistory?.timeGroupedThreads;
38+
if (
39+
threadHistory?.threads &&
40+
!isEqual(threadHistory.threads, oldValue?.timeGroupedThreads)
41+
) {
42+
timeGroupedThreads = groupByDate(threadHistory.threads);
43+
}
44+
45+
set({
46+
threadHistory: {
47+
...threadHistory,
48+
timeGroupedThreads
49+
}
50+
});
51+
}
52+
}));

0 commit comments

Comments
 (0)