|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { FC } from 'react'; |
| 21 | +import { Card, Table } from 'react-bootstrap'; |
| 22 | +import { useTranslation } from 'react-i18next'; |
| 23 | + |
| 24 | +import dayjs from 'dayjs'; |
| 25 | + |
| 26 | +import { useQueryActivityLogDaily } from '@/services'; |
| 27 | + |
| 28 | +// What happened on each of the last 14 days; every number opens the |
| 29 | +// activity log in a new tab, already filtered to that day and those actions |
| 30 | +const COLUMNS: { key: string; actions: string }[] = [ |
| 31 | + { key: 'questions', actions: 'question.create' }, |
| 32 | + { key: 'answers', actions: 'answer.create' }, |
| 33 | + { key: 'comments', actions: 'comment.create,comment.reply' }, |
| 34 | + { key: 'reviews', actions: 'review.queued' }, |
| 35 | + { key: 'badges', actions: 'badge.award' }, |
| 36 | + { key: 'views', actions: 'page.view' }, |
| 37 | +]; |
| 38 | + |
| 39 | +const DailyActivity: FC = () => { |
| 40 | + const { t } = useTranslation('translation', { |
| 41 | + keyPrefix: 'admin.dashboard.daily_activity', |
| 42 | + }); |
| 43 | + const { data } = useQueryActivityLogDaily(14); |
| 44 | + const today = dayjs().format('YYYY-MM-DD'); |
| 45 | + |
| 46 | + const logLink = (date: string, actions: string) => |
| 47 | + `/admin/activity-log?range=custom&from=${date}&to=${date}&action=${encodeURIComponent(actions)}`; |
| 48 | + |
| 49 | + return ( |
| 50 | + <Card className="mb-4"> |
| 51 | + <Card.Body> |
| 52 | + <h6 className="mb-3">{t('title')}</h6> |
| 53 | + <Table size="sm" responsive className="mb-0 align-middle"> |
| 54 | + <thead> |
| 55 | + <tr> |
| 56 | + <th>{t('day')}</th> |
| 57 | + {COLUMNS.map((c) => ( |
| 58 | + <th key={c.key} className="text-end"> |
| 59 | + {t(c.key)} |
| 60 | + </th> |
| 61 | + ))} |
| 62 | + </tr> |
| 63 | + </thead> |
| 64 | + <tbody> |
| 65 | + {(data || []).map((row) => ( |
| 66 | + <tr key={row.date}> |
| 67 | + <td className="text-nowrap"> |
| 68 | + <a |
| 69 | + href={logLink(row.date, '')} |
| 70 | + target="_blank" |
| 71 | + rel="noreferrer" |
| 72 | + className={ |
| 73 | + row.date === today ? 'fw-bold' : 'link-secondary' |
| 74 | + }> |
| 75 | + {row.date} |
| 76 | + {row.date === today ? ` (${t('today')})` : ''} |
| 77 | + </a> |
| 78 | + </td> |
| 79 | + {COLUMNS.map((c) => { |
| 80 | + const n = Number(row[c.key] || 0); |
| 81 | + return ( |
| 82 | + <td key={c.key} className="text-end"> |
| 83 | + {n > 0 ? ( |
| 84 | + <a |
| 85 | + href={logLink(row.date, c.actions)} |
| 86 | + target="_blank" |
| 87 | + rel="noreferrer" |
| 88 | + title={t('open_log')}> |
| 89 | + {n} |
| 90 | + </a> |
| 91 | + ) : ( |
| 92 | + <span className="text-secondary">0</span> |
| 93 | + )} |
| 94 | + </td> |
| 95 | + ); |
| 96 | + })} |
| 97 | + </tr> |
| 98 | + ))} |
| 99 | + </tbody> |
| 100 | + </Table> |
| 101 | + </Card.Body> |
| 102 | + </Card> |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +export default DailyActivity; |
0 commit comments