@tracktor/shared-module 2.23.0 → 2.24.0

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/README.md CHANGED
@@ -39,10 +39,11 @@ export default App;
39
39
  ```
40
40
 
41
41
  ## Providers
42
- | Module | Description | Dependencies |
43
- |-------------------------------|------------------------------------------------|--------------|
44
- | InjectDependenciesProvider | Inject dependencies for other shared component | - |
45
- | QueryClientProviderWithConfig | React Query provider with default config | React Query |
42
+ | Module | Description | Dependencies |
43
+ |-------------------------------|-----------------------------------------------------|--------------|
44
+ | InjectDependenciesProvider | Inject dependencies for other shared component | - |
45
+ | QueryClientProviderWithConfig | React Query provider with default config | React Query |
46
+ | ChatProvider | Shared WebSocket chat connection provider via context | - |
46
47
 
47
48
 
48
49
  ## Components
@@ -66,22 +67,36 @@ export default App;
66
67
  | useInfiniteDataGrid | This hook is used to handle the infinite scroll of the DataGrid component. | - |
67
68
  | useCurrentLanguage | This get the current language of app | - |
68
69
  | useFilters | Hook to handle filter | - |
69
- | useChat | Hook for chat WebSocket connection (threads, messaging, presence) | - |
70
+ | useChat | Hook to consume the ChatProvider context (threads, messaging, presence) | ChatProvider |
70
71
 
71
72
 
72
73
  ## WebSocket
73
74
 
74
75
  The chat module provides a WebSocket client for the Tracktor chat protocol (`/v2/threads/ws`). It supports thread subscription, messaging, read receipts, and presence events.
75
76
 
76
- **With the React hook** (recommended):
77
+ **With the React provider** (recommended):
78
+
79
+ First, wrap your app with `ChatProvider` (inside `InjectDependenciesProvider`):
80
+
81
+ ```typescript jsx
82
+ import { ChatProvider, InjectDependenciesProvider } from "@tracktor/shared-module";
83
+
84
+ const App = () => (
85
+ <InjectDependenciesProvider apiURL={import.meta.env.VITE_API_URL} libraries={libraries} localStorageKeys={localStorageKeys}>
86
+ <ChatProvider>
87
+ <MyApp />
88
+ </ChatProvider>
89
+ </InjectDependenciesProvider>
90
+ );
91
+ ```
92
+
93
+ Then use `useChat()` in any component:
77
94
 
78
95
  ```typescript jsx
79
96
  import { useChat } from "@tracktor/shared-module";
80
97
 
81
98
  const Chat = ({ threadId }: { threadId: string }) => {
82
- const { isReady, sendMessage, joinThread } = useChat({
83
- onNewMessage: (event) => console.log("New message:", event.message),
84
- });
99
+ const { isReady, sendMessage, joinThread } = useChat();
85
100
 
86
101
  return (
87
102
  <div>
@@ -93,10 +108,10 @@ const Chat = ({ threadId }: { threadId: string }) => {
93
108
  };
94
109
  ```
95
110
 
96
- The hook reads `apiURL` and the user token from `InjectDependenciesProvider` automatically. You can override both via props:
111
+ The provider reads `apiURL` and the user token from `InjectDependenciesProvider` automatically. You can override both via props:
97
112
 
98
113
  ```typescript jsx
99
- useChat({ url: "wss://app.api.dev.tracktor.fr/v2/threads/ws", token: "my-jwt" });
114
+ <ChatProvider url="wss://app.api.dev.tracktor.fr/v2/threads/ws" token="my-jwt">
100
115
  ```
101
116
 
102
117
  **Without React** (class only):
@@ -119,8 +134,9 @@ client.sendMessage("thread-1", "Hello!");
119
134
  ### Chat WebSocket
120
135
  | Module | Description | Dependencies |
121
136
  |------------|-----------------------------------------------------------|--------------|
122
- | ChatClient | Framework-agnostic WebSocket client for the chat protocol | - |
123
- | useChat | React hook wrapping ChatClient | - |
137
+ | ChatClient | Framework-agnostic WebSocket client for the chat protocol | - |
138
+ | ChatProvider | Provider for shared WebSocket connection via context | - |
139
+ | useChat | React hook to consume the ChatProvider context | ChatProvider |
124
140
 
125
141
 
126
142
  ## Config
@@ -0,0 +1,14 @@
1
+ import { PropsWithChildren } from 'react';
2
+ import { UseChatParams } from '../chat/types';
3
+ export interface ChatContextValue {
4
+ isConnected: boolean;
5
+ isReady: boolean;
6
+ joinThread: (threadId: string) => void;
7
+ leaveThread: (threadId: string) => void;
8
+ listThreads: (limit?: number, offset?: number) => void;
9
+ markRead: (threadId: string) => void;
10
+ sendMessage: (threadId: string, body: string) => void;
11
+ }
12
+ export declare const ChatContext: import('react').Context<ChatContextValue | null>;
13
+ declare const ChatProvider: ({ children, ...params }: PropsWithChildren<UseChatParams>) => import("react/jsx-runtime").JSX.Element;
14
+ export default ChatProvider;
@@ -1,11 +1,8 @@
1
- import { UseChatParams } from '../chat/types';
2
- export declare const useChat: (params?: UseChatParams) => {
3
- isConnected: boolean;
4
- isReady: boolean;
5
- joinThread: (threadId: string) => void;
6
- leaveThread: (threadId: string) => void;
7
- listThreads: (limit?: number, offset?: number) => void;
8
- markRead: (threadId: string) => void;
9
- sendMessage: (threadId: string, body: string) => void;
10
- };
1
+ import { ChatContextValue } from '../context/ChatProvider';
2
+ /**
3
+ * Hook to access the shared WebSocket chat connection.
4
+ * Provides methods to join/leave threads, send messages, mark as read, and list threads.
5
+ * Must be used within a ChatProvider.
6
+ */
7
+ export declare const useChat: () => ChatContextValue;
11
8
  export default useChat;
package/dist/main.d.ts CHANGED
@@ -19,6 +19,8 @@ export * from './components/Utils/RequireAuth';
19
19
  export { default as RequireAuth } from './components/Utils/RequireAuth';
20
20
  export * from './config/orval';
21
21
  export { default as getOrvalConfig } from './config/orval';
22
+ export * from './context/ChatProvider';
23
+ export { default as ChatProvider } from './context/ChatProvider';
22
24
  export * from './context/InjectDependenciesProvider';
23
25
  export { default as InjectDependenciesProvider } from './context/InjectDependenciesProvider';
24
26
  export * from './context/QueryClientProviderWithConfig';