Files
5chan/src/components/subplebbit-stats/subplebbit-stats.tsx
T

109 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-03-20 15:12:19 +01:00
import { useState } from 'react';
2024-04-08 17:32:12 +02:00
import { useLocation, useParams } from 'react-router-dom';
2024-04-25 22:02:55 +02:00
import { useAccountComment, useComment, useSubplebbit, useSubplebbitStats } from '@plebbit/plebbit-react-hooks';
import styles from './subplebbit-stats.module.css';
import { Trans, useTranslation } from 'react-i18next';
2024-04-08 17:32:12 +02:00
import { isDescriptionView, isRulesView } from '../../lib/utils/view-utils';
2024-03-20 15:12:19 +01:00
const SubplebbitStats = () => {
const { t } = useTranslation();
2024-04-25 22:02:55 +02:00
const params = useParams();
const accountComment = useAccountComment({ commentIndex: params?.accountCommentIndex as any });
const subplebbitAddress = params?.subplebbitAddress || accountComment?.subplebbitAddress;
const subplebbit = useSubplebbit({ subplebbitAddress });
const { address, createdAt } = subplebbit || {};
const stats = useSubplebbitStats({ subplebbitAddress: address });
const [showStats, setShowStats] = useState(true);
2024-04-08 17:32:12 +02:00
const location = useLocation();
const isInDescriptionView = isDescriptionView(location.pathname, params);
const isInRulesView = isRulesView(location.pathname, params);
2024-04-25 22:02:55 +02:00
const comment = useComment({ commentCid: params?.commentCid });
const { deleted, locked, removed } = comment || {};
const hideStats = !stats.allPostCount || deleted || locked || removed || isInDescriptionView || isInRulesView;
2024-03-20 15:12:19 +01:00
const unixToMMDDYYYY = (timestamp: number) => {
const date = new Date(timestamp * 1000);
const month = ('0' + (date.getMonth() + 1)).slice(-2);
const day = ('0' + date.getDate()).slice(-2);
const year = date.getFullYear().toString().slice(-2);
return month + '/' + day + '/' + year;
};
return (
<div className={`${styles.content} ${hideStats ? styles.hide : styles.show}`}>
2024-03-20 15:12:19 +01:00
<table className={styles.blotter}>
2024-03-26 21:12:29 +01:00
<thead>
<tr>
<td>
<hr />
</td>
</tr>
</thead>
2024-03-20 15:12:19 +01:00
{showStats && (
<tbody>
2024-03-20 15:12:19 +01:00
<tr>
<td>
<Trans
i18nKey='board_stats_hour'
values={{ userCount: stats.hourActiveUserCount, postCount: stats.hourPostCount }}
components={{ 1: <span className={styles.statValue} /> }}
/>
{' / '}
<Trans
i18nKey='board_stats_day'
values={{ userCount: stats.dayActiveUserCount, postCount: stats.dayPostCount }}
components={{ 1: <span className={styles.statValue} /> }}
/>
2024-03-20 15:12:19 +01:00
</td>
</tr>
<tr>
<td>
<Trans
i18nKey='board_stats_week'
values={{ userCount: stats.weekActiveUserCount, postCount: stats.weekPostCount }}
components={{ 1: <span className={styles.statValue} /> }}
/>
{' / '}
<Trans
i18nKey='board_stats_month'
values={{ userCount: stats.monthActiveUserCount, postCount: stats.monthPostCount }}
components={{ 1: <span className={styles.statValue} /> }}
/>
2024-03-20 15:12:19 +01:00
</td>
</tr>
<tr>
<td className={styles.lowercase}>
2024-03-22 15:31:02 +01:00
{createdAt && unixToMMDDYYYY(createdAt)} {t('board_created')}
{' / '}
<Trans
i18nKey='board_stats_all'
values={{ userCount: stats.allActiveUserCount, postCount: stats.allPostCount }}
components={{ 1: <span className={styles.statValue} /> }}
/>
2024-03-20 15:12:19 +01:00
</td>
</tr>
</tbody>
)}
<tfoot>
<tr>
<td colSpan={2}>
[
<span className={styles.hideButton} onClick={() => setShowStats(!showStats)}>
{showStats ? t('hide') : t('show_stats')}
2024-03-20 15:12:19 +01:00
</span>
]
</td>
</tr>
</tfoot>
</table>
</div>
);
};
export default SubplebbitStats;