Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate plugins sortable #17587

Merged
merged 8 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/scenes/plugins/tabs/apps/AppView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function AppView({
</>
}
>
<LemonButton onClick={() => openReorderModal()} noPadding>
<LemonButton onClick={openReorderModal} noPadding>
{orderedIndex ? (
<LemonBadge.Number status="primary" count={orderedIndex} maxDigits={3} />
) : (
Expand Down
72 changes: 41 additions & 31 deletions frontend/src/scenes/plugins/tabs/apps/InstalledAppsReorderModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ import { useValues, useActions } from 'kea'
import { pluginsLogic } from 'scenes/plugins/pluginsLogic'
import { LemonBadge, LemonButton } from '@posthog/lemon-ui'
import { PluginTypeWithConfig } from 'scenes/plugins/types'
import { SortEndHandler, SortableContainer, SortableElement } from 'react-sortable-hoc'
import { PluginImage } from 'scenes/plugins/plugin/PluginImage'
import { SortableContext, arrayMove, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable'
import { DndContext, DragEndEvent } from '@dnd-kit/core'
import { restrictToParentElement, restrictToVerticalAxis } from '@dnd-kit/modifiers'
import { CSS } from '@dnd-kit/utilities'

const MinimalAppView = ({ plugin, order }: { plugin: PluginTypeWithConfig; order: number }): JSX.Element => {
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: plugin.id })

return (
<div
className="flex gap-2 cursor-move border rounded p-2 items-center bg-light"
// eslint-disable-next-line react/forbid-dom-props
ref={setNodeRef}
className="flex gap-2 cursor-move border rounded p-2 items-center bg-bg-light"
style={{
zIndex: 999999,
position: 'relative',
transform: CSS.Transform.toString(transform),
transition,
zIndex: isDragging ? 999999 : undefined,
}}
{...attributes}
{...listeners}
>
<LemonBadge.Number count={order + 1} maxDigits={3} />
<PluginImage plugin={plugin} size="small" />
Expand All @@ -22,36 +32,34 @@ const MinimalAppView = ({ plugin, order }: { plugin: PluginTypeWithConfig; order
)
}

const SortableAppView = SortableElement(MinimalAppView)

const SortableAppList = SortableContainer(({ children }: { children: React.ReactNode }) => {
return <span className="flex flex-col gap-2">{children}</span>
})

export function InstalledAppsReorderModal(): JSX.Element {
const { reorderModalOpen, sortableEnabledPlugins, temporaryOrder, pluginConfigsLoading } = useValues(pluginsLogic)
const { closeReorderModal, setTemporaryOrder, cancelRearranging, savePluginOrders } = useActions(pluginsLogic)

const onSortEnd: SortEndHandler = ({ oldIndex, newIndex }) => {
const cloned = [...sortableEnabledPlugins]
const [removed] = cloned.splice(oldIndex, 1)
cloned.splice(newIndex, 0, removed)

const newTemporaryOrder = cloned.reduce((acc, plugin, index) => {
return {
...acc,
[plugin.id]: index + 1,
}
}, {})

setTemporaryOrder(newTemporaryOrder, removed.id)
}

const onClose = (): void => {
cancelRearranging()
closeReorderModal()
}

const handleDragEnd = ({ active, over }: DragEndEvent): void => {
const itemIds = sortableEnabledPlugins.map((item) => item.id)

if (over && active.id !== over.id) {
const oldIndex = itemIds.indexOf(Number(active.id))
const newIndex = itemIds.indexOf(Number(over.id))
const newOrder = arrayMove(sortableEnabledPlugins, oldIndex, newIndex)

const newTemporaryOrder = newOrder.reduce((acc, plugin, index) => {
return {
...acc,
[plugin.id]: index + 1,
}
}, {})

setTemporaryOrder(newTemporaryOrder, Number(active.id))
}
}

return (
<LemonModal
onClose={onClose}
Expand Down Expand Up @@ -79,12 +87,14 @@ export function InstalledAppsReorderModal(): JSX.Element {
</>
}
>
<div className="space-y-4">
<SortableAppList onSortEnd={onSortEnd} axis="y" lockAxis="y" lockToContainerEdges distance={5}>
{sortableEnabledPlugins.map((plugin, index) => (
<SortableAppView key={`item-${index}`} index={index} order={index} plugin={plugin} />
))}
</SortableAppList>
<div className="flex flex-col gap-2">
<DndContext modifiers={[restrictToVerticalAxis, restrictToParentElement]} onDragEnd={handleDragEnd}>
<SortableContext items={sortableEnabledPlugins} strategy={verticalListSortingStrategy}>
{sortableEnabledPlugins.map((item, index) => (
<MinimalAppView key={item.id} plugin={item} order={index} />
))}
</SortableContext>
</DndContext>
</div>
</LemonModal>
)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.