• Not Exported

    Check if navigating to a previous track based on local history

    Analyzes the local track history to determine if a track change appears to be backward navigation (i.e., the user clicked "previous") rather than a skip or normal track progression.

    This function uses a sophisticated algorithm that considers:

    • Position of the track in recent history
    • Timing of recent navigations
    • Progress through the current track
    • Navigation patterns and behaviors

    Parameters

    • newTrackId: string

      ID of the track being navigated to

    • previousTrackId: string

      ID of the track being navigated from

    • progressPercent: number

      How far through the previous track the user was (0.0-1.0)

    Returns boolean

    Whether this appears to be backward navigation

    // Check if a track change was backward navigation
    const wasGoingBack = isBackwardNavigationInLocalHistory(
    newTrackId,
    previousTrackId,
    0.15 // User was 15% through the previous track
    );

    if (wasGoingBack) {
    console.log('User navigated to a previous track (clicked prev button)');
    }
    function isBackwardNavigationInLocalHistory(
    newTrackId: string,
    previousTrackId: string,
    progressPercent: number,
    ): boolean {
    if (!newTrackId || recentlyPlayedHistory.length <= 1) return false;

    const now = Date.now();

    // If we're coming from a track we recently navigated to,
    // this is probably a skip, not backward navigation
    if (
    lastNavigatedToTrackId === previousTrackId &&
    now - lastNavigationTimestamp < NAVIGATION_EXPIRY_TIME &&
    progressPercent < 0.3 // Less than 30% through the track
    ) {
    store.saveLog(
    `User likely skipping a previously navigated-to track (${previousTrackId})`,
    "DEBUG",
    );
    return false;
    }

    // Skip the first entry (current track) and look for the new track ID
    let positionInHistory = -1;
    for (let i = 1; i < recentlyPlayedHistory.length; i++) {
    if (recentlyPlayedHistory[i].id === newTrackId) {
    positionInHistory = i;
    break;
    }
    }

    if (positionInHistory === -1) return false;

    // Only consider it backward navigation if:
    // 1. The track is in our recent history (position found)
    // 2. It's relatively recent (one of the last 10 tracks)
    // 3. OR it was played very recently (last 2 minutes)
    const isRecentPosition = positionInHistory < 10;
    const trackTimestamp = recentlyPlayedHistory[positionInHistory].timestamp;
    const isRecentTime = now - trackTimestamp < 120000; // 2 minutes

    if (isRecentPosition || isRecentTime) {
    store.saveLog(
    `Local history indicates backward navigation to previously played track at position ${positionInHistory}`,
    "DEBUG",
    );

    // Remember this as a backward navigation for future reference
    lastNavigatedToTrackId = newTrackId;
    lastNavigationTimestamp = now;

    return true;
    }

    // If the track is in history but not recent, don't consider it backward navigation
    // This handles the case of skipping to a track that was played long ago
    return false;
    }