mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-23 04:46:08 -08:00
feat(stats): overhaul the Trends tab (#325)
This commit is contained in:
1 parent
f89f78b861
commit
269c0708cb
73 files changed
+6085
-3354
No files matched your search
@@ -89,6 +89,67 @@ test('deleteSession throws when the stats API delete request fails', async () =>
|
||||
);
|
||||
});
|
||||
|
||||
test('getTrendsDashboard encodes a custom range and title filter', async () => {
|
||||
await withFetchStub(
|
||||
() => jsonResponse({}),
|
||||
async (requests) => {
|
||||
await apiClient.getTrendsDashboard(
|
||||
{ startDay: 20_000, endDay: 20_010 },
|
||||
'week',
|
||||
true,
|
||||
'A & B',
|
||||
);
|
||||
assert.equal(
|
||||
requests[0]?.url,
|
||||
`${BASE_URL}/api/stats/trends/dashboard?range=custom&fromDay=20000&toDay=20010&groupBy=week&fillEmpty=true&title=A+%26+B`,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('getDictionaryWords sends only the filters in use', async () => {
|
||||
await withFetchStub(
|
||||
() => jsonResponse({ rows: [], total: 0, maxSeen: 0 }),
|
||||
async (requests) => {
|
||||
await apiClient.getDictionaryWords({
|
||||
search: '猫',
|
||||
sort: 'rank',
|
||||
descending: false,
|
||||
hideKnown: true,
|
||||
hideNames: false,
|
||||
hideKana: false,
|
||||
maxRank: 10_000,
|
||||
limit: 25,
|
||||
offset: 50,
|
||||
});
|
||||
const url = new URL(requests[0]!.url, 'http://stats.local');
|
||||
assert.equal(url.pathname, '/api/stats/vocabulary/words');
|
||||
assert.deepEqual(Object.fromEntries(url.searchParams), {
|
||||
sort: 'rank',
|
||||
order: 'asc',
|
||||
limit: '25',
|
||||
offset: '50',
|
||||
search: '猫',
|
||||
hideKnown: 'true',
|
||||
maxRank: '10000',
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('getSessions narrows to a span of days when given one', async () => {
|
||||
await withFetchStub(
|
||||
() => jsonResponse([]),
|
||||
async (requests) => {
|
||||
await apiClient.getSessions(500, { startDay: 20_000, endDay: 20_006 });
|
||||
assert.equal(
|
||||
requests[0]?.url,
|
||||
`${BASE_URL}/api/stats/sessions?limit=500&fromDay=20000&toDay=20006`,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('getTrendsDashboard accepts 365d range and builds correct URL', async () => {
|
||||
await withFetchStub(
|
||||
() =>
|
||||
|
||||
@@ -16,6 +16,8 @@ import type {
|
||||
StatsMoveVideoRequest,
|
||||
StatsMoveVideoResponse,
|
||||
StatsTmdbAssignment,
|
||||
StatsDayRange,
|
||||
StatsDictionaryWordQuery,
|
||||
StatsTrendGroupBy,
|
||||
StatsTrendRange,
|
||||
StatsVideoWatchedRequest,
|
||||
@@ -67,7 +69,13 @@ export const apiClient = {
|
||||
fetchJson('dailyRollups', `/api/stats/daily-rollups?limit=${limit}`),
|
||||
getMonthlyRollups: (limit = 24) =>
|
||||
fetchJson('monthlyRollups', `/api/stats/monthly-rollups?limit=${limit}`),
|
||||
getSessions: (limit = 50) => fetchJson('sessions', `/api/stats/sessions?limit=${limit}`),
|
||||
getSessions: (limit = 50, dayRange?: StatsDayRange) =>
|
||||
fetchJson(
|
||||
'sessions',
|
||||
dayRange
|
||||
? `/api/stats/sessions?limit=${limit}&fromDay=${dayRange.startDay}&toDay=${dayRange.endDay}`
|
||||
: `/api/stats/sessions?limit=${limit}`,
|
||||
),
|
||||
getSessionTimeline: (id: number, limit?: number) =>
|
||||
fetchJson(
|
||||
'sessionTimeline',
|
||||
@@ -87,6 +95,23 @@ export const apiClient = {
|
||||
getVocabulary: (limit = 100) => fetchJson('vocabulary', `/api/stats/vocabulary?limit=${limit}`),
|
||||
getVocabularySummary: () => fetchJson('vocabularySummary', '/api/stats/vocabulary/summary'),
|
||||
getVocabularyCharts: () => fetchJson('vocabularyCharts', '/api/stats/vocabulary/charts'),
|
||||
getDictionaryWords: (query: StatsDictionaryWordQuery) => {
|
||||
const params = new URLSearchParams({
|
||||
sort: query.sort,
|
||||
order: query.descending ? 'desc' : 'asc',
|
||||
limit: String(query.limit),
|
||||
offset: String(query.offset),
|
||||
});
|
||||
if (query.search) params.set('search', query.search);
|
||||
if (query.hideKnown) params.set('hideKnown', 'true');
|
||||
if (query.hideNames) params.set('hideNames', 'true');
|
||||
if (query.hideKana) params.set('hideKana', 'true');
|
||||
if (query.partOfSpeech) params.set('pos', query.partOfSpeech);
|
||||
if (query.maxRank !== undefined) params.set('maxRank', String(query.maxRank));
|
||||
if (query.minTitles !== undefined) params.set('minTitles', String(query.minTitles));
|
||||
return fetchJson('dictionaryWords', `/api/stats/vocabulary/words?${params.toString()}`);
|
||||
},
|
||||
getVocabularyCoverage: () => fetchJson('vocabularyCoverage', '/api/stats/vocabulary/coverage'),
|
||||
getExcludedWords: () => fetchJson('excludedWords', '/api/stats/excluded-words'),
|
||||
setExcludedWords: async (words: StatsExcludedWord[]): Promise<void> => {
|
||||
await fetchResponse('/api/stats/excluded-words', {
|
||||
@@ -108,10 +133,17 @@ export const apiClient = {
|
||||
});
|
||||
return res.json() as Promise<StatsDuplicateLineCleanupResult>;
|
||||
},
|
||||
getWordOccurrences: (headword: string, word: string, reading: string, limit = 50, offset = 0) =>
|
||||
getWordOccurrences: (
|
||||
headword: string,
|
||||
word: string,
|
||||
reading: string,
|
||||
limit = 50,
|
||||
offset = 0,
|
||||
allForms = false,
|
||||
) =>
|
||||
fetchJson(
|
||||
'wordOccurrences',
|
||||
`/api/stats/vocabulary/occurrences?headword=${encodeURIComponent(headword)}&word=${encodeURIComponent(word)}&reading=${encodeURIComponent(reading)}&limit=${limit}&offset=${offset}`,
|
||||
`/api/stats/vocabulary/occurrences?headword=${encodeURIComponent(headword)}&word=${encodeURIComponent(word)}&reading=${encodeURIComponent(reading)}&limit=${limit}&offset=${offset}${allForms ? '&allForms=true' : ''}`,
|
||||
),
|
||||
searchSentences: (query: string, limit = 50, searchByHeadword = true) =>
|
||||
fetchJson(
|
||||
@@ -159,11 +191,26 @@ export const apiClient = {
|
||||
fetchJson('newAnimePerDay', `/api/stats/trends/new-anime-per-day?limit=${limit}`),
|
||||
getWatchTimePerAnime: (limit = 90) =>
|
||||
fetchJson('watchTimePerAnime', `/api/stats/trends/watch-time-per-anime?limit=${limit}`),
|
||||
getTrendsDashboard: (range: StatsTrendRange, groupBy: StatsTrendGroupBy, fillEmpty = true) =>
|
||||
fetchJson(
|
||||
'trendsDashboard',
|
||||
`/api/stats/trends/dashboard?range=${encodeURIComponent(range)}&groupBy=${encodeURIComponent(groupBy)}&fillEmpty=${fillEmpty ? 'true' : 'false'}`,
|
||||
),
|
||||
getTrendsDashboard: (
|
||||
range: StatsTrendRange,
|
||||
groupBy: StatsTrendGroupBy,
|
||||
fillEmpty = true,
|
||||
title: string | null = null,
|
||||
) => {
|
||||
const params = new URLSearchParams(
|
||||
typeof range === 'string'
|
||||
? { range }
|
||||
: {
|
||||
range: 'custom',
|
||||
fromDay: String(range.startDay),
|
||||
toDay: String(range.endDay),
|
||||
},
|
||||
);
|
||||
params.set('groupBy', groupBy);
|
||||
params.set('fillEmpty', fillEmpty ? 'true' : 'false');
|
||||
if (title !== null) params.set('title', title);
|
||||
return fetchJson('trendsDashboard', `/api/stats/trends/dashboard?${params.toString()}`);
|
||||
},
|
||||
getWordDetail: (wordId: number) =>
|
||||
fetchJson('wordDetail', `/api/stats/vocabulary/${wordId}/detail`),
|
||||
getKanjiDetail: (kanjiId: number) =>
|
||||
|
||||
@@ -5,6 +5,8 @@ import {
|
||||
epochDayToDate,
|
||||
epochMsFromDbTimestamp,
|
||||
formatClockDuration,
|
||||
isoDateToEpochDay,
|
||||
localDayFromMs,
|
||||
formatRelativeDate,
|
||||
formatSessionDayLabel,
|
||||
} from './formatters';
|
||||
@@ -160,3 +162,20 @@ test('formatClockDuration keeps seconds for sub-hour spans', () => {
|
||||
assert.equal(formatClockDuration(372_000), '6m 12s');
|
||||
assert.equal(formatClockDuration(3_780_000), '1h 3m');
|
||||
});
|
||||
|
||||
test('isoDateToEpochDay reads date input values and rejects dates before 1970', () => {
|
||||
assert.equal(isoDateToEpochDay('1970-01-02'), 1);
|
||||
assert.equal(isoDateToEpochDay('2026-03-01'), 20_513);
|
||||
assert.equal(isoDateToEpochDay('1969-12-31'), null);
|
||||
// Date.UTC would read this year as 1999.
|
||||
assert.equal(isoDateToEpochDay('0099-01-01'), null);
|
||||
assert.equal(isoDateToEpochDay(''), null);
|
||||
});
|
||||
|
||||
test('localDayFromMs numbers the local calendar day in every time zone', () => {
|
||||
const justAfterMidnight = new Date(2026, 0, 2, 0, 30).getTime();
|
||||
const lateEvening = new Date(2026, 0, 2, 23, 30).getTime();
|
||||
const january2 = Date.UTC(2026, 0, 2) / 86_400_000;
|
||||
assert.equal(localDayFromMs(justAfterMidnight), january2);
|
||||
assert.equal(localDayFromMs(lateEvening), january2);
|
||||
});
|
||||
@@ -10,6 +10,21 @@ export function formatNumber(n: number): string {
|
||||
return n.toLocaleString();
|
||||
}
|
||||
|
||||
const COMPACT_NUMBER = new Intl.NumberFormat(undefined, {
|
||||
notation: 'compact',
|
||||
maximumFractionDigits: 1,
|
||||
});
|
||||
|
||||
/** Short form for tight spaces like chart axes: 24000 becomes 24K. */
|
||||
export function formatCompactNumber(n: number): string {
|
||||
return COMPACT_NUMBER.format(n);
|
||||
}
|
||||
|
||||
/** Watch time for headline numbers: minutes below an hour, then hours to one decimal. */
|
||||
export function formatHours(minutes: number): string {
|
||||
return minutes < 60 ? `${Math.round(minutes)}m` : `${(minutes / 60).toFixed(1)}h`;
|
||||
}
|
||||
|
||||
export function formatPercent(ratio: number | null): string {
|
||||
if (ratio == null) return '\u2014';
|
||||
return `${Math.round(ratio * 100)}%`;
|
||||
@@ -42,10 +57,25 @@ export function epochDayToDate(epochDay: number): Date {
|
||||
return new Date(utcDate.getUTCFullYear(), utcDate.getUTCMonth(), utcDate.getUTCDate());
|
||||
}
|
||||
|
||||
/** `YYYY-MM-DD` for a date input. */
|
||||
export function epochDayToIsoDate(epochDay: number): string {
|
||||
return new Date(epochDay * 86_400_000).toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
/** Epoch day for a date input value, or null when the input is empty, invalid, or before 1970. */
|
||||
export function isoDateToEpochDay(value: string): number | null {
|
||||
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
|
||||
if (!match) return null;
|
||||
const year = Number(match[1]);
|
||||
if (year < 1970) return null;
|
||||
const ms = Date.UTC(year, Number(match[2]) - 1, Number(match[3]));
|
||||
return Number.isNaN(ms) ? null : ms / 86_400_000;
|
||||
}
|
||||
|
||||
/** The local calendar day as an epoch day, the same numbering the stats server uses. */
|
||||
export function localDayFromMs(ms: number): number {
|
||||
const d = new Date(ms);
|
||||
const localMidnight = new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
||||
return Math.floor(localMidnight / 86_400_000);
|
||||
return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) / 86_400_000;
|
||||
}
|
||||
|
||||
export function todayLocalDay(): number {
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
const KANA_ONLY_TEXT = /^[\p{Script=Hiragana}\p{Script=Katakana}\u30fc\u309d\u309e\u30fd\u30fe]+$/u;
|
||||
|
||||
export function isKanaOnlyTokenText(text: string): boolean {
|
||||
const trimmed = text.trim();
|
||||
return trimmed.length > 0 && KANA_ONLY_TEXT.test(trimmed);
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import {
|
||||
buildStatsMineCardParams,
|
||||
describeStreamedLines,
|
||||
getStatsMineCardError,
|
||||
getStatsMineCardUnavailableReason,
|
||||
} from './mining';
|
||||
@@ -67,6 +68,24 @@ test('buildStatsMineCardParams returns null when stored timing has no positive d
|
||||
);
|
||||
});
|
||||
|
||||
test('getStatsMineCardUnavailableReason says a line without a file came from a stream', () => {
|
||||
assert.match(getStatsMineCardUnavailableReason(makeResult({ sourcePath: null })) ?? '', /stream/);
|
||||
assert.equal(getStatsMineCardUnavailableReason(makeResult()), null);
|
||||
});
|
||||
|
||||
for (const c of [
|
||||
{ name: 'no streamed lines', lines: 10, streamed: 0, expected: null },
|
||||
{ name: 'some streamed lines', lines: 2_863, streamed: 935, match: /935 of 2,863 lines/ },
|
||||
{ name: 'only streamed lines', lines: 4, streamed: 4, match: /All 4 lines.*can't be mined yet/ },
|
||||
{ name: 'a single streamed line', lines: 1, streamed: 1, match: /only line came from a stream/ },
|
||||
]) {
|
||||
test(`describeStreamedLines handles ${c.name}`, () => {
|
||||
const text = describeStreamedLines(c.lines, c.streamed);
|
||||
if (c.match) assert.match(text ?? '', c.match);
|
||||
else assert.equal(text, null);
|
||||
});
|
||||
}
|
||||
|
||||
test('getStatsMineCardError surfaces partial media failures', () => {
|
||||
assert.equal(
|
||||
getStatsMineCardError({ noteId: 1, errors: ['audio: ffmpeg failed'] }),
|
||||
|
||||
+17
-1
@@ -7,11 +7,27 @@ import type {
|
||||
|
||||
export type { StatsMineCardParams, StatsMineCardResponse, StatsMineMode } from '../types/stats';
|
||||
|
||||
// Cards cut their audio and image from the video file. Only streamed videos have no
|
||||
// local path, so a missing path means the line came from a stream.
|
||||
const STREAMED_LINE_REASON =
|
||||
"Can't mine: from a stream, so there's no video file to cut clips from.";
|
||||
|
||||
/** Explains how a word's lines affect mining, or null when every line can be mined. */
|
||||
export function describeStreamedLines(lineCount: number, streamLineCount: number): string | null {
|
||||
if (streamLineCount === 0) return null;
|
||||
if (streamLineCount >= lineCount) {
|
||||
return lineCount === 1
|
||||
? "This word's only line came from a stream, so it can't be mined yet."
|
||||
: `All ${lineCount.toLocaleString()} lines with this word came from streams, so it can't be mined yet.`;
|
||||
}
|
||||
return `${streamLineCount.toLocaleString()} of ${lineCount.toLocaleString()} lines with this word came from streams and can't be mined.`;
|
||||
}
|
||||
|
||||
export function getStatsMineCardUnavailableReason(
|
||||
result: Pick<SentenceSearchResult, 'sourcePath' | 'segmentStartMs' | 'segmentEndMs'>,
|
||||
): string | null {
|
||||
if (!result.sourcePath) {
|
||||
return 'This source has no local file path.';
|
||||
return STREAMED_LINE_REASON;
|
||||
}
|
||||
if (result.segmentStartMs == null || result.segmentEndMs == null) {
|
||||
return 'This line is missing segment timing.';
|
||||
|
||||
@@ -24,3 +24,19 @@ export function writeBooleanPreference(key: string, value: boolean): void {
|
||||
// Storage can be blocked in private/restricted contexts; keep the in-memory choice.
|
||||
}
|
||||
}
|
||||
|
||||
export function readStringPreference(key: string): string | null {
|
||||
try {
|
||||
return getPreferenceStorage()?.getItem(key) ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function writeStringPreference(key: string, value: string): void {
|
||||
try {
|
||||
getPreferenceStorage()?.setItem(key, value);
|
||||
} catch {
|
||||
// Storage can be blocked in private/restricted contexts; keep the in-memory choice.
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ test('getSentenceSearchMineAvailability disables every mining mode without sourc
|
||||
canMineSentence: false,
|
||||
canMineWordAudio: false,
|
||||
exactMatch: true,
|
||||
unavailableReason: 'This source has no local file path.',
|
||||
unavailableReason: "Can't mine: from a stream, so there's no video file to cut clips from.",
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -5,8 +5,11 @@ import {
|
||||
createInitialStatsView,
|
||||
getSessionNavigationTarget,
|
||||
navigateToAnime,
|
||||
navigateToSessionsDays,
|
||||
openAnimeEpisodeDetail,
|
||||
openOverviewMediaDetail,
|
||||
openSessionsMediaDetail,
|
||||
openTrendsMediaDetail,
|
||||
switchTab,
|
||||
type StatsViewState,
|
||||
} from './stats-navigation';
|
||||
@@ -18,6 +21,7 @@ test('openAnimeEpisodeDetail opens dedicated media detail from anime context', (
|
||||
activeTab: 'anime',
|
||||
selectedAnimeId: 42,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: {
|
||||
videoId: 7,
|
||||
initialSessionId: null,
|
||||
@@ -36,6 +40,7 @@ test('closeMediaDetail returns to originating anime detail state', () => {
|
||||
activeTab: 'anime',
|
||||
selectedAnimeId: 42,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: null,
|
||||
} satisfies StatsViewState);
|
||||
});
|
||||
@@ -45,6 +50,7 @@ test('openOverviewMediaDetail opens dedicated media detail from overview context
|
||||
activeTab: 'overview',
|
||||
selectedAnimeId: null,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: {
|
||||
videoId: 9,
|
||||
initialSessionId: null,
|
||||
@@ -68,6 +74,7 @@ test('switchTab clears dedicated media detail state', () => {
|
||||
activeTab: 'sessions',
|
||||
selectedAnimeId: null,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: null,
|
||||
} satisfies StatsViewState);
|
||||
});
|
||||
@@ -92,6 +99,7 @@ test('openOverviewMediaDetail can carry a target session id for auto-expansion',
|
||||
activeTab: 'overview',
|
||||
selectedAnimeId: null,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: {
|
||||
videoId: 9,
|
||||
initialSessionId: 33,
|
||||
@@ -101,3 +109,25 @@ test('openOverviewMediaDetail can carry a target session id for auto-expansion',
|
||||
},
|
||||
} satisfies StatsViewState);
|
||||
});
|
||||
|
||||
const OCT_5 = { startDay: 20_366, endDay: 20_366, label: 'Oct 5' };
|
||||
|
||||
test('navigateToSessionsDays opens Sessions narrowed to those days', () => {
|
||||
const state = navigateToSessionsDays(createInitialStatsView(), OCT_5);
|
||||
assert.equal(state.activeTab, 'sessions');
|
||||
assert.deepEqual(state.sessionsDayFilter, OCT_5);
|
||||
});
|
||||
|
||||
test('the sessions day filter survives a media detail round trip but not a tab switch', () => {
|
||||
const filtered = navigateToSessionsDays(createInitialStatsView(), OCT_5);
|
||||
const back = closeMediaDetail(openSessionsMediaDetail(filtered, 3));
|
||||
assert.equal(back.activeTab, 'sessions');
|
||||
assert.deepEqual(back.sessionsDayFilter, OCT_5);
|
||||
assert.equal(switchTab(filtered, 'sessions').sessionsDayFilter, null);
|
||||
});
|
||||
|
||||
test('media detail opened from Trends returns to Trends', () => {
|
||||
const state = closeMediaDetail(openTrendsMediaDetail(createInitialStatsView(), 3));
|
||||
assert.equal(state.activeTab, 'trends');
|
||||
assert.equal(state.mediaDetail, null);
|
||||
});
|
||||
@@ -1,10 +1,11 @@
|
||||
import type { SessionSummary } from '../types/stats';
|
||||
import type { SessionSummary, StatsDayRange } from '../types/stats';
|
||||
import type { TabId } from '../components/layout/TabBar';
|
||||
|
||||
export type MediaDetailOrigin =
|
||||
| { type: 'anime'; animeId: number }
|
||||
| { type: 'overview' }
|
||||
| { type: 'sessions' };
|
||||
| { type: 'sessions' }
|
||||
| { type: 'trends' };
|
||||
|
||||
export interface MediaDetailState {
|
||||
videoId: number;
|
||||
@@ -12,10 +13,16 @@ export interface MediaDetailState {
|
||||
origin: MediaDetailOrigin;
|
||||
}
|
||||
|
||||
/** Narrows the Sessions tab to a span of days, set when arriving from a Trends chart. */
|
||||
export interface SessionsDayFilter extends StatsDayRange {
|
||||
label: string;
|
||||
}
|
||||
|
||||
export interface StatsViewState {
|
||||
activeTab: TabId;
|
||||
selectedAnimeId: number | null;
|
||||
focusedSessionId: number | null;
|
||||
sessionsDayFilter: SessionsDayFilter | null;
|
||||
mediaDetail: MediaDetailState | null;
|
||||
}
|
||||
|
||||
@@ -24,6 +31,7 @@ export function createInitialStatsView(): StatsViewState {
|
||||
activeTab: 'overview',
|
||||
selectedAnimeId: null,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: null,
|
||||
};
|
||||
}
|
||||
@@ -33,6 +41,7 @@ export function switchTab(state: StatsViewState, tabId: TabId): StatsViewState {
|
||||
activeTab: tabId,
|
||||
selectedAnimeId: null,
|
||||
focusedSessionId: tabId === 'sessions' ? state.focusedSessionId : null,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: null,
|
||||
};
|
||||
}
|
||||
@@ -51,6 +60,20 @@ export function navigateToSession(state: StatsViewState, sessionId: number): Sta
|
||||
...state,
|
||||
activeTab: 'sessions',
|
||||
focusedSessionId: sessionId,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: null,
|
||||
};
|
||||
}
|
||||
|
||||
export function navigateToSessionsDays(
|
||||
state: StatsViewState,
|
||||
sessionsDayFilter: SessionsDayFilter,
|
||||
): StatsViewState {
|
||||
return {
|
||||
...state,
|
||||
activeTab: 'sessions',
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter,
|
||||
mediaDetail: null,
|
||||
};
|
||||
}
|
||||
@@ -65,6 +88,7 @@ export function openAnimeEpisodeDetail(
|
||||
activeTab: 'anime',
|
||||
selectedAnimeId: animeId,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: {
|
||||
videoId,
|
||||
initialSessionId: sessionId,
|
||||
@@ -85,6 +109,7 @@ export function openOverviewMediaDetail(
|
||||
activeTab: 'overview',
|
||||
selectedAnimeId: null,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: {
|
||||
videoId,
|
||||
initialSessionId: sessionId,
|
||||
@@ -100,6 +125,7 @@ export function openSessionsMediaDetail(state: StatsViewState, videoId: number):
|
||||
activeTab: 'sessions',
|
||||
selectedAnimeId: null,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: state.sessionsDayFilter,
|
||||
mediaDetail: {
|
||||
videoId,
|
||||
initialSessionId: null,
|
||||
@@ -110,33 +136,43 @@ export function openSessionsMediaDetail(state: StatsViewState, videoId: number):
|
||||
};
|
||||
}
|
||||
|
||||
export function openTrendsMediaDetail(_state: StatsViewState, videoId: number): StatsViewState {
|
||||
return {
|
||||
activeTab: 'trends',
|
||||
selectedAnimeId: null,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: {
|
||||
videoId,
|
||||
initialSessionId: null,
|
||||
origin: {
|
||||
type: 'trends',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function closeMediaDetail(state: StatsViewState): StatsViewState {
|
||||
if (!state.mediaDetail) {
|
||||
return state;
|
||||
}
|
||||
|
||||
if (state.mediaDetail.origin.type === 'overview') {
|
||||
const { origin } = state.mediaDetail;
|
||||
if (origin.type === 'anime') {
|
||||
return {
|
||||
activeTab: 'overview',
|
||||
selectedAnimeId: null,
|
||||
focusedSessionId: null,
|
||||
mediaDetail: null,
|
||||
};
|
||||
}
|
||||
|
||||
if (state.mediaDetail.origin.type === 'sessions') {
|
||||
return {
|
||||
activeTab: 'sessions',
|
||||
selectedAnimeId: null,
|
||||
activeTab: 'anime',
|
||||
selectedAnimeId: origin.animeId,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: null,
|
||||
mediaDetail: null,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
activeTab: 'anime',
|
||||
selectedAnimeId: state.mediaDetail.origin.animeId,
|
||||
activeTab: origin.type,
|
||||
selectedAnimeId: null,
|
||||
focusedSessionId: null,
|
||||
sessionsDayFilter: origin.type === 'sessions' ? state.sessionsDayFilter : null,
|
||||
mediaDetail: null,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user