astrogators-shared-ui 0.6.0 → 0.6.1

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.
Files changed (3) hide show
  1. package/README.md +137 -203
  2. package/dist/index.js +31 -19
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,172 +1,114 @@
1
- # @psytor/astrogators-shared-ui
1
+ # astrogators-shared-ui
2
2
 
3
- Shared UI components and utilities for Astrogator's Table applications.
3
+ Shared React components, auth, and API client for the Astrogator's Table
4
+ frontends (`astrogators-hub`, `mod-ledger-ui`, `nightwatcher-ui`). Published
5
+ to the public npm registry as `astrogators-shared-ui` (unscoped).
6
+
7
+ > The git repo lives under `psytor/astrogators-shared-ui` on GitHub, but the
8
+ > package itself is published to **npmjs.org**, not GitHub Packages. No
9
+ > `.npmrc` scope config is needed to install.
10
+
11
+ This is a library — there is no app shell here. See `PUBLISHING.md` for the
12
+ release flow and `CLAUDE.md` for architecture notes.
4
13
 
5
14
  ## Installation
6
15
 
7
16
  ```bash
8
- npm install @psytor/astrogators-shared-ui
17
+ npm install astrogators-shared-ui
9
18
  ```
10
19
 
11
- ## Setup
12
-
13
- ### 1. Initialize API Client
20
+ Peer dependencies: `react` and `react-dom` (18 or 19).
14
21
 
15
- In your application's entry point (e.g., `main.tsx` or `App.tsx`):
16
-
17
- ```tsx
18
- import { initializeApiClient } from '@psytor/astrogators-shared-ui';
22
+ ## Setup
19
23
 
20
- initializeApiClient({
21
- baseURL: 'http://localhost:8000', // Your backend API URL
22
- onUnauthorized: () => {
23
- // Handle unauthorized (e.g., redirect to login)
24
- window.location.href = '/login';
25
- },
26
- });
27
- ```
24
+ ### 1. Wrap the app in `AuthProvider`
28
25
 
29
- ### 2. Wrap Application with AuthProvider
26
+ `AuthProvider` initializes the API client with the given `apiBaseUrl` and
27
+ manages auth, feature flags, and ally codes for the whole app. Pass the
28
+ **prefixed** backend URL — workspace backends mount their routes under
29
+ `/<service-name>` (see the workspace `CLAUDE.md`).
30
30
 
31
31
  ```tsx
32
- import { AuthProvider } from '@psytor/astrogators-shared-ui';
32
+ import { AuthProvider } from 'astrogators-shared-ui';
33
+ import 'astrogators-shared-ui/styles';
33
34
 
34
35
  function App() {
35
36
  return (
36
- <AuthProvider>
37
- {/* Your app content */}
37
+ <AuthProvider apiBaseUrl={import.meta.env.VITE_API_BASE_URL}>
38
+ {/* your app */}
38
39
  </AuthProvider>
39
40
  );
40
41
  }
41
42
  ```
42
43
 
43
- ### 3. Import Global Styles
44
-
45
- ```tsx
46
- import '@psytor/astrogators-shared-ui/styles';
47
- ```
48
-
49
- ## Components
44
+ `VITE_API_BASE_URL` looks like `http://localhost:8000/astrogators-table` in
45
+ dev.
50
46
 
51
- ### Layout Components
47
+ ### 2. (Optional) Reconfigure the API client
52
48
 
53
- #### TopBar
49
+ `AuthProvider` already calls `initializeApiClient`. Call it yourself only if
50
+ you need a custom `onUnauthorized` handler (e.g. router-driven redirects):
54
51
 
55
52
  ```tsx
56
- import { TopBar } from '@psytor/astrogators-shared-ui';
53
+ import { initializeApiClient } from 'astrogators-shared-ui';
57
54
 
58
- <TopBar
59
- logo={<div>Astrogator's Table</div>}
60
- rightContent={<button>Login</button>}
61
- />
55
+ initializeApiClient({
56
+ baseURL: import.meta.env.VITE_API_BASE_URL,
57
+ onUnauthorized: () => navigate('/login'),
58
+ });
62
59
  ```
63
60
 
