feat: add toggle to show NSFW content by default

This commit is contained in:
codytseng 2025-08-14 22:56:44 +08:00
parent 352eecc416
commit cb2ad30b1d
23 changed files with 108 additions and 55 deletions

View file

@ -1,32 +0,0 @@
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>
)
}

View file

@ -0,0 +1,48 @@
import storage from '@/services/local-storage.service'
import { createContext, useContext, useState } from 'react'
type TContentPolicyContext = {
autoplay: boolean
setAutoplay: (autoplay: boolean) => void
defaultShowNsfw: boolean
setDefaultShowNsfw: (showNsfw: boolean) => void
}
const ContentPolicyContext = createContext<TContentPolicyContext | undefined>(undefined)
export const useContentPolicy = () => {
const context = useContext(ContentPolicyContext)
if (!context) {
throw new Error('useContentPolicy must be used within an ContentPolicyProvider')
}
return context
}
export function ContentPolicyProvider({ children }: { children: React.ReactNode }) {
const [autoplay, setAutoplay] = useState<boolean>(storage.getAutoplay())
const [defaultShowNsfw, setDefaultShowNsfw] = useState<boolean>(storage.getDefaultShowNsfw())
const updateAutoplay = (autoplay: boolean) => {
storage.setAutoplay(autoplay)
setAutoplay(autoplay)
}
const updateDefaultShowNsfw = (defaultShowNsfw: boolean) => {
storage.setDefaultShowNsfw(defaultShowNsfw)
setDefaultShowNsfw(defaultShowNsfw)
}
return (
<ContentPolicyContext.Provider
value={{
autoplay,
setAutoplay: updateAutoplay,
defaultShowNsfw,
setDefaultShowNsfw: updateDefaultShowNsfw
}}
>
{children}
</ContentPolicyContext.Provider>
)
}