-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f23b1d6
commit e253b3e
Showing
17 changed files
with
979 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'renterd': minor | ||
--- | ||
|
||
Files can now be moved by dragging into or out of directories. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@siafoundation/design-system': minor | ||
--- | ||
|
||
The Table now supports drag and drop on rows. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { ObjectData } from './types' | ||
import { useCallback, useState } from 'react' | ||
import { | ||
DragStartEvent, | ||
DragEndEvent, | ||
DragOverEvent, | ||
DragMoveEvent, | ||
DragCancelEvent, | ||
} from '@dnd-kit/core' | ||
import { FullPathSegments, getDirectorySegmentsFromPath } from './paths' | ||
import { useObjectRename } from '@siafoundation/react-renterd' | ||
import { triggerErrorToast } from '@siafoundation/design-system' | ||
import { getRenameParams } from './rename' | ||
|
||
type Props = { | ||
activeDirectory: FullPathSegments | ||
setActiveDirectory: ( | ||
func: (directory: FullPathSegments) => FullPathSegments | ||
) => void | ||
dataset?: ObjectData[] | ||
mutate: () => void | ||
} | ||
|
||
export function useMove({ | ||
dataset, | ||
activeDirectory, | ||
setActiveDirectory, | ||
mutate, | ||
}: Props) { | ||
const [draggingObject, setDraggingObject] = useState<ObjectData | null>(null) | ||
const [, setNavTimeout] = useState<NodeJS.Timeout>() | ||
const rename = useObjectRename() | ||
|
||
const moveFiles = useCallback( | ||
async (e: DragEndEvent) => { | ||
const { bucket, from, to, mode } = getRenameParams(e, activeDirectory) | ||
if (from === to) { | ||
return | ||
} | ||
const response = await rename.post({ | ||
payload: { | ||
force: false, | ||
bucket, | ||
from, | ||
to, | ||
mode, | ||
}, | ||
}) | ||
mutate() | ||
if (response.error) { | ||
triggerErrorToast(response.error) | ||
} | ||
}, | ||
[mutate, rename, activeDirectory] | ||
) | ||
|
||
const delayedNavigation = useCallback( | ||
(directory?: FullPathSegments) => { | ||
if (!directory) { | ||
setNavTimeout((t) => { | ||
if (t) { | ||
clearTimeout(t) | ||
} | ||
return null | ||
}) | ||
return | ||
} | ||
const newTimeout = setTimeout(() => { | ||
setActiveDirectory(() => directory) | ||
}, 1000) | ||
setNavTimeout((t) => { | ||
if (t) { | ||
clearTimeout(t) | ||
} | ||
return newTimeout | ||
}) | ||
}, | ||
[setNavTimeout, setActiveDirectory] | ||
) | ||
|
||
const scheduleNavigation = useCallback( | ||
(e: { collisions: { id: string | number }[] }) => { | ||
if (!e.collisions.length) { | ||
delayedNavigation(undefined) | ||
} else { | ||
const path = e.collisions?.[0].id as string | ||
if (path === '..') { | ||
delayedNavigation(activeDirectory.slice(0, -1)) | ||
} else { | ||
delayedNavigation(getDirectorySegmentsFromPath(path)) | ||
} | ||
} | ||
}, | ||
[delayedNavigation, activeDirectory] | ||
) | ||
|
||
const onDragStart = useCallback( | ||
(e: DragStartEvent) => { | ||
setDraggingObject(dataset.find((d) => d.id === e.active.id) || null) | ||
}, | ||
[dataset, setDraggingObject] | ||
) | ||
|
||
const onDragOver = useCallback( | ||
(e: DragOverEvent) => { | ||
scheduleNavigation(e) | ||
}, | ||
[scheduleNavigation] | ||
) | ||
|
||
const onDragMove = useCallback( | ||
(e: DragMoveEvent) => { | ||
scheduleNavigation(e) | ||
}, | ||
[scheduleNavigation] | ||
) | ||
|
||
const onDragEnd = useCallback( | ||
async (e: DragEndEvent) => { | ||
delayedNavigation(undefined) | ||
setDraggingObject(undefined) | ||
moveFiles(e) | ||
}, | ||
[setDraggingObject, delayedNavigation, moveFiles] | ||
) | ||
|
||
const onDragCancel = useCallback( | ||
async (e: DragCancelEvent) => { | ||
delayedNavigation(undefined) | ||
setDraggingObject(undefined) | ||
}, | ||
[setDraggingObject, delayedNavigation] | ||
) | ||
|
||
return { | ||
onDragEnd, | ||
onDragOver, | ||
onDragCancel, | ||
onDragMove, | ||
onDragStart, | ||
draggingObject, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.