All files / Bandstand/src/components ChartScreen.tsx

80.84% Statements 173/214
30.43% Branches 7/23
16.66% Functions 2/12
80.84% Lines 173/214

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283    1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                   1x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                       2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x   2x 2x   2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x                 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x     2x 2x   2x 2x 2x 2x 2x 2x 2x 2x   2x 2x         2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x   2x 2x 2x 2x   2x 2x 6x 2x 2x                   2x 2x                         2x 1x 1x 1x 1x 1x 1x 1x 1x   2x   2x           2x             2x 2x 2x 2x   2x                     2x   2x   1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x   2x  
'use client'
 
import Link from 'next/link'
import {useMemo, useState} from 'react'
import {detectKey, parseChordPro, transposeChart} from '@/lib/chordpro'
import {shortDate} from '@/lib/dates'
import {formatLength} from '@/lib/gig'
import ChartBody from '@/components/chart/ChartBody'
import ChordPopover from '@/components/chart/ChordPopover'
import PdfDialog from '@/components/PdfDialog'
import {useStoredState} from '@/components/useStoredState'
import {useCueSlots} from '@/components/cues/useCueSlots'
import type {Cue} from '@/lib/cues'
 
export type ChartSong = {
  id: string
  title: string
  writer: string | null
  leadSinger: string | null
  guitars: number | null
  seconds: number | null
  keys: string | null
  percussion: string | null
  youtubeUrl: string | null
  lyricsUrl: string | null
  notes: string | null
  ready: boolean
}
 
export const TEXT_SIZES = [14, 16, 18, 21, 24, 28]
 
