-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
163 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { ContentCopy, PieChartRounded, ReadMore, Replay, SvgIconComponent } from "@mui/icons-material"; | ||
import { Avatar, Dialog, List, ListItem, ListItemAvatar, ListItemButton, ListItemText } from "@mui/material"; | ||
import copy from "copy-to-clipboard"; | ||
import { useSnackbar } from "notistack"; | ||
import React, { AnchorHTMLAttributes, useCallback, useEffect, useReducer } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { GameRecord, PlayerRecord } from "../../../data/types"; | ||
import Conf from "../../../utils/conf"; | ||
import { generatePlayerPathById } from "../routeUtils"; | ||
|
||
const Action = ({ | ||
Icon, | ||
text, | ||
...props | ||
}: { | ||
Icon: SvgIconComponent; | ||
text: string; | ||
} & Parameters<typeof ListItemButton>[0] & | ||
AnchorHTMLAttributes<HTMLAnchorElement>) => ( | ||
<ListItem disableGutters> | ||
<ListItemButton {...props}> | ||
<ListItemAvatar> | ||
<Avatar> | ||
<Icon fontSize="inherit" /> | ||
</Avatar> | ||
</ListItemAvatar> | ||
<ListItemText primary={text} primaryTypographyProps={{ variant: "body2", sx: { pr: 1 } }} /> | ||
</ListItemButton> | ||
</ListItem> | ||
); | ||
export const ActionsDialog = React.memo( | ||
({ player, game, onClose }: { player?: PlayerRecord; game?: GameRecord; onClose: () => void }) => { | ||
const { t } = useTranslation(); | ||
const { enqueueSnackbar } = useSnackbar(); | ||
const [savedGame, updateGame] = useReducer( | ||
(prev: GameRecord | undefined, cur: GameRecord | undefined) => cur || prev, | ||
game, | ||
(game) => game | ||
); | ||
useEffect(() => { | ||
updateGame(game); | ||
}, [game]); | ||
const isMasked = !(game || savedGame)?.uuid || (game || savedGame)?._masked; | ||
const gameLink = !game ? "#" : (isMasked ? GameRecord.getMaskedRecordLink : GameRecord.getRecordLink)(game, player); | ||
const copyLink = useCallback(() => { | ||
if (!gameLink) { | ||
return; | ||
} | ||
copy(gameLink); | ||
enqueueSnackbar(t("链接复制成功"), { variant: "success", autoHideDuration: 2000 }); | ||
}, [gameLink, enqueueSnackbar, t]); | ||
return ( | ||
<Dialog open={!!game} onClose={onClose} onClick={onClose} maxWidth="xs"> | ||
<List> | ||
<Action Icon={Replay} text={t("查看牌谱")} href={gameLink} target="_blank" /> | ||
{!isMasked && <Action Icon={ContentCopy} onClick={copyLink} text={t("复制链接")} />} | ||
<Action | ||
Icon={ReadMore} | ||
text={t("玩家详细")} | ||
href={player?.accountId ? generatePlayerPathById(player.accountId) : "#"} | ||
/> | ||
{Conf.features.aiReview && !isMasked && ( | ||
<Action | ||
Icon={PieChartRounded} | ||
text={t("AI 检讨")} | ||
target="_blank" | ||
href={ | ||
game && player | ||
? `${t("https://mjai.ekyu.moe/zh-cn.html")}?url=${encodeURIComponent( | ||
GameRecord.getRecordLink(game, player) | ||
)}` | ||
: "#" | ||
} | ||
/> | ||
)} | ||
</List> | ||
</Dialog> | ||
); | ||
} | ||
); | ||
|
||
export default ActionsDialog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { ReactNode, useCallback, useMemo, useState } from "react"; | ||
import { GameRecord, PlayerRecord } from "../../../data/types"; | ||
import Loadable from "../../misc/customizedLoadable"; | ||
|
||
const ActionsDialog = Loadable({ | ||
loader: () => import(/* webpackMode: "lazy" */ /* webpackFetchPriority: "low" */ "./dialog"), | ||
loading: () => <></>, | ||
}); | ||
|
||
const Context = React.createContext<{ open: (player: PlayerRecord, game: GameRecord) => void }>({ | ||
open: () => { | ||
/* Placeholder */ | ||
}, | ||
}); | ||
|
||
export const useGameLinkActions = () => React.useContext(Context); | ||
|
||
const GameLinkActionsProvider = ({ children }: { children: ReactNode }) => { | ||
const [info, setInfo] = useState<{ player: PlayerRecord; game: GameRecord } | null>(null); | ||
const { player, game } = info || {}; | ||
const open = useCallback( | ||
(player: PlayerRecord, game: GameRecord) => { | ||
setInfo({ player, game }); | ||
}, | ||
[setInfo] | ||
); | ||
const close = useCallback(() => setInfo(null), [setInfo]); | ||
const value = useMemo(() => ({ open }), [open]); | ||
return ( | ||
<Context.Provider value={value}> | ||
<ActionsDialog player={player} game={game} onClose={close} /> | ||
{children} | ||
</Context.Provider> | ||
); | ||
}; | ||
export default GameLinkActionsProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters