// imports import clsx from 'clsx'; import Link from 'next/link'; import React from 'react'; import initials from 'initials'; import IconPlus from '@chirper/react/src/icons/Plus'; import { useParams } from 'next/navigation'; import { ReactSortable } from 'react-sortablejs'; // locals import Tooltip from '../../Tooltip'; import useTeam from '../../../hooks/useTeam'; import ApiSignal from '../../../signals/Api'; import { useSignal } from '../../../signal'; import { update as TeamUpdateSignal } from '../../Team/Globals'; import TeamsSignal, { onUpdate as onUpdateTeam } from '../../../signals/Teams'; /** * component app teams * * @param props */ const ComponentAppTeams = (props = {}) => { // use signal const [sApi] = useSignal(ApiSignal); const [sTeams] = useSignal(TeamsSignal); const [,setTeamUpdate] = useSignal(TeamUpdateSignal); // sorted teams const sortedTeams = sTeams.sort((a, b) => { // check if a or b is root if (a.order < b.order) return -1; if (a.order > b.order) return 1; return 0; }); // params const params = useParams(); // use team const team = useTeam(params.team); // handle sort const handleSort = (e) => { // set indexes const newIndex = e.newIndex; const currentIndex = e.oldIndex; // actual item const actualItem = sortedTeams[currentIndex]; // slot into new index if (newIndex < currentIndex) { // If moving the item to an earlier position sortedTeams.splice(currentIndex, 1); // Remove the item from its current position sortedTeams.splice(newIndex, 0, actualItem); // Insert the item at the new position } else { // If moving the item to a later position // We need to adjust the insertion index because removing the item first will shift the array sortedTeams.splice(newIndex + 1, 0, actualItem); // Insert the item at the adjusted new position sortedTeams.splice(currentIndex, 1); // Remove the item from its original position } // create updates const updates = []; // loop through sorted teams sortedTeams.forEach((team, index) => { // check order if (team.order !== index) { // set order team.order = index; updates.push(team); } }); // loop updates updates.forEach(onUpdateTeam); // push to api sApi.Team.order(updates.map((team) => ({ id : team.id, order : team.order, }))); }; // return jsx return (