mirror of
https://github.com/uqbar-dao/nectar.git
synced 2024-11-26 11:53:31 +03:00
widgets: make draggable (really janky)
This commit is contained in:
parent
0394da4f0a
commit
2ed0f56a20
@ -1,19 +1,68 @@
|
|||||||
import useHomepageStore from "../store/homepageStore"
|
import useHomepageStore, { HomepageApp } from "../store/homepageStore"
|
||||||
import Widget from "./Widget"
|
import Widget from "./Widget"
|
||||||
import usePersistentStore from "../store/persistentStore"
|
import usePersistentStore from "../store/persistentStore"
|
||||||
|
import { DragDropContext, Draggable, DropResult, Droppable } from '@hello-pangea/dnd'
|
||||||
|
import { useEffect, useState } from "react"
|
||||||
|
|
||||||
const Widgets = () => {
|
const Widgets = () => {
|
||||||
const { apps } = useHomepageStore()
|
const { apps } = useHomepageStore()
|
||||||
const { widgetSettings } = usePersistentStore();
|
const { widgetSettings, widgetOrder, setWidgetOrder } = usePersistentStore();
|
||||||
|
const [orderedWidgets, setOrderedWidgets] = useState<HomepageApp[]>([]);
|
||||||
|
|
||||||
return <div id="widgets">
|
useEffect(() => {
|
||||||
{apps.filter((app) => app.widget && !widgetSettings[app.id]?.hide).map((app) => <Widget
|
const visibleWidgets = apps.filter((app) => app.widget && !widgetSettings[app.id]?.hide);
|
||||||
id={app.id}
|
const orderedVisibleWidgets = visibleWidgets.sort((a, b) => {
|
||||||
label={app.label}
|
return widgetOrder.indexOf(a.id) - widgetOrder.indexOf(b.id);
|
||||||
widget={app.widget!}
|
});
|
||||||
key={app.id}
|
setOrderedWidgets(orderedVisibleWidgets);
|
||||||
/>)}
|
}, [apps, widgetSettings, widgetOrder]);
|
||||||
</div>
|
|
||||||
|
const onDragEnd = (result: DropResult) => {
|
||||||
|
if (!result.destination) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const reorderedWidgets = Array.from(orderedWidgets);
|
||||||
|
const [reorderedItem] = reorderedWidgets.splice(result.source.index, 1);
|
||||||
|
reorderedWidgets.splice(result.destination.index, 0, reorderedItem);
|
||||||
|
|
||||||
|
const newWidgetOrder = reorderedWidgets.map(widget => widget.id);
|
||||||
|
setWidgetOrder(newWidgetOrder);
|
||||||
|
setOrderedWidgets(reorderedWidgets);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<DragDropContext onDragEnd={onDragEnd}>
|
||||||
|
<Droppable droppableId="widgets">
|
||||||
|
{(provided, _snapshot) => (
|
||||||
|
<div
|
||||||
|
id="widgets"
|
||||||
|
ref={provided.innerRef}
|
||||||
|
{...provided.droppableProps}
|
||||||
|
>
|
||||||
|
{orderedWidgets.map((app, index) => (
|
||||||
|
<Draggable key={app.id} draggableId={app.id} index={index}>
|
||||||
|
{(provided, _snapshot) => (
|
||||||
|
<div
|
||||||
|
ref={provided.innerRef}
|
||||||
|
{...provided.draggableProps}
|
||||||
|
{...provided.dragHandleProps}
|
||||||
|
>
|
||||||
|
<Widget
|
||||||
|
id={app.id}
|
||||||
|
label={app.label}
|
||||||
|
widget={app.widget!}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</Draggable>
|
||||||
|
))}
|
||||||
|
{provided.placeholder}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</Droppable>
|
||||||
|
</DragDropContext>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Widgets
|
export default Widgets
|
@ -13,6 +13,8 @@ export interface PersistentStore {
|
|||||||
setWidgetSettings: (widgetSettings: PersistentStore['widgetSettings']) => void
|
setWidgetSettings: (widgetSettings: PersistentStore['widgetSettings']) => void
|
||||||
toggleWidgetVisibility: (package_id: string) => void
|
toggleWidgetVisibility: (package_id: string) => void
|
||||||
setWidgetSize: (package_id: string, size: 'small' | 'large') => void,
|
setWidgetSize: (package_id: string, size: 'small' | 'large') => void,
|
||||||
|
widgetOrder: string[]
|
||||||
|
setWidgetOrder: (widgetOrder: string[]) => void
|
||||||
appOrder: string[]
|
appOrder: string[]
|
||||||
setAppOrder: (appOrder: string[]) => void
|
setAppOrder: (appOrder: string[]) => void
|
||||||
}
|
}
|
||||||
@ -48,6 +50,8 @@ const usePersistentStore = create<PersistentStore>()(
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
widgetOrder: [],
|
||||||
|
setWidgetOrder: (widgetOrder: string[]) => set({ widgetOrder }),
|
||||||
appOrder: [],
|
appOrder: [],
|
||||||
setAppOrder: (appOrder: string[]) => set({ appOrder }),
|
setAppOrder: (appOrder: string[]) => set({ appOrder }),
|
||||||
}),
|
}),
|
||||||
|
Loading…
Reference in New Issue
Block a user