User can now choose to override browser's state from that of disk's, or vice versa

This commit is contained in:
Aria Minaei 2021-08-09 22:34:56 +02:00
parent f08d9bf9c1
commit 93f4500b20
5 changed files with 75 additions and 19 deletions

View file

@ -83,7 +83,7 @@ export default async function initialiseProjectState(
} else { } else {
if ( if (
browserState.revisionHistory.indexOf( browserState.revisionHistory.indexOf(
onDiskState.revisionHistory[onDiskState.revisionHistory.length - 1], onDiskState.revisionHistory[0],
) == -1 ) == -1
) { ) {
browserStateIsNotBasedOnDiskState(onDiskState) browserStateIsNotBasedOnDiskState(onDiskState)

View file

@ -27,8 +27,8 @@ import type {Store} from 'redux'
import {persistStateOfStudio} from './persistStateOfStudio' import {persistStateOfStudio} from './persistStateOfStudio'
import {isSheetObject} from '@theatre/shared/instanceTypes' import {isSheetObject} from '@theatre/shared/instanceTypes'
import globals from '@theatre/shared/globals' import globals from '@theatre/shared/globals'
import {nanoid} from 'nanoid'
import type {OnDiskState} from '@theatre/core/projects/store/storeTypes' import type {OnDiskState} from '@theatre/core/projects/store/storeTypes'
import {generateDiskStateRevision} from './generateDiskStateRevision'
export type Drafts = { export type Drafts = {
historic: Draft<StudioHistoricState> historic: Draft<StudioHistoricState>
@ -273,17 +273,14 @@ export default class StudioStore {
} }
createExportedStateOfProject(projectId: string): OnDiskState { createExportedStateOfProject(projectId: string): OnDiskState {
const revision = nanoid(16) const revision = generateDiskStateRevision()
// let's assume projectId is already loaded // let's assume projectId is already loaded
this.tempTransaction(({drafts}) => { this.tempTransaction(({stateEditors}) => {
const state = drafts.historic.coreByProject[projectId] stateEditors.coreByProject.historic.revisionHistory.add({
projectId,
const maxNumOfRevisionsToKeep = 50 revision,
state.revisionHistory.unshift(revision) })
if (state.revisionHistory.length > maxNumOfRevisionsToKeep) {
state.revisionHistory.length = maxNumOfRevisionsToKeep
}
}).commit() }).commit()
const projectHistoricState = const projectHistoricState =

View file

@ -0,0 +1,5 @@
import {nanoid} from 'nanoid'
export function generateDiskStateRevision() {
return nanoid(16)
}

View file

@ -1,5 +1,8 @@
import type Project from '@theatre/core/projects/Project' import type Project from '@theatre/core/projects/Project'
import {val} from '@theatre/dataverse'
import {usePrism} from '@theatre/dataverse-react'
import getStudio from '@theatre/studio/getStudio' import getStudio from '@theatre/studio/getStudio'
import {generateDiskStateRevision} from '@theatre/studio/StudioStore/generateDiskStateRevision'
import React, {useCallback, useState} from 'react' import React, {useCallback, useState} from 'react'
const ProjectDetails: React.FC<{ const ProjectDetails: React.FC<{
@ -7,6 +10,42 @@ const ProjectDetails: React.FC<{
}> = ({projects}) => { }> = ({projects}) => {
const project = projects[0] const project = projects[0]
const projectId = project.address.projectId
const nn = usePrism(() => {
const loadingState = val(
getStudio().atomP.ephemeral.coreByProject[projectId].loadingState,
)
if (!loadingState) return
if (loadingState.type === 'browserStateIsNotBasedOnDiskState') {
const useBrowserState = () => {
getStudio().transaction(({stateEditors}) => {
stateEditors.coreByProject.historic.revisionHistory.add({
projectId,
revision: loadingState.onDiskState.revisionHistory[0],
})
stateEditors.coreByProject.historic.revisionHistory.add({
projectId,
revision: generateDiskStateRevision(),
})
})
}
const useOnDiskState = () => {
getStudio().transaction(({drafts}) => {
drafts.historic.coreByProject[projectId] = loadingState.onDiskState
})
}
return (
<div>
Browser state is not based on disk state.
<button onClick={useBrowserState}>Use browser's state</button>
<button onClick={useOnDiskState}>Use disk state</button>
</div>
)
}
}, [project])
const [downloaded, setDownloaded] = useState(false) const [downloaded, setDownloaded] = useState(false)
const exportProject = useCallback(() => { const exportProject = useCallback(() => {
@ -35,14 +74,17 @@ const ProjectDetails: React.FC<{
}, []) }, [])
return ( return (
<div> <>
<button {nn}
onClick={!downloaded ? exportProject : undefined} <div>
disabled={downloaded} <button
> onClick={!downloaded ? exportProject : undefined}
Export project {downloaded ? 'Done' : ''} disabled={downloaded}
</button> >
</div> Export project {downloaded ? 'Done' : ''}
</button>
</div>
</>
) )
} }

View file

@ -363,6 +363,18 @@ namespace stateEditors {
} }
export namespace coreByProject { export namespace coreByProject {
export namespace historic { export namespace historic {
export namespace revisionHistory {
export function add(p: ProjectAddress & {revision: string}) {
const revisionHistory =
drafts().historic.coreByProject[p.projectId].revisionHistory
const maxNumOfRevisionsToKeep = 50
revisionHistory.unshift(p.revision)
if (revisionHistory.length > maxNumOfRevisionsToKeep) {
revisionHistory.length = maxNumOfRevisionsToKeep
}
}
}
export namespace sheetsById { export namespace sheetsById {
export function _ensure( export function _ensure(
p: WithoutSheetInstance<SheetAddress>, p: WithoutSheetInstance<SheetAddress>,