]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/results/Item.js
improved user context
[alttp.git] / resources / js / components / results / Item.js
index 5cdd3be42751c1b678508a1e3800a065b080bf93..2e29d752afd2bd6938e82a5123c3b607bdea7308 100644 (file)
 import PropTypes from 'prop-types';
-import React from 'react';
-import { withTranslation } from 'react-i18next';
+import React, { useState } from 'react';
+import { Button } from 'react-bootstrap';
+import { useTranslation } from 'react-i18next';
 
+import DetailDialog from './DetailDialog';
 import Icon from '../common/Icon';
 import Box from '../users/Box';
-import { formatTime } from '../../helpers/Result';
-import { findResult } from '../../helpers/Participant';
+import { getIcon, getTime } from '../../helpers/Result';
 import { maySeeResults } from '../../helpers/permissions';
-import { withUser } from '../../helpers/UserContext';
+import { findResult } from '../../helpers/User';
+import { useUser } from '../../hooks/user';
+
+const getClassName = result => {
+       const classNames = ['status'];
+       if (result && result.has_finished) {
+               classNames.push('finished');
+               if (result.comment) {
+                       classNames.push('has-comment');
+               }
+       } else {
+               classNames.push('pending');
+       }
+       return classNames.join(' ');
+};
+
+const twitchReg = /^https?:\/\/(www\.)?twitch\.tv/;
+const youtubeReg = /^https?:\/\/(www\.)?youtu(\.be|be\.)/;
+
+const getVoDVariant = result => {
+       if (!result || !result.vod) return 'outline-secondary';
+       if (twitchReg.test(result.vod)) {
+               return 'twitch';
+       }
+       if (youtubeReg.test(result.vod)) {
+               return 'outline-youtube';
+       }
+       return 'outline-secondary';
+};
+
+const getVoDIcon = result => {
+       const variant = getVoDVariant(result);
+       if (variant === 'twitch') {
+               return <Icon.TWITCH title="" />;
+       }
+       if (variant === 'outline-youtube') {
+               return <Icon.YOUTUBE title="" />;
+       }
+       return <Icon.VIDEO title="" />;
+};
 
 const Item = ({
-       participant,
        round,
        tournament,
        user,
 }) => {
-       const result = findResult(participant, round);
-       return (
-               <div className="result">
-                       <Box user={participant.user} />
-                       <div className="status">
+       const [showDialog, setShowDialog] = useState(false);
+
+       const { t } = useTranslation();
+       const { user: authUser } = useUser();
+
+       const result = React.useMemo(
+               () => findResult(user, round),
+               [round, user],
+       );
+       const maySee = React.useMemo(
+               () => maySeeResults(authUser, tournament, round),
+               [authUser, round, tournament],
+       );
+
+       return <div className="result">
+               <Box user={user} />
+               <div className="d-flex align-items-center justify-content-between">
+                       <Button
+                               className={getClassName(result)}
+                               onClick={() => setShowDialog(true)}
+                               title={maySee && result && result.comment ? result.comment : null}
+                       >
                                <span className="time">
-                                       {result && maySeeResults(user, tournament, round) ?
-                                               formatTime(result)
-                                       : null}
+                                       {getTime(result, maySee)}
                                </span>
-                               {result && result.has_finished ?
-                                       <Icon.FINISHED className="text-success" size="lg" />
-                               :
-                                       <Icon.PENDING size="lg" />
-                               }
-                       </div>
+                               {getIcon(result, maySee)}
+                       </Button>
+                       {maySee && result && result.vod ?
+                               <Button
+                                       className="vod-link"
+                                       href={result.vod}
+                                       size="sm"
+                                       target="_blank"
+                                       title={t('results.vod')}
+                                       variant={getVoDVariant(result)}
+                               >
+                                       {getVoDIcon(result)}
+                               </Button>
+                       : null}
                </div>
-       );
+               <DetailDialog
+                       onHide={() => setShowDialog(false)}
+                       round={round}
+                       show={showDialog}
+                       tournament={tournament}
+                       user={user}
+               />
+       </div>;
 };
 
 Item.propTypes = {
-       participant: PropTypes.shape({
-               user: PropTypes.shape({
-               }),
-       }),
        round: PropTypes.shape({
        }),
        tournament: PropTypes.shape({
@@ -48,4 +113,4 @@ Item.propTypes = {
        }),
 };
 
-export default withTranslation()(withUser(Item));
+export default Item;