Skip to content

Commit

Permalink
chore: add settings saving
Browse files Browse the repository at this point in the history
  • Loading branch information
ratmirslv committed Jan 30, 2024
1 parent b07ffb6 commit 36f1680
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
15 changes: 13 additions & 2 deletions components/ModalOpenMedia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Modal, Group, Button, SimpleGrid, FileInput, Select } from '@mantine/co
import { useForm } from '@mantine/form'
import { useDisclosure } from '@mantine/hooks'
import ISO6391 from 'iso-639-1'
import { useEffect } from 'react'

import { useLatestRef } from '@/hooks/useLatestRef'
import { usePlayerStore } from '@/stores/usePlayerStore'
import { convertToVTT } from '@/utils/convertToVTT'

Expand All @@ -26,8 +28,8 @@ export function ModalOpenMedia() {
initialValues: {
video: null,
subtitle: null,
userLanguage: content.userLanguage,
subtitleLanguage: content.subtitleLanguage,
userLanguage: '',
subtitleLanguage: '',
},
validate: {
video: value => (value ? null : 'You should choose video'),
Expand All @@ -36,6 +38,15 @@ export function ModalOpenMedia() {
},
})

const latestFormRef = useLatestRef(form)

useEffect(() => {
latestFormRef.current.setValues({
userLanguage: content.userLanguage,
subtitleLanguage: content.subtitleLanguage,
})
}, [content.subtitleLanguage, content.userLanguage, latestFormRef])

return (
<>
<Modal opened={opened} onClose={close} title="Open media" centered>
Expand Down
9 changes: 9 additions & 0 deletions hooks/useLatestRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as React from 'react'

export function useLatestRef<T>(value: T) {
const ref = React.useRef(value)
React.useEffect(() => {
ref.current = value
})
return ref
}
7 changes: 7 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ import { ColorScheme, ColorSchemeProvider, MantineProvider } from '@mantine/core
import { useLocalStorage } from '@mantine/hooks'
import { Notifications } from '@mantine/notifications'
import type { AppProps } from 'next/app'
import { useEffect } from 'react'

import { Layout } from '@/components/Layout'
import { usePlayerStore } from '@/stores/usePlayerStore'

export default function App({ Component, pageProps }: AppProps) {
useEffect(() => {
usePlayerStore.persist.rehydrate()
}, [])

const [colorScheme, setColorScheme] = useLocalStorage<ColorScheme>({
key: 'mantine-color-scheme',
defaultValue: 'dark',
getInitialValueInEffect: true,
})

const toggleColorScheme = (value?: ColorScheme) =>
setColorScheme(value || (colorScheme === 'dark' ? 'light' : 'dark'))

Expand Down
54 changes: 35 additions & 19 deletions stores/usePlayerStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'

interface ContentPlayer {
video: string | null
Expand All @@ -12,27 +13,42 @@ interface PlayerStore {
setPlayerContent: (content: ContentPlayer) => void
}

export const usePlayerStore = create<PlayerStore>()((set, get) => ({
content: {
video: null,
subtitle: null,
userLanguage: '',
subtitleLanguage: '',
},
setPlayerContent: content =>
set(state => {
const prevVideo = get().content.video
const prevSubtitle = get().content.subtitle
export const usePlayerStore = create<PlayerStore>()(
persist(
(set, get) => ({
content: {
video: null,
subtitle: null,
userLanguage: '',
subtitleLanguage: '',
},
setPlayerContent: content =>
set(state => {
const prevVideo = get().content.video
const prevSubtitle = get().content.subtitle

if (prevVideo) URL.revokeObjectURL(prevVideo)
if (prevVideo) URL.revokeObjectURL(prevVideo)

if (prevSubtitle) URL.revokeObjectURL(prevSubtitle)
if (prevSubtitle) URL.revokeObjectURL(prevSubtitle)

return {
return {
content: {
...state.content,
...content,
},
}
}),
}),
{
name: 'player-storage',
skipHydration: true,
partialize: (store: PlayerStore) => ({
content: {
...state.content,
...content,
userLanguage: store.content.userLanguage,
subtitleLanguage: store.content.subtitleLanguage,
},
}
}),
}))
}),
storage: createJSONStorage(() => localStorage),
},
),
)

0 comments on commit 36f1680

Please sign in to comment.