Files
Joshen Limandgithub-tidy-bot 53c5500be4 feat/sql editor support bulk deletes (#21004)
* Revert "Revert "Feat/sql editor support bulk deletes" (#21003)"

This reverts commit 30245c9490.

* ci: Autofix updates from GitHub workflow

* Lint

---------

Co-authored-by: github-tidy-bot <github-tidy-bot@supabase.com>
2024-02-07 18:03:15 +08:00

24 lines
996 B
TypeScript

import { uniq } from 'lodash'
export const selectItemsInRange = (id: string, snippets: any[], selectedSnippets: string[]) => {
const lastSelectedItemIndex = snippets.findIndex(
(x) => x.id === selectedSnippets[selectedSnippets.length - 1]
)
const selectedItemIndex = snippets.findIndex((x) => x.id === id)
// Get the start and end index of the range to select
const start = Math.min(selectedItemIndex, lastSelectedItemIndex)
const end = Math.max(selectedItemIndex, lastSelectedItemIndex)
// Get the range to select and reverse the order if necessary
const rangeToSelect = snippets.slice(start, end + 1)
if (selectedItemIndex < lastSelectedItemIndex) rangeToSelect.reverse()
if (selectedSnippets.includes(id)) {
const rangeToDeselectIds = rangeToSelect.map((item) => item.id)
return selectedSnippets.filter((x) => x === id || !rangeToDeselectIds.includes(x))
} else {
return uniq(selectedSnippets.concat(rangeToSelect.map((x) => x.id) as string[]))
}
}