@tribe-nest/forge 3.39.0 → 3.42.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/package.json +2 -2
- package/src/_tests/publishedResolvability.spec.ts +81 -3
- package/src/data/queries/useAuthActions.ts +15 -2
- package/src/data/queries/useBroadcasts.ts +60 -0
- package/src/i18n/de.json +5 -0
- package/src/i18n/en.json +5 -0
- package/src/ui/headless/broadcast/useBroadcastPoll.ts +225 -0
- package/src/ui/index.ts +4 -0
- package/src/ui/styled/ForgotPasswordForm.tsx +12 -2
- package/src/ui/styled/_tests/broadcastPollVote.spec.ts +127 -0
- package/src/ui/styled/_tests/broadcastStage.spec.ts +41 -0
- package/src/ui/styled/_tests/pollResults.spec.ts +66 -0
- package/src/ui/styled/broadcast/BroadcastMoqStage.tsx +124 -0
- package/src/ui/styled/broadcast/BroadcastPlayer.tsx +178 -10
- package/src/ui/styled/broadcast/BroadcastPollPanel.tsx +124 -0
- package/src/ui/styled/broadcast/BroadcastRealtimeStage.tsx +6 -3
- package/src/ui/styled/broadcast/broadcastStage.ts +38 -2
- package/src/ui/styled/broadcast/pollResults.ts +66 -0
- package/src/ui/styled/broadcast/vendor/moq-watch.bundle.d.ts +12 -0
- package/src/ui/styled/broadcast/vendor/moq-watch.bundle.js +549 -0
- package/src/ui/styled/forge-utilities.css +7 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tribe-nest/forge",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.42.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@stripe/react-stripe-js": "^3.8.0",
|
|
32
32
|
"@stripe/stripe-js": "^7.6.1",
|
|
33
33
|
"@tanstack/react-query": "^5.62.7",
|
|
34
|
-
"@tribe-nest/media-client": "^0.1
|
|
34
|
+
"@tribe-nest/media-client": "^0.2.1",
|
|
35
35
|
"@tribe-nest/media-protocol": "^1.0.0",
|
|
36
36
|
"axios": "^1.7.9",
|
|
37
37
|
"lucide-react": "^0.510.0",
|
|
@@ -30,12 +30,23 @@ import { join } from "path";
|
|
|
30
30
|
* creator site, while the whole suite stayed green and the starter's release
|
|
31
31
|
* notes announced the feature.
|
|
32
32
|
*
|
|
33
|
-
* So there are
|
|
33
|
+
* So there are three properties here and they are separate:
|
|
34
34
|
*
|
|
35
35
|
* 1. every bare import in the published tree names a package the consumer's
|
|
36
36
|
* install will actually produce (a dependency, or a peer that npm installs
|
|
37
|
-
* because it is NOT marked optional),
|
|
38
|
-
* 2. every workspace package Forge depends on is one npm will publish
|
|
37
|
+
* because it is NOT marked optional),
|
|
38
|
+
* 2. every workspace package Forge depends on is one npm will publish, and
|
|
39
|
+
* 3. the RANGE Forge declares for a workspace package accepts the version that
|
|
40
|
+
* package is actually at.
|
|
41
|
+
*
|
|
42
|
+
* The third failed too, and silently. Forge went on declaring
|
|
43
|
+
* `"@tribe-nest/media-client": "^0.1.0"` after that package moved to `0.2.0`,
|
|
44
|
+
* and `^0.1.0` does not accept `0.2.x`. Inside the monorepo nothing shows it -
|
|
45
|
+
* the symlink means every build, every spec and every `check-types` run sees
|
|
46
|
+
* the 0.2 source - while a creator site resolved 0.1.1 from npm and got a
|
|
47
|
+
* `useRemoteTrack` that never detached a media element, so a remount left an
|
|
48
|
+
* orphan still playing the call's audio. The published surface was a minor
|
|
49
|
+
* version behind the surface the tests were green against.
|
|
39
50
|
*
|
|
40
51
|
* Specs are excluded: they never ship (`files` is `["src"]`, but a consumer
|
|
41
52
|
* never imports a `.spec` file), and they legitimately reach for vitest, jsdom
|
|
@@ -47,6 +58,7 @@ const PACKAGES_ROOT = join(FORGE_ROOT, "..");
|
|
|
47
58
|
|
|
48
59
|
type PackageJson = {
|
|
49
60
|
name: string;
|
|
61
|
+
version?: string;
|
|
50
62
|
private?: boolean;
|
|
51
63
|
files?: string[];
|
|
52
64
|
exports?: Record<string, unknown>;
|
|
@@ -181,4 +193,70 @@ describe("what a creator site's install has to produce", () => {
|
|
|
181
193
|
it("still exports the media entry the Forge manifest advertises", () => {
|
|
182
194
|
expect(forge.exports?.["./media"]).toBe("./src/ui/media/index.ts");
|
|
183
195
|
});
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The declared range has to accept the version the package is AT.
|
|
199
|
+
*
|
|
200
|
+
* Deliberately hand-rolled rather than reaching for `semver`: this package
|
|
201
|
+
* declares its dependencies honestly (that is the whole subject of this
|
|
202
|
+
* file), and `semver` is only present here as somebody else's transitive.
|
|
203
|
+
* The cost is that only the range forms this repo actually uses are
|
|
204
|
+
* understood - anything else fails loudly rather than passing vacuously,
|
|
205
|
+
* which is the right way round for a guard.
|
|
206
|
+
*/
|
|
207
|
+
const satisfies = (range: string, version: string): boolean => {
|
|
208
|
+
const parse = (value: string): [number, number, number] => {
|
|
209
|
+
const parts = value.split(".").map((n) => Number.parseInt(n, 10));
|
|
210
|
+
if (parts.length !== 3 || parts.some((n) => !Number.isFinite(n))) {
|
|
211
|
+
throw new Error(`not a plain semver version: ${value}`);
|
|
212
|
+
}
|
|
213
|
+
return [parts[0]!, parts[1]!, parts[2]!];
|
|
214
|
+
};
|
|
215
|
+
const atLeast = (a: [number, number, number], b: [number, number, number]) =>
|
|
216
|
+
a[0] !== b[0] ? a[0] > b[0] : a[1] !== b[1] ? a[1] > b[1] : a[2] >= b[2];
|
|
217
|
+
|
|
218
|
+
const [major, minor, patch] = parse(version);
|
|
219
|
+
if (range === "*") return true;
|
|
220
|
+
if (/^\d/.test(range)) return range === version; // exact pin
|
|
221
|
+
if (range.startsWith("^")) {
|
|
222
|
+
const floor = parse(range.slice(1));
|
|
223
|
+
if (!atLeast([major, minor, patch], floor)) return false;
|
|
224
|
+
// Caret on a 0.x version pins the MINOR, which is where a pre-1.0
|
|
225
|
+
// package puts its breaking changes. `^0.1.0` does not accept `0.2.0`,
|
|
226
|
+
// and that is exactly the case this test exists for.
|
|
227
|
+
return floor[0] === 0 ? minor === floor[1] : major === floor[0];
|
|
228
|
+
}
|
|
229
|
+
throw new Error(`unsupported range form "${range}" - teach this helper before using it`);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
it("declares a range that accepts each workspace package's current version", () => {
|
|
233
|
+
const workspaceDeps = Object.entries(forge.dependencies ?? {}).filter(([name]) =>
|
|
234
|
+
name.startsWith("@tribe-nest/"),
|
|
235
|
+
);
|
|
236
|
+
expect(workspaceDeps.length).toBeGreaterThan(0);
|
|
237
|
+
|
|
238
|
+
const stale = workspaceDeps
|
|
239
|
+
.map(([name, range]) => {
|
|
240
|
+
const dependency = readPackage(join(PACKAGES_ROOT, name.replace("@tribe-nest/", "")));
|
|
241
|
+
return { name, range, version: dependency.version };
|
|
242
|
+
})
|
|
243
|
+
.filter(({ range, version }) => !satisfies(range, version!))
|
|
244
|
+
.map(({ name, range, version }) => `${name}: declared ${range}, package is at ${version}`);
|
|
245
|
+
|
|
246
|
+
expect(stale).toEqual([]);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("the range check itself knows that a 0.x caret pins the minor", () => {
|
|
250
|
+
// Without this the test above is only as good as a helper nobody checked,
|
|
251
|
+
// and the bug it guards is precisely a 0.x caret being read as permissive.
|
|
252
|
+
expect(satisfies("^0.1.0", "0.1.1")).toBe(true);
|
|
253
|
+
expect(satisfies("^0.1.0", "0.2.0")).toBe(false);
|
|
254
|
+
expect(satisfies("^0.2.1", "0.2.1")).toBe(true);
|
|
255
|
+
expect(satisfies("^0.2.1", "0.2.0")).toBe(false);
|
|
256
|
+
expect(satisfies("^1.0.0", "1.0.1")).toBe(true);
|
|
257
|
+
expect(satisfies("^1.0.0", "2.0.0")).toBe(false);
|
|
258
|
+
expect(satisfies("1.0.1", "1.0.1")).toBe(true);
|
|
259
|
+
expect(satisfies("1.0.1", "1.0.2")).toBe(false);
|
|
260
|
+
expect(() => satisfies(">=1.0.0", "1.0.0")).toThrow();
|
|
261
|
+
});
|
|
184
262
|
});
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { useForge } from "../../provider/ForgeProvider";
|
|
2
2
|
import { useMutation } from "@tanstack/react-query";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Where a Forge site's reset-password page lives.
|
|
6
|
+
*
|
|
7
|
+
* Every functional page on a code website is under `/i/`, and the emailed link
|
|
8
|
+
* is built from whatever the site sends here.
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_RESET_PASSWORD_PATH = "/i/reset-password";
|
|
11
|
+
|
|
4
12
|
// Stateless account actions (no session state) — kept out of PublicAuthContext.
|
|
5
13
|
//
|
|
6
14
|
// ## Why these hit `/public/sessions/*` and not `/public/accounts/*`
|
|
@@ -33,8 +41,8 @@ import { useMutation } from "@tanstack/react-query";
|
|
|
33
41
|
export function useForgotPassword() {
|
|
34
42
|
const { client, profileId } = useForge();
|
|
35
43
|
|
|
36
|
-
return useMutation<unknown, unknown, { email: string; origin?: string; profileId?: string }>({
|
|
37
|
-
mutationFn: async ({ email, origin, profileId: overrideProfileId }) => {
|
|
44
|
+
return useMutation<unknown, unknown, { email: string; origin?: string; profileId?: string; resetPath?: string }>({
|
|
45
|
+
mutationFn: async ({ email, origin, profileId: overrideProfileId, resetPath }) => {
|
|
38
46
|
const contextId = overrideProfileId ?? profileId;
|
|
39
47
|
if (!contextId) {
|
|
40
48
|
// Better than posting without it: the endpoint would 400 on schema
|
|
@@ -47,6 +55,11 @@ export function useForgotPassword() {
|
|
|
47
55
|
// The reset link is built server-side from this, and the association
|
|
48
56
|
// endpoint requires it (the global one treated it as optional).
|
|
49
57
|
origin: origin ?? (typeof window !== "undefined" ? window.location.origin : undefined),
|
|
58
|
+
// And the PATH, because the backend cannot guess it. Its default is
|
|
59
|
+
// `apps/client`'s `/reset-password`, which does not exist on a code
|
|
60
|
+
// website - every functional page there is under `/i/`, so the emailed
|
|
61
|
+
// link 404'd for everyone on the current default storefront.
|
|
62
|
+
resetPath: resetPath ?? DEFAULT_RESET_PASSWORD_PATH,
|
|
50
63
|
});
|
|
51
64
|
return res.data;
|
|
52
65
|
},
|
|
@@ -181,6 +181,66 @@ export type BroadcastViewerCredentials = {
|
|
|
181
181
|
* the SAME call to the same address. A session that is pinged on one stack and
|
|
182
182
|
* not the other reports a different audience count for the same room.
|
|
183
183
|
*/
|
|
184
|
+
export type BroadcastPollOption = { id: string; label: string; position: number; voteCount: number };
|
|
185
|
+
|
|
186
|
+
export type BroadcastPoll = {
|
|
187
|
+
id: string;
|
|
188
|
+
question: string;
|
|
189
|
+
status: "draft" | "live" | "closed";
|
|
190
|
+
/**
|
|
191
|
+
* The ASKING of this question in this broadcast, which is what a vote names.
|
|
192
|
+
*
|
|
193
|
+
* A poll belongs to a stream template and can be asked again in a later
|
|
194
|
+
* broadcast; each asking has its own votes and its own one-vote-per-viewer
|
|
195
|
+
* rule. So the same question next month is a different run, and somebody who
|
|
196
|
+
* answered last month may answer again.
|
|
197
|
+
*/
|
|
198
|
+
runId: string;
|
|
199
|
+
options: BroadcastPollOption[];
|
|
200
|
+
totalVotes: number;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Reading and answering the poll a broadcast is asking.
|
|
205
|
+
*
|
|
206
|
+
* Both endpoints are public and take no bearer token, like the broadcast page
|
|
207
|
+
* they belong to. The read returns only what is already on everyone's screen,
|
|
208
|
+
* and the write is identified by a key the viewer's own browser made up.
|
|
209
|
+
*/
|
|
210
|
+
export function useBroadcastPollApi() {
|
|
211
|
+
const { client } = useForge();
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
/**
|
|
215
|
+
* The question being asked right now, or null.
|
|
216
|
+
*
|
|
217
|
+
* Fetched once when the player mounts, for the viewer who arrived in the
|
|
218
|
+
* middle of a poll. Everything after that arrives on the socket, so this is
|
|
219
|
+
* not polled: a timer would be a second, later-arriving source of truth for
|
|
220
|
+
* numbers that are already being pushed.
|
|
221
|
+
*/
|
|
222
|
+
livePoll: async (broadcastId: string): Promise<BroadcastPoll | null> => {
|
|
223
|
+
const res = await client.get(`/streams/v2/public/polls/live`, { params: { broadcastId } });
|
|
224
|
+
return res.data?.poll ?? null;
|
|
225
|
+
},
|
|
226
|
+
/**
|
|
227
|
+
* One vote.
|
|
228
|
+
*
|
|
229
|
+
* `alreadyVoted` comes back true rather than an error when this viewer has
|
|
230
|
+
* already answered, which is the ordinary case for somebody with the stream
|
|
231
|
+
* open in two tabs.
|
|
232
|
+
*/
|
|
233
|
+
vote: async (payload: {
|
|
234
|
+
runId: string;
|
|
235
|
+
optionId: string;
|
|
236
|
+
voterKey: string;
|
|
237
|
+
}): Promise<{ poll: BroadcastPoll; alreadyVoted: boolean }> => {
|
|
238
|
+
const res = await client.post(`/streams/v2/public/polls/vote`, payload);
|
|
239
|
+
return res.data;
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
184
244
|
export function useBroadcastSessionApi() {
|
|
185
245
|
const { client } = useForge();
|
|
186
246
|
|
package/src/i18n/de.json
CHANGED
|
@@ -175,6 +175,11 @@
|
|
|
175
175
|
"forge.broadcast_player.sound_on": "Ton einschalten",
|
|
176
176
|
"forge.broadcast_player.started": "Vor {duration} gestartet",
|
|
177
177
|
"forge.broadcast_player.watching_now": "{count} schauen gerade zu",
|
|
178
|
+
"forge.broadcast_poll.closed": "Umfrage beendet",
|
|
179
|
+
"forge.broadcast_poll.live": "Umfrage",
|
|
180
|
+
"forge.broadcast_poll.votes_one": "{count} Stimme",
|
|
181
|
+
"forge.broadcast_poll.votes_other": "{count} Stimmen",
|
|
182
|
+
"forge.broadcast_poll.your_vote": "(deine Stimme)",
|
|
178
183
|
"forge.broadcast_ticket_purchase.buy_tickets": "Tickets kaufen",
|
|
179
184
|
"forge.broadcast_ticket_purchase.from_price": "Ab {price}",
|
|
180
185
|
"forge.broadcast_watch.leave": "Übertragung verlassen",
|
package/src/i18n/en.json
CHANGED
|
@@ -175,6 +175,11 @@
|
|
|
175
175
|
"forge.broadcast_player.sound_on": "Turn sound on",
|
|
176
176
|
"forge.broadcast_player.started": "Started {duration} ago",
|
|
177
177
|
"forge.broadcast_player.watching_now": "{count} watching now",
|
|
178
|
+
"forge.broadcast_poll.closed": "Poll closed",
|
|
179
|
+
"forge.broadcast_poll.live": "Poll",
|
|
180
|
+
"forge.broadcast_poll.votes_one": "{count} vote",
|
|
181
|
+
"forge.broadcast_poll.votes_other": "{count} votes",
|
|
182
|
+
"forge.broadcast_poll.your_vote": "(your vote)",
|
|
178
183
|
"forge.broadcast_ticket_purchase.buy_tickets": "Buy tickets",
|
|
179
184
|
"forge.broadcast_ticket_purchase.from_price": "From {price}",
|
|
180
185
|
"forge.broadcast_watch.leave": "Leave broadcast",
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { useBroadcastPollApi, type BroadcastPoll } from "../../../data/queries/useBroadcasts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The poll a viewer is being asked, and their answer to it.
|
|
7
|
+
*
|
|
8
|
+
* ## Where the numbers come from
|
|
9
|
+
*
|
|
10
|
+
* One fetch when the player mounts, for somebody who arrived mid-question, and
|
|
11
|
+
* the socket for everything after. The player owns the socket (it is the same
|
|
12
|
+
* connection chat rides on), so it hands the events down here rather than this
|
|
13
|
+
* opening a second one.
|
|
14
|
+
*
|
|
15
|
+
* ## The voter key
|
|
16
|
+
*
|
|
17
|
+
* A random id kept in this browser, per site, not per broadcast: the same
|
|
18
|
+
* person watching two streams is one voter with one key, and the server's
|
|
19
|
+
* uniqueness is per POLL anyway. It identifies nobody. It exists so that
|
|
20
|
+
* pressing the button twice counts once, and it is honestly defeatable by
|
|
21
|
+
* clearing storage, which is the trade the feature was designed around.
|
|
22
|
+
*
|
|
23
|
+
* Storage can throw (a private window, a browser set to block site data), and a
|
|
24
|
+
* viewer who cannot be remembered must still be able to vote. So a failure
|
|
25
|
+
* falls back to a key held in memory for this page view.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const VOTER_KEY_STORAGE = "tribenest.broadcast.voter";
|
|
29
|
+
|
|
30
|
+
function randomKey(): string {
|
|
31
|
+
const bytes = new Uint8Array(16);
|
|
32
|
+
if (typeof crypto !== "undefined" && crypto.getRandomValues) {
|
|
33
|
+
crypto.getRandomValues(bytes);
|
|
34
|
+
} else {
|
|
35
|
+
for (let i = 0; i < bytes.length; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
36
|
+
}
|
|
37
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function readVoterKey(): string {
|
|
41
|
+
if (typeof localStorage === "undefined") return randomKey();
|
|
42
|
+
try {
|
|
43
|
+
const held = localStorage.getItem(VOTER_KEY_STORAGE);
|
|
44
|
+
if (held && held.length >= 8) return held;
|
|
45
|
+
const minted = randomKey();
|
|
46
|
+
localStorage.setItem(VOTER_KEY_STORAGE, minted);
|
|
47
|
+
return minted;
|
|
48
|
+
} catch {
|
|
49
|
+
// Blocked or full. One vote per page view is worse than one per browser and
|
|
50
|
+
// much better than no vote at all.
|
|
51
|
+
return randomKey();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Which ASKINGS this browser has answered, so the UI opens on the results.
|
|
57
|
+
*
|
|
58
|
+
* Keyed by run and not by poll, which is the whole difference. A question
|
|
59
|
+
* belongs to a stream template and gets asked again - later in the same
|
|
60
|
+
* broadcast, or in next week's - and each asking has its own votes and its own
|
|
61
|
+
* one-vote rule. Keyed by poll, this remembered "you answered this question"
|
|
62
|
+
* for ever: a returning viewer met a fresh poll already showing bars and
|
|
63
|
+
* "(your vote)", with no way to take part. The server would have accepted their
|
|
64
|
+
* vote; the panel never offered it.
|
|
65
|
+
*/
|
|
66
|
+
const votedKey = (runId: string) => `tribenest.broadcast.voted.run.${runId}`;
|
|
67
|
+
|
|
68
|
+
function readVoted(runId: string): string | null {
|
|
69
|
+
if (typeof localStorage === "undefined") return null;
|
|
70
|
+
try {
|
|
71
|
+
return localStorage.getItem(votedKey(runId));
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function forgetVoted(runId: string): void {
|
|
78
|
+
if (typeof localStorage === "undefined") return;
|
|
79
|
+
try {
|
|
80
|
+
localStorage.removeItem(votedKey(runId));
|
|
81
|
+
} catch {
|
|
82
|
+
// Nothing to undo if it could not be written in the first place.
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function rememberVoted(runId: string, optionId: string): void {
|
|
87
|
+
if (typeof localStorage === "undefined") return;
|
|
88
|
+
try {
|
|
89
|
+
localStorage.setItem(votedKey(runId), optionId);
|
|
90
|
+
} catch {
|
|
91
|
+
// Nothing to do. The server still refuses the second vote; the viewer just
|
|
92
|
+
// sees the form again in a new tab.
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* What a viewer's own vote should be after a poll update arrives.
|
|
98
|
+
*
|
|
99
|
+
* This exists because of a bug that made the first click look ignored. Voting
|
|
100
|
+
* publishes results to EVERY viewer, including the one who just voted, and
|
|
101
|
+
* that push used to re-read the vote from storage. The push routinely beat the
|
|
102
|
+
* HTTP response it was caused by, storage had not been written yet, and the
|
|
103
|
+
* panel flipped back to buttons - so the viewer clicked again, and only the
|
|
104
|
+
* second click appeared to work. Their first vote had counted all along.
|
|
105
|
+
*
|
|
106
|
+
* The rule that fixes it: an update to the same ASKING never changes who this
|
|
107
|
+
* viewer voted for. Only a different asking re-reads storage.
|
|
108
|
+
*
|
|
109
|
+
* The identity is the RUN, not the poll. The same question asked again is a new
|
|
110
|
+
* asking with an empty tally that this viewer has not answered, so comparing
|
|
111
|
+
* polls would carry their old answer into it and lock them out of voting.
|
|
112
|
+
*/
|
|
113
|
+
export function votedOptionForIncoming(args: {
|
|
114
|
+
currentRunId: string | null;
|
|
115
|
+
incomingRunId: string | null;
|
|
116
|
+
votedOptionId: string | null;
|
|
117
|
+
/** What storage holds for the incoming asking, if anything. */
|
|
118
|
+
storedForIncoming: string | null;
|
|
119
|
+
}): string | null {
|
|
120
|
+
if (!args.incomingRunId) return null;
|
|
121
|
+
if (args.incomingRunId === args.currentRunId) {
|
|
122
|
+
// Held in memory beats storage: a vote in flight is not in storage yet, and
|
|
123
|
+
// a viewer whose browser refuses storage still gets to keep their answer
|
|
124
|
+
// for as long as the page is open.
|
|
125
|
+
return args.votedOptionId ?? args.storedForIncoming;
|
|
126
|
+
}
|
|
127
|
+
return args.storedForIncoming;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export type BroadcastPollApi = {
|
|
131
|
+
poll: BroadcastPoll | null;
|
|
132
|
+
/** The option this browser chose, if it has voted on the current poll. */
|
|
133
|
+
votedOptionId: string | null;
|
|
134
|
+
voting: boolean;
|
|
135
|
+
error: string | null;
|
|
136
|
+
vote: (optionId: string) => void;
|
|
137
|
+
/** Feed socket payloads in. Null ends the poll. */
|
|
138
|
+
onPollEvent: (poll: BroadcastPoll | null) => void;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export function useBroadcastPoll(broadcastId?: string): BroadcastPollApi {
|
|
142
|
+
const api = useBroadcastPollApi();
|
|
143
|
+
const [poll, setPoll] = useState<BroadcastPoll | null>(null);
|
|
144
|
+
const [votedOptionId, setVotedOptionId] = useState<string | null>(null);
|
|
145
|
+
const [voting, setVoting] = useState(false);
|
|
146
|
+
const [error, setError] = useState<string | null>(null);
|
|
147
|
+
const voterKeyRef = useRef<string | null>(null);
|
|
148
|
+
|
|
149
|
+
const voterKey = () => {
|
|
150
|
+
if (!voterKeyRef.current) voterKeyRef.current = readVoterKey();
|
|
151
|
+
return voterKeyRef.current;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
if (!broadcastId) return;
|
|
156
|
+
let cancelled = false;
|
|
157
|
+
api
|
|
158
|
+
.livePoll(broadcastId)
|
|
159
|
+
.then((live) => {
|
|
160
|
+
if (cancelled) return;
|
|
161
|
+
setPoll(live);
|
|
162
|
+
setVotedOptionId(live?.runId ? readVoted(live.runId) : null);
|
|
163
|
+
})
|
|
164
|
+
.catch(() => {
|
|
165
|
+
// A poll that cannot be fetched is a poll the viewer does not see. It
|
|
166
|
+
// is not worth an error on a page whose job is to play a stream.
|
|
167
|
+
if (!cancelled) setPoll(null);
|
|
168
|
+
});
|
|
169
|
+
return () => {
|
|
170
|
+
cancelled = true;
|
|
171
|
+
};
|
|
172
|
+
// The api object is rebuilt per render; the broadcast is what identifies
|
|
173
|
+
// the fetch.
|
|
174
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
175
|
+
}, [broadcastId]);
|
|
176
|
+
|
|
177
|
+
const onPollEvent = useCallback((next: BroadcastPoll | null) => {
|
|
178
|
+
setPoll((current) => {
|
|
179
|
+
setVotedOptionId((held) =>
|
|
180
|
+
votedOptionForIncoming({
|
|
181
|
+
currentRunId: current?.runId ?? null,
|
|
182
|
+
incomingRunId: next?.runId ?? null,
|
|
183
|
+
votedOptionId: held,
|
|
184
|
+
storedForIncoming: next?.runId ? readVoted(next.runId) : null,
|
|
185
|
+
}),
|
|
186
|
+
);
|
|
187
|
+
return next;
|
|
188
|
+
});
|
|
189
|
+
setError(null);
|
|
190
|
+
}, []);
|
|
191
|
+
|
|
192
|
+
const vote = useCallback(
|
|
193
|
+
(optionId: string) => {
|
|
194
|
+
const current = poll;
|
|
195
|
+
if (!current || current.status !== "live" || !current.runId || votedOptionId || voting) return;
|
|
196
|
+
|
|
197
|
+
setVoting(true);
|
|
198
|
+
setError(null);
|
|
199
|
+
// Both recorded before the request goes out, not in its `then`. The
|
|
200
|
+
// results push this vote causes routinely arrives BEFORE the response to
|
|
201
|
+
// it, and anything reading storage at that moment has to already find the
|
|
202
|
+
// vote there.
|
|
203
|
+
setVotedOptionId(optionId);
|
|
204
|
+
rememberVoted(current.runId, optionId);
|
|
205
|
+
api
|
|
206
|
+
.vote({ runId: current.runId, optionId, voterKey: voterKey() })
|
|
207
|
+
.then((result) => setPoll(result.poll))
|
|
208
|
+
.catch((err: unknown) => {
|
|
209
|
+
setVotedOptionId(null);
|
|
210
|
+
forgetVoted(current.runId);
|
|
211
|
+
const message = (err as { response?: { data?: { message?: string } } })?.response?.data?.message;
|
|
212
|
+
setError(message ?? "That vote did not go through.");
|
|
213
|
+
})
|
|
214
|
+
.finally(() => setVoting(false));
|
|
215
|
+
},
|
|
216
|
+
// `api` is rebuilt per render and holds no state of its own.
|
|
217
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
218
|
+
[poll, votedOptionId, voting],
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
return useMemo(
|
|
222
|
+
() => ({ poll, votedOptionId, voting, error, vote, onPollEvent }),
|
|
223
|
+
[poll, votedOptionId, voting, error, vote, onPollEvent],
|
|
224
|
+
);
|
|
225
|
+
}
|
package/src/ui/index.ts
CHANGED
|
@@ -183,6 +183,10 @@ export { ReplayList, type ReplayListProps } from "./styled/ReplayList";
|
|
|
183
183
|
export { LiveBroadcastList, type LiveBroadcastListProps } from "./styled/LiveBroadcastList";
|
|
184
184
|
export { BroadcastWatch, type BroadcastWatchProps } from "./styled/BroadcastWatch";
|
|
185
185
|
export { EndedBroadcast, type EndedBroadcastProps } from "./styled/broadcast/EndedBroadcast";
|
|
186
|
+
// Shared with the admin studio, which renders the same tallies in its Polls
|
|
187
|
+
// tab and burns them into the stage. Three surfaces, one rounding.
|
|
188
|
+
export { pollPercentages, type PollLike, type PollResult } from "./styled/broadcast/pollResults";
|
|
189
|
+
export { BroadcastPollPanel } from "./styled/broadcast/BroadcastPollPanel";
|
|
186
190
|
export { BroadcastPassValidation, type BroadcastPassValidationProps } from "./styled/broadcast/BroadcastPassValidation";
|
|
187
191
|
export { BroadcastTicketPurchase, type BroadcastTicketPurchaseProps } from "./styled/broadcast/BroadcastTicketPurchase";
|
|
188
192
|
export {
|
|
@@ -6,12 +6,18 @@ import { useForgeTheme } from "../theme/ForgeThemeProvider";
|
|
|
6
6
|
export interface ForgotPasswordFormProps {
|
|
7
7
|
/** Href for the "Back to login" / "Login" links. */
|
|
8
8
|
loginHref: string;
|
|
9
|
+
/**
|
|
10
|
+
* The path the emailed reset link points at, on this site's own origin.
|
|
11
|
+
* Defaults to `/i/reset-password`, which is where the starter puts it. Pass
|
|
12
|
+
* one only if the site moved that route.
|
|
13
|
+
*/
|
|
14
|
+
resetPath?: string;
|
|
9
15
|
}
|
|
10
16
|
|
|
11
17
|
const msg = (e: unknown) => (e as { response?: { data?: { message?: string } } })?.response?.data?.message;
|
|
12
18
|
|
|
13
19
|
/** Themed forgot-password request form, built on `useForgotPassword`. */
|
|
14
|
-
export function ForgotPasswordForm({ loginHref }: ForgotPasswordFormProps) {
|
|
20
|
+
export function ForgotPasswordForm({ loginHref, resetPath }: ForgotPasswordFormProps) {
|
|
15
21
|
const t = useForgeT();
|
|
16
22
|
const theme = useForgeTheme();
|
|
17
23
|
const forgotPassword = useForgotPassword();
|
|
@@ -50,7 +56,11 @@ export function ForgotPasswordForm({ loginHref }: ForgotPasswordFormProps) {
|
|
|
50
56
|
setError("");
|
|
51
57
|
setIsSubmitting(true);
|
|
52
58
|
try {
|
|
53
|
-
await forgotPassword.mutateAsync({
|
|
59
|
+
await forgotPassword.mutateAsync({
|
|
60
|
+
email,
|
|
61
|
+
origin: window.location.origin,
|
|
62
|
+
...(resetPath ? { resetPath } : {}),
|
|
63
|
+
});
|
|
54
64
|
setIsSuccess(true);
|
|
55
65
|
} catch (err) {
|
|
56
66
|
setError(msg(err) || t("forge.forgot_password_form.error"));
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { votedOptionForIncoming } from "../../headless/broadcast/useBroadcastPoll";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The rule that decides whether a viewer keeps their own vote when a poll
|
|
7
|
+
* update arrives.
|
|
8
|
+
*
|
|
9
|
+
* Reported as "I have to click twice before it records my answer". Voting
|
|
10
|
+
* pushes results to every viewer INCLUDING the one who just voted, that push
|
|
11
|
+
* routinely beat the HTTP response it was caused by, and the handler re-read
|
|
12
|
+
* the vote from storage that the response had not written yet. The panel
|
|
13
|
+
* flipped back to buttons, the viewer clicked again, and their first vote had
|
|
14
|
+
* been counted the whole time.
|
|
15
|
+
*
|
|
16
|
+
* The identity being compared is the ASKING. It was the poll once, and the
|
|
17
|
+
* second report followed: a question asked again showed a returning viewer
|
|
18
|
+
* "(your vote)" over last time's answer and refused to let them take part.
|
|
19
|
+
*/
|
|
20
|
+
describe("votedOptionForIncoming", () => {
|
|
21
|
+
it("keeps a vote that is still in flight when the results it caused arrive first", () => {
|
|
22
|
+
// The exact double-click bug: same poll, vote held in memory, storage not
|
|
23
|
+
// written yet. Answering null here is what sent the viewer back to the
|
|
24
|
+
// buttons.
|
|
25
|
+
expect(
|
|
26
|
+
votedOptionForIncoming({
|
|
27
|
+
currentRunId: "run-1",
|
|
28
|
+
incomingRunId: "run-1",
|
|
29
|
+
votedOptionId: "option-a",
|
|
30
|
+
storedForIncoming: null,
|
|
31
|
+
}),
|
|
32
|
+
).toBe("option-a");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("keeps the vote through every later result push on the same poll", () => {
|
|
36
|
+
expect(
|
|
37
|
+
votedOptionForIncoming({
|
|
38
|
+
currentRunId: "run-1",
|
|
39
|
+
incomingRunId: "run-1",
|
|
40
|
+
votedOptionId: "option-a",
|
|
41
|
+
storedForIncoming: "option-a",
|
|
42
|
+
}),
|
|
43
|
+
).toBe("option-a");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("never lets another viewer's vote overwrite this one", () => {
|
|
47
|
+
// Storage would only disagree after a stale write, but the rule has to be
|
|
48
|
+
// stated: what is held in memory for the current poll wins.
|
|
49
|
+
expect(
|
|
50
|
+
votedOptionForIncoming({
|
|
51
|
+
currentRunId: "run-1",
|
|
52
|
+
incomingRunId: "run-1",
|
|
53
|
+
votedOptionId: "option-a",
|
|
54
|
+
storedForIncoming: "option-b",
|
|
55
|
+
}),
|
|
56
|
+
).toBe("option-a");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("recovers a vote from storage on a reload mid-poll", () => {
|
|
60
|
+
expect(
|
|
61
|
+
votedOptionForIncoming({
|
|
62
|
+
currentRunId: "run-1",
|
|
63
|
+
incomingRunId: "run-1",
|
|
64
|
+
votedOptionId: null,
|
|
65
|
+
storedForIncoming: "option-a",
|
|
66
|
+
}),
|
|
67
|
+
).toBe("option-a");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("starts a NEW asking fresh, so the last answer does not carry over", () => {
|
|
71
|
+
expect(
|
|
72
|
+
votedOptionForIncoming({
|
|
73
|
+
currentRunId: "run-1",
|
|
74
|
+
incomingRunId: "run-2",
|
|
75
|
+
votedOptionId: "option-a",
|
|
76
|
+
storedForIncoming: null,
|
|
77
|
+
}),
|
|
78
|
+
).toBeNull();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("opens a new asking on its results when this browser already answered THAT asking", () => {
|
|
82
|
+
expect(
|
|
83
|
+
votedOptionForIncoming({
|
|
84
|
+
currentRunId: "run-1",
|
|
85
|
+
incomingRunId: "run-2",
|
|
86
|
+
votedOptionId: null,
|
|
87
|
+
storedForIncoming: "option-x",
|
|
88
|
+
}),
|
|
89
|
+
).toBe("option-x");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("lets a viewer answer the same question asked again", () => {
|
|
93
|
+
// The second bug, in one assertion. The question is identical and this
|
|
94
|
+
// browser answered it last time; the ASKING is new, so the panel must open
|
|
95
|
+
// on buttons rather than on "(your vote)".
|
|
96
|
+
expect(
|
|
97
|
+
votedOptionForIncoming({
|
|
98
|
+
currentRunId: "run-1",
|
|
99
|
+
incomingRunId: "run-2",
|
|
100
|
+
votedOptionId: "option-a",
|
|
101
|
+
storedForIncoming: null,
|
|
102
|
+
}),
|
|
103
|
+
).toBeNull();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("clears the vote when the poll ends", () => {
|
|
107
|
+
expect(
|
|
108
|
+
votedOptionForIncoming({
|
|
109
|
+
currentRunId: "run-1",
|
|
110
|
+
incomingRunId: null,
|
|
111
|
+
votedOptionId: "option-a",
|
|
112
|
+
storedForIncoming: null,
|
|
113
|
+
}),
|
|
114
|
+
).toBeNull();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("handles the first poll of the broadcast, with nothing current", () => {
|
|
118
|
+
expect(
|
|
119
|
+
votedOptionForIncoming({
|
|
120
|
+
currentRunId: null,
|
|
121
|
+
incomingRunId: "run-1",
|
|
122
|
+
votedOptionId: null,
|
|
123
|
+
storedForIncoming: null,
|
|
124
|
+
}),
|
|
125
|
+
).toBeNull();
|
|
126
|
+
});
|
|
127
|
+
});
|