UNPKG

4.31 kBTypeScriptView Raw
1import React, { useState, useEffect } from 'react'
2import { listFiles } from './storage-libs';
3import { withStorageSsrRendering } from '../components/attach-storage';
4import ExecutionEnvironment from 'exenv';
5//import hash from 'object-hash';
6
7export interface IFilesList {
8 children: any,
9 storageId: string,
10 prefix: string | undefined,
11 mode: string,
12 data: any,
13 onSetRefetch?: (refetch: ()=> any) => void
14};
15
16const STATE = {
17 //UNDEFINED: "UNDEFINED",
18 LOADING: "LOADING",
19 //SSR: "SSR",
20 ERROR: "ERROR",
21 RESPONSE: "RESPONSE"
22}
23
24export default withStorageSsrRendering(function ({renderSsr, config, isOffline, preloadedFiles, renderListResults, ...props}) {
25
26
27 const hashValue = require("infrastructure-components").hash({
28 storageId: props.storageId,
29 prefix: props.prefix ? props.prefix : "",
30 mode: props.mode,
31 data: props.data
32 });
33
34 //console.log("hashValue: ", hashValue);
35
36 const [state, setState] = useState({state: STATE.LOADING, data: undefined, setRefetch: true, ignorePreloaded: false});
37
38 const refetch = () => {
39 setState({state: STATE.LOADING, data: undefined, setRefetch: true, ignorePreloaded: true});
40 };
41
42 const loadData = (onSuccess, onError) => {
43 listFiles(
44 props.storageId,
45 props.prefix ? props.prefix : "",
46 props.mode,
47 props.data,
48 onSuccess,
49 onError,
50 config,
51 isOffline
52 );
53 }
54
55 const getPreloaded = () => {
56 if (state.ignorePreloaded) {
57 return undefined;
58 }
59
60 if (!ExecutionEnvironment.canUseDOM) {
61
62 if (renderListResults) {
63 const foundVal = renderListResults.find(el => el.hashValue == hashValue);
64 if (foundVal) {
65 return foundVal;
66 }
67 }
68
69 if (state.state === STATE.LOADING) {
70 renderSsr(loadData, hashValue);
71 return undefined;
72 }
73
74 } else {
75 return preloadedFiles !== undefined ? preloadedFiles.find(el => el.hashValue == hashValue) : undefined;
76 }
77
78 return undefined;
79 };
80
81 const preloaded = getPreloaded();
82 //console.log("preloaded: ", preloaded);
83
84
85 if (preloaded && state.state === STATE.LOADING) {
86 setState({
87 state: STATE.RESPONSE,
88 data: {
89 data: preloaded.data,
90 files: preloaded.files,
91 },
92 setRefetch: state.setRefetch,
93 ignorePreloaded: state.ignorePreloaded
94 });
95 };
96
97 //console.log("state: ", state.data);
98
99 useEffect(() => {
100 if (props.onSetRefetch && state.setRefetch /*state.state === STATE.LOADING /*!isRefetchSet*/) {
101 props.onSetRefetch(()=>refetch);
102 }
103
104 if (state.state === STATE.LOADING) {
105 loadData(
106 ({data, files, folders}) => {
107 //console.log(data, files, folders)
108 setState({
109 state: STATE.RESPONSE,
110 data: {
111 data: data,
112 files: files,
113 },
114 setRefetch: false,
115 ignorePreloaded: state.ignorePreloaded
116 });
117 },
118 (err) => {
119 setState({
120 state: STATE.RESPONSE,
121 data: err,
122 setRefetch: false,
123 ignorePreloaded: state.ignorePreloaded
124 });
125
126 });
127 } else if (state.setRefetch) {
128 setState({
129 state: state.state,
130 data: state.data,
131 setRefetch: false,
132 ignorePreloaded: state.ignorePreloaded
133 });
134 }
135
136 }, [state]);
137
138
139
140
141 return props.children({
142 loading: state.state === STATE.LOADING /*|| state.state === STATE.SSR*/,
143 data: state.state === STATE.RESPONSE ? state.data.data : undefined,
144 files: state.state === STATE.RESPONSE ? state.data.files : undefined,
145 error: state.state === STATE.ERROR ? state.data : undefined,
146 });
147});
\No newline at end of file