|
| 1 | +import { |
| 2 | + Alert, |
| 3 | + Button, |
| 4 | + Dialog, |
| 5 | + DialogActions, |
| 6 | + DialogContent, |
| 7 | + DialogTitle, |
| 8 | + Typography, |
| 9 | +} from "@mui/material"; |
| 10 | +import { FC, useState } from "react"; |
| 11 | +import { IGNORE_BREAKING_CHANGE_NOTES_PIPEDS } from "~/constants/localstorage"; |
| 12 | +import ReactMarkdown from "react-markdown"; |
| 13 | +import "github-markdown-css/github-markdown.css"; |
| 14 | + |
| 15 | +type Props = { |
| 16 | + notes?: string | null; |
| 17 | +}; |
| 18 | + |
| 19 | +const getVersionsIgnoredWarning = (): string[] => { |
| 20 | + try { |
| 21 | + const rawIgnoredNotes = |
| 22 | + localStorage.getItem(IGNORE_BREAKING_CHANGE_NOTES_PIPEDS) || "[]"; |
| 23 | + return JSON.parse(rawIgnoredNotes) as string[]; |
| 24 | + } catch { |
| 25 | + return []; |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +const shouldIgnoredBreakingChangeNotes = (): boolean => { |
| 30 | + const version = process.env.PIPECD_VERSION; |
| 31 | + if (!version) return false; |
| 32 | + |
| 33 | + try { |
| 34 | + const ignoredNotes = getVersionsIgnoredWarning(); |
| 35 | + return ignoredNotes.includes(version); |
| 36 | + } catch { |
| 37 | + return false; |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +const BreakingChangeNotes: FC<Props> = ({ notes }) => { |
| 42 | + const [showDialog, setShowDialog] = useState(false); |
| 43 | + const [showNotes, setShowNotes] = useState( |
| 44 | + !shouldIgnoredBreakingChangeNotes() |
| 45 | + ); |
| 46 | + |
| 47 | + const onIgnoreWarning = (): void => { |
| 48 | + setShowDialog(false); |
| 49 | + const pipedVersion = process.env.PIPECD_VERSION; |
| 50 | + if (!pipedVersion) return; |
| 51 | + |
| 52 | + try { |
| 53 | + const ignoredVersions = JSON.parse( |
| 54 | + localStorage.getItem(IGNORE_BREAKING_CHANGE_NOTES_PIPEDS) || "[]" |
| 55 | + ); |
| 56 | + |
| 57 | + if (!ignoredVersions.includes(pipedVersion)) { |
| 58 | + ignoredVersions.push(pipedVersion); |
| 59 | + } |
| 60 | + |
| 61 | + localStorage.setItem( |
| 62 | + IGNORE_BREAKING_CHANGE_NOTES_PIPEDS, |
| 63 | + JSON.stringify(ignoredVersions) |
| 64 | + ); |
| 65 | + } finally { |
| 66 | + setShowNotes(false); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + if (!notes || !showNotes) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + return ( |
| 74 | + <> |
| 75 | + <Alert |
| 76 | + severity="warning" |
| 77 | + sx={{ alignItems: "center" }} |
| 78 | + action={ |
| 79 | + <Button onClick={() => setShowDialog(true)}>View details</Button> |
| 80 | + } |
| 81 | + > |
| 82 | + <Typography |
| 83 | + sx={{ |
| 84 | + overflow: "hidden", |
| 85 | + textOverflow: "ellipsis", |
| 86 | + display: "-webkit-box", |
| 87 | + WebkitLineClamp: "2", |
| 88 | + WebkitBoxOrient: "vertical", |
| 89 | + whiteSpace: "pre-wrap", |
| 90 | + }} |
| 91 | + > |
| 92 | + {notes} |
| 93 | + </Typography> |
| 94 | + </Alert> |
| 95 | + |
| 96 | + <Dialog |
| 97 | + open={showDialog} |
| 98 | + onClose={() => setShowDialog(false)} |
| 99 | + maxWidth="lg" |
| 100 | + > |
| 101 | + <DialogTitle>Breaking Changes</DialogTitle> |
| 102 | + <DialogContent |
| 103 | + sx={{ |
| 104 | + maxHeight: "60vh", |
| 105 | + overflowX: "hidden", |
| 106 | + overflowY: "auto", |
| 107 | + }} |
| 108 | + > |
| 109 | + <div className="markdown-body"> |
| 110 | + <ReactMarkdown linkTarget="_blank">{notes}</ReactMarkdown> |
| 111 | + </div> |
| 112 | + </DialogContent> |
| 113 | + <DialogActions> |
| 114 | + <Button onClick={() => onIgnoreWarning()}>Ignore</Button> |
| 115 | + <Button onClick={() => setShowDialog(false)}>Close</Button> |
| 116 | + </DialogActions> |
| 117 | + </Dialog> |
| 118 | + </> |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +export default BreakingChangeNotes; |
0 commit comments