64
- #### Container
65
-
66
- ```tsx
67
- import { Container } from '@psytor/astrogators-shared-ui';
61
+ ## Components
68
62
 
69
- <Container maxWidth="lg" padding>
70
- {/* Content */}
71
- </Container>
72
- ```
63
+ All components are styled via CSS Modules and the global design tokens in
64
+ `./styles`. Override the design system by redefining CSS variables on `:root`
65
+ (see "Theming").
73
66
 
74
- #### Footer
67
+ ### Layout
75
68
 
76
69
  ```tsx
77
- import { Footer } from '@psytor/astrogators-shared-ui';
70
+ import { TopBar, Container, Footer } from 'astrogators-shared-ui';
78
71
 
72
+ <TopBar logo={<Logo />} rightContent={<UserMenu />} />
73
+ <Container maxWidth="lg" padding>{children}</Container>
79
74
  <Footer />
80
75
  ```
81
76
 
82
- ### Form Components
83
-
84
- #### Button
77
+ ### Forms
85
78
 
86
79
  ```tsx
87
- import { Button } from '@psytor/astrogators-shared-ui';
80
+ import { Button, Input, Select, AllyCodeDropdown } from 'astrogators-shared-ui';
88
81
 
89
- <Button variant="primary" size="md" onClick={handleClick}>
90
- Click Me
91
- </Button>
82
+ <Button variant="primary" size="md" loading={submitting}>Save</Button>
83
+ <Input label="Email" type="email" required error={errors.email} />
84
+ <Select label="Profile" options={profiles} placeholder="Choose…" />
92
85
 
93
- <Button variant="outline" loading>
94
- Loading...
95
- </Button>
86
+ // Wired into useAuth — manages the user's ally codes (DB-backed when
87
+ // authenticated, localStorage when anonymous):
88
+ <AllyCodeDropdown />
96
89
  ```
97
90
 
98
- #### Input
91
+ `Button` variants: `primary | secondary | outline | ghost | danger`.
99
92
 
100
- ```tsx
101
- import { Input } from '@psytor/astrogators-shared-ui';
102
-
103
- <Input
104
- label="Email"
105
- type="email"
106
- placeholder="Enter your email"
107
- required
108
- error={errors.email}
109
- />
110
- ```
111
-
112
- #### Select
93
+ ### Display
113
94
 
114
95
  ```tsx
115
- import { Select } from '@psytor/astrogators-shared-ui';
116
-
117
- <Select
118
- label="Choose Profile"
119
- options={[
120
- { value: 'standard', label: 'Standard' },
121
- { value: 'speed', label: 'Speed Focus' },
122
- ]}
123
- placeholder="Select a profile"
124
- />
125
- ```
126
-
127
- ### Display Components
128
-
129
- #### Card
130
-
131
- ```tsx
132
- import { Card } from '@psytor/astrogators-shared-ui';
96
+ import { Card, Badge, Modal } from 'astrogators-shared-ui';
133
97
 
134
98
  <Card variant="elevated" chamfered chamferSize="md" padding="lg" hoverable>
135
- <h3>The Mod Ledger</h3>
136
- <p>Analyze your mods</p>
99
+
137
100
  </Card>
138
- ```
139
-
140
- #### Badge
141
-
142
- ```tsx
143
- import { Badge } from '@psytor/astrogators-shared-ui';
144
101
 
145
102
  <Badge variant="success">Active</Badge>
146
103
  <Badge variant="warning" size="sm">Beta</Badge>
147
- ```
148
104
 
149
- #### Modal
150
-
151
- ```tsx
152
- import { Modal } from '@psytor/astrogators-shared-ui';
153
-
154
- <Modal
155
- isOpen={isOpen}
156
- onClose={() => setIsOpen(false)}
157
- title="Login"
158
- size="md"
159
- >
160
- {/* Modal content */}
161
- </Modal>
105
+ <Modal isOpen={open} onClose={close} title="Login" size="md">…</Modal>
162
106
  ```
163
107
 
164
- ### Feedback Components
165
-
166
- #### Loader
108
+ ### Feedback
167
109
 
