Files
grungnet/src/components/tile/SortableTiles.js

104 lines
2.9 KiB
JavaScript
Raw Normal View History

import React from "react";
import { SortableContext } from "@dnd-kit/sortable";
2021-05-13 16:26:59 +10:00
import { moveGroupsInto } from "../../helpers/group";
2021-05-25 15:47:52 +10:00
import { keyBy } from "../../helpers/shared";
import SortableTile from "./SortableTile";
import LazyTile from "./LazyTile";
import {
useTileDragId,
useTileDragCursor,
useTileOverGroupId,
BASE_SORTABLE_ID,
GROUP_SORTABLE_ID,
} from "../../contexts/TileDragContext";
import { useGroup } from "../../contexts/GroupContext";
function SortableTiles({ renderTile, subgroup }) {
const dragId = useTileDragId();
const dragCursor = useTileDragCursor();
const overGroupId = useTileOverGroupId();
const {
2021-06-05 16:38:01 +10:00
groups,
selectedGroupIds: allSelectedIds,
2021-06-05 16:38:01 +10:00
filter,
openGroupId,
openGroupItems,
2021-06-05 16:38:01 +10:00
filteredGroupItems,
} = useGroup();
2021-06-05 16:38:01 +10:00
const activeGroups = subgroup
? openGroupItems
: filter
? filteredGroupItems
: groups;
const sortableId = subgroup ? GROUP_SORTABLE_ID : BASE_SORTABLE_ID;
// Only populate selected groups if needed
let selectedGroupIds = [];
if ((subgroup && openGroupId) || (!subgroup && !openGroupId)) {
selectedGroupIds = allSelectedIds;
2021-05-13 16:26:59 +10:00
}
2021-06-05 16:38:01 +10:00
const disableSorting = (openGroupId && !subgroup) || filter;
const disableGrouping = subgroup || disableSorting || filter;
2021-05-13 16:26:59 +10:00
2021-05-25 15:47:52 +10:00
function renderSortableGroup(group, selectedGroups) {
if (overGroupId === group.id && dragId && group.id !== dragId) {
// If dragging over a group render a preview of that group
const previewGroup = moveGroupsInto(
2021-05-25 15:47:52 +10:00
[group, ...selectedGroups],
0,
selectedGroups.map((_, i) => i + 1)
)[0];
return renderTile(previewGroup);
}
return renderTile(group);
}
2021-05-25 15:47:52 +10:00
function renderTiles() {
2021-06-05 16:38:01 +10:00
const groupsByIds = keyBy(activeGroups, "id");
2021-05-25 15:47:52 +10:00
const selectedGroupIdsSet = new Set(selectedGroupIds);
let selectedGroups = [];
let hasSelectedContainerGroup = false;
for (let groupId of selectedGroupIds) {
const group = groupsByIds[groupId];
if (group) {
selectedGroups.push(group);
if (group.type === "group") {
hasSelectedContainerGroup = true;
}
}
}
2021-06-05 16:38:01 +10:00
return activeGroups.map((group) => {
2021-05-25 15:47:52 +10:00
const isDragging = dragId && selectedGroupIdsSet.has(group.id);
const disableTileGrouping =
disableGrouping || isDragging || hasSelectedContainerGroup;
return (
<LazyTile key={group.id}>
<SortableTile
id={group.id}
disableGrouping={disableTileGrouping}
disableSorting={disableSorting}
hidden={group.id === openGroupId}
isDragging={isDragging}
cursor={dragCursor}
>
{renderSortableGroup(group, selectedGroups)}
</SortableTile>
</LazyTile>
2021-05-25 15:47:52 +10:00
);
});
}
2021-05-13 16:26:59 +10:00
return (
2021-06-05 16:38:01 +10:00
<SortableContext items={activeGroups} id={sortableId}>
{renderTiles()}
</SortableContext>
2021-05-13 16:26:59 +10:00
);
}
export default SortableTiles;