import { h, withState, Html } from 'lib/rx'
import * as Options from 'view/options'
import * as Chord from 'chord'
interface ViewParams {
options: Options.Model
}
export function view({ options }: ViewParams): Html {
const initChords: Array<[string, string]> = [['', ''], Chord.generate(options), Chord.generate(options)]
return withState(initChords, chords =>
withState(undefined, beat => {
let chordBeat = 1
const interval = setInterval(() => {
if (chordBeat === options.beatsPerChord) {
chords.update(xs => {
xs.shift()
xs.push(Chord.generate(options))
return xs
})
chordBeat = 1
} else {
beat.update(_ => undefined)
chordBeat += 1
}
}, 60 / options.bpm * 1000)
return h('div',
{ className: 'g-Play',
onunmount: () => clearInterval(interval)
},
chords.map(xs =>
h('div',
{ className: 'g-Chords' },
xs.map((chord, i) => {
if (i == 0) {
return viewChord(chord, 'g-Chord--Disappear')
} else if (i == 1) {
return beat.map(_ => viewChord(chord))
} else {
return viewChord(chord)
}
})
)
)
)
})
)
}
function viewChord([base, alteration]: [string, string], className: string = ''): Html {
return h('div',
{ className: `g-Chord ${className}` },
base,
h('sup', alteration)
)
}