Skip to content

Commit

Permalink
fix links in broadcast chatp
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Aug 2, 2024
1 parent 13039ad commit 5b17e3e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 25 deletions.
5 changes: 5 additions & 0 deletions ui/analyse/src/study/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,8 @@ export interface WithChapterId {

export type WithWhoAndPos = WithWho & WithPosition;
export type WithWhoAndChap = WithWho & WithChapterId;

export interface ChapterSelect {
is: (idOrNumber: ChapterId | number) => boolean;
set: (idOrNumber: ChapterId | number, force?: boolean) => Promise<boolean>;
}
2 changes: 1 addition & 1 deletion ui/analyse/src/study/multiBoard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function view(ctrl: MultiBoardCtrl, study: StudyCtrl): MaybeVNode {
'div.now-playing',
{
hook: {
insert: gameLinksListener(study.setChapter),
insert: gameLinksListener(study.chapterSelect),
},
},
pager.currentPageResults.map(makePreview(basePath, study.vm.chapterId, cloudEval)),
Expand Down
6 changes: 3 additions & 3 deletions ui/analyse/src/study/relay/relayCtrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RelayData, LogEvent, RelaySync, RelayRound, RoundId } from './interfaces';
import { BothClocks, ChapterId, Federations, ServerClockMsg } from '../interfaces';
import { BothClocks, ChapterId, ChapterSelect, Federations, ServerClockMsg } from '../interfaces';
import { StudyMemberCtrl } from '../studyMembers';
import { AnalyseSocketSend } from '../../socket';
import { Prop, Toggle, defined, notNull, prop, toggle } from 'common';
Expand Down Expand Up @@ -36,7 +36,7 @@ export default class RelayCtrl {
private readonly chapters: StudyChapters,
private readonly multiCloudEval: MultiCloudEval,
private readonly federations: () => Federations | undefined,
setChapter: (id: ChapterId | number) => Promise<boolean>,
chapterSelect: ChapterSelect,
) {
this.tourShow = toggle((location.pathname.split('/broadcast/')[1].match(/\//g) || []).length < 5);
const locationTab = location.hash.replace(/^#/, '') as RelayTab;
Expand All @@ -47,7 +47,7 @@ export default class RelayCtrl {
: 'boards';
this.tab = prop<RelayTab>(initialTab);
this.teams = data.tour.teamTable
? new RelayTeams(id, this.multiCloudEval, setChapter, this.roundPath, redraw)
? new RelayTeams(id, this.multiCloudEval, chapterSelect, this.roundPath, redraw)
: undefined;
this.leaderboard = data.tour.leaderboard
? new RelayLeaderboard(data.tour.id, this.federations, redraw)
Expand Down
6 changes: 3 additions & 3 deletions ui/analyse/src/study/relay/relayTeams.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MaybeVNodes, Redraw, VNode, onInsert, looseH as h } from 'common/snabbdom';
import * as xhr from 'common/xhr';
import { RoundId } from './interfaces';
import { ChapterId, ChapterPreview, ChapterPreviewPlayer, StatusStr } from '../interfaces';
import { ChapterId, ChapterPreview, ChapterPreviewPlayer, ChapterSelect, StatusStr } from '../interfaces';
import { MultiCloudEval, renderScoreAtDepth } from '../multiCloudEval';
import { spinnerVdom as spinner } from 'common/spinner';
import { playerFed } from '../playerBars';
Expand Down Expand Up @@ -31,7 +31,7 @@ export default class RelayTeams {
constructor(
private readonly roundId: RoundId,
readonly multiCloudEval: MultiCloudEval,
readonly setChapter: (id: ChapterId | number) => Promise<boolean>,
readonly chapterSelect: ChapterSelect,
readonly roundPath: () => string,
private readonly redraw: Redraw,
) {}
Expand All @@ -53,7 +53,7 @@ export const teamsView = (ctrl: RelayTeams, chapters: StudyChapters) =>
class: { loading: ctrl.loading, nodata: !ctrl.teams },
hook: {
insert: vnode => {
gameLinksListener(ctrl.setChapter)(vnode);
gameLinksListener(ctrl.chapterSelect)(vnode);
ctrl.loadFromXhr(true);
},
},
Expand Down
2 changes: 1 addition & 1 deletion ui/analyse/src/study/relay/relayTourView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const tourSide = (ctx: RelayViewContext) => {
'aside.relay-tour__side',
{
hook: {
insert: gameLinksListener(study.setChapter),
insert: gameLinksListener(study.chapterSelect),
},
},
[
Expand Down
32 changes: 16 additions & 16 deletions ui/analyse/src/study/studyChapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Federations,
ChapterPreviewPlayerFromServer,
ChapterPreviewPlayer,
ChapterSelect,
} from './interfaces';
import StudyCtrl from './studyCtrl';
import { opposite } from 'chessops/util';
Expand Down Expand Up @@ -138,22 +139,21 @@ export function resultOf(tags: TagArray[], isWhite: boolean): string | undefined
export const gameLinkAttrs = (basePath: string, game: { id: ChapterId }) => ({
href: `${basePath}/${game.id}`,
});
export const gameLinksListener =
(setChapter: (id: ChapterId | number) => Promise<boolean>) => (vnode: VNode) =>
(vnode.elm as HTMLElement).addEventListener(
'click',
async e => {
let target = e.target as HTMLLinkElement;
while (target && target.tagName !== 'A') target = target.parentNode as HTMLLinkElement;
const href = target?.href;
const id = target?.dataset['board'] || href?.match(/^[^?#]*/)?.[0].slice(-8);
if (id) {
if (!href?.match(/[?&]embed=/)) e.preventDefault();
await setChapter(id);
}
},
{ passive: false },
);
export const gameLinksListener = (select: ChapterSelect) => (vnode: VNode) =>
(vnode.elm as HTMLElement).addEventListener(
'click',
async e => {
let target = e.target as HTMLLinkElement;
while (target && target.tagName !== 'A') target = target.parentNode as HTMLLinkElement;
const href = target?.href;
const id = target?.dataset['board'] || href?.match(/^[^?#]*/)?.[0].slice(-8);
if (id && select.is(id)) {
if (!href?.match(/[?&]embed=/)) e.preventDefault();
await select.set(id);
}
},
{ passive: false },
);

export function view(ctrl: StudyCtrl): VNode {
const canContribute = ctrl.members.canContribute(),
Expand Down
8 changes: 7 additions & 1 deletion ui/analyse/src/study/studyCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
StudyDataFromServer,
StudyData,
ChapterPreviewFromServer,
ChapterSelect,
} from './interfaces';
import GamebookPlayCtrl from './gamebook/gamebookPlayCtrl';
import { DescriptionCtrl } from './description';
Expand Down Expand Up @@ -169,7 +170,7 @@ export default class StudyCtrl {
this.chapters.list,
this.multiCloudEval,
() => this.data.federations,
id => this.setChapter(id),
this.chapterSelect,
);
this.multiBoard = new MultiBoardCtrl(
this.chapters.list,
Expand Down Expand Up @@ -481,6 +482,11 @@ export default class StudyCtrl {
return true;
};

chapterSelect: ChapterSelect = {
is: (idOrNumber: ChapterId | number) => defined(this.chapters.list.get(idOrNumber)),
set: this.setChapter,
};

private deltaChapter = (delta: number): ChapterPreview | undefined => {
const chs = this.chapters.list.all();
const i = chs.findIndex(ch => ch.id === this.vm.chapterId);
Expand Down

0 comments on commit 5b17e3e

Please sign in to comment.