@@ -3,34 +3,53 @@ import blobUtil from 'blob-util';
33import mime from 'mime' ;
44import { PLAINTEXT_FILE_REGEX } from '../../../server/utils/fileUtils' ;
55
6+ // Define types for File, State, and Action
7+ interface File {
8+ id : string ;
9+ name : string ;
10+ content : string ;
11+ blobUrl ?: string ;
12+ children : string [ ] ;
13+ }
14+
15+ interface State extends Array < File > { }
16+
17+ interface Action {
18+ type : string ;
19+ files ?: File [ ] ;
20+ }
21+
622// https://gist.github.com/fnky/7d044b94070a35e552f3c139cdf80213
7- export function useSelectors ( state , mapStateToSelectors ) {
23+ export function useSelectors < T > (
24+ state : T ,
25+ mapStateToSelectors : ( state : T ) => any
26+ ) {
827 const selectors = useMemo ( ( ) => mapStateToSelectors ( state ) , [ state ] ) ;
928 return selectors ;
1029}
1130
12- export function getFileSelectors ( state ) {
31+ export function getFileSelectors ( state : State ) {
1332 return {
1433 getHTMLFile : ( ) => state . filter ( ( file ) => file . name . match ( / .* \. h t m l $ / i) ) [ 0 ] ,
1534 getJSFiles : ( ) => state . filter ( ( file ) => file . name . match ( / .* \. j s $ / i) ) ,
1635 getCSSFiles : ( ) => state . filter ( ( file ) => file . name . match ( / .* \. c s s $ / i) )
1736 } ;
1837}
1938
20- function sortedChildrenId ( state , children ) {
39+ function sortedChildrenId ( state : State , children : string [ ] ) : string [ ] {
2140 const childrenArray = state . filter ( ( file ) => children . includes ( file . id ) ) ;
2241 childrenArray . sort ( ( a , b ) => ( a . name > b . name ? 1 : - 1 ) ) ;
2342 return childrenArray . map ( ( child ) => child . id ) ;
2443}
2544
26- export function setFiles ( files ) {
45+ export function setFiles ( files : File [ ] ) {
2746 return {
2847 type : 'SET_FILES' ,
2948 files
3049 } ;
3150}
3251
33- export function createBlobUrl ( file ) {
52+ export function createBlobUrl ( file : File ) : string {
3453 if ( file . blobUrl ) {
3554 blobUtil . revokeObjectURL ( file . blobUrl ) ;
3655 }
@@ -43,7 +62,7 @@ export function createBlobUrl(file) {
4362 return blobURL ;
4463}
4564
46- export function createBlobUrls ( state ) {
65+ export function createBlobUrls ( state : State ) : State {
4766 return state . map ( ( file ) => {
4867 if ( file . name . match ( PLAINTEXT_FILE_REGEX ) ) {
4968 const blobUrl = createBlobUrl ( file ) ;
@@ -53,10 +72,10 @@ export function createBlobUrls(state) {
5372 } ) ;
5473}
5574
56- export function filesReducer ( state , action ) {
75+ export function filesReducer ( state : State , action : Action ) : State {
5776 switch ( action . type ) {
5877 case 'SET_FILES' :
59- return createBlobUrls ( action . files ) ;
78+ return createBlobUrls ( action . files || [ ] ) ;
6079 default :
6180 return state . map ( ( file ) => {
6281 file . children = sortedChildrenId ( state , file . children ) ;
0 commit comments