168
110
  ```tsx
169
- import { Loader } from '@psytor/astrogators-shared-ui';
111
+ import { Loader } from 'astrogators-shared-ui';
170
112
 
171
113
  <Loader size="md" variant="spinner" />
172
114
  <Loader size="lg" variant="dots" />
@@ -174,82 +116,96 @@ import { Loader } from '@psytor/astrogators-shared-ui';
174
116
 
175
117
  ## Authentication
176
118
 
177
- ### useAuth Hook
119
+ `useAuth` returns the full auth + ally-code surface. The hook must be called
120
+ inside `AuthProvider`.
178
121
 
179
122
  ```tsx
180
- import { useAuth } from '@psytor/astrogators-shared-ui';
123
+ import { useAuth } from 'astrogators-shared-ui';
181
124
 
182
- function MyComponent() {
183
- const { user, isAuthenticated, login, logout, isLoading } = useAuth();
125
+ const {
126
+ // session
127
+ user, isAuthenticated, isLoading,
128
+ login, register, logout, refreshUser,
129
+ forgotPassword, resetPassword, resendVerification,
184
130
 
185
- const handleLogin = async () => {
186
- try {
187
- await login({
188
- email: 'user@example.com',
189
- password: 'password',
190
- });
191
- } catch (error) {
192
- console.error('Login failed:', error);
193
- }
194
- };
131
+ // backend feature flags (e.g. auth_enabled)
132
+ authEnabled, isLoadingFeatures,
195
133
 
196
- if (isLoading) return <Loader />;
134
+ // ally codes DB-backed when logged in, localStorage when anonymous
135
+ allyCodes, selectedAllyCode, isLoadingAllyCodes,
136
+ fetchAllyCodes, addAllyCode, removeAllyCode,
137
+ selectAllyCode, updateAllyCodeLastUsed,
197
138
 
198
- return (
199
- <div>
200
- {isAuthenticated ? (
201
- <div>
202
- <p>Welcome, {user?.username}!</p>
203
- <button onClick={logout}>Logout</button>
204
- </div>
205
- ) : (
206
- <button onClick={handleLogin}>Login</button>
207
- )}
208
- </div>
209
- );
210
- }
139
+ // localStorage → DB migration prompt for users who sign up after
140
+ // adding ally codes anonymously
141
+ migrationPrompt, dismissMigrationPrompt, migrateLocalStorageCodes,
142
+ } = useAuth();
211
143
  ```
212
144
 
213
- ## API Client
145
+ Tokens are stored in `localStorage`. Registration does **not** auto-login —
146
+ it requires email verification.
214
147
 
215
- ### Using the API Client
148
+ ## API client
216
149
 
217
150
  ```tsx
218
- import { apiClient } from '@psytor/astrogators-shared-ui';
151
+ import { apiClient } from 'astrogators-shared-ui';
219
152
 
220
- // GET request
221
- const data = await apiClient.get('/api/v1/game-data/characters');
222
-
223
- // POST request
153
+ const characters = await apiClient.get('/api/v1/game-data/characters');
224
154
  const result = await apiClient.post('/api/v1/mod-ledger/evaluate/123456789', {
225
155
  profile_name: 'standard',
226
156
  });
227
157
  ```
228
158
 
229
- The API client automatically:
230
- - Injects JWT access token in Authorization header
231
- - Refreshes expired tokens
232
- - Retries failed requests after token refresh
233
- - Calls `onUnauthorized` callback on auth failure
159
+ The client:
160
+ - injects the access token into `Authorization`
161
+ - on `401`, transparently refreshes via `/api/v1/auth/refresh-token` and
162
+ retries the original request once
163
+ - calls `onUnauthorized` if refresh fails
164
+
165
+ Endpoints are written **without** the service prefix — the prefix lives in
166
+ the configured `baseURL`.
234
167
 
235
- ## TypeScript Types
168
+ ## Ally code utilities
236
169
 
237
- All TypeScript types are exported:
170
+ For UI that needs to format/validate the 9-digit SWGOH player IDs outside the
171
+ context of `useAuth`:
172
+
173
+ ```tsx
174
+ import {
175
+ formatAllyCode, // "123456789" → "123-456-789"
176
+ unformatAllyCode, // "123-456-789" → "123456789"
177
+ getAllyCodesFromStorage,
178
+ saveAllyCodeToStorage,
179
+ removeAllyCodeFromStorage,
180
+ getSelectedAllyCode,
181
+ setSelectedAllyCode,
182
+ clearAllyCodes,
183
+ } from 'astrogators-shared-ui';
184
+ ```
185
+
186
+ Prefer `useAuth` when you can — it keeps DB and localStorage in sync.
187
+
188
+ ## TypeScript
189
+
190
+ All public types are re-exported from the package root, including:
238
191
 
239
192
  ```tsx
