Skip to content

Commit

Permalink
Adding a share button (#100)
Browse files Browse the repository at this point in the history
* Adding a share button

* Auto hide and change severity to info

---------

Co-authored-by: Nicolas Vuillamy <[email protected]>
  • Loading branch information
itayox and nvuillam authored Aug 17, 2023
1 parent c40adb1 commit 79e4375
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ _Can be run using `npm run codetotal:beta`_
- Add link to packages' registry in SBOM panel
- Fix Safari not showing Score component correctly #81
- Display "snippet" in results page instead of md5: xxxx
- Add a share button in the footer

- Back-End
- Bug fix: SBOM packages not showing up in report page. Async parsing of packages information in SBOM module
Expand Down
11 changes: 10 additions & 1 deletion packages/app/src/app/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { FC } from "react";
import { makeStyles } from "tss-react/mui";
import { ReactComponent as BackgroundSvg } from "../../assets/bg.svg";
import oxLogo from "../../assets/ox.svg";
import { ShareButton } from "./ShareButton";
import { ToggleThemeButton } from "./ToggleThemeButton";

export const Footer: FC = () => {
const { classes } = useStyles();


return (
<div className={classes.footer}>
Expand Down Expand Up @@ -45,7 +47,10 @@ export const Footer: FC = () => {
MegaLinter
</a>
</Typography>
<ToggleThemeButton />
<div className={classes.actions}>
<ShareButton />
<ToggleThemeButton />
</div>
</div>
</Container>
<BackgroundSvg />
Expand Down Expand Up @@ -97,4 +102,8 @@ const useStyles = makeStyles()((theme: Theme) => ({
width: "1.3em",
height: "1.3em",
},
actions: {
display: "flex",
gap: theme.spacing(2),
},
}));
92 changes: 92 additions & 0 deletions packages/app/src/app/components/ShareButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Alert, AlertTitle, Button, Slide, Snackbar } from "@mui/material";
import { FC, useState } from "react";
import { makeStyles } from "tss-react/mui";

const CODE_TOTAL_URL = "https://codetotal.io";

export const ShareButton: FC = () => {
const { classes } = useStyles();
const [error, setError] = useState(false);
const [loading, setLoading] = useState(false);
const [success, setSuccess] = useState(false);

const handleShare = async () => {
try {
setLoading(true);
await navigator.clipboard.writeText(CODE_TOTAL_URL);
setSuccess(true);
} catch (err) {
setError(true);
setSuccess(false);
} finally {
setLoading(false);
}
};

const handleClose = () => {
setError(false);
setSuccess(false);
};

return (
<>
<Button
size="small"
startIcon={<i style={{ fontSize: "1em" }}>&#x1F496;</i>}
onClick={handleShare}
disabled={loading}
>
Share
</Button>

<Snackbar
open={error}
onClose={handleClose}
autoHideDuration={10000}
anchorOrigin={{ vertical: "top", horizontal: "center" }}
TransitionComponent={Slide}
>
<Alert onClose={handleClose} severity="error">
<AlertTitle>Unable to copy URL to clipboard</AlertTitle>
Please copy or click the following URL manually to share:&nbsp;
<a
href={CODE_TOTAL_URL}
target="_blank"
rel="noreferrer"
className={classes.link}
>
{CODE_TOTAL_URL}
</a>
</Alert>
</Snackbar>

<Snackbar
open={success}
onClose={handleClose}
autoHideDuration={10000}
anchorOrigin={{ vertical: "top", horizontal: "center" }}
TransitionComponent={Slide}
>
<Alert severity="info" onClose={handleClose}>
<AlertTitle>URL copied successfully</AlertTitle>
<div className={classes.thankYou}>
Thank you for sharing CodeTotal with the community&nbsp;
<span style={{ fontSize: "2em" }}>&#x1F497;</span>
</div>
</Alert>
</Snackbar>
</>
);
};

const useStyles = makeStyles()((theme) => ({
link: {
color: "inherit",
fontWeight: "bold",
},
thankYou: {
display: "flex",
alignItems: "center",
gap: theme.spacing(1),
},
}));

0 comments on commit 79e4375

Please sign in to comment.