export default function ChartScreen({
  song,
  source,
  version,
  versions,
  editedBy,
  editedAt,
  imported = false,
  cues = [],
  demo = false,
}: {
  song: ChartSong
  source: string
  version: number
  versions: number
  editedBy: string | null
  editedAt: string | null
  imported?: boolean
  /** Your own cues on this song */
  cues?: Cue[]
  /** The tour's sample song: everything shows, nothing saves or leaves */
  demo?: boolean
}) {
  const [steps, setSteps] = useStoredState(`mw:transpose:${song.id}`, 0)
  const [sizeIdx, setSizeIdx] = useStoredState('mw:text-size', 2)
  const [pdfOpen, setPdfOpen] = useState(false)
 
  const original = useMemo(() => parseChordPro(source), [source])
  const chart = useMemo(
    () => transposeChart(original, steps),
    [original, steps],
  )
  const originalKey = detectKey(original)
  const key = detectKey(chart)
  const size = TEXT_SIZES[Math.min(Math.max(sizeIdx, 0), TEXT_SIZES.length - 1)]
  const [cueMode, setCueMode] = useState(false)
  const slots = useCueSlots({
    songId: song.id,
    chart,
    songKey: originalKey,
    steps,
    initial: cues,
    editing: cueMode,
  })
 
  const details = [
    song.writer,
    song.leadSinger && `Lead ${song.leadSinger}`,
    [song.keys, song.percussion].filter(Boolean).join(' · '),
    song.guitars != null &&
      `${song.guitars} guitar${song.guitars === 1 ? '' : 's'}`,
    song.seconds && formatLength(song.seconds),
  ].filter(Boolean)
 
  return (
    <main className="mx-auto max-w-[1400px] px-4 pb-10 pt-4 md:px-7">
      <div className="flex flex-wrap items-center gap-x-4 gap-y-3">
        <div className="min-w-0 flex-1 basis-72">
          <Link
            href="/songs"
            className="text-sm text-muted no-underline hover:text-text"
          >
            ‹ All songs
          </Link>
          <h1 className="mt-1 text-3xl font-extrabold leading-tight md:text-4xl">
            {song.title}
          </h1>
        </div>
        <div className="flex flex-wrap items-center gap-2">
          <div
            role="group"
            aria-label="Key"
            className="flex items-center rounded-lg border border-line-2"
          >
            <button
              type="button"
              aria-label="Transpose down a semitone"
              onClick={() => setSteps(steps - 1)}
              className="h-11 w-11 text-xl"
            >
              −
            </button>
            <span className="min-w-12 text-center font-mono text-lg font-bold text-amber">
              {key ?? '—'}
            </span>
            <button
              type="button"
              aria-label="Transpose up a semitone"
              onClick={() => setSteps(steps + 1)}
              className="h-11 w-11 text-xl"
            >
              +
            </button>
          </div>
          {steps % 12 !== 0 && (
            <button
              type="button"
              onClick={() => setSteps(0)}
              className="min-h-11 rounded-lg px-3 text-sm text-muted underline"
            >
              Back to {originalKey}
            </button>
          )}
          <div
            role="group"
            aria-label="Text size"
            className="flex items-center rounded-lg border border-line-2"
          >
            <button
              type="button"
              aria-label="Smaller text"
              onClick={() => setSizeIdx(Math.max(0, sizeIdx - 1))}
              className="h-11 w-11 text-sm font-semibold"
            >
              A
            </button>
            <button
              type="button"
              aria-label="Larger text"
              onClick={() =>
                setSizeIdx(Math.min(TEXT_SIZES.length - 1, sizeIdx + 1))
              }
              className="h-11 w-11 text-xl font-semibold"
            >
              A
            </button>
          </div>
          <button
            type="button"
            data-tour="cues"
            aria-pressed={cueMode}
            onClick={() => !demo && setCueMode(!cueMode)}
            className={`inline-flex min-h-11 items-center gap-1.5 rounded-lg px-4 ${cueMode ? 'bg-accent font-bold text-on-accent' : 'border border-line-2'}`}
          >
            {cueMode ? 'Done' : 'My cues'}
            {!cueMode && slots.count > 0 && (
              <span className="font-mono text-sm text-amber">
                {slots.count}
              </span>
            )}
          </button>
          <Link
            href={demo ? '#' : `/songs/${song.id}/edit`}
            onClick={demo ? (e) => e.preventDefault() : undefined}
            className="inline-flex min-h-11 items-center rounded-lg border border-line-2 px-4 no-underline"
          >
            Edit
          </Link>
          <button
            type="button"
            data-tour="pdf"
            onClick={() => !demo && setPdfOpen(true)}
            className="inline-flex min-h-11 items-center gap-2 rounded-lg bg-accent px-4 font-bold text-on-accent"
          >
            <DownloadIcon /> PDF
          </button>
        </div>
      </div>
 
      <div className="mt-3 flex flex-wrap items-center gap-x-5 gap-y-2 border-b border-line pb-3 text-sm text-muted">
        {details.map((d, i) => (
          <span key={i}>{d}</span>
        ))}
        {song.youtubeUrl && (
          <a
            href={song.youtubeUrl}
            target="_blank"
            rel="noreferrer"
            className="text-sky"
          >
            Recording ↗
          </a>
        )}
        <span className="flex-1" />
        {editedBy && editedAt && (
          <span>
            {imported ? (
              <>Imported · {shortDate(editedAt)}</>
            ) : (
              <>
                Edited by <strong className="text-text">{editedBy}</strong> ·{' '}
                {shortDate(editedAt)}
              </>
            )}
          </span>
        )}
        {/* A song with no chart yet has no history to show */}
        {versions > 0 && (
          <Link
            data-tour="history"
            href={demo ? '#' : `/songs/${song.id}/history`}
            onClick={demo ? (e) => e.preventDefault() : undefined}
            className="inline-flex min-h-8 items-center rounded-full border border-line-2 px-3 text-text no-underline"
          >
            History · {versions} version{versions === 1 ? '' : 's'}
          </Link>
        )}
      </div>
 
      {song.notes && (
        <p className="mt-3 whitespace-pre-wrap rounded-lg bg-panel px-4 py-3 text-[15px]">
          {song.notes}
        </p>
      )}
 
      {cueMode && (
        <p className="mt-3 rounded-lg border border-amber/60 px-4 py-2 text-sm text-muted">
          Your cues — notes, pictures of a few bars, or notation — sit on the
          chart where you put them. Only you see them; the chart itself doesn’t
          change.
        </p>
      )}
      <div className="mt-5" style={{fontSize: size}}>
        <ChartBody chart={chart} top={slots.top} extra={slots.extra} />
        <ChordPopover />
      </div>
 
      {pdfOpen && (
        <PdfDialog
          title={song.title}
          onClose={() => setPdfOpen(false)}
          baseUrl={`/api/songs/${song.id}/pdf`}
          shownKey={steps % 12 !== 0 ? key : null}
          originalKey={originalKey}
          hasCues={slots.count > 0}
          footnote={`Every page says version ${version}${editedBy ? `, edited by ${editedBy}` : ''}, so an old printout is easy to spot.`}
        />
      )}
    </main>
  )
}
 
export function DownloadIcon() {
  return (
    <svg
      width="18"
      height="18"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2.2"
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
    >
      <path d="M12 4v11" />
      <path d="M7 10l5 5 5-5" />
      <path d="M5 20h14" />
    </svg>
  )
}