240
193
  import type {
241
- User,
242
- LoginRequest,
243
- LoginResponse,
244
- ParsedMod,
245
- ModEvaluation,
246
- ApiError,
247
- } from '@psytor/astrogators-shared-ui';
194
+ User, LoginRequest, LoginResponse,
195
+ RegisterRequest, ForgotPasswordRequest, ResetPasswordRequest,
196
+ AllyCode, AllyCodeCreate, AllyCodeListResponse, StoredAllyCode,
197
+ ApiResponse, ApiError, PaginatedResponse,
198
+ ParsedMod, ModStat, ModEvaluation, EvaluationRequest, EvaluationResponse,
199
+ } from 'astrogators-shared-ui';
248
200
  ```
249
201
 
250
- ## CSS Variables
202
+ Consumers should set `"moduleResolution": "bundler"` (or `"node16"`) in
203
+ `tsconfig.json` so the bundled `.d.ts` files resolve.
251
204
 
252
- Customize the design system by overriding CSS variables:
205
+ ## Theming
206
+
207
+ Design tokens are CSS variables on `:root`. Override anything you need in
208
+ your own stylesheet, loaded after the library styles:
253
209
 
254
210
  ```css
255
211
  :root {
@@ -261,57 +217,35 @@ Customize the design system by overriding CSS variables:
261
217
  }
262
218
  ```
263
219
 
264
- ## Chamfered Boxes
220
+ ### Chamfered boxes
265
221
 
266
- Use the chamfered box effect on any element:
222
+ Sci-fi cut-corner effect, available as utility classes or via `Card`:
267
223
 
268
224
  ```tsx
269
- <div className="chamfered-box">
270
- {/* Content with cut corners */}
271
- </div>
272
-
273
- <div className="chamfered-box-lg">
274
- {/* Large chamfered corners */}
275
- </div>
225
+ <div className="chamfered-box">…</div>
226
+ <div className="chamfered-box-sm">…</div>
227
+ <div className="chamfered-box-lg">…</div>
276
228
 
277
- <div className="chamfered-box-sm">
278
- {/* Small chamfered corners */}
279
- </div>
280
- ```
281
-
282
- Or use the Card component:
283
-
284
- ```tsx
285
- <Card chamfered chamferSize="lg">
286
- {/* Content */}
287
- </Card>
229
+ <Card chamfered chamferSize="lg">…</Card>
288
230
  ```
289
231
 
290
232
  ## Development
291
233
 
292
234
  ```bash
293
- # Install dependencies
294
235
  npm install
