Skip to content

Commit

Permalink
feat: file backups (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz authored May 12, 2022
1 parent b671ecb commit 942226e
Show file tree
Hide file tree
Showing 20 changed files with 499 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ type Props = {
onClickPreprocessing?: () => Promise<void>
}

const isHandlingFileDrag = (event: DragEvent) =>
event.dataTransfer?.items && Array.from(event.dataTransfer.items).some((item) => item.kind === 'file')
const isHandlingFileDrag = (event: DragEvent, application: WebApplication) => {
const items = event.dataTransfer?.items

if (!items) {
return false
}

return Array.from(items).some((item) => {
const isFile = item.kind === 'file'
const fileName = item.getAsFile()?.name || ''
const isBackupMetadataFile = application.files.isFileNameFileBackupMetadataFile(fileName)
return isFile && !isBackupMetadataFile
})
}

export const AttachedFilesButton: FunctionComponent<Props> = observer(
({ application, appState, onClickPreprocessing }) => {
Expand Down Expand Up @@ -234,16 +246,19 @@ export const AttachedFilesButton: FunctionComponent<Props> = observer(
const [isDraggingFiles, setIsDraggingFiles] = useState(false)
const dragCounter = useRef(0)

const handleDrag = (event: DragEvent) => {
if (isHandlingFileDrag(event)) {
event.preventDefault()
event.stopPropagation()
}
}
const handleDrag = useCallback(
(event: DragEvent) => {
if (isHandlingFileDrag(event, application)) {
event.preventDefault()
event.stopPropagation()
}
},
[application],
)

const handleDragIn = useCallback(
(event: DragEvent) => {
if (!isHandlingFileDrag(event)) {
if (!isHandlingFileDrag(event, application)) {
return
}

Expand All @@ -268,29 +283,32 @@ export const AttachedFilesButton: FunctionComponent<Props> = observer(
}
}
},
[open, toggleAttachedFilesMenu],
[open, toggleAttachedFilesMenu, application],
)

const handleDragOut = (event: DragEvent) => {
if (!isHandlingFileDrag(event)) {
return
}
const handleDragOut = useCallback(
(event: DragEvent) => {
if (!isHandlingFileDrag(event, application)) {
return
}

event.preventDefault()
event.stopPropagation()
event.preventDefault()
event.stopPropagation()

dragCounter.current = dragCounter.current - 1
dragCounter.current = dragCounter.current - 1

if (dragCounter.current > 0) {
return
}
if (dragCounter.current > 0) {
return
}

setIsDraggingFiles(false)
}
setIsDraggingFiles(false)
},
[application],
)

const handleDrop = useCallback(
(event: DragEvent) => {
if (!isHandlingFileDrag(event)) {
if (!isHandlingFileDrag(event, application)) {
return
}

Expand Down Expand Up @@ -330,7 +348,7 @@ export const AttachedFilesButton: FunctionComponent<Props> = observer(
dragCounter.current = 0
}
},
[appState.files, appState.features.hasFiles, attachFileToNote, currentTab],
[appState.files, appState.features.hasFiles, attachFileToNote, currentTab, application],
)

useEffect(() => {
Expand All @@ -345,7 +363,7 @@ export const AttachedFilesButton: FunctionComponent<Props> = observer(
window.removeEventListener('dragover', handleDrag)
window.removeEventListener('drop', handleDrop)
}
}, [handleDragIn, handleDrop])
}, [handleDragIn, handleDrop, handleDrag, handleDragOut])

return (
<div ref={containerRef}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const ChallengeModal: FunctionComponent<Props> = ({
const valuesToProcess: ChallengeValue[] = []
for (const inputValue of Object.values(validatedValues)) {
const rawValue = inputValue.value
const value = new ChallengeValue(inputValue.prompt, rawValue)
const value = { prompt: inputValue.prompt, value: rawValue }
valuesToProcess.push(value)
}
const processingPrompts = valuesToProcess.map((v) => v.prompt)
Expand Down
12 changes: 6 additions & 6 deletions app/assets/javascripts/Components/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Footer extends PureComponent<Props, State> {

override componentDidMount(): void {
super.componentDidMount()
this.application.getStatusManager().onStatusChange((message) => {
this.application.status.addEventObserver((_event, message) => {
this.setState({
arbitraryStatusMessage: message,
})
Expand Down Expand Up @@ -124,7 +124,7 @@ export class Footer extends PureComponent<Props, State> {
}

override onAppStateEvent(eventName: AppStateEvent, data: any) {
const statusService = this.application.getStatusManager()
const statusService = this.application.status
switch (eventName) {
case AppStateEvent.EditorFocused:
if (data.eventSource === EventSource.UserInteraction) {
Expand Down Expand Up @@ -172,7 +172,7 @@ export class Footer extends PureComponent<Props, State> {
break
case ApplicationEvent.CompletedFullSync:
if (!this.completedInitialSync) {
this.application.getStatusManager().setMessage('')
this.application.status.setMessage('')
this.completedInitialSync = true
}
if (!this.didCheckForOffline) {
Expand Down Expand Up @@ -202,7 +202,7 @@ export class Footer extends PureComponent<Props, State> {
break
case ApplicationEvent.WillSync:
if (!this.completedInitialSync) {
this.application.getStatusManager().setMessage('Syncing…')
this.application.status.setMessage('Syncing…')
}
break
}
Expand All @@ -213,7 +213,7 @@ export class Footer extends PureComponent<Props, State> {
}

updateSyncStatus() {
const statusManager = this.application.getStatusManager()
const statusManager = this.application.status
const syncStatus = this.application.sync.getSyncStatus()
const stats = syncStatus.getStats()
if (syncStatus.hasError()) {
Expand Down Expand Up @@ -243,7 +243,7 @@ export class Footer extends PureComponent<Props, State> {
}

updateLocalDataStatus() {
const statusManager = this.application.getStatusManager()
const statusManager = this.application.status
const syncStatus = this.application.sync.getSyncStatus()
const stats = syncStatus.getStats()
const encryption = this.application.isEncryptionAvailable()
Expand Down
Loading

0 comments on commit 942226e

Please sign in to comment.