import { Block , ComponentTuple , Pages , Specimen , Specimens , State } from '../types' ; type SetState = ( callback : ( state : State ) => State ) => any ; const getActions = ( setState ) => { const actions = { /** Navigate to a given specimen */ setActiveSpecimen ( specimenId ) { setState ( state => ( { ... state , activeView : { type : 'specimen' , specimenId } } ) ) ; } , /** Navigate to a given page */ setActivePage ( pageId ) { setState ( state => ( { ... state , activeView : { type : 'page' , pageId } } ) ) ; } , /** Navigate to specimens area */ navToSpecimens ( ) { setState ( state => ( { ... state , activeView : { type : 'specimen' } } ) ) ; } , /** Navigate to docs area */ navToDocs ( ) { setState ( state => { const pageId = Object . keys ( state . pages || { } ) [ 0 ] ; return { ... state , activeView : { type : 'page' , pageId } } ; } ) ; } , /** Resize for responsive mode */ setFrameWidth ( width ) { setState ( state => ( { ... state , specimenView : { ... state . specimenView , frameWidth : width } } ) ) ; } , setPages ( pages ) { setState ( state => ( { ... state , pages } ) ) ; } , setSpecimens ( specimens ) { setState ( state => ( { ... state , specimens } ) ) ; } , mergeUiConfig ( conf ) { setState ( state => ( { ... state , uiConfig : { ... state . uiConfig , ... conf } } ) ) ; } , addSpecimen ( specimen ) { if ( ! specimen . id ) { throw new Error ( `addSpecimen: 'id' is required` ) ; } setState ( state => ( { ... state , specimens : { ... state . specimens , [ specimen . id ] : specimen } } ) ) ; return ( ) => { actions . removeSpecimen ( specimen . id ) ; } ; } , removeSpecimen ( id ) { setState ( state => { const { specimens } = state ; if ( ! specimens ) return state ; const { [ id ] : _ , ... otherSpecimens } = specimens ; return { ... state , specimens : otherSpecimens } ; } ) ; } , addPage ( { id , view } ) { setState ( state => ( { ... state , pages : { ... state . pages , [ id ] : { id , view } } } ) ) ; } , removePage ( id ) { setState ( state => { const { pages } = state ; if ( ! pages ) return state ; const { [ id ] : _ , ... others } = pages ; return { ... state , pages : others } ; } ) ; } , /**
         * Returns an undo function (useful for useEffect).
         */ addBlock ( { domain , id , priority , view } ) { setState ( state => { const blocksObj = state . blocks || { } ; const domainObj = blocksObj [ domain ] || { } ; const priorityObj = domainObj [ priority ] || { } ; return { ... state , blocks : { ... blocksObj , [ domain ] : { ... domainObj , [ priority ] : { ... priorityObj , [ id ] : view } } } } ; } ) ; const undo = ( ) => { actions . addBlock ( { domain , id , priority , view : null } ) ; } ; return undo ; } , example setData ( domain , update ) { setState ( state => { const data = ( state && state . data ) || { } ; const value = ( data && data [ domain ] ) || { } ; return { ... state , data : { ... data , [ domain ] : update ( value ) } } ; } ) ; } , /** Dangerous passthru for plugins to use */ __setState__ : setState } ; return actions ; } ; export default getActions ;