diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..f3b42f4 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,12 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Markdown personal settings +/markdown.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..fe4b997 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,60 @@ + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/dahoam.iml b/.idea/dahoam.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/dahoam.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b8d2530 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/prettier.xml b/.idea/prettier.xml new file mode 100644 index 0000000..b0c1c68 --- /dev/null +++ b/.idea/prettier.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/PLAN.md b/PLAN.md index b633b2b..74b0501 100644 --- a/PLAN.md +++ b/PLAN.md @@ -99,13 +99,14 @@ scripts/sync-collabry.sh copies them from upstream at a pinned commit `dashboard`, the `wallpaper` table is keyed by dashboard (migration `0007_dashboard_wallpaper` copies what each user had to all their dashboards). `/settings?d=` is the dashboard whose wallpaper is shown, and where "Back to dashboard" goes. +- [x] 16. Cards are dragged by their title bar: `card.position` (migration `0008_card_position`), + `saveLayout` (was `moveLinks`) stores the order of the cards and of their links. ## Not doing (not asked) - Unraid version/uptime chip and the Sonarr/Plex notification pills from the screenshot - Inline SearXNG result rendering (search opens the engine's result page) -- Reordering the cards themselves (they stay in the order they were made), free placement with - gaps in the grid, per-user SearXNG URL (SSRF risk — it is a server env) +- Free placement of cards with gaps in the grid, per-user SearXNG URL (SSRF risk — it is a server env) - Per dashboard clock or search engine (they stay per user), moving a card to another dashboard, reordering dashboards, remembering the last opened dashboard (the default opens) - A list of the icons a user uploaded, to use one again for another link diff --git a/README.md b/README.md index b3f1d36..5786fb7 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,8 @@ Build it yourself: `docker build -t dahoam .` edit or delete it. - **Move links** by dragging, inside a card or to another card. Touch: press and hold first. Keyboard: focus a link, Space picks it up, arrow keys move it, Space puts it down. +- **Move cards** by dragging their title bar, the links come along. Touch: press and hold + first. Keyboard: Space on the grip next to the plus, arrow keys, Space. - **Icons**: leave the field empty and the icon is looked up by name in [dashboard-icons](https://github.com/homarr-labs/dashboard-icons), then from the site's `/favicon.ico`. Or type in the field to search that library and click an icon, diff --git a/drizzle/0008_card_position.sql b/drizzle/0008_card_position.sql new file mode 100644 index 0000000..862dfee --- /dev/null +++ b/drizzle/0008_card_position.sql @@ -0,0 +1 @@ +ALTER TABLE "card" ADD COLUMN "position" integer DEFAULT 0 NOT NULL; \ No newline at end of file diff --git a/drizzle/meta/0008_snapshot.json b/drizzle/meta/0008_snapshot.json new file mode 100644 index 0000000..ff4f409 --- /dev/null +++ b/drizzle/meta/0008_snapshot.json @@ -0,0 +1,909 @@ +{ + "id": "eddd02da-41ed-40a6-b0b0-e5976ea2c371", + "prevId": "c89fef31-f0cf-4f87-8683-df4f9eb49fa8", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.account": { + "name": "account", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "account_id": { + "name": "account_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "access_token": { + "name": "access_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "refresh_token": { + "name": "refresh_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "id_token": { + "name": "id_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "access_token_expires_at": { + "name": "access_token_expires_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "refresh_token_expires_at": { + "name": "refresh_token_expires_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "scope": { + "name": "scope", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "account_user_id_idx": { + "name": "account_user_id_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "account_user_id_user_id_fk": { + "name": "account_user_id_user_id_fk", + "tableFrom": "account", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.card": { + "name": "card", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "dashboard_id": { + "name": "dashboard_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "position": { + "name": "position", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "card_user_id_idx": { + "name": "card_user_id_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "card_dashboard_id_idx": { + "name": "card_dashboard_id_idx", + "columns": [ + { + "expression": "dashboard_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "card_user_id_user_id_fk": { + "name": "card_user_id_user_id_fk", + "tableFrom": "card", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "card_dashboard_id_dashboard_id_fk": { + "name": "card_dashboard_id_dashboard_id_fk", + "tableFrom": "card", + "tableTo": "dashboard", + "columnsFrom": [ + "dashboard_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.dashboard": { + "name": "dashboard", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "wallpaper_url": { + "name": "wallpaper_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "wallpaper_dim": { + "name": "wallpaper_dim", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 40 + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "dashboard_user_id_idx": { + "name": "dashboard_user_id_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "dashboard_user_id_user_id_fk": { + "name": "dashboard_user_id_user_id_fk", + "tableFrom": "dashboard", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.icon": { + "name": "icon", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "mime": { + "name": "mime", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "data": { + "name": "data", + "type": "bytea", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "icon_user_id_idx": { + "name": "icon_user_id_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "icon_user_id_user_id_fk": { + "name": "icon_user_id_user_id_fk", + "tableFrom": "icon", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.link": { + "name": "link", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "card_id": { + "name": "card_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "position": { + "name": "position", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "icon": { + "name": "icon", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "link_user_id_idx": { + "name": "link_user_id_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "link_card_id_idx": { + "name": "link_card_id_idx", + "columns": [ + { + "expression": "card_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "link_user_id_user_id_fk": { + "name": "link_user_id_user_id_fk", + "tableFrom": "link", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "link_card_id_card_id_fk": { + "name": "link_card_id_card_id_fk", + "tableFrom": "link", + "tableTo": "card", + "columnsFrom": [ + "card_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.session": { + "name": "session", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "token": { + "name": "token", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "ip_address": { + "name": "ip_address", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "user_agent": { + "name": "user_agent", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "session_user_id_idx": { + "name": "session_user_id_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "session_user_id_user_id_fk": { + "name": "session_user_id_user_id_fk", + "tableFrom": "session", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "session_token_unique": { + "name": "session_token_unique", + "nullsNotDistinct": false, + "columns": [ + "token" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.settings": { + "name": "settings", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'Dahoam'" + }, + "default_dashboard_id": { + "name": "default_dashboard_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "search_engine": { + "name": "search_engine", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'duckduckgo'" + }, + "clock_font": { + "name": "clock_font", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'serif'" + }, + "clock_style": { + "name": "clock_style", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'solid'" + } + }, + "indexes": {}, + "foreignKeys": { + "settings_user_id_user_id_fk": { + "name": "settings_user_id_user_id_fk", + "tableFrom": "settings", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "settings_default_dashboard_id_dashboard_id_fk": { + "name": "settings_default_dashboard_id_dashboard_id_fk", + "tableFrom": "settings", + "tableTo": "dashboard", + "columnsFrom": [ + "default_dashboard_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.user": { + "name": "user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "email_verified": { + "name": "email_verified", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "image": { + "name": "image", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "user_email_unique": { + "name": "user_email_unique", + "nullsNotDistinct": false, + "columns": [ + "email" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.verification": { + "name": "verification", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "identifier": { + "name": "identifier", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "verification_identifier_idx": { + "name": "verification_identifier_idx", + "columns": [ + { + "expression": "identifier", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.wallpaper": { + "name": "wallpaper", + "schema": "", + "columns": { + "dashboard_id": { + "name": "dashboard_id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "mime": { + "name": "mime", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "data": { + "name": "data", + "type": "bytea", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "wallpaper_dashboard_id_dashboard_id_fk": { + "name": "wallpaper_dashboard_id_dashboard_id_fk", + "tableFrom": "wallpaper", + "tableTo": "dashboard", + "columnsFrom": [ + "dashboard_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "wallpaper_user_id_user_id_fk": { + "name": "wallpaper_user_id_user_id_fk", + "tableFrom": "wallpaper", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index 1e0cddf..ac71915 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -57,6 +57,13 @@ "when": 1795021173823, "tag": "0007_dashboard_wallpaper", "breakpoints": true + }, + { + "idx": 8, + "version": "7", + "when": 1795106775546, + "tag": "0008_card_position", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/components/card-grid.tsx b/src/components/card-grid.tsx index d8008db..82f8d71 100644 --- a/src/components/card-grid.tsx +++ b/src/components/card-grid.tsx @@ -8,7 +8,6 @@ import { TouchSensor, closestCenter, pointerWithin, - useDroppable, useSensor, useSensors, } from "@dnd-kit/core" @@ -21,13 +20,14 @@ import { SortableContext, rectSortingStrategy, sortableKeyboardCoordinates, + useSortable, } from "@dnd-kit/sortable" -import { Plus } from "lucide-react" +import { GripVertical, Plus } from "lucide-react" import { glass } from "@/components/glass" import { LinkTile, TileFace } from "@/components/link-tile" import type { HomeLink } from "@/components/link-tile" -import { moveLinks } from "@/lib/dashboard.functions" -import { drop, moveOver, order } from "@/lib/layout" +import { saveLayout } from "@/lib/dashboard.functions" +import { drop, moveCard, moveOver, order } from "@/lib/layout" import { Button } from "@/vendor/collabry/components/Button/Button" import { cn } from "@/vendor/collabry/lib/cn" @@ -39,20 +39,25 @@ type Handlers = { onEditCard: (card: DashCard) => void } -/** The cards with their links. Links can be dragged inside a card and between cards. */ +/** + * The cards with their links. Links can be dragged inside a card and between cards, + * cards by their title bar. + */ export function CardGrid({ cards: saved, onAddCard, ...handlers }: { cards: DashCard[]; onAddCard: () => void } & Handlers) { const router = useRouter() - // the layout while a link is dragged, and until the server has stored it + // the layout while a link or a card is dragged, and until the server has stored it const [dragged, setDragged] = useState(null) const [activeId, setActiveId] = useState(null) const drags = useRef(0) const cards = dragged ?? saved const links = cards.flatMap((c) => c.links) const active = links.find((l) => l.id === activeId) + const activeCard = cards.find((c) => c.id === activeId) + const isCard = (id: UniqueIdentifier) => cards.some((c) => c.id === id) const sensors = useSensors( // a few pixels of slack, so a plain click still opens the link @@ -82,10 +87,17 @@ export function CardGrid({ ), }) const cardArgs = only(cards.map((c) => c.id)) - // the keyboard has no pointer: take the nearest card then - const hit = pointerWithin(cardArgs).at(0) ?? closestCenter(cardArgs).at(0) + const dragsCard = isCard(args.active.id) + // The keyboard has no pointer: take the nearest card then. A dragged card waits + // while the pointer is between cards: the nearest one could swap it back and forth. + const hit = + pointerWithin(cardArgs).at(0) ?? + (dragsCard && args.pointerCoordinates + ? undefined + : closestCenter(cardArgs).at(0)) const card = cards.find((c) => c.id === hit?.id) if (!hit || !card) return [] + if (dragsCard) return [hit] const nearest = closestCenter(only(card.links.map((l) => l.id))) return nearest.length ? nearest : [hit] } @@ -97,7 +109,7 @@ export function CardGrid({ setDragged(next) const drag = drags.current // on failure the reload below puts the links back where they were - await moveLinks({ data: { cards: order(next) } }).catch(() => {}) + await saveLayout({ data: { cards: order(next) } }).catch(() => {}) await router.invalidate() // a newer drag owns the layout by now if (drags.current === drag) setDragged(null) @@ -110,12 +122,18 @@ export function CardGrid({ cards.find((c) => c.id === id || c.links.some((l) => l.id === id))?.name const announcements: Announcements = { onDragStart: ({ active: a }) => `Picked up ${name(a.id)}.`, - onDragOver: ({ active: a, over }) => - over ? `${name(a.id)} is in ${cardName(over.id)}.` : undefined, + onDragOver: ({ active: a, over }) => { + if (!over || over.id === a.id) return + return isCard(a.id) + ? `${name(a.id)} took the place of ${name(over.id)}.` + : `${name(a.id)} is in ${cardName(over.id)}.` + }, onDragEnd: ({ active: a, over }) => - over - ? `${name(a.id)} was put down in ${cardName(over.id)}.` - : `${name(a.id)} was put back.`, + isCard(a.id) + ? `${name(a.id)} was put down.` + : over + ? `${name(a.id)} was put down in ${cardName(over.id)}.` + : `${name(a.id)} was put back.`, onDragCancel: ({ active: a }) => `${name(a.id)} was put back.`, } @@ -132,7 +150,10 @@ export function CardGrid({ setDragged(cards) }} onDragOver={({ active: a, over }) => { - if (over) setDragged(moveOver(cards, String(a.id), String(over.id))) + if (!over) return + // each of the two leaves the other kind of drag alone + const [id, overId] = [String(a.id), String(over.id)] + setDragged(moveCard(moveOver(cards, id, overId), id, overId)) }} onDragEnd={({ active: a, over }) => finish(over ? drop(cards, String(a.id), String(over.id)) : cards) @@ -142,18 +163,22 @@ export function CardGrid({ setDragged(null) }} > - {/* cards in a row share a height, so their edges line up */} -
- {cards.map((card) => ( - l.id === activeId)} - {...handlers} - /> - ))} -
+ {/* A dragged card moves in the list itself (onDragOver), so no strategy shifts + the others: cards differ in size, and a shifted one would not fit its slot. */} + c.id)} strategy={() => null}> + {/* cards in a row share a height, so their edges line up */} +
+ {cards.map((card) => ( + l.id === activeId)} + {...handlers} + /> + ))} +
+
- {active && ( + {/* one expression: an array of two empty ones would still count as a child */} + {active ? ( + ) : ( + activeCard && )} ) } +// 6rem is a tile's width. On a narrow screen fewer columns fit: they stay +// centered in the card, and a last row that is not full starts at the left. +const tiles = + "mx-auto grid max-w-full grid-cols-[repeat(auto-fill,6rem)] justify-center gap-y-2" +/** One column per link, at most 8. An empty card is as wide as two links. */ +const tilesWidth = (card: DashCard) => ({ + width: `${(Math.min(card.links.length, 8) || 2) * 6}rem`, +}) + +/** What follows the pointer while a card is dragged: the card without its buttons. */ +function CardFace({ card }: { card: DashCard }) { + return ( + // the overlay is as large as the card, h-full fills it for an empty card too +
+
+ {card.name} +
+
+ {card.links.map((link) => ( + + ))} +
+
+ ) +} + function CardPanel({ card, target, @@ -181,11 +235,16 @@ function CardPanel({ onEditCard, }: { card: DashCard; target: boolean } & Handlers) { const titleId = useId() - const { setNodeRef } = useDroppable({ id: card.id }) - // one column per link, at most 8. An empty card is as wide as two links. - const columns = Math.min(card.links.length, 8) || 2 + // `card` tells the link tiles that a card is dragged, see LinkTile + const { setNodeRef, setActivatorNodeRef, attributes, listeners, isDragging } = + useSortable({ id: card.id, data: { card: true } }) const subtle = "rounded-lg outline-none focus-visible:ring-2 focus-visible:ring-white/60" + // shown on hover, always on touch + const action = cn( + subtle, + "grid size-7 flex-none place-items-center text-white/70 opacity-0 transition-opacity group-hover/card:opacity-100 hover:bg-white/10 hover:text-white focus-visible:opacity-100 pointer-coarse:opacity-100" + ) return (
- {/* the title starts where the first icon does, the plus ends where the last one does */} -
+ {/* The title starts where the first icon does, the plus ends where the last one does. + The whole bar drags the card. The keyboard picks it up on the grip only. */} +

+ @@ -227,12 +298,7 @@ function CardPanel({ items={card.links.map((l) => l.id)} strategy={rectSortingStrategy} > - {/* 6rem is a tile's width. On a narrow screen fewer columns fit: they stay - centered in the card, and a last row that is not full starts at the left. */} -
+
{card.links.map((link) => ( void }) { + // while a card is dragged, the arrow keys go from card to card, not through the links + const cardDragged = !!useDndContext().active?.data.current?.card const { attributes, listeners, @@ -92,7 +95,7 @@ export function LinkTile({ transform, transition, isDragging, - } = useSortable({ id: link.id }) + } = useSortable({ id: link.id, disabled: { droppable: cardDragged } }) return (
dashboard.id, { onDelete: "cascade" }), + // order on the dashboard + position: integer("position").notNull().default(0), name: text("name").notNull(), createdAt: timestamp("created_at").notNull().defaultNow(), }, diff --git a/src/lib/dashboard.functions.ts b/src/lib/dashboard.functions.ts index f7bfc29..ccb6910 100644 --- a/src/lib/dashboard.functions.ts +++ b/src/lib/dashboard.functions.ts @@ -85,7 +85,8 @@ export const getDashboard = createServerFn({ method: "GET" }) .select({ id: card.id, name: card.name }) .from(card) .where(and(eq(card.userId, userId), eq(card.dashboardId, dashboardId))) - .orderBy(asc(card.createdAt)), + // cards that were never moved all have position 0: oldest first + .orderBy(asc(card.position), asc(card.createdAt)), db .select({ id: link.id, @@ -173,7 +174,13 @@ export const saveCard = createServerFn({ method: "POST" }) .where(and(eq(card.id, id), eq(card.userId, user.id))) } else { await requireDashboard(user.id, dashboardId) - await db.insert(card).values({ name, dashboardId, userId: user.id }) + await db.insert(card).values({ + name, + dashboardId, + userId: user.id, + // after the last card of the dashboard + position: sql`(select coalesce(max(${card.position}) + 1, 0) from ${card} where ${card.dashboardId} = ${dashboardId})`, + }) } }) @@ -217,8 +224,8 @@ export const deleteCard = createServerFn({ method: "POST" }) .where(and(eq(card.id, data.id), eq(card.userId, user.id))) }) -/** Stores the result of a drag: for each card, its links in order. */ -export const moveLinks = createServerFn({ method: "POST" }) +/** Stores the result of a drag: the cards in order and, for each card, its links in order. */ +export const saveLayout = createServerFn({ method: "POST" }) .validator( z.object({ cards: z @@ -234,15 +241,20 @@ export const moveLinks = createServerFn({ method: "POST" }) .from(card) .where(and(eq(card.userId, user.id), inArray(card.id, cardIds))) if (owned.length !== cardIds.length) throw new Error("Unknown card") - // shortcut: one UPDATE per link, fine for a few hundred links; switch to a - // single UPDATE ... FROM (VALUES ...) if a save gets slow + // shortcut: one UPDATE per card and link, fine for a few hundred links; switch + // to a single UPDATE ... FROM (VALUES ...) if a save gets slow await db.transaction(async (tx) => { - for (const c of data.cards) + for (const [at, c] of data.cards.entries()) { + await tx + .update(card) + .set({ position: at }) + .where(and(eq(card.id, c.id), eq(card.userId, user.id))) for (const [position, id] of c.linkIds.entries()) await tx .update(link) .set({ cardId: c.id, position }) .where(and(eq(link.id, id), eq(link.userId, user.id))) + } }) }) diff --git a/src/lib/layout.test.ts b/src/lib/layout.test.ts index e55583b..0b7361b 100644 --- a/src/lib/layout.test.ts +++ b/src/lib/layout.test.ts @@ -1,6 +1,6 @@ import assert from "node:assert/strict" import { test } from "node:test" -import { drop, moveOver, order } from "./layout.ts" +import { drop, moveCard, moveOver, order } from "./layout.ts" const cards = () => [ { id: "apps", links: [{ id: "a" }, { id: "b" }, { id: "c" }] }, @@ -31,6 +31,18 @@ test("a link dropped on a card goes to its end", () => { assert.deepEqual(ids(drop(cards(), "a", "empty")), ["bc", "x", "a"]) }) +test("a card takes the place of the card it is over, with its links", () => { + assert.deepEqual(ids(moveCard(cards(), "apps", "empty")), ["x", "", "abc"]) + assert.deepEqual(ids(moveCard(cards(), "empty", "apps")), ["", "abc", "x"]) +}) + +test("a card over itself, or a link over a card, moves no card", () => { + const before = cards() + assert.equal(moveCard(before, "apps", "apps"), before) + assert.equal(moveCard(before, "a", "web"), before) + assert.equal(drop(before, "apps", "web"), before) +}) + test("unknown ids change nothing", () => { const before = cards() assert.equal(drop(before, "nope", "x"), before) diff --git a/src/lib/layout.ts b/src/lib/layout.ts index 4e89f16..4a052e3 100644 --- a/src/lib/layout.ts +++ b/src/lib/layout.ts @@ -1,4 +1,4 @@ -// Where links sit in their cards. Pure, so drag and drop can be tested without a browser. +// Where the cards sit, and the links in them. Pure, so drag and drop can be tested without a browser. type Card = { id: string; links: { id: string }[] } @@ -28,6 +28,20 @@ export function moveOver( ) } +/** While dragging a card: it takes the place of the card it hovers over. */ +export function moveCard( + cards: TCard[], + cardId: string, + overId: string +): TCard[] { + const from = cards.findIndex((c) => c.id === cardId) + const to = cards.findIndex((c) => c.id === overId) + if (from < 0 || to < 0 || from === to) return cards + const next = [...cards] + next.splice(to, 0, ...next.splice(from, 1)) + return next +} + /** On drop: the link takes the place of the link it was dropped on. */ export function drop( cards: TCard[], @@ -45,6 +59,6 @@ export function drop( return cards.map((c) => (c === card ? { ...c, links } : c)) } -/** What the server stores: per card, its link ids in order. */ +/** What the server stores: the cards in order and, per card, its link ids in order. */ export const order = (cards: Card[]) => cards.map((c) => ({ id: c.id, linkIds: c.links.map((l) => l.id) }))