From e585e87bec51b00b662b58f9cff7a1405a89d1be Mon Sep 17 00:00:00 2001 From: GyDi <segydi@foxmail.com> Date: Sat, 19 Mar 2022 15:31:58 +0800 Subject: [PATCH] feat: parse update log --- package.json | 1 + src/components/layout/update-button.tsx | 4 +++- src/components/layout/update-dialog.tsx | 26 ++++++++++++++++++++----- yarn.lock | 5 +++++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index ee13757..6d659d5 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "react-router-dom": "^6.2.2", "react-virtuoso": "^2.7.0", "recoil": "^0.6.1", + "snarkdown": "^2.0.0", "swr": "^1.2.1" }, "devDependencies": { diff --git a/src/components/layout/update-button.tsx b/src/components/layout/update-button.tsx index 8a83824..5bccb4c 100644 --- a/src/components/layout/update-button.tsx +++ b/src/components/layout/update-button.tsx @@ -32,7 +32,9 @@ const UpdateButton = (props: Props) => { New </Button> - <UpdateDialog open={dialogOpen} onClose={() => setDialogOpen(false)} /> + {dialogOpen && ( + <UpdateDialog open={dialogOpen} onClose={() => setDialogOpen(false)} /> + )} </> ); }; diff --git a/src/components/layout/update-dialog.tsx b/src/components/layout/update-dialog.tsx index e330834..c8b94e3 100644 --- a/src/components/layout/update-dialog.tsx +++ b/src/components/layout/update-dialog.tsx @@ -1,12 +1,14 @@ import useSWR from "swr"; -import { useState } from "react"; +import snarkdown from "snarkdown"; +import { useState, useMemo } from "react"; import { + Box, Button, Dialog, DialogActions, DialogContent, - DialogContentText, DialogTitle, + styled, } from "@mui/material"; import { relaunch } from "@tauri-apps/api/process"; import { checkUpdate, installUpdate } from "@tauri-apps/api/updater"; @@ -18,6 +20,10 @@ interface Props { onClose: () => void; } +const UpdateLog = styled(Box)(() => ({ + "h1,h2,h3,ul,ol,p": { margin: "0.5em 0", color: "inherit" }, +})); + let uploadingState = false; const UpdateDialog = (props: Props) => { @@ -46,19 +52,29 @@ const UpdateDialog = (props: Props) => { } }; + // markdown parser + const parseContent = useMemo(() => { + if (!updateInfo?.manifest?.body) { + return "New Version is available"; + } + return snarkdown(updateInfo?.manifest?.body); + }, [updateInfo]); + return ( <Dialog open={open} onClose={onClose}> <DialogTitle>New Version v{updateInfo?.manifest?.version}</DialogTitle> + <DialogContent sx={{ minWidth: 360, maxWidth: 400, maxHeight: "50vh" }}> - <DialogContentText>{updateInfo?.manifest?.body}</DialogContentText> + <UpdateLog dangerouslySetInnerHTML={{ __html: parseContent }} /> </DialogContent> + <DialogActions> <Button onClick={onClose}>Cancel</Button> <Button - variant="contained" autoFocus - onClick={onUpdate} + variant="contained" disabled={uploading} + onClick={onUpdate} > Update </Button> diff --git a/yarn.lock b/yarn.lock index 399e9f2..a92cf69 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1831,6 +1831,11 @@ signal-exit@^3.0.2: resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.7.tgz#a9a1767f8af84155114eaabd73f99273c8f59ad9" integrity sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ== +snarkdown@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/snarkdown/-/snarkdown-2.0.0.tgz#b1feb4db91b9f94a8ebbd7a50f3e99aee18b1e03" + integrity sha512-MgL/7k/AZdXCTJiNgrO7chgDqaB9FGM/1Tvlcenenb7div6obaDATzs16JhFyHHBGodHT3B7RzRc5qk8pFhg3A== + "source-map-js@>=0.6.2 <2.0.0", source-map-js@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c" -- GitLab