-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathCollectionTrackFieldArray.tsx
More file actions
128 lines (117 loc) · 3.49 KB
/
Copy pathCollectionTrackFieldArray.tsx
File metadata and controls
128 lines (117 loc) · 3.49 KB
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
import type { CSSProperties, HTMLAttributes, ReactElement } from 'react'
import {
DragDropContext,
Draggable,
Droppable
} from '@atlaskit/pragmatic-drag-and-drop-react-beautiful-dnd-migration'
import {
TrackForEdit,
TrackForUpload,
isTrackForUpload
} from '@audius/common/store'
import { FieldArray, useField } from 'formik'
import { CollectionTrackField } from './CollectionTrackField'
// Types matching react-beautiful-dnd API
type DropResult = {
draggableId: string
type: string
source: {
droppableId: string
index: number
}
destination: {
droppableId: string
index: number
} | null
reason: 'DROP' | 'CANCEL'
}
type DroppableProvided = {
innerRef: (element: HTMLElement | null) => void
droppableProps: {
'data-rbd-droppable-id': string
'data-rbd-droppable-context-id': string
}
placeholder: ReactElement | null
}
type DraggableProvided = {
innerRef: (element: HTMLElement | null) => void
draggableProps: HTMLAttributes<HTMLElement> & {
'data-rbd-draggable-context-id'?: string
'data-rbd-draggable-id'?: string
style?: CSSProperties
}
dragHandleProps:
| (HTMLAttributes<HTMLElement> & {
'data-rbd-drag-handle-draggable-id'?: string
'data-rbd-drag-handle-context-id'?: string
'aria-describedby'?: string
})
| null
}
const messages = {
trackList: 'Track List'
}
const makeTrackKey = (track: TrackForUpload | TrackForEdit, index: number) => {
if (isTrackForUpload(track)) {
return track.file.name ?? `${index}`
}
const suffix = 'metadata_time' in track ? `-${track.metadata_time}` : ''
return `${track.metadata.track_id}${suffix}`
}
type CollectionTrackFieldArrayProps = {
isUpload?: boolean
}
export const CollectionTrackFieldArray = ({
isUpload = false
}: CollectionTrackFieldArrayProps = {}) => {
const [{ value: tracks }] =
useField<(TrackForUpload | TrackForEdit)[]>('tracks')
return (
<FieldArray name='tracks'>
{({ move, remove }) => (
<DragDropContext
onDragEnd={(result: DropResult) => {
if (!result.destination) {
return
}
move(result.source.index, result.destination.index)
}}
>
<Droppable droppableId='tracks'>
{(provided: DroppableProvided) => (
<div
{...provided.droppableProps}
ref={provided.innerRef}
role='list'
aria-label={messages.trackList}
>
{tracks.map((track, index) => {
const id = makeTrackKey(track, index)
return (
<Draggable key={id} draggableId={id} index={index}>
{(provided: DraggableProvided) => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...(provided.dragHandleProps ?? {})}
role='listitem'
>
<CollectionTrackField
index={index}
remove={remove}
disableDelete={isUpload && tracks.length === 1}
/>
</div>
)}
</Draggable>
)
})}
{provided.placeholder}
</div>
)}
</Droppable>
</DragDropContext>
)}
</FieldArray>
)
}