@cometchat/skills 3.0.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.
@@ -0,0 +1,717 @@
1
+ ---
2
+ name: cometchat-react-router-patterns
3
+ description: "Framework-specific patterns for integrating CometChat React UI Kit v6 into React Router projects (v6 library mode and v7 framework mode). Covers SSR prevention, routing patterns, outlet nesting, and common pitfalls."
4
+ license: "MIT"
5
+ compatibility: "Node.js >=18; React >=18; react-router-dom ^6 or react-router ^7; @cometchat/chat-uikit-react ^6; @cometchat/chat-sdk-javascript ^4"
6
+ allowed-tools: "executeBash, readFile, fileSearch, listDirectory"
7
+ metadata:
8
+ author: "CometChat"
9
+ version: "3.0.0"
10
+ tags: "chat cometchat react-router remix routing ssr patterns"
11
+ ---
12
+
13
+ ## Purpose
14
+
15
+ This skill teaches Claude how to integrate CometChat into React Router projects. React Router exists in two distinct modes with very different integration patterns:
16
+
17
+ - **v6 library mode:** React Router is used as a routing library in a Vite/CRA app. No SSR. Simple.
18
+ - **v7 framework mode:** React Router is a full framework (successor to Remix) with file-system routing, loaders, actions, and SSR. Complex -- similar SSR concerns as Next.js.
19
+
20
+ **Read these companion skills first:**
21
+ - `cometchat-core` -- initialization, login, CSS, provider pattern, anti-patterns
22
+ - `cometchat-components` -- component catalog and composition patterns
23
+ - `cometchat-placement` -- WHERE to put chat (route, modal, drawer, embedded)
24
+
25
+ ---
26
+
27
+ ## 1. Project detection
28
+
29
+ ### Identifying React Router
30
+
31
+ ```bash
32
+ # Check package.json for React Router
33
+ grep -E '"react-router-dom"|"react-router"' package.json
34
+ ```
35
+
36
+ ### Distinguishing v6 library mode from v7 framework mode
37
+
38
+ ```bash
39
+ # v7 framework mode: has a config file
40
+ ls react-router.config.ts react-router.config.js 2>/dev/null
41
+
42
+ # v6 library mode: uses createBrowserRouter or <BrowserRouter> in source
43
+ grep -rn "createBrowserRouter\|BrowserRouter\|<Routes" src/ --include="*.tsx" --include="*.jsx" 2>/dev/null | head -5
44
+ ```
45
+
46
+ **Decision:**
47
+ - If `react-router.config.ts` or `react-router.config.js` exists: **v7 framework mode**
48
+ - If `react-router-dom` is in `package.json` with `createBrowserRouter` or `<BrowserRouter>` in source: **v6 library mode**
49
+ - If `react-router` (not `react-router-dom`) is in `package.json` without a config file: likely v7 in library mode -- treat as v6 library mode
50
+
51
+ ---
52
+
53
+ ## 2. v6 library mode patterns
54
+
55
+ v6 library mode is essentially a plain React app with routing. There are no SSR concerns. CometChat integration follows the same patterns as the `cometchat-react-patterns` skill, with routing-specific additions.
56
+
57
+ ### CometChatProvider placement
58
+
59
+ Mount the provider at the router level, wrapping the entire router or a specific route subtree:
60
+
61
+ ```tsx
62
+ // src/main.tsx
63
+ import React from "react";
64
+ import ReactDOM from "react-dom/client";
65
+ import { RouterProvider } from "react-router-dom";
66
+ import { CometChatProvider } from "./providers/CometChatProvider";
67
+ import { router } from "./router";
68
+ import "@cometchat/chat-uikit-react/css-variables.css";
69
+ import "./index.css";
70
+
71
+ ReactDOM.createRoot(document.getElementById("root")!).render(
72
+ <React.StrictMode>
73
+ <CometChatProvider>
74
+ <RouterProvider router={router} />
75
+ </CometChatProvider>
76
+ </React.StrictMode>
77
+ );
78
+ ```
79
+
80
+ The provider implementation is the same as in `cometchat-react-patterns` section 2 -- uses `import.meta.env.VITE_COMETCHAT_*` for env vars.
81
+
82
+ ### Adding a route with createBrowserRouter
83
+
84
+ Find the router configuration and add a new route:
85
+
86
+ ```tsx
87
+ // src/router.tsx
88
+ import { createBrowserRouter } from "react-router-dom";
89
+ import Layout from "./components/Layout";
90
+ import HomePage from "./pages/HomePage";
91
+ import ChatPage from "./pages/ChatPage";
92
+
93
+ export const router = createBrowserRouter([
94
+ {
95
+ path: "/",
96
+ element: <Layout />,
97
+ children: [
98
+ { index: true, element: <HomePage /> },
99
+ { path: "messages", element: <ChatPage /> },
100
+ // ... existing routes
101
+ ],
102
+ },
103
+ ]);
104
+ ```
105
+
106
+ ### Adding a route with JSX Routes
107
+
108
+ ```tsx
109
+ // In App.tsx
110
+ import { Routes, Route } from "react-router-dom";
111
+ import Layout from "./components/Layout";
112
+ import ChatPage from "./pages/ChatPage";
113
+
114
+ function App() {
115
+ return (
116
+ <Routes>
117
+ <Route path="/" element={<Layout />}>
118
+ <Route index element={<HomePage />} />
119
+ <Route path="messages" element={<ChatPage />} />
120
+ </Route>
121
+ </Routes>
122
+ );
123
+ }
124
+ ```
125
+
126
+ ### Nested conversation routes with Outlet
127
+
128
+ A powerful pattern for React Router: use nested routes to show conversation details alongside the conversation list.
129
+
130
+ ```tsx
131
+ // Router config
132
+ export const router = createBrowserRouter([
133
+ {
134
+ path: "/",
135
+ element: <Layout />,
136
+ children: [
137
+ {
138
+ path: "messages",
139
+ element: <ChatLayout />,
140
+ children: [
141
+ { index: true, element: <EmptyState /> },
142
+ { path: ":conversationId", element: <ConversationView /> },
143
+ ],
144
+ },
145
+ ],
146
+ },
147
+ ]);
148
+ ```
149
+
150
+ ```tsx
151
+ // ChatLayout.tsx -- renders conversation list + Outlet for message view
152
+ import { Outlet } from "react-router-dom";
153
+ import { CometChatConversations } from "@cometchat/chat-uikit-react";
154
+ import { useNavigate } from "react-router-dom";
155
+
156
+ export default function ChatLayout() {
157
+ const navigate = useNavigate();
158
+
159
+ return (
160
+ <div style={{ display: "flex", height: "100vh" }}>
161
+ <div style={{ width: "360px", borderRight: "1px solid #eee" }}>
162
+ <CometChatConversations
163
+ onItemClick={(conversation) => {
164
+ const id = conversation.getConversationId();
165
+ navigate(`/messages/${id}`);
166
+ }}
167
+ />
168
+ </div>
169
+ <div style={{ flex: 1, display: "flex", flexDirection: "column" }}>
170
+ <Outlet />
171
+ </div>
172
+ </div>
173
+ );
174
+ }
175
+ ```
176
+
177
+ ```tsx
178
+ // ConversationView.tsx -- renders messages for the selected conversation
179
+ import { useEffect, useState } from "react";
180
+ import { useParams } from "react-router-dom";
181
+ import {
182
+ CometChatMessageHeader,
183
+ CometChatMessageList,
184
+ CometChatMessageComposer,
185
+ } from "@cometchat/chat-uikit-react";
186
+ import { CometChat } from "@cometchat/chat-sdk-javascript";
187
+
188
+ export default function ConversationView() {
189
+ const { conversationId } = useParams();
190
+ const [user, setUser] = useState<CometChat.User>();
191
+ const [group, setGroup] = useState<CometChat.Group>();
192
+
193
+ useEffect(() => {
194
+ if (!conversationId) return;
195
+
196
+ // Conversation IDs follow the pattern: "user_<uid>" or "group_<guid>"
197
+ if (conversationId.startsWith("user_")) {
198
+ const uid = conversationId.replace("user_", "");
199
+ CometChat.getUser(uid).then((u) => {
200
+ setUser(u);
201
+ setGroup(undefined);
202
+ });
203
+ } else if (conversationId.startsWith("group_")) {
204
+ const guid = conversationId.replace("group_", "");
205
+ CometChat.getGroup(guid).then((g) => {
206
+ setUser(undefined);
207
+ setGroup(g);
208
+ });
209
+ }
210
+ }, [conversationId]);
211
+
212
+ if (!user && !group) return null;
213
+
214
+ return (
215
+ <>
216
+ {user && <CometChatMessageHeader user={user} />}
217
+ {group && <CometChatMessageHeader group={group} />}
218
+ {user && <CometChatMessageList user={user} />}
219
+ {group && <CometChatMessageList group={group} />}
220
+ {user && <CometChatMessageComposer user={user} />}
221
+ {group && <CometChatMessageComposer group={group} />}
222
+ </>
223
+ );
224
+ }
225
+ ```
226
+
227
+ ### useNavigate inside CometChat callbacks
228
+
229
+ CometChat's `onItemClick` callbacks can use React Router's `useNavigate` to drive URL changes:
230
+
231
+ ```tsx
232
+ import { useNavigate } from "react-router-dom";
233
+
234
+ function ConversationsList() {
235
+ const navigate = useNavigate();
236
+
237
+ return (
238
+ <CometChatConversations
239
+ onItemClick={(conversation) => {
240
+ const entity = conversation.getConversationWith();
241
+ if (entity instanceof CometChat.User) {
242
+ navigate(`/messages/user_${entity.getUid()}`);
243
+ } else if (entity instanceof CometChat.Group) {
244
+ navigate(`/messages/group_${entity.getGuid()}`);
245
+ }
246
+ }}
247
+ />
248
+ );
249
+ }
250
+ ```
251
+
252
+ This works because `useNavigate` returns a stable function that CometChat's callback invokes in the browser.
253
+
254
+ ---
255
+
256
+ ## 3. v7 framework mode patterns
257
+
258
+ React Router v7 in framework mode is a full meta-framework with SSR, file-system routing, loaders, and actions. It has the same SSR concerns as Next.js: CometChat components cannot run on the server.
259
+
260
+ ### SSR prevention
261
+
262
+ v7 renders components on the server by default. CometChat components will crash during server rendering. Use a `ClientOnly` wrapper:
263
+
264
+ ```tsx
265
+ // app/components/ClientOnly.tsx
266
+ import { useState, useEffect, type ReactNode } from "react";
267
+
268
+ interface ClientOnlyProps {
269
+ children: ReactNode;
270
+ fallback?: ReactNode;
271
+ }
272
+
273
+ export function ClientOnly({ children, fallback = null }: ClientOnlyProps) {
274
+ const [mounted, setMounted] = useState(false);
275
+
276
+ useEffect(() => {
277
+ setMounted(true);
278
+ }, []);
279
+
280
+ return mounted ? <>{children}</> : <>{fallback}</>;
281
+ }
282
+ ```
283
+
284
+ Or use `React.lazy` + `Suspense`:
285
+
286
+ ```tsx
287
+ import { lazy, Suspense } from "react";
288
+
289
+ const ChatView = lazy(() => import("../components/ChatView"));
290
+
291
+ export default function MessagesRoute() {
292
+ return (
293
+ <ClientOnly fallback={<div>Loading chat...</div>}>
294
+ <Suspense fallback={<div>Loading chat...</div>}>
295
+ <ChatView />
296
+ </Suspense>
297
+ </ClientOnly>
298
+ );
299
+ }
300
+ ```
301
+
302
+ ### CometChatProvider for v7 framework mode
303
+
304
+ The provider needs `ClientOnly` wrapping because `useEffect` runs on the client but the module import of `@cometchat/chat-uikit-react` happens on the server too:
305
+
306
+ ```tsx
307
+ // app/providers/CometChatProvider.tsx
308
+ import React, { useEffect, useState, createContext, useContext } from "react";
309
+
310
+ interface CometChatContextValue {
311
+ isReady: boolean;
312
+ error: string | null;
313
+ }
314
+
315
+ const CometChatContext = createContext<CometChatContextValue>({
316
+ isReady: false,
317
+ error: null,
318
+ });
319
+
320
+ export const useCometChat = () => useContext(CometChatContext);
321
+
322
+ // Module-level state prevents both double-init AND double-login in React
323
+ // StrictMode. Without the loginInFlight guard, a second mount calls
324
+ // login() while the first is still pending and the SDK throws
325
+ // "Please wait until the previous login request ends."
326
+ let initialized = false;
327
+ let loginInFlight: Promise<unknown> | null = null;
328
+
329
+ async function ensureLoggedIn(
330
+ uid: string,
331
+ authToken?: string,
332
+ ): Promise<void> {
333
+ const existing = await CometChatUIKit.getLoggedinUser();
334
+ if (existing) return;
335
+ if (loginInFlight) {
336
+ await loginInFlight;
337
+ return;
338
+ }
339
+ loginInFlight = authToken
340
+ ? CometChatUIKit.loginWithAuthToken(authToken)
341
+ : CometChatUIKit.login(uid);
342
+ try {
343
+ await loginInFlight;
344
+ } finally {
345
+ loginInFlight = null;
346
+ }
347
+ }
348
+
349
+ export function CometChatProvider({ children }: { children: React.ReactNode }) {
350
+ const [isReady, setIsReady] = useState(false);
351
+ const [error, setError] = useState<string | null>(null);
352
+
353
+ useEffect(() => {
354
+ async function setup() {
355
+ try {
356
+ // Dynamic import to prevent server-side module resolution
357
+ const { CometChatUIKit, UIKitSettingsBuilder } = await import(
358
+ "@cometchat/chat-uikit-react"
359
+ );
360
+
361
+ if (!initialized) {
362
+ initialized = true;
363
+
364
+ const settings = new UIKitSettingsBuilder()
365
+ .setAppId(import.meta.env.VITE_COMETCHAT_APP_ID)
366
+ .setRegion(import.meta.env.VITE_COMETCHAT_REGION)
367
+ .setAuthKey(import.meta.env.VITE_COMETCHAT_AUTH_KEY)
368
+ .subscribePresenceForAllUsers()
369
+ .build();
370
+
371
+ await CometChatUIKit.init(settings);
372
+ }
373
+
374
+ await ensureLoggedIn("cometchat-uid-1"); // DEVELOPMENT ONLY — see cometchat-production skill
375
+
376
+ setIsReady(true);
377
+ } catch (e) {
378
+ setError(String(e));
379
+ }
380
+ }
381
+
382
+ setup();
383
+ }, []);
384
+
385
+ if (error) {
386
+ return (
387
+ <div style={{ color: "red", padding: 16, fontFamily: "monospace" }}>
388
+ CometChat Error: {error}
389
+ </div>
390
+ );
391
+ }
392
+
393
+ if (!isReady) return null;
394
+
395
+ return (
396
+ <CometChatContext.Provider value={{ isReady, error }}>
397
+ {children}
398
+ </CometChatContext.Provider>
399
+ );
400
+ }
401
+ ```
402
+
403
+ **Key difference from the plain React provider:** The `import("@cometchat/chat-uikit-react")` is done dynamically inside `useEffect`, NOT at the top level. This prevents the server from trying to resolve the module.
404
+
405
+ ### Mount the provider
406
+
407
+ **Option A — Chat-only app (all routes use CometChat):**
408
+
409
+ Wrap the entire app. This disables SSR for all routes since `ClientOnly` renders nothing on the server.
410
+
411
+ ```tsx
412
+ // app/root.tsx
413
+ import { Outlet } from "react-router";
414
+ import { ClientOnly } from "./components/ClientOnly";
415
+ import { CometChatProvider } from "./providers/CometChatProvider";
416
+
417
+ export default function Root() {
418
+ return (
419
+ <html lang="en">
420
+ <body>
421
+ <ClientOnly>
422
+ <CometChatProvider>
423
+ <Outlet />
424
+ </CometChatProvider>
425
+ </ClientOnly>
426
+ </body>
427
+ </html>
428
+ );
429
+ }
430
+ ```
431
+
432
+ **Option B — Mixed app (some routes need SSR):**
433
+
434
+ Keep the root clean and scope CometChat to a layout route. Non-chat routes keep full SSR.
435
+
436
+ ```tsx
437
+ // app/root.tsx — no CometChat here, SSR works normally
438
+ import { Outlet } from "react-router";
439
+
440
+ export default function Root() {
441
+ return (
442
+ <html lang="en">
443
+ <body>
444
+ <Outlet />
445
+ </body>
446
+ </html>
447
+ );
448
+ }
449
+ ```
450
+
451
+ ```tsx
452
+ // app/routes/chat.tsx — layout route for all /chat/* paths
453
+ import { Outlet } from "react-router";
454
+ import { ClientOnly } from "../components/ClientOnly";
455
+ import { CometChatProvider } from "../providers/CometChatProvider";
456
+
457
+ export default function ChatLayout() {
458
+ return (
459
+ <ClientOnly fallback={<div>Loading chat...</div>}>
460
+ <CometChatProvider>
461
+ <Outlet />
462
+ </CometChatProvider>
463
+ </ClientOnly>
464
+ );
465
+ }
466
+ ```
467
+
468
+ Then nest chat routes under this layout (e.g. `app/routes/chat.messages.tsx`). Marketing pages, dashboards, and other non-chat routes render server-side as normal.
469
+
470
+ **Use Option A** for apps where every page involves chat (messaging apps, social platforms). **Use Option B** for apps that add chat to an existing product (SaaS, marketplaces, support).
471
+
472
+ ### File-system routing
473
+
474
+ v7 framework mode uses file-based routing. Create a route file:
475
+
476
+ ```tsx
477
+ // app/routes/messages.tsx
478
+ import { lazy, Suspense } from "react";
479
+ import { ClientOnly } from "../components/ClientOnly";
480
+
481
+ const ChatView = lazy(() => import("../components/ChatView"));
482
+
483
+ export default function MessagesRoute() {
484
+ return (
485
+ <ClientOnly fallback={<div>Loading chat...</div>}>
486
+ <Suspense fallback={<div>Loading chat...</div>}>
487
+ <ChatView />
488
+ </Suspense>
489
+ </ClientOnly>
490
+ );
491
+ }
492
+ ```
493
+
494
+ ### Loaders and actions
495
+
496
+ **NEVER put CometChat initialization or SDK calls in a `loader` or `action`.** Loaders and actions run on the server in v7 framework mode. CometChat requires browser APIs.
497
+
498
+ ```tsx
499
+ // WRONG -- loader runs on the server
500
+ export async function loader() {
501
+ const { CometChat } = await import("@cometchat/chat-sdk-javascript");
502
+ const user = await CometChat.getUser("uid"); // crashes on server
503
+ return { user };
504
+ }
505
+
506
+ // CORRECT -- use clientLoader if you need chat data at route entry
507
+ export async function clientLoader() {
508
+ const { CometChat } = await import("@cometchat/chat-sdk-javascript");
509
+ const user = await CometChat.getUser("uid");
510
+ return { user };
511
+ }
512
+ ```
513
+
514
+ `clientLoader` runs only in the browser. It is the right place for CometChat data fetching at the route level. However, most CometChat integrations do not need loaders at all -- the components handle their own data fetching.
515
+
516
+ ---
517
+
518
+ ## 4. Outlet patterns for nested conversations
519
+
520
+ This pattern works in both v6 and v7. Chat as a parent route with nested child routes:
521
+
522
+ ```
523
+ /messages → Conversation list (left pane), empty state (right pane)
524
+ /messages/:conversationId → Conversation list (left pane), messages (right pane)
525
+ ```
526
+
527
+ ### v6 route config
528
+
529
+ ```tsx
530
+ {
531
+ path: "messages",
532
+ element: <ChatLayout />,
533
+ children: [
534
+ { index: true, element: <div style={{ flex: 1, display: "flex", alignItems: "center", justifyContent: "center", color: "#999" }}>Select a conversation</div> },
535
+ { path: ":conversationId", element: <ConversationView /> },
536
+ ],
537
+ }
538
+ ```
539
+
540
+ ### v7 file-system routes
541
+
542
+ ```
543
+ app/routes/
544
+ messages.tsx → ChatLayout (renders <Outlet />)
545
+ messages._index.tsx → Empty state
546
+ messages.$conversationId.tsx → ConversationView
547
+ ```
548
+
549
+ ```tsx
550
+ // app/routes/messages.tsx
551
+ import { Outlet } from "react-router";
552
+ import { ClientOnly } from "../components/ClientOnly";
553
+ import { lazy, Suspense } from "react";
554
+
555
+ const ConversationList = lazy(() => import("../components/ConversationList"));
556
+
557
+ export default function MessagesLayout() {
558
+ return (
559
+ <div style={{ display: "flex", height: "100vh" }}>
560
+ <div style={{ width: "360px", borderRight: "1px solid #eee" }}>
561
+ <ClientOnly>
562
+ <Suspense fallback={<div>Loading...</div>}>
563
+ <ConversationList />
564
+ </Suspense>
565
+ </ClientOnly>
566
+ </div>
567
+ <div style={{ flex: 1, display: "flex", flexDirection: "column" }}>
568
+ <Outlet />
569
+ </div>
570
+ </div>
571
+ );
572
+ }
573
+ ```
574
+
575
+ ---
576
+
577
+ ## 5. Environment variables
578
+
579
+ ### v6 library mode (Vite-based)
580
+
581
+ Same as `cometchat-react-patterns` -- uses `VITE_` prefix:
582
+
583
+ ```env
584
+ VITE_COMETCHAT_APP_ID=your_app_id
585
+ VITE_COMETCHAT_REGION=us
586
+ VITE_COMETCHAT_AUTH_KEY=your_auth_key
587
+ ```
588
+
589
+ Access: `import.meta.env.VITE_COMETCHAT_APP_ID`
590
+
591
+ ### v7 framework mode
592
+
593
+ Also uses Vite under the hood, so the same `VITE_` prefix applies:
594
+
595
+ ```env
596
+ VITE_COMETCHAT_APP_ID=your_app_id
597
+ VITE_COMETCHAT_REGION=us
598
+ VITE_COMETCHAT_AUTH_KEY=your_auth_key
599
+ ```
600
+
601
+ Server-only variables (for auth token generation in actions) can omit the `VITE_` prefix so they are never exposed to the client bundle:
602
+
603
+ ```env
604
+ COMETCHAT_AUTH_TOKEN=your_server_secret
605
+ ```
606
+
607
+ Access server-only vars in loaders/actions via `process.env`:
608
+
609
+ ```tsx
610
+ // app/routes/api.cometchat-token.tsx (runs server-side only)
611
+ export async function action({ request }: ActionFunctionArgs) {
612
+ const secret = process.env.COMETCHAT_AUTH_TOKEN;
613
+ // ...
614
+ }
615
+ ```
616
+
617
+ > **Note:** `process.env` is available in loaders/actions when using the
618
+ > default Node adapter (`@react-router/node`). Other adapters (Cloudflare,
619
+ > Deno) expose env differently — check your adapter's docs.
620
+
621
+ ---
622
+
623
+ ## 6. CSS import
624
+
625
+ ### v6 library mode
626
+
627
+ Import in `src/main.tsx`:
628
+
629
+ ```tsx
630
+ import "@cometchat/chat-uikit-react/css-variables.css";
631
+ ```
632
+
633
+ ### v7 framework mode
634
+
635
+ Import in `app/root.tsx`:
636
+
637
+ ```tsx
638
+ import "@cometchat/chat-uikit-react/css-variables.css";
639
+ ```
640
+
641
+ Or use the `links` export:
642
+
643
+ ```tsx
644
+ // app/root.tsx
645
+ import cometchatStyles from "@cometchat/chat-uikit-react/css-variables.css?url";
646
+
647
+ export function links() {
648
+ return [{ rel: "stylesheet", href: cometchatStyles }];
649
+ }
650
+ ```
651
+
652
+ The `?url` suffix tells Vite to return a URL instead of injecting the CSS, which works with React Router's `links` convention.
653
+
654
+ ---
655
+
656
+ ## 7. Common pitfalls
657
+
658
+ ### Loaders are server-side in v7
659
+
660
+ The most common mistake in v7 framework mode. Loaders and actions run on the server. Any CometChat code in a loader crashes with `window is not defined`. Use `clientLoader` instead, or handle data fetching in the component.
661
+
662
+ ### v7's unstable_middleware
663
+
664
+ React Router v7's `unstable_middleware` feature does NOT affect CometChat. CometChat has no middleware requirements. Do not add CometChat-related middleware.
665
+
666
+ ### useNavigate in CometChat callbacks
667
+
668
+ `useNavigate` works inside CometChat's event callbacks (`onItemClick`, etc.) because these callbacks execute in the browser within the React tree. This is safe:
669
+
670
+ ```tsx
671
+ const navigate = useNavigate();
672
+
673
+ <CometChatConversations
674
+ onItemClick={(conv) => navigate(`/messages/${conv.getConversationId()}`)}
675
+ />
676
+ ```
677
+
678
+ ### Don't init in loaders
679
+
680
+ Even `clientLoader` is not the right place for CometChat initialization. Init should happen once in the provider (app root), not per-route. `clientLoader` is only appropriate for CometChat data queries (like `CometChat.getUser()`) that need to complete before the route renders.
681
+
682
+ ### v6 to v7 migration
683
+
684
+ If a project is migrating from v6 library mode to v7 framework mode, the CometChat integration must change:
685
+ - Add `ClientOnly` wrapper
686
+ - Move from static imports to dynamic imports for CometChat modules
687
+ - Move from `createBrowserRouter` routes to file-system routes
688
+ - Add SSR guards
689
+
690
+ Do not mix v6 and v7 patterns. Detect the mode (section 1) and use the correct pattern.
691
+
692
+ ---
693
+
694
+ ## 8. Complete integration checklist (v6 library mode)
695
+
696
+ 1. Install packages: `npm install @cometchat/chat-uikit-react @cometchat/chat-sdk-javascript`
697
+ 2. Create `.env` with `VITE_COMETCHAT_APP_ID`, `VITE_COMETCHAT_REGION`, `VITE_COMETCHAT_AUTH_KEY`
698
+ 3. Add `.env` to `.gitignore`
699
+ 4. Import `@cometchat/chat-uikit-react/css-variables.css` in `src/main.tsx`
700
+ 5. Create `src/providers/CometChatProvider.tsx` (uses `import.meta.env.VITE_*`)
701
+ 6. Mount `CometChatProvider` in `src/main.tsx` wrapping `<RouterProvider>`
702
+ 7. Create `src/pages/ChatPage.tsx` (see `cometchat-placement` for patterns)
703
+ 8. Add `{ path: "messages", element: <ChatPage /> }` to the router config
704
+ 9. Add a "Messages" link to the layout's nav
705
+
706
+ ## 9. Complete integration checklist (v7 framework mode)
707
+
708
+ 1. Install packages: `npm install @cometchat/chat-uikit-react @cometchat/chat-sdk-javascript`
709
+ 2. Create `.env` with `VITE_COMETCHAT_APP_ID`, `VITE_COMETCHAT_REGION`, `VITE_COMETCHAT_AUTH_KEY`
710
+ 3. Add `.env` to `.gitignore`
711
+ 4. Import `@cometchat/chat-uikit-react/css-variables.css` in `app/root.tsx`
712
+ 5. Create `app/components/ClientOnly.tsx` (section 3)
713
+ 6. Create `app/providers/CometChatProvider.tsx` with dynamic imports (section 3)
714
+ 7. Mount `ClientOnly` + `CometChatProvider` in `app/root.tsx`
715
+ 8. Create `app/routes/messages.tsx` with `ClientOnly` + lazy import (section 3)
716
+ 9. Add a "Messages" link to the layout's nav
717
+ 10. Verify: loaders/actions contain NO CometChat imports