TelegramPWA/src/global/reducers/statistics.ts
Alexander Zinchuk 9daa5f1a19 Statistics: Monetization Stats for Channels (#4843)
Co-authored-by: zubiden <19638254+zubiden@users.noreply.github.com>
2024-08-29 15:52:26 +02:00

81 lines
2.2 KiB
TypeScript

import type {
ApiChannelMonetizationStatistics,
ApiChannelStatistics, ApiGroupStatistics, ApiPostStatistics, StatisticsGraph,
} from '../../api/types';
import type { GlobalState, TabArgs } from '../types';
import { getCurrentTabId } from '../../util/establishMultitabRole';
import { selectTabState } from '../selectors';
import { updateTabState } from './tabs';
export function updateStatistics<T extends GlobalState>(
global: T, chatId: string, statistics: ApiChannelStatistics | ApiGroupStatistics,
...[tabId = getCurrentTabId()]: TabArgs<T>
): T {
return updateTabState(global, {
statistics: {
byChatId: {
...selectTabState(global, tabId).statistics.byChatId,
[chatId]: statistics,
},
},
}, tabId);
}
export function updateMessageStatistics<T extends GlobalState>(
global: T, statistics: ApiPostStatistics,
...[tabId = getCurrentTabId()]: TabArgs<T>
): T {
return updateTabState(global, {
statistics: {
...selectTabState(global, tabId).statistics,
currentMessage: statistics,
currentStory: undefined,
},
}, tabId);
}
export function updateStoryStatistics<T extends GlobalState>(
global: T, statistics: ApiPostStatistics,
...[tabId = getCurrentTabId()]: TabArgs<T>
): T {
return updateTabState(global, {
statistics: {
...selectTabState(global, tabId).statistics,
currentStory: statistics,
currentMessage: undefined,
},
}, tabId);
}
export function updateStatisticsGraph<T extends GlobalState>(
global: T, chatId: string, name: string, update: StatisticsGraph,
...[tabId = getCurrentTabId()]: TabArgs<T>
): T {
const { statistics } = selectTabState(global, tabId);
return updateTabState(global, {
statistics: {
...statistics,
byChatId: {
...statistics.byChatId,
[chatId]: {
...(statistics.byChatId[chatId] || {}),
[name]: update,
},
},
},
}, tabId);
}
export function updateChannelMonetizationStatistics<T extends GlobalState>(
global: T, statistics: ApiChannelMonetizationStatistics,
...[tabId = getCurrentTabId()]: TabArgs<T>
): T {
return updateTabState(global, {
statistics: {
...selectTabState(global, tabId).statistics,
monetization: statistics,
},
}, tabId);
}