You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
12 lines
1.6 KiB
12 lines
1.6 KiB
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = void 0;
|
|
|
|
var _default = function _default(relEntryFile) {
|
|
return relEntryFile ? "import { useState, useEffect, useCallback } from 'react';\nimport { Models } from '../../plugin-model/useModel';\nimport * as app from '../../../app';\n\nexport type InitialState = Models<'@@initialState'>;\nasync function getInitialState() {\n return await app.getInitialState();\n}\n\ntype ThenArg<T> = T extends Promise<infer U> ? U : T\n\nconst initState = {\n initialState: undefined as ThenArg<ReturnType<typeof getInitialState>> | undefined,\n loading: true,\n error: undefined as Error | undefined,\n}\n\nexport default () => {\n const [ state, setState ] = useState(initState);\n\n const refresh = useCallback(async() => {\n setState(s => ({ ...s, loading: true, error: undefined }))\n try {\n const asyncFunc = () => new Promise<ReturnType<typeof getInitialState>>(res => res(getInitialState()));\n const ret = await asyncFunc();\n setState(s => ({ ...s, initialState: ret, loading: false }));\n } catch(e) {\n setState(s => ({ ...s, error: e, loading: false }));\n }\n }, []);\n\n const setInitialState = useCallback((initialState: ThenArg<ReturnType<typeof getInitialState>> | undefined) => {\n setState(s => ({ ...s, initialState, loading: false }))\n }, []);\n\n useEffect(()=>{\n refresh();\n }, []);\n\n return {\n ...state,\n refresh,\n setInitialState,\n }\n}\n" : 'export default () => ({ loading: false, refresh: () => {} })';
|
|
};
|
|
|
|
exports.default = _default; |