All files / app-play/src drop-navigation.ts

100% Statements 12/12
100% Branches 7/7
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27                              20x 16x 10x 9x 9x   16x   9x 2x    
import { groupChartsByFolder, type BrowseSnapshot, type LibraryChart } from '@be-music/player-scene-model';
 
/**
 * Where the select screen opens after a drop, as a browse snapshot (`@be-music/player-scene-model`): in the dropped folder when
 * the drop added one and root navigation was not requested, or on the folder list with the cursor on the first added folder. Undefined when
 * the drop added no songs (a theme or a replay alone), so the screen stays where it was.
 *
 * Folders are the select screen's top-level groups (`groupChartsByFolder`), so a dropped pack opens as the pack, and a
 * dropped song folder as that song. Chart folders come first at the root, so a folder's place among them is its bar.
 */
export function navigationAfterDrop(
  chartEntries: readonly LibraryChart[],
  previousChartIds: ReadonlySet<string>,
  showLibraryRoot = false,
): BrowseSnapshot | undefined {
  const added = chartEntries.filter((chartEntry) => !previousChartIds.has(chartEntry.id));
  if (added.length === 0) return undefined;
  const addedLabels = new Set(groupChartsByFolder(added).map((folder) => folder.label));
  const folders = groupChartsByFolder(chartEntries);
  const first = Math.max(
    0,
    folders.findIndex((folder) => addedLabels.has(folder.label)),
  );
  if (!showLibraryRoot && addedLabels.size === 1) return { folderPath: [...addedLabels], cursors: [first, 0] };
  return { folderPath: [], cursors: [first] };
}