From a0bd87456002dbca8978111a5c1aa53181d19a89 Mon Sep 17 00:00:00 2001 From: Wren Hawthorne Date: Sun, 14 Jul 2024 20:44:05 -0700 Subject: [PATCH] Added share step --- src/App.css | 4 ++ src/components/AllSteps.tsx | 43 ++++++++++++++++-- src/components/ChordStep.tsx | 1 + src/components/DrumStep.tsx | 2 +- src/components/MelodyStep.tsx | 14 +++++- src/components/NewSongStep.tsx | 62 +++++++++++++------------ src/components/ShareStep.tsx | 72 ++++++++++++++++++++++++++++++ src/components/WorkflowContext.tsx | 14 +++++- src/hooks/useChordPart.ts | 16 +++---- src/utils/workflow.ts | 1 + 10 files changed, 186 insertions(+), 43 deletions(-) create mode 100644 src/components/ShareStep.tsx diff --git a/src/App.css b/src/App.css index 8adee32..f4cc7b4 100644 --- a/src/App.css +++ b/src/App.css @@ -309,6 +309,10 @@ sl-select::part(base) { background-color: var(--sl-color-teal-500); } +.share-tab::part(base) { + background-color: var(--sl-color-cyan-500); +} + sl-tab[active]::part(base) { font-weight: bolder; text-decoration: underline; diff --git a/src/components/AllSteps.tsx b/src/components/AllSteps.tsx index 6a079d5..24e3769 100644 --- a/src/components/AllSteps.tsx +++ b/src/components/AllSteps.tsx @@ -18,6 +18,7 @@ import { ChordStep } from "./ChordStep" import { NewSongStep } from "./NewSongStep" import { getTransport } from "tone"; import { MelodyStep } from "./MelodyStep"; +import { ShareStep } from "./ShareStep"; export type Song = { title: string; @@ -59,7 +60,7 @@ const parseChordListParam = (param: string[] | null) => { } const parseSeventhsParam = (param: string[] | null, length: number) => { - const sevenths = Array.from({ length: INITIAL_CHORD_LIST.length }, () => false) + const sevenths = Array.from({ length }, () => false) param?.forEach((i) => { const num = Number.parseInt(i) if (Number.isInteger(num) && num > 0 && num < length) { @@ -89,9 +90,12 @@ export const AllSteps = () => { const urlParams = new URLSearchParams(window.location.search); const { isStarted, step, areDrumsEnabled, areChordsEnabled, isMelodyEnabled } = useContext(WorkflowContext) const dispatch = useContext(WorkflowDispatchContext) + const [title, setTitle] = React.useState(urlParams.get('title') || '') const [tempo, setTempo] = React.useState(Number.parseInt(urlParams.get('tempo') || `${DEFAULT_TEMPO}`) ?? DEFAULT_TEMPO) + const isLoadedSong = urlParams.get('title') != null + useEffect(() => { const transport = getTransport() transport.bpm.rampTo(tempo, 2) @@ -112,14 +116,33 @@ export const AllSteps = () => { const [arpPattern, setArpPattern] = React.useState(parseArpPatternParam(urlParams.get('arp'))) useChordPart({ chordList, chordPattern, arpPattern, chordSynth, isStarted, playChords: areChordsEnabled, useSeventh }) - const [lyrics, setLyrics] = React.useState('') + const [lyrics, setLyrics] = React.useState(urlParams.get('lyrics') || '') + + const shareUrl = React.useMemo(() => { + const params = new URLSearchParams({ + title, + tempo: tempo.toFixed(0), + drums: drumPreset, + chordPattern, + arp: arpPattern, + lyrics, + }) + chordList.forEach((c) => params.append('chords', c)) + useSeventh.forEach((s, i) => { + if (s) { + params.append('sev', i.toFixed(0)) + } + }) + return `${window.location.origin}${window.location.pathname}?${params.toString()}` + }, [title, tempo, drumPreset, chordPattern, arpPattern, lyrics, chordList, useSeventh]) + return ( <> {/*