(event)
| 830 | }; |
| 831 | |
| 832 | const handleDragEnd = async (event) => { |
| 833 | const { active, over } = event; |
| 834 | |
| 835 | if (!over || active.id === over.id) { |
| 836 | return; |
| 837 | } |
| 838 | |
| 839 | const activeIndex = rows.findIndex((row) => row.id === active.id); |
| 840 | const overIndex = rows.findIndex((row) => row.id === over.id); |
| 841 | |
| 842 | if (activeIndex === -1 || overIndex === -1) { |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | const activeChannel = rows[activeIndex].original; |
| 847 | const overChannel = rows[overIndex].original; |
| 848 | |
| 849 | try { |
| 850 | // Optimistically update the local state |
| 851 | const reorderedData = [...data]; |
| 852 | const [movedItem] = reorderedData.splice(activeIndex, 1); |
| 853 | reorderedData.splice(overIndex, 0, movedItem); |
| 854 | useChannelsTableStore.setState({ channels: reorderedData }); |
| 855 | |
| 856 | // Call backend to reorder |
| 857 | await API.reorderChannel( |
| 858 | activeChannel.id, |
| 859 | overIndex > activeIndex |
| 860 | ? overChannel.id |
| 861 | : rows[overIndex - 1]?.original.id || null |
| 862 | ); |
| 863 | |
| 864 | // Refetch to get updated channel numbers |
| 865 | await API.requeryChannels(); |
| 866 | } catch (error) { |
| 867 | // Revert on error |
| 868 | console.error('Failed to reorder channel:', error); |
| 869 | await API.requeryChannels(); |
| 870 | } |
| 871 | }; |
| 872 | |
| 873 | /** |
| 874 | * useEffect |
nothing calls this directly
no test coverage detected