123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- import React from "react";
- import styled from "styled-components";
- import { Link, useHistory } from "react-router-dom";
- import { useTranslation } from "react-i18next";
- import { deleteGame, getBestTranslationFromConfig } from "../utils/api";
- import { confirmAlert } from "react-confirm-alert";
- import { toast } from "react-toastify";
- import { useMutation, useQueryClient } from "react-query";
- import { media2Url } from "../mediaLibrary";
- const Game = styled.li`
- position: relative;
- padding: 0em;
- margin: 0px;
- min-width: 0; /* Fix for ellipsis */
- & .game-name {
- max-width: 80%;
- line-height: 1.1em;
- overflow: hidden;
- margin-bottom: 3px;
- margin: 0.1em 0 0.1em 0;
- font-size: 2.2vw;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- }
- & .unpublished {
- position: absolute;
- top: 0.5em;
- left: 0.5em;
- padding: 0em;
- }
- & .button.play {
- margin: 0 2px;
- background-color: var(--color-secondary);
- }
- & .play {
- position: absolute;
- bottom: 0.5em;
- right: 0.5em;
- }
- & .extra-actions {
- position: absolute;
- top: 0.5em;
- right: 0.5em;
- display: none;
- z-index: 2;
- & .button {
- border-radius: 4px;
- }
- }
- & .baseline {
- max-height: 3em;
- overflow: hidden;
- }
- &:hover .extra-actions,
- & .extra-actions:hover {
- display: block;
- }
- & .img-wrapper {
- display: block;
- position: relative;
- margin: 0;
- padding: 0;
- width: 100%;
- padding-top: 64.5%;
- & > span {
- background-color: var(--color-blueGrey);
- ${({ other }) => (!other ? "" : "border: 1px solid red")};
- position: absolute;
- inset: 0;
- overflow: hidden;
- display: block;
- display: flex;
- justify-content: center;
- align-items: center;
- border-radius: 5px;
- & > .back {
- filter: blur(5px);
- background-size: cover;
- position: absolute;
- inset: 0;
- }
- & > h2 {
- position: absolute;
- width: 100%;
- top: calc(50%-0.6em);
- z-index: 200;
- left: 0;
- text-align: center;
- display: inline;
- font-size: 2em;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- margin: 0;
- padding: 0.2em 0.5em;
- line-height: 1.2em;
- background-color: #111111a0;
- }
- }
- }
- & .img {
- object-fit: contain;
- width: 100%;
- height: 100%;
- z-index: 1;
- }
- & .details {
- display: flex;
- flex-direction: row;
- color: var(--font-color2);
- font-size: 14px;
- padding-top: 1em;
- }
- & .details > span {
- display: flex;
- align-items: center;
- padding-right: 5px;
- margin-right: 5px;
- border-right: 1px solid var(--font-color2);
- }
- & .details > span:last-child {
- border: none;
- }
- & .details img {
- margin-right: 0.5em;
- }
- @media screen and (max-width: 1024px) {
- & {
- flex-basis: 45%;
- }
- & .details {
- font-size: 12px;
- }
- & .game-name {
- font-size: 28px;
- }
- }
- @media screen and (max-width: 640px) {
- & {
- flex-basis: 100%;
- }
- & .game-name {
- font-size: 24px;
- }
- }
- `;
- const getGameUrl = (id) => `${window.location.origin}/playgame/${id}`;
- const GameListItem = ({
- game: {
- owner,
- id,
- board: {
- minAge,
- materialLanguage,
- duration,
- playerCount,
- published,
- imageUrl,
- keepTitle,
- },
- },
- game,
- userId,
- onClick: propOnClick,
- onDelete,
- isAdmin = false,
- studio = false,
- }) => {
- const { t, i18n } = useTranslation();
- const history = useHistory();
- const queryClient = useQueryClient();
- const deleteMutation = useMutation((gameId) => deleteGame(gameId), {
- onSuccess: () => {
- queryClient.invalidateQueries("ownGames");
- queryClient.invalidateQueries("games");
- },
- });
- const realImageUrl = media2Url(imageUrl);
- const [showImage, setShowImage] = React.useState(Boolean(realImageUrl));
- const translation = React.useMemo(
- () => getBestTranslationFromConfig(game.board, i18n.languages),
- [game, i18n.languages]
- );
- const onClick = React.useCallback(
- (e) => {
- e.stopPropagation();
- e.preventDefault();
- if (propOnClick) {
- return propOnClick(id);
- } else {
- history.push(`/playgame/${id}`);
- }
- },
- [history, id, propOnClick]
- );
- const onShare = React.useCallback(
- async (e) => {
- e.stopPropagation();
- e.preventDefault();
- await navigator.clipboard.writeText(getGameUrl(id));
- toast.info(t("Url copied to clipboard!"), { autoClose: 1000 });
- },
- [id, t]
- );
- const deleteGameHandler = async () => {
- confirmAlert({
- title: t("Confirmation"),
- message: t("Do you really want to remove this game?"),
- buttons: [
- {
- label: t("Yes"),
- onClick: async () => {
- try {
- deleteMutation.mutate(id);
- if (onDelete) onDelete(id);
- toast.success(t("Game deleted"), { autoClose: 1500 });
- } catch (e) {
- if (e.message === "Forbidden") {
- toast.error(t("Action forbidden. Try logging in again."));
- } else {
- console.log(e);
- toast.error(t("Error while deleting game. Try again later..."));
- }
- }
- },
- },
- {
- label: t("No"),
- onClick: () => {},
- },
- ],
- });
- };
- let playerCountDisplay = undefined;
- if (playerCount && playerCount.length) {
- const [min, max] = playerCount;
- if (min === max) {
- if (max === 9) {
- playerCountDisplay = ["9+"];
- } else {
- playerCountDisplay = [max];
- }
- } else {
- if (max === 9) {
- playerCountDisplay = [min, "9+"];
- } else {
- playerCountDisplay = [min, max];
- }
- }
- }
- let durationDisplay = undefined;
- if (duration && duration.length) {
- const [min, max] = duration;
- if (min === max) {
- if (max === 90) {
- durationDisplay = "90+";
- } else {
- durationDisplay = `~${max}`;
- }
- } else {
- if (max === 90) {
- durationDisplay = `${min}~90+`;
- } else {
- durationDisplay = `${min}~${max}`;
- }
- }
- }
- let materialLanguageDisplay = t(materialLanguage);
- const owned = userId && (userId === owner || !owner);
- return (
- <Game other={!owned && studio}>
- <a href={`/playgame/${id}`} className="img-wrapper button">
- <span onClick={onClick}>
- {showImage && (
- <>
- <span
- className="back"
- style={{ backgroundImage: `url(${realImageUrl})` }}
- />
- <img
- className="img"
- src={realImageUrl}
- alt={translation.name}
- onError={() => setShowImage(false)}
- />
- </>
- )}
- {(!showImage || keepTitle) && <h2>{translation.name}</h2>}
- </span>
- </a>
- <span className="extra-actions">
- <a
- href={getGameUrl(id)}
- className="button edit icon-only success"
- onClick={onShare}
- >
- <img
- src="https://icongr.am/feather/share-2.svg?size=16&color=ffffff"
- alt={t("Share game link")}
- title={t("Share game link")}
- />
- </a>
- {(owned || isAdmin) && (
- <>
- <button
- onClick={deleteGameHandler}
- className="button edit icon-only error"
- >
- <img
- src="https://icongr.am/feather/trash.svg?size=16&color=ffffff"
- alt={t("Delete")}
- title={t("Delete")}
- />
- </button>
- <Link to={`/game/${id}/edit`} className="button edit icon-only ">
- <img
- src="https://icongr.am/feather/edit.svg?size=16&color=ffffff"
- alt={t("Edit")}
- title={t("Edit")}
- />
- </Link>
- </>
- )}
- </span>
- {!published && (
- <img
- className="unpublished"
- src="https://icongr.am/entypo/eye-with-line.svg?size=32&color=888886"
- alt={t("Unpublished")}
- />
- )}
- <div className="details">
- {playerCountDisplay && (
- <span>
- {playerCountDisplay.length === 2 &&
- t("{{min}} - {{max}} players", {
- min: playerCountDisplay[0],
- max: playerCountDisplay[1],
- })}
- {playerCountDisplay.length === 1 &&
- t("{{count}} player", {
- count: playerCountDisplay[0],
- })}
- </span>
- )}
- {durationDisplay && <span>{durationDisplay} mins</span>}
- {minAge && <span>age {minAge}+</span>}
- {materialLanguageDisplay && <span>{materialLanguageDisplay}</span>}
- </div>
- <h2 className="game-name">{translation.name}</h2>
- <p className="baseline">{translation.baseline}</p>
- </Game>
- );
- };
- export default GameListItem;
|