11import { Store } from 'pinia' ;
22import { v4 as uuid } from 'uuid' ;
3+ import { entries } from 'idb-keyval' ;
34import { fileDialog } from 'file-select-dialog' ;
45import { has , head , sortBy , debounce , startsWith , cloneDeep } from 'lodash' ;
56import useCurrentTab from './useCurrentTab' ;
@@ -9,8 +10,10 @@ import { computed, ref, useContext } from '@nuxtjs/composition-api';
910
1011export const namespace = 'pages/' ;
1112
12- const getPagesFromStorage = ( ) => {
13- return Object . keys ( window . localStorage ) . filter ( ( key ) => key . startsWith ( namespace ) ) ;
13+ const getPagesFromStorage = async ( ) => {
14+ return ( await entries ( ) )
15+ . filter ( ( [ key ] ) => key . startsWith ( namespace ) )
16+ . map ( ( [ key , value ] ) => [ key , JSON . parse ( value ) ] ) ;
1417} ;
1518
1619export default function ( ) {
@@ -38,17 +41,18 @@ export default function () {
3841 * Make a new project store.
3942 *
4043 * @param {String } id
44+ * @param {Object|null } initialValue
4145 *
4246 * @returns {Store }
4347 */
44- const makeProjectStore = ( id = null ) => {
48+ const makeProjectStore = ( id = null , initialValue = null ) => {
4549 id = id ?? uuid ( ) ;
4650
4751 const name = startsWith ( id , namespace ) ? id : namespace + id ;
4852
49- const factory = useProjectStoreFactory ( name ) ;
53+ const make = useProjectStoreFactory ( name , initialValue ) ;
5054
51- const store = factory ( ) ;
55+ const store = make ( ) ;
5256
5357 store . load ( ) ;
5458
@@ -195,8 +199,10 @@ export default function () {
195199 /**
196200 * Hydrate the projects from local storage.
197201 */
198- const hydrateFromStorage = ( ) => {
199- const stored = getPagesFromStorage ( ) . map ( ( id ) => makeProjectStore ( id ) ) ;
202+ const hydrateFromStorage = async ( ) => {
203+ const stored = ( await getPagesFromStorage ( ) ) . map ( ( [ key , value ] ) =>
204+ makeProjectStore ( key , value )
205+ ) ;
200206
201207 projects . value = sortBy ( stored , ( { tab } ) => tab . order ?? tab . created_at ) ;
202208 } ;
0 commit comments