@compagnat/dc-common-ui 0.1.0 → 0.2.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 ADDED
@@ -0,0 +1,104 @@
1
+ # @compagnat/dc-common-ui
2
+
3
+ Reusable React components and hooks library.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @compagnat/dc-common-ui
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### AuthProvider
14
+
15
+ Wraps your app with Cognito authentication. The configuration must be provided via the `config` prop.
16
+
17
+ ```tsx
18
+ import { AuthProvider, useAuth, type AuthConfig } from '@compagnat/dc-common-ui';
19
+ import AdminPanelSettingsIcon from '@mui/icons-material/AdminPanelSettings';
20
+ import PersonOutlineIcon from '@mui/icons-material/PersonOutline';
21
+
22
+ const authConfig: AuthConfig = {
23
+ userPoolId: 'YOUR_USER_POOL_ID',
24
+ clientId: 'YOUR_CLIENT_ID',
25
+ getUserIcon: (username) => {
26
+ if (username === 'compagnat') return <AdminPanelSettingsIcon />;
27
+ return <PersonOutlineIcon />;
28
+ },
29
+ };
30
+
31
+ function App() {
32
+ return (
33
+ <AuthProvider config={authConfig}>
34
+ <YourApp />
35
+ </AuthProvider>
36
+ );
37
+ }
38
+ ```
39
+
40
+ Consume auth state with the `useAuth` hook:
41
+
42
+ ```tsx
43
+ function Profile() {
44
+ const { isAuthenticated, username, roles, userIcon, login, logout } = useAuth();
45
+ // ...
46
+ }
47
+ ```
48
+
49
+ #### `AuthConfig`
50
+
51
+ | Property | Type | Required | Description |
52
+ |---|---|---|---|
53
+ | `userPoolId` | `string` | yes | Cognito User Pool ID |
54
+ | `clientId` | `string` | yes | Cognito App Client ID |
55
+ | `getUserIcon` | `(username: string \| null \| undefined) => ReactNode` | no | Resolves a user icon from the username |
56
+
57
+ #### Standalone API helpers
58
+
59
+ These functions work outside React components (e.g. in API layers):
60
+
61
+ ```ts
62
+ import { GetAccessToken, GetClientKey, HasRole } from '@compagnat/dc-common-ui';
63
+ ```
64
+
65
+ - `GetAccessToken()` — returns the current JWT or `null`
66
+ - `GetClientKey()` — returns the `custom:clientKey` claim or `''`
67
+ - `GetSubClientKey()` — returns the `custom:subClientKey` claim or `''`
68
+ - `HasRole(role)` — checks if the user has the given Cognito group role
69
+
70
+ > **Note:** These functions rely on the `AuthProvider` being mounted to initialise the Cognito pool.
71
+
72
+ ### CookieConsent
73
+
74
+ ```tsx
75
+ import { CookieConsentProvider, useCookieConsent, CookieConsentBanner } from '@compagnat/dc-common-ui';
76
+ ```
77
+
78
+ Gestion du consentement aux cookies avec GTM.
79
+
80
+ ### Logger
81
+
82
+ Pino logger configurable at runtime. By default it's disabled (`silent` level). Call `configureLogger` early in your app boot:
83
+
84
+ ```ts
85
+ import { configureLogger, logger } from '@compagnat/dc-common-ui';
86
+
87
+ configureLogger({ level: 'info', name: 'my-app' });
88
+ logger.info('App started');
89
+ ```
90
+
91
+ #### `LoggerConfig`
92
+
93
+ | Property | Type | Required | Description |
94
+ |----------|--------|----------|---------------------------------|
95
+ | `level` | `string` | no | Log level (`trace`, `debug`, `info`, `warn`, `error`, `fatal`). Defaults to `'error'` |
96
+ | `name` | `string` | no | Logger name passed to pino |
97
+
98
+ ### MessageProvider
99
+
100
+ ```tsx
101
+ import { MessageProvider, useMessage, sendGlobalSuccessMessage } from '@compagnat/dc-common-ui';
102
+ ```
103
+
104
+ Global message queue for toasts / snackbars.