295
-
296
- # Build library
297
- npm run build
298
-
299
- # Type check
300
- npm run type-check
236
+ npm run build # tsc && vite build → dist/
237
+ npm run type-check # tsc --noEmit
301
238
  ```
302
239
 
303
- ## Publishing
240
+ There is no `dev` server worth running (this is a library, not an app).
241
+ Iterate by rebuilding and reinstalling in a consumer.
304
242
 
305
- ```bash
306
- # Bump version
307
- npm version patch # or minor, or major
308
-
309
- # Build
310
- npm run build
311
-
312
- # Publish to GitHub Packages
313
- npm publish
314
- ```
243
+ See `PUBLISHING.md` for the release procedure. Two non-negotiable rules:
244
+ - **Always `npm run build` before `npm publish`** — `dist/` is gitignored
245
+ but is the only thing shipped, so skipping the build re-publishes stale
246
+ code.
247
+ - **`npm login` and `npm publish` must be run by the user**, not by Claude
248
+ — publishing needs interactive npm auth.
315
249
 
316
250
  ## License
317
251
 
package/dist/index.js CHANGED
@@ -417,29 +417,41 @@ var x = "astrogators_access_token", S = "astrogators_refresh_token", C = () => l
417
417
  y(!1);
418
418
  }
419
419
  }, [r]), I = n(async (e) => {
420
+ r && typeof e == "number" ? (await N.put(`/api/v1/users/me/ally-codes/${e}/use`, {}), await F()) : !r && typeof e == "string" && (B(e), h(L()));
421
+ }, [r, F]), G = n((e) => {
422
+ if (_(e), H(e), e && r) {
423
+ let t = m.find((t) => "ally_code" in t && t.ally_code === e);
424
+ t && "id" in t && I(t.id);
425
+ } else e && !r && B(e);
426
+ }, [
427
+ r,
428
+ m,
429
+ I
430
+ ]), K = n(async (e) => {
420
431
  if (!/^\d{9}$/.test(e)) throw Error("Ally code must be exactly 9 digits");
421
432
  if (r) {
422
433
  let t = await N.post("/api/v1/users/me/ally-codes", { ally_code: e });
423
- h((e) => [t, ...e]), m.length === 0 && K(e);
434
+ h((e) => [t, ...e]), m.length === 0 && G(e);
424
435
  } else {
425
436
  let t = await N.get(`/api/v1/player-data/player/${e}`);
426
437
  if (!t) throw Error("Invalid ally code - player not found");
427
438
  R({
428
439
  ally_code: e,
429
- player_name: t?.data?.name || null,
440
+ player_name: t.data?.name ?? null,
430
441
  last_used_at: (/* @__PURE__ */ new Date()).toISOString()
431
- }), h(L()), m.length === 0 && K(e);
442
+ }), h(L()), m.length === 0 && G(e);
432
443
  }
433
- }, [r, m.length]), G = n(async (e) => {
434
- r && typeof e == "number" ? (await N.delete(`/api/v1/users/me/ally-codes/${e}`), h((t) => t.filter((t) => "id" in t && t.id !== e))) : !r && typeof e == "string" && (z(e), h(L()), g === e && K(null));
435
- }, [r, g]), K = n((e) => {
436
- if (_(e), H(e), e && r) {
437
- let t = m.find((t) => "ally_code" in t && t.ally_code === e);
438
- t && "id" in t && q(t.id);
439
- } else e && !r && B(e);
440
- }, [r, m]), q = n(async (e) => {
441
- r && typeof e == "number" ? (await N.put(`/api/v1/users/me/ally-codes/${e}/use`, {}), await F()) : !r && typeof e == "string" && (B(e), h(L()));
442
- }, [r, F]), J = n(() => {
444
+ }, [
445
+ r,
446
+ m.length,
447
+ G
448
+ ]), q = n(async (e) => {
449
+ r && typeof e == "number" ? (await N.delete(`/api/v1/users/me/ally-codes/${e}`), h((t) => t.filter((t) => "id" in t && t.id !== e))) : !r && typeof e == "string" && (z(e), h(L()), g === e && G(null));
450
+ }, [
451
+ r,
452
+ g,
453
+ G
454
+ ]), J = n(() => {
443
455
  x({
444
456
  show: !1,
445
457
  localStorageCodes: []
@@ -448,14 +460,14 @@ var x = "astrogators_access_token", S = "astrogators_refresh_token", C = () => l
448
460
  if (!r) return;
449
461
  let e = L();
450
462
  for (let t of e) try {
451
- await I(t.ally_code);
463
+ await K(t.ally_code);
452
464
  } catch (e) {
453
465
  console.error(`Failed to migrate ally code ${t.ally_code}:`, e);
454
466
  }
455
467
  U(), J();
456
468
  }, [
457
469
  r,
458
- I,
470
+ K,
459
471
  J
460
472
  ]);
461
473
  i(() => {
@@ -478,10 +490,10 @@ var x = "astrogators_access_token", S = "astrogators_refresh_token", C = () => l
478
490
  selectedAllyCode: g,
479
491
  isLoadingAllyCodes: v,
480
492
  fetchAllyCodes: F,
481
- addAllyCode: I,
482
- removeAllyCode: G,
483
- selectAllyCode: K,
484
- updateAllyCodeLastUsed: q,
493
+ addAllyCode: K,
494
+ removeAllyCode: q,
495
+ selectAllyCode: G,
496
+ updateAllyCodeLastUsed: I,
485
497
  migrationPrompt: b,
486
498
  dismissMigrationPrompt: J,
487
499
  migrateLocalStorageCodes: Y
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astrogators-shared-ui",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Shared UI components and utilities for Astrogator's Table applications",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",