feat: add autoplay switch
This commit is contained in:
parent
aa24ad83e5
commit
7b882c72cb
21 changed files with 228 additions and 81 deletions
32
src/providers/AutoplayProvider.tsx
Normal file
32
src/providers/AutoplayProvider.tsx
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { createContext, useContext, useState } from 'react'
|
||||
import storage from '@/services/local-storage.service'
|
||||
|
||||
type TAutoplayContext = {
|
||||
autoplay: boolean
|
||||
setAutoplay: (autoplay: boolean) => void
|
||||
}
|
||||
|
||||
const AutoplayContext = createContext<TAutoplayContext | undefined>(undefined)
|
||||
|
||||
export const useAutoplay = () => {
|
||||
const context = useContext(AutoplayContext)
|
||||
if (!context) {
|
||||
throw new Error('useAutoplay must be used within an AutoplayProvider')
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
export function AutoplayProvider({ children }: { children: React.ReactNode }) {
|
||||
const [autoplay, setAutoplay] = useState<boolean>(storage.getAutoplay())
|
||||
|
||||
const updateAutoplay = (autoplay: boolean) => {
|
||||
storage.setAutoplay(autoplay)
|
||||
setAutoplay(autoplay)
|
||||
}
|
||||
|
||||
return (
|
||||
<AutoplayContext.Provider value={{ autoplay, setAutoplay: updateAutoplay }}>
|
||||
{children}
|
||||
</AutoplayContext.Provider>
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue