-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathshareButton.tsx
More file actions
68 lines (63 loc) · 1.81 KB
/
shareButton.tsx
File metadata and controls
68 lines (63 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Icon } from "@iconify/react";
import {
Alert,
Grid2 as Grid,
IconButton,
Snackbar,
Tooltip,
} from "@mui/material";
import { useAtomValue } from "jotai";
import { useState } from "react";
import { boardAtom, boardOrientationAtom, gameAtom } from "../states";
import { getGameToSave } from "@/lib/chess";
import { buildShareUrl } from "@/lib/shareGame";
export default function ShareButton() {
const game = useAtomValue(gameAtom);
const board = useAtomValue(boardAtom);
const orientation = useAtomValue(boardOrientationAtom);
const [snackbar, setSnackbar] = useState<{
open: boolean;
message: string;
severity: "success" | "warning";
}>({ open: false, message: "", severity: "success" });
const hasGame = game.history().length > 0 || board.history().length > 0;
const handleShare = () => {
const gameToShare = getGameToSave(game, board);
const url = buildShareUrl(gameToShare.pgn(), orientation);
navigator.clipboard?.writeText?.(url);
setSnackbar({
open: true,
message: "Link copied to clipboard!",
severity: "success",
});
};
return (
<>
<Tooltip title="Share game link">
<Grid>
<IconButton
onClick={handleShare}
disabled={!hasGame}
sx={{ paddingX: 1.2, paddingY: 0.5 }}
>
<Icon icon="ri:link" />
</IconButton>
</Grid>
</Tooltip>
<Snackbar
open={snackbar.open}
autoHideDuration={3000}
onClose={() => setSnackbar((s) => ({ ...s, open: false }))}
anchorOrigin={{ vertical: "bottom", horizontal: "center" }}
>
<Alert
severity={snackbar.severity}
variant="filled"
sx={{ width: "100%" }}
>
{snackbar.message}
</Alert>
</Snackbar>
</>
);
}