@hadoku/prefs-client 1.1.25 → 1.1.26

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.
@@ -24,8 +24,11 @@ type LinkProvider = (typeof LINK_PROVIDERS)[number];
24
24
  * Render it as an `<img>`; never fetch it server-side.
25
25
  * - `sprite` is the person's character (a watchparty character id) whenever
26
26
  * they have one, WHATEVER the source: games use it as the piece and the
27
- * name colour even when the picture is a Discord avatar. Absent = never
28
- * picked; use your own default. The app that owns the art (watchparty, its
27
+ * name colour even when the picture is a Discord avatar. Every live account
28
+ * has one: prefs-api deals a random character from `@wolffm/rooms-protocol`'s
29
+ * AVATAR_POOL to an account that never picked, and saves it, so never pick
30
+ * one for an account yourself. Absent only for a userId that is no live
31
+ * account (unknown, or closed). The app that owns the art (watchparty, its
29
32
  * public GET /watchparty/api/characters) owns its URL and revision.
30
33
  *
31
34
  * A source that stops being available (an unlink, an admin removal, an account
@@ -77,6 +80,7 @@ interface AvatarSettings {
77
80
  avatar: ResolvedAvatar;
78
81
  /** What the user picked; null = never picked (resolves to sprite). */
79
82
  choice: AvatarSource | null;
83
+ /** The character: picked, or dealt at random on the first read when never picked. */
80
84
  sprite: string | null;
81
85
  linked: Partial<Record<LinkProvider, LinkedAccountView>>;
82
86
  upload: UploadView | null;
package/dist/avatar.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { L as LoadoutUpdate } from './cosmetics-contract-CKe1lNNV.js';
2
2
  export { A as AvatarCosmetics, C as CosmeticSlot, a as CosmeticsView, b as Loadout, P as Placement } from './cosmetics-contract-CKe1lNNV.js';
3
- import { A as AvatarSettings, a as AvatarSource, b as AvatarUpdate, L as LinkProvider, c as LinkStart, R as ResolvedPerson } from './avatar-contract-DbFGb4K-.js';
4
- export { d as AVATAR_SOURCES, e as AvatarUploadLimits, f as LINK_CALLBACK_PATH, g as LINK_MESSAGE_TYPE, h as LINK_PROVIDERS, i as LinkMessage, j as LinkedAccountView, k as RESOLVE_MAX_IDS, l as ResolveRequest, m as ResolveResponse, n as ResolvedAvatar, S as SPRITE_ID_PATTERN, U as UploadView } from './avatar-contract-DbFGb4K-.js';
3
+ import { A as AvatarSettings, a as AvatarSource, b as AvatarUpdate, L as LinkProvider, c as LinkStart, R as ResolvedPerson } from './avatar-contract-vrX2StQI.js';
4
+ export { d as AVATAR_SOURCES, e as AvatarUploadLimits, f as LINK_CALLBACK_PATH, g as LINK_MESSAGE_TYPE, h as LINK_PROVIDERS, i as LinkMessage, j as LinkedAccountView, k as RESOLVE_MAX_IDS, l as ResolveRequest, m as ResolveResponse, n as ResolvedAvatar, S as SPRITE_ID_PATTERN, U as UploadView } from './avatar-contract-vrX2StQI.js';
5
5
 
6
6
  /** sessionStorage key the callback page reads to know it runs in the link popup. */
7
7
  declare const LINK_POPUP_MARK = "hadoku-avatar-link-popup";
@@ -63,5 +63,27 @@ interface ResolveAvatarsOptions extends AvatarClientOptions {
63
63
  * RESOLVE_MAX_IDS for you. Every requested id is in the result.
64
64
  */
65
65
  declare function resolveAvatars(opts: ResolveAvatarsOptions, userIds: readonly string[]): Promise<Record<string, ResolvedPerson>>;
66
+ /**
67
+ * The window event the fleet settings popout (`ConnectedSettings`,
68
+ * @wolffm/task-ui-components) fires after each avatar change the person makes,
69
+ * whichever app on the page drew it. `detail` is the new AvatarSettings.
70
+ */
71
+ declare const AVATAR_EVENT = "hadoku:avatar";
72
+ /** The character in a settings read: the person's own pick, else the one prefs-api resolved. */
73
+ declare function characterOf(settings: Pick<AvatarSettings, 'sprite' | 'avatar'> | null | undefined): string | null;
74
+ /**
75
+ * The signed-in viewer's character, or null. Never throws: a room joins
76
+ * without one rather than not at all.
77
+ *
78
+ * Quiet nulls: 401/403 (signed out) and 404 (no prefs-api on this origin, as
79
+ * on a local dev or e2e server). Anything else is a fault, passed to `onError`
80
+ * (console.warn by default), and still null. `baseUrl` defaults to THIS page's
81
+ * origin (`/prefs`), not hadoku.me, so a local run never reaches production.
82
+ */
83
+ declare function fetchCharacter(opts?: AvatarClientOptions & {
84
+ onError?: (err: unknown) => void;
85
+ }): Promise<string | null>;
86
+ /** Call `onChange` with the character after each popout change; returns the unsubscribe. */
87
+ declare function onAvatarChange(onChange: (character: string | null, settings: AvatarSettings) => void): () => void;
66
88
 
67
- export { type AvatarClient, type AvatarClientOptions, AvatarError, AvatarSettings, AvatarSource, AvatarUpdate, LINK_POPUP_MARK, LinkProvider, LinkStart, LoadoutUpdate, type ResolveAvatarsOptions, ResolvedPerson, createAvatarClient, resolveAvatars };
89
+ export { AVATAR_EVENT, type AvatarClient, type AvatarClientOptions, AvatarError, AvatarSettings, AvatarSource, AvatarUpdate, LINK_POPUP_MARK, LinkProvider, LinkStart, LoadoutUpdate, type ResolveAvatarsOptions, ResolvedPerson, characterOf, createAvatarClient, fetchCharacter, onAvatarChange, resolveAvatars };
package/dist/avatar.js CHANGED
@@ -134,7 +134,34 @@ async function resolveAvatars(opts, userIds) {
134
134
  }
135
135
  return out;
136
136
  }
137
+ var AVATAR_EVENT = "hadoku:avatar";
138
+ function characterOf(settings) {
139
+ return settings?.sprite ?? settings?.avatar?.sprite ?? null;
140
+ }
141
+ async function fetchCharacter(opts = {}) {
142
+ const origin = typeof location === "undefined" ? void 0 : location.origin;
143
+ const baseUrl = opts.baseUrl ?? (origin ? `${origin}/prefs` : void 0);
144
+ if (!baseUrl) return null;
145
+ try {
146
+ return characterOf(await createAvatarClient({ baseUrl, fetch: opts.fetch }).get());
147
+ } catch (err) {
148
+ if (err instanceof AvatarError && [401, 403, 404].includes(err.status)) return null;
149
+ (opts.onError ?? ((e) => console.warn("[prefs-client] avatar settings unreadable", e)))(err);
150
+ return null;
151
+ }
152
+ }
153
+ function onAvatarChange(onChange) {
154
+ if (typeof window === "undefined") return () => {
155
+ };
156
+ const listener = (e) => {
157
+ const settings = e.detail;
158
+ if (settings) onChange(characterOf(settings), settings);
159
+ };
160
+ window.addEventListener(AVATAR_EVENT, listener);
161
+ return () => window.removeEventListener(AVATAR_EVENT, listener);
162
+ }
137
163
  export {
164
+ AVATAR_EVENT,
138
165
  AVATAR_SOURCES,
139
166
  AvatarError,
140
167
  LINK_CALLBACK_PATH,
@@ -143,6 +170,9 @@ export {
143
170
  LINK_PROVIDERS,
144
171
  RESOLVE_MAX_IDS,
145
172
  SPRITE_ID_PATTERN,
173
+ characterOf,
146
174
  createAvatarClient,
175
+ fetchCharacter,
176
+ onAvatarChange,
147
177
  resolveAvatars
148
178
  };
package/dist/games.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { n as ResolvedAvatar } from './avatar-contract-DbFGb4K-.js';
1
+ import { n as ResolvedAvatar } from './avatar-contract-vrX2StQI.js';
2
2
  import './cosmetics-contract-CKe1lNNV.js';
3
3
 
4
4
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/prefs-client",
3
- "version": "1.1.25",
4
- "gitHead": "155e8fbf36a57da2948b06923ae8dba64a88e09c",
3
+ "version": "1.1.26",
4
+ "gitHead": "595d6bff486fa432d50957c2d0e17ce517267e37",
5
5
  "description": "Client SDK for the hadoku unified preferences store. Wraps /prefs/api/v1/* with localStorage caching, debounced writes, Zod-validated reads, and a usePrefs() React hook, plus match history, ratings and leaderboards for every game (./games), the one resolved avatar per user (./avatar), and the avatar cosmetics renderer (./cosmetics).",
6
6
  "type": "module",
7
7
  "main": "./dist/index.js",