]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/protocol/Item.js
2ea8ea77ea774e7588651ca97fe2378d9ed713b1
[alttp.git] / resources / js / components / protocol / Item.js
1 import moment from 'moment';
2 import PropTypes from 'prop-types';
3 import React from 'react';
4 import { ListGroup } from 'react-bootstrap';
5 import { Trans, withTranslation } from 'react-i18next';
6
7 import Icon from '../common/Icon';
8 import Spoiler from '../common/Spoiler';
9 import { formatTime } from '../../helpers/Result';
10 import { getUserName } from '../../helpers/User';
11 import i18n from '../../i18n';
12
13 const getEntryDate = entry => {
14         const dateStr = moment(entry.created_at).fromNow();
15         return entry.user
16                 ? `${entry.user.username} ${dateStr}`
17                 : dateStr;
18 };
19
20 const getEntryDetailsUsername = entry => {
21         if (!entry || !entry.details || !entry.details.user) return 'Anonymous';
22         return getUserName(entry.details.user);
23 };
24
25 const getEntryRoundNumber = entry =>
26         (entry && entry.details && entry.details.round && entry.details.round.number) || '?';
27
28 const getEntryResultComment = entry => {
29         if (!entry || !entry.details || !entry.details.result || !entry.details.result.comment) {
30                 return '';
31         }
32         return entry.details.result.comment;
33 };
34
35 const getEntryResultTime = entry => {
36         if (!entry || !entry.details || !entry.details.result) return 'ERROR';
37         const result = entry.details.result;
38         if (result.forfeit) return 'DNF XX';
39         return formatTime(result);
40 };
41
42 const getEntryDescription = entry => {
43         switch (entry.type) {
44                 case 'application.accepted':
45                 case 'application.received':
46                 case 'application.rejected':
47                         return i18n.t(
48                                 `protocol.description.${entry.type}`,
49                                 {
50                                         ...entry,
51                                         username: getEntryDetailsUsername(entry),
52                                 },
53                         );
54                 case 'result.comment': {
55                         const comment = getEntryResultComment(entry);
56                         return <Trans i18nKey={`protocol.description.${entry.type}`}>
57                                 <Spoiler>{{comment}}</Spoiler>,
58                         </Trans>;
59                 }
60                 case 'result.report': {
61                         const time = getEntryResultTime(entry);
62                         return <Trans i18nKey={`protocol.description.${entry.type}`}>
63                                 <Spoiler>{{time}}</Spoiler>,
64                         </Trans>;
65                 }
66                 case 'round.create':
67                 case 'round.lock':
68                 case 'round.seed':
69                 case 'round.unlock':
70                         return i18n.t(
71                                 `protocol.description.${entry.type}`,
72                                 {
73                                         ...entry,
74                                         number: getEntryRoundNumber(entry),
75                                 },
76                         );
77                 case 'tournament.close':
78                 case 'tournament.discord':
79                 case 'tournament.lock':
80                 case 'tournament.open':
81                 case 'tournament.unlock':
82                         return i18n.t(
83                                 `protocol.description.${entry.type}`,
84                                 entry,
85                         );
86                 default:
87                         return i18n.t('protocol.description.unknown', entry);
88         }
89 };
90
91 const getEntryIcon = entry => {
92         switch (entry.type) {
93                 case 'result.report':
94                         return <Icon.RESULT />;
95                 case 'round.create':
96                         return <Icon.ADD />;
97                 case 'round.lock':
98                 case 'tournament.close':
99                 case 'tournament.lock':
100                         return <Icon.LOCKED />;
101                 case 'round.unlock':
102                 case 'tournament.open':
103                 case 'tournament.unlock':
104                         return <Icon.UNLOCKED />;
105                 case 'tournament.discord':
106                         return <Icon.DISCORD />;
107                 default:
108                         return <Icon.PROTOCOL />;
109         }
110 };
111
112 const Item = ({ entry }) =>
113         <ListGroup.Item className="d-flex align-items-center">
114                 <div className="pe-3 text-muted">
115                         {getEntryIcon(entry)}
116                 </div>
117                 <div>
118                         <div>
119                                 {getEntryDescription(entry)}
120                         </div>
121                         <div
122                                 className="text-muted"
123                                 title={moment(entry.created_at).format('LLLL')}
124                         >
125                                 {getEntryDate(entry)}
126                         </div>
127                 </div>
128         </ListGroup.Item>;
129
130 Item.propTypes = {
131         entry: PropTypes.shape({
132                 created_at: PropTypes.string,
133         }),
134 };
135
136 Item.defaultProps = {
137         entry: {},
138 };
139
140 export default withTranslation()(Item);