-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from klay2000/presets
Presets
- Loading branch information
Showing
19 changed files
with
564 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import './PresetsModal.scss' | ||
import Modal from '@/components/Modal/Modal' | ||
import Card from '@/components/Card/Card' | ||
import { useStatusStore } from '@/App' | ||
import { useState } from 'react' | ||
|
||
const timeSince = (timeStamp) => { | ||
var now = new Date(), | ||
secondsPast = (now.getTime() - timeStamp) / 1000; | ||
if (secondsPast < 60) { | ||
return parseInt(secondsPast) + 's'; | ||
} | ||
if (secondsPast < 3600) { | ||
return parseInt(secondsPast / 60) + 'm'; | ||
} | ||
if (secondsPast <= 86400) { | ||
return parseInt(secondsPast / 3600) + 'h'; | ||
} | ||
if (secondsPast > 86400) { | ||
let day = timeStamp.getDate(); | ||
let month = timeStamp.toDateString().match(/ [a-zA-Z]*/)[0].replace(" ", ""); | ||
let year = timeStamp.getFullYear() == now.getFullYear() ? "" : " " + timeStamp.getFullYear(); | ||
return day + " " + month + year; | ||
} | ||
} | ||
|
||
const PresetItem = ({ index, onClick, presetState }) => { | ||
const preset = useStatusStore((state) => state.status.presets[index]) | ||
const name = preset.name | ||
const last_used = timeSince(new Date(preset.last_used * 1000)) // js expects milliseconds from epoch | ||
// const last_used = 0 | ||
|
||
// show checkmark if presetState is 'done' | ||
// show spinner if presetState is 'loading' | ||
return ( | ||
<div className="preset-item" onClick={() => onClick(index)}> | ||
<div className="preset-name-and-last-used"> | ||
<div>{name}</div> | ||
<div className="preset-item-last-used">{last_used}</div> | ||
</div> | ||
|
||
{presetState === 'done' && <div className="preset-item-icon">✓</div>} | ||
{presetState === 'loading' && <div className="preset-item-icon">⏳</div>} | ||
</div> | ||
) | ||
} | ||
|
||
|
||
const PresetsModal = ({ onClose }) => { | ||
const presets = useStatusStore((state) => state.status.presets) | ||
const [presetStates, setPresetStates] = useState(presets.map((preset) => false)) | ||
|
||
// resize presetStates (without overriding) if length changes | ||
if (presetStates.length > presets.length) { | ||
setPresetStates(presetStates.slice(0, presets.length)) | ||
} else if (presetStates.length < presets.length) { | ||
setPresetStates([...presetStates, ...Array(presets.length - presetStates.length).fill(false)]) | ||
} | ||
|
||
const apply = (index) => { | ||
setPresetStates(presetStates.map((state, i) => (i === index ? 'loading' : state))) | ||
const id = presets[index].id | ||
console.log(`/api/presets/${id}/load`) | ||
fetch(`/api/presets/${id}/load`, { method: 'POST', accept: 'application/json' }) | ||
.then(() => setPresetStates(presetStates.map((state, i) => (i === index ? 'done' : state)))) | ||
.catch(() => setPresetStates(presetStates.map((state, i) => (i === index ? false : state)))) | ||
} | ||
|
||
const presetItems = presets.map((preset, index) => ( | ||
<PresetItem | ||
key={index} | ||
index={index} | ||
onClick={apply} | ||
presetState={presetStates[index]} | ||
/> | ||
)) | ||
|
||
return ( | ||
<Modal className="presets-modal" onClose={onClose}> | ||
<Card className="presets-modal-card"> | ||
<div className="presets-modal-header"> | ||
Select Preset | ||
</div> | ||
<div className="presets-modal-body"> | ||
{presetItems} | ||
</div> | ||
</Card> | ||
</Modal> | ||
) | ||
} | ||
|
||
export default PresetsModal | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
@use "src/general.scss"; | ||
|
||
.presets-modal{ | ||
@include general.header-font; | ||
font-size: 1.5rem; | ||
color: general.$text-color; | ||
} | ||
|
||
.presets-modal-card{ | ||
max-height: 75vh; | ||
overflow: hidden; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.presets-modal-header { | ||
margin: 0.5rem; | ||
font-size: 3rem; | ||
text-align: center; | ||
} | ||
|
||
.presets-modal-body { | ||
padding: 1rem; | ||
overflow-y: scroll; | ||
//the below is to push the scrollbar out of the card to hide it | ||
padding-right: 16px; | ||
width: 100%; | ||
height: 100%; | ||
} | ||
|
||
.presets-modal-list-item{ | ||
margin-bottom: 1rem; | ||
} | ||
|
||
.preset-item { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
// align-items: center; | ||
// padding: 0.5rem; | ||
// border: 1px solid #000000; | ||
// border-radius: 5px; | ||
// margin-bottom: 0.5rem; | ||
} | ||
|
||
.preset-name-and-last-used { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: flex-start; | ||
} | ||
|
||
.preset-item-last-used { | ||
// just want to make the font smaller | ||
color: general.$select-color-gray; | ||
font-size: 1.25rem; | ||
padding-left: 8px; | ||
} | ||
|
||
.preset-item-icon { | ||
font-size: 1.5rem; | ||
padding-right: 2rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.