managarten/apps-archived/mukke/apps/mobile/services/queueService.ts
Till JS 7a56699d45 feat(mukke): rename LightWrite to Mukke and add music library, player, playlists
Combines LightWrite (beat/lyrics editor) and Mukke (iOS music player) into
a single web-based music workspace app. Archives the old Mukke mobile app.

- Rename: @lightwrite/* → @mukke/*, all branding, configs, Dockerfiles
- New DB schemas: songs, playlists, playlist_songs + songId FK on projects
- New backend modules: SongModule, PlaylistModule, LibraryModule
- New web: app shell with sidebar, library (songs/albums/artists/genres),
  web player (queue/shuffle/repeat/MediaSession), playlists, search,
  upload, dashboard, album/artist/genre detail pages
- Auth: add forgot-password + reset-password pages, extend auth store
- Tests: 40 backend unit tests (song, playlist, library services)
- Config: env generation, MinIO bucket, docker-compose prod, cloudflare
- Docs: update CLAUDE.md, auth guidelines with SvelteKit checklist

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 09:55:56 +01:00

60 lines
1.7 KiB
TypeScript

import type { Song } from '~/types';
export interface QueueState {
queue: Song[];
originalQueue: Song[];
currentIndex: number;
}
export function createQueue(songs: Song[], startIndex: number = 0): QueueState {
return {
queue: [...songs],
originalQueue: [...songs],
currentIndex: startIndex,
};
}
export function shuffleQueue(state: QueueState): QueueState {
const currentSong = state.queue[state.currentIndex];
const remaining = state.queue.filter((_, i) => i !== state.currentIndex);
// Fisher-Yates shuffle
for (let i = remaining.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[remaining[i], remaining[j]] = [remaining[j], remaining[i]];
}
return {
...state,
queue: currentSong ? [currentSong, ...remaining] : remaining,
currentIndex: 0,
};
}
export function unshuffleQueue(state: QueueState): QueueState {
const currentSong = state.queue[state.currentIndex];
const newIndex = currentSong ? state.originalQueue.findIndex((s) => s.id === currentSong.id) : 0;
return {
...state,
queue: [...state.originalQueue],
currentIndex: Math.max(0, newIndex),
};
}
export function getNextIndex(state: QueueState, repeatMode: 'off' | 'all' | 'one'): number | null {
if (repeatMode === 'one') return state.currentIndex;
if (state.currentIndex < state.queue.length - 1) return state.currentIndex + 1;
if (repeatMode === 'all') return 0;
return null;
}
export function getPreviousIndex(
state: QueueState,
repeatMode: 'off' | 'all' | 'one'
): number | null {
if (repeatMode === 'one') return state.currentIndex;
if (state.currentIndex > 0) return state.currentIndex - 1;
if (repeatMode === 'all') return state.queue.length - 1;
return null;
}