2021-01-21 18:11:37 +00:00
|
|
|
import { useState, useEffect, useCallback } from 'react'
|
|
|
|
|
2021-01-23 17:44:39 +00:00
|
|
|
function FrameChooser({onChange}) {
|
2021-01-21 18:11:37 +00:00
|
|
|
const [frames, setFrames] = useState([])
|
|
|
|
const [choosenFrame, setChoosenFrame] = useState(null)
|
|
|
|
|
2021-04-20 22:00:34 +00:00
|
|
|
const choosenFrameSRC = !!choosenFrame ? choosenFrame.src : null
|
|
|
|
|
2021-01-21 18:11:37 +00:00
|
|
|
useEffect(() => {
|
|
|
|
async function loadFrames(){
|
|
|
|
Promise.all(
|
|
|
|
[
|
2021-04-20 22:55:50 +00:00
|
|
|
'0xSOM', // SOM = Stripes of Movement
|
2021-06-23 16:16:31 +00:00
|
|
|
'5xSOM-Pride',
|
2021-04-20 22:55:50 +00:00
|
|
|
'4xSOM-Blue-Red-Yellow-Green',
|
2021-09-09 16:59:43 +00:00
|
|
|
'Volt-Stars',
|
2021-04-20 22:55:50 +00:00
|
|
|
'5xSOM-Blue',
|
|
|
|
'5xSOM-Green',
|
|
|
|
'5xSOM-Red',
|
|
|
|
'5xSOM-Yellow',
|
|
|
|
// '5xSOM-White',
|
|
|
|
// '2xSOM-Blue-Green',
|
|
|
|
// '2xSOM-Red-Yellow',
|
2021-01-21 18:11:37 +00:00
|
|
|
]
|
|
|
|
.map(async frame_filename => {
|
|
|
|
return {
|
|
|
|
name: frame_filename,
|
2021-04-20 22:00:34 +00:00
|
|
|
src: (await import(`./frames/${frame_filename}.png`)).default,
|
2021-01-21 18:11:37 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.then(new_frames => {
|
|
|
|
setFrames(new_frames)
|
2021-04-20 22:00:34 +00:00
|
|
|
setChoosenFrame(new_frames[0])
|
2021-01-21 18:11:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
loadFrames()
|
|
|
|
}, [])
|
|
|
|
|
2021-04-20 22:00:34 +00:00
|
|
|
const handleImageChoosing = useCallback(frame => {
|
|
|
|
setChoosenFrame(frame)
|
2021-01-21 18:11:37 +00:00
|
|
|
}, [setChoosenFrame])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-01-23 17:44:39 +00:00
|
|
|
onChange(choosenFrame)
|
|
|
|
}, [onChange, choosenFrame])
|
2021-01-21 18:11:37 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="FrameChooser">
|
|
|
|
{
|
|
|
|
frames.map(frame => {
|
2021-04-20 22:00:34 +00:00
|
|
|
const frame_src_path = frame.src
|
|
|
|
const isChoosen = choosenFrameSRC === frame_src_path
|
2021-01-23 20:46:44 +00:00
|
|
|
return <div
|
|
|
|
key={frame_src_path}
|
|
|
|
data-src={frame_src_path}
|
|
|
|
className={isChoosen ? 'frame choosen' : 'frame'}
|
2021-04-20 22:00:34 +00:00
|
|
|
onClick={() => handleImageChoosing(frame)}
|
2021-01-23 20:46:44 +00:00
|
|
|
>
|
|
|
|
<img alt={frame.name} src={frame_src_path} />
|
|
|
|
</div>
|
2021-01-21 18:11:37 +00:00
|
|
|
})
|
2021-04-20 21:39:24 +00:00
|
|
|
}
|
2021-01-21 18:11:37 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-21 21:14:53 +00:00
|
|
|
export default FrameChooser
|