From 2ed0f56a207cc80e59efd695e2dcb43bae01771b Mon Sep 17 00:00:00 2001 From: dr-frmr Date: Fri, 2 Aug 2024 13:56:51 +0300 Subject: [PATCH] widgets: make draggable (really janky) --- .../homepage/ui/src/components/Widgets.tsx | 69 ++++++++++++++++--- .../homepage/ui/src/store/persistentStore.ts | 4 ++ 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/kinode/packages/homepage/ui/src/components/Widgets.tsx b/kinode/packages/homepage/ui/src/components/Widgets.tsx index c56671a6..1877b332 100644 --- a/kinode/packages/homepage/ui/src/components/Widgets.tsx +++ b/kinode/packages/homepage/ui/src/components/Widgets.tsx @@ -1,19 +1,68 @@ -import useHomepageStore from "../store/homepageStore" +import useHomepageStore, { HomepageApp } from "../store/homepageStore" import Widget from "./Widget" import usePersistentStore from "../store/persistentStore" +import { DragDropContext, Draggable, DropResult, Droppable } from '@hello-pangea/dnd' +import { useEffect, useState } from "react" const Widgets = () => { const { apps } = useHomepageStore() - const { widgetSettings } = usePersistentStore(); + const { widgetSettings, widgetOrder, setWidgetOrder } = usePersistentStore(); + const [orderedWidgets, setOrderedWidgets] = useState([]); - return
- {apps.filter((app) => app.widget && !widgetSettings[app.id]?.hide).map((app) => )} -
+ useEffect(() => { + const visibleWidgets = apps.filter((app) => app.widget && !widgetSettings[app.id]?.hide); + const orderedVisibleWidgets = visibleWidgets.sort((a, b) => { + return widgetOrder.indexOf(a.id) - widgetOrder.indexOf(b.id); + }); + setOrderedWidgets(orderedVisibleWidgets); + }, [apps, widgetSettings, widgetOrder]); + + 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 ( + + + {(provided, _snapshot) => ( +
+ {orderedWidgets.map((app, index) => ( + + {(provided, _snapshot) => ( +
+ +
+ )} +
+ ))} + {provided.placeholder} +
+ )} +
+
+ ); } export default Widgets \ No newline at end of file diff --git a/kinode/packages/homepage/ui/src/store/persistentStore.ts b/kinode/packages/homepage/ui/src/store/persistentStore.ts index 31dc66ba..7e3e0cc6 100644 --- a/kinode/packages/homepage/ui/src/store/persistentStore.ts +++ b/kinode/packages/homepage/ui/src/store/persistentStore.ts @@ -13,6 +13,8 @@ export interface PersistentStore { setWidgetSettings: (widgetSettings: PersistentStore['widgetSettings']) => void toggleWidgetVisibility: (package_id: string) => void setWidgetSize: (package_id: string, size: 'small' | 'large') => void, + widgetOrder: string[] + setWidgetOrder: (widgetOrder: string[]) => void appOrder: string[] setAppOrder: (appOrder: string[]) => void } @@ -48,6 +50,8 @@ const usePersistentStore = create()( } }) }, + widgetOrder: [], + setWidgetOrder: (widgetOrder: string[]) => set({ widgetOrder }), appOrder: [], setAppOrder: (appOrder: string[]) => set({ appOrder }), }),