From d622c680d69a150583609fc39c51e441daa38a6a Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Wed, 4 Jan 2023 16:50:51 +0200 Subject: [PATCH] Separate DownloadButton component that can be clicked on --- src/components/DownloadButton.tsx | 31 +++++++++++++++++++++++++++++++ src/components/ZwoOutput.tsx | 16 ++-------------- src/download.ts | 12 ------------ 3 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 src/components/DownloadButton.tsx delete mode 100644 src/download.ts diff --git a/src/components/DownloadButton.tsx b/src/components/DownloadButton.tsx new file mode 100644 index 0000000..692fec1 --- /dev/null +++ b/src/components/DownloadButton.tsx @@ -0,0 +1,31 @@ +import React from "react"; +import { Workout, generateZwo } from "zwiftout"; +import styled from "styled-components"; + +const Button = styled.a` + border: 1px solid #bbb; + border-radius: 3px; + padding: 3px 8px; + margin-left: 16px; + font-size: 14px; + color: inherit; + text-decoration: none; + background-color: #eee; + &:hover { + background-color: #86af1c; + border-color: #86af1c; + } +`; + +// Creates .zwo file name from workout name +const workoutFileName = (name: string): string => name.replace(/[^\w]/, "-").replace(/-+/, "-").toLowerCase() + ".zwo"; + +const downloadDataUrl = (xml: string): string => URL.createObjectURL(new Blob([xml], { type: "application/xml" })); + +export const DownloadButton: React.FC<{ workout: Workout }> = ({ workout }) => { + return ( + + ); +}; diff --git a/src/components/ZwoOutput.tsx b/src/components/ZwoOutput.tsx index e8030ef..08e1f4d 100644 --- a/src/components/ZwoOutput.tsx +++ b/src/components/ZwoOutput.tsx @@ -1,7 +1,7 @@ import React, { useState } from "react"; import { Workout, generateZwo } from "zwiftout"; import styled from "styled-components"; -import { download } from "../download"; +import { DownloadButton } from "./DownloadButton"; const Header = styled.h2` font-weight: normal; @@ -21,25 +21,13 @@ const ShowHideButton = styled.button` float: right; `; -const DownloadButton = styled.button` - border: 1px solid #bbb; - border-radius: 3px; - padding: 3px 8px; - margin-left: 16px; -`; - -// Creates .zwo file name from workout name -const workoutFileName = (name: string): string => name.replace(/[^\w]/, "-").replace(/-+/, "-").toLowerCase() + ".zwo"; - export const ZwoOutput: React.FC<{ workout: Workout }> = ({ workout }) => { const [expanded, setExpanded] = useState(false); return (
Generated Zwift workout file (.zwo): - download(workoutFileName(workout.name), generateZwo(workout))}> - Download - +
setExpanded(!expanded)}>{expanded ? "Hide ZWO" : "Show ZWO"} diff --git a/src/download.ts b/src/download.ts deleted file mode 100644 index 3cd16b3..0000000 --- a/src/download.ts +++ /dev/null @@ -1,12 +0,0 @@ -export function download(filename: string, xml: string) { - const file = new Blob([xml], { type: "application/xml" }); - const url = URL.createObjectURL(file); - - var a = document.createElement("a"); - document.body.appendChild(a); - a.style.display = "none"; - a.href = url; - a.download = filename; - a.click(); - URL.revokeObjectURL(url); -}