-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): create "save-combo" button
- Loading branch information
Showing
3 changed files
with
95 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useState } from 'react' | ||
import { faker } from '@faker-js/faker' | ||
|
||
import { useComboStore } from '@/stores/combo-store' | ||
import { useSoundsStateStore } from '@/stores/sounds-state-store' | ||
import { useThemeStore } from '@/stores/theme-store' | ||
|
||
import { actionButton } from '@/shared/styles/action-button' | ||
import { input } from './styles' | ||
import { FiCheck } from 'react-icons/fi' | ||
|
||
export function SaveComboButton() { | ||
const sounds = useSoundsStateStore(state => state.sounds) | ||
const theme = useThemeStore(state => state.theme) | ||
const saveCombo = useComboStore(state => state.saveCombo) | ||
|
||
const [comboName, setComboName] = useState('') | ||
const [showSuccess, setShowSuccess] = useState(false) | ||
|
||
function save() { | ||
if (!comboName) return | ||
if (!sounds.some(sound => sound.active)) return | ||
|
||
const activeSounds = sounds.filter(sound => sound.active) | ||
|
||
saveCombo({ | ||
id: faker.string.alphanumeric({ casing: 'lower', length: 6 }), | ||
name: comboName, | ||
sounds: activeSounds | ||
}) | ||
|
||
setComboName('') | ||
setShowSuccess(true) | ||
|
||
setTimeout(() => setShowSuccess(false), 2000) | ||
} | ||
|
||
const disabled = showSuccess || !sounds.some(sound => sound.active) | ||
|
||
return ( | ||
<div className="flex gap-1"> | ||
{sounds.some(sound => sound.active) && ( | ||
<input | ||
disabled={disabled} | ||
type="text" | ||
placeholder="combo name..." | ||
value={comboName} | ||
onChange={e => setComboName(e.target.value)} | ||
className={input({ theme })} | ||
/> | ||
)} | ||
<button | ||
disabled={disabled} | ||
onClick={save} | ||
className={actionButton({ | ||
theme, | ||
className: /*tw:*/ 'flex items-center' | ||
})} | ||
title="Save current combo" | ||
> | ||
{showSuccess ? ( | ||
<span className="inline-flex w-10 justify-center"> | ||
<FiCheck /> | ||
</span> | ||
) : ( | ||
<span className="inline-flex w-10 justify-center">save</span> | ||
)} | ||
</button> | ||
</div> | ||
) | ||
} |
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,20 @@ | ||
import { tv } from 'tailwind-variants' | ||
|
||
export const input = tv({ | ||
base: /*tw:*/ 'form-input rounded-xl text-white bg-white/5 px-2 py-0 border-none focus:ring-0 leading-none tracking-wider duration-300 w-32 text-center animate-show-input placeholder:text-sm opacity-30 hover:opacity-100 focus:opacity-100', | ||
variants: { | ||
theme: { | ||
transition: /*tw:*/ '', | ||
dark: /*tw:*/ 'text-dark-foreground placeholder:text-dark-foreground/60 bg-dark-foreground/5', | ||
light: | ||
/*tw:*/ 'text-light-foreground placeholder:text-light-foreground/60 bg-light-foreground/5', | ||
'blue-room': | ||
/*tw:*/ 'text-blue-room placeholder:text-blue-room/60 bg-blue-room/5', | ||
train: /*tw:*/ 'text-train placeholder:text-train/60 bg-train/5', | ||
waterfall: | ||
/*tw:*/ 'text-waterfall placeholder:text-waterfall/60 bg-waterfall/5', | ||
'camping-fire': | ||
/*tw:*/ 'text-camping-fire placeholder:text-camping-fire/60 bg-camping-fire/5' | ||
} | ||
} | ||
}) |