-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathfiles.ts
167 lines (142 loc) · 4.36 KB
/
files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import type { DispatchFn, StateProvider } from '~/store/helpers'
import type { Action } from '~/store/actions'
import {
type Notification,
NotificationType,
newNotificationId,
newAddNotificationAction,
newAddNotificationsAction,
} from '~/store/notifications'
import { saveWorkspaceState } from '../config'
import { WorkspaceAction, type FileUpdatePayload, type FilePayload } from '../actions'
import { readFile, dedupFiles } from './utils'
import { type WorkspaceState, defaultFiles } from '../state'
const AUTOSAVE_INTERVAL = 1000
const isAutosaveEnabled = (getState: StateProvider) => {
const { settings } = getState()
return settings.autoSave
}
let saveTimeout: NodeJS.Timeout
const cancelPendingAutosave = () => {
clearTimeout(saveTimeout)
}
const scheduleWorkspaceAutosave = (getState: StateProvider) => {
cancelPendingAutosave()
if (!isAutosaveEnabled(getState)) {
return
}
saveTimeout = setTimeout(() => {
const {
settings: { autoSave },
workspace: { snippet, ...wp },
} = getState()
if (snippet?.id || !autoSave) {
// abort autosave when loaded external snippet.
return
}
saveWorkspaceState(wp)
}, AUTOSAVE_INTERVAL)
}
const fileNamesFromState = (getState: StateProvider) => {
const { workspace } = getState()
return workspace.files ? Object.keys(workspace.files) : []
}
/**
* Reads and imports files to a workspace.
* File names are deduplicated.
*
* @param files
*/
export const dispatchImportFile = (files: FileList) => async (dispatch: DispatchFn, getState: StateProvider) => {
const existingNames = new Set(fileNamesFromState(getState))
const filePayloads: FileUpdatePayload[] = []
const errors: Notification[] = []
// We can't control if user won't select multiple files with identical names.
let dedupFailures = 0
const iter = dedupFiles(files, existingNames, () => dedupFailures++)
for (const file of iter) {
try {
filePayloads.push(await readFile(file))
} catch (err) {
errors.push({
id: newNotificationId(),
type: NotificationType.Error,
title: `Failed to import "${file.name}"`,
description: `${err}`,
canDismiss: true,
})
}
}
if (dedupFailures) {
errors.push({
id: newNotificationId(),
type: NotificationType.Error,
title: 'Error during file import',
description: `Failed to deduplicate ${dedupFailures} identical file names.`,
canDismiss: true,
})
}
if (errors.length > 0) {
dispatch(newAddNotificationsAction(errors))
}
if (filePayloads.length > 0) {
dispatch({
type: WorkspaceAction.ADD_FILE,
payload: filePayloads,
})
}
scheduleWorkspaceAutosave(getState)
}
export const dispatchCreateFile =
(filename: string, content: string) => (dispatch: DispatchFn, getState: StateProvider) => {
const existingNames = new Set(fileNamesFromState(getState))
if (existingNames.has(filename)) {
dispatch(
newAddNotificationAction({
id: newNotificationId(),
type: NotificationType.Error,
title: 'File already exists',
description: `File "${filename}" already exists in workspace.`,
canDismiss: true,
}),
)
return
}
dispatch({
type: WorkspaceAction.ADD_FILE,
payload: [{ filename, content }],
})
scheduleWorkspaceAutosave(getState)
}
export const dispatchRemoveFile = (filename: string) => (dispatch: DispatchFn, getState: StateProvider) => {
dispatch({
type: WorkspaceAction.REMOVE_FILE,
payload: { filename },
})
scheduleWorkspaceAutosave(getState)
}
export const dispatchUpdateFile =
(filename: string, content: string) => (dispatch: DispatchFn, getState: StateProvider) => {
dispatch({
type: WorkspaceAction.UPDATE_FILE,
payload: { filename, content },
})
scheduleWorkspaceAutosave(getState)
}
export const dispatchImportSource = (files: Record<string, string>) => (dispatch: DispatchFn, _: StateProvider) => {
const selectedFile = Object.keys(files)[0]
dispatch<WorkspaceState>({
type: WorkspaceAction.WORKSPACE_IMPORT,
payload: {
selectedFile,
files,
},
})
}
export const newFileSelectAction = (filename: string): Action<FilePayload> => ({
type: WorkspaceAction.SELECT_FILE,
payload: {
filename,
},
})
export const dispatchResetWorkspace = dispatchImportSource(defaultFiles)