Skip to content

Commit

Permalink
clear appstate and fix for async state loader
Browse files Browse the repository at this point in the history
  • Loading branch information
jgentes committed May 26, 2024
1 parent 0ff28d3 commit 27d2c17
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
68 changes: 38 additions & 30 deletions app/api/models/appState.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,50 @@ const uiState = proxy<UiState>({
modal: { openState: false },
})

// Pull latest persistent state from Dexie and populate Valtio store
let seeded = false
const initialMixState = (await db.appState.get('mixState')) as MixState
const mixState = proxy<MixState>(initialMixState)
let mixState = proxy<MixState>()
let userState = proxy<UserState>()

watch(async get => {
get(mixState)
//@ts-ignore dexie typescript failure
if (seeded) db.appState.put(snapshot(mixState), 'mixState')
})
const clearAppState = async () => await db.appState.clear()

let initialUserState = (await db.appState.get('userState')) as UserState
// ensure that we ref the stemsDirHandle
if (initialUserState?.stemsDirHandle)
initialUserState = {
...initialUserState,
stemsDirHandle: ref(initialUserState.stemsDirHandle),
const initAudioState = async () => {
// Pull latest persistent state from Dexie and populate Valtio store
let seeded = false
const initialMixState = ((await db.appState.get('mixState')) as MixState) || {
tracks: [],
trackState: {},
}
const userState = proxy<UserState>(initialUserState)
mixState = proxy(initialMixState)

watch(async get => {
get(userState)
//@ts-ignore dexie typescript failure
if (seeded) db.appState.put(snapshot(userState), 'userState')
})
watch(async get => {
get(mixState)
//@ts-ignore dexie typescript failure
if (seeded) db.appState.put(snapshot(mixState), 'mixState')
})

seeded = true
let initialUserState = (await db.appState.get('userState')) as UserState
// ensure that we ref the stemsDirHandle
if (initialUserState?.stemsDirHandle)
initialUserState = {
...initialUserState,
stemsDirHandle: ref(initialUserState.stemsDirHandle),
}
userState = proxy(initialUserState)

if (Env === 'development') {
devtools(uiState, { name: 'uiState', enable: true })
devtools(mixState, { name: 'mixState', enable: true })
devtools(userState, { name: 'userState', enable: true })
// audioState waveforms cause memory issues in devtools
}
watch(async get => {
get(userState)
//@ts-ignore dexie typescript failure
if (seeded) db.appState.put(snapshot(userState), 'userState')
})

seeded = true

if (Env === 'development') {
devtools(uiState, { name: 'uiState', enable: true })
devtools(mixState, { name: 'mixState', enable: true })
devtools(userState, { name: 'userState', enable: true })
// audioState waveforms cause memory issues in devtools
}

const initAudioState = async () => {
// Start audioState init (if we have a mix in localstorage (valtio))
const tracks = mixState.tracks

Expand All @@ -80,4 +88,4 @@ const initAudioState = async () => {

initAudioState()

export { uiState, audioState, mixState, userState }
export { clearAppState, uiState, audioState, mixState, userState }
19 changes: 9 additions & 10 deletions app/components/layout/SettingsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@ import {
Tooltip
} from '@nextui-org/react'
import type { Key } from 'react'
import { uiState } from '~/api/models/appState.client'
import { clearAppState, uiState } from '~/api/models/appState.client'
import { SettingsIcon } from '~/components/icons'

const buttonAction = (key: Key) => {
const clearLocalStorage = () => {
localStorage.removeItem('mixState')
localStorage.removeItem('userState')
const clearLocalStorage = async () => {
await clearAppState()
// refresh the page
window.location.reload()
}

switch (key) {
case 'localstorage':
case 'appstate':
clearLocalStorage()
break
case 'indexeddb':
uiState.modal = {
openState: true,
headerText: 'Are you sure?',
bodyText:
'Clearing IndexedDB will remove all tracks, resetting Mixpoint to a fresh state.',
'Clearing IndexedDB will remove all tracks, resetting Mixpoint to a fresh state. No files will be deleted from your computer.',
confirmColor: 'danger',
confirmText: 'Remove everything',
onConfirm: async () => {
Expand Down Expand Up @@ -69,16 +68,16 @@ const SettingsButton = ({ className }: { className?: string }) => (
aria-label="Settings dropdown"
onAction={buttonAction}
>
<DropdownItem key="localstorage" description="Fix issues with mix state">
Clear LocalStorage
<DropdownItem key="appstate" description="Fix issues with mix state">
Clear Mixpoint State
</DropdownItem>
<DropdownItem
key="indexeddb"
className="text-danger"
color="danger"
description="Reset Mixpoint to starting state"
description="Remove Mixpoint data from IndexedDB"
>
Clear IndexedDB
Clear Mixpoint Data
</DropdownItem>
</DropdownMenu>
</Dropdown>
Expand Down
1 change: 0 additions & 1 deletion app/components/tracks/TrackTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const TrackTable = () => {
const [dragOver, setDragOver] = useState(false)

// Retrieve sort state from database

const {
sortDirection = 'descending',
sortColumn = 'lastModified',
Expand Down

0 comments on commit 27d2c17

Please sign in to comment.