Skip to content

Commit b3999e7

Browse files
committed
filesReducer:update to TS, no-verify
1 parent e7357d3 commit b3999e7

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed
Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,53 @@ import blobUtil from 'blob-util';
33
import mime from 'mime';
44
import { 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(/.*\.html$/i))[0],
1534
getJSFiles: () => state.filter((file) => file.name.match(/.*\.js$/i)),
1635
getCSSFiles: () => state.filter((file) => file.name.match(/.*\.css$/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);

package-lock.json

Lines changed: 37 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,11 @@
136136
"@testing-library/jest-dom": "^5.15.0",
137137
"@testing-library/react": "^12.1.2",
138138
"@types/bcryptjs": "^2.4.6",
139+
"@types/blob-util": "^1.3.3",
139140
"@types/classnames": "^2.3.0",
140141
"@types/friendly-words": "^1.2.2",
141142
"@types/jest": "^29.5.14",
143+
"@types/mime": "^3.0.4",
142144
"@types/mjml": "^4.7.4",
143145
"@types/node": "^16.18.126",
144146
"@types/nodemailer": "^7.0.1",

0 commit comments

Comments
 (0)