Skip to content

HTTP Server Search Issue #43

@Proofeek

Description

@Proofeek

I noticed that the search in the HTTP server always returns only one result. I used AI to fix this — it rewrote the DoSearch function in the HTML, and now the search works correctly. I'm not familiar with the code and don't know how good the AI-written code is, but it works, and I thought it might be helpful to someone.

function DoSearch() {
    const search_term = document.getElementById('searchbox').value.toLowerCase();

    // Cancel the previous timeout and request, if any
    if (searchTimeout) {
        clearTimeout(searchTimeout);
    }
    if (currentFetchController) {
        currentFetchController.abort();
    }

    // Create a new AbortController for the current request
    currentFetchController = new AbortController();
    const { signal } = currentFetchController;

    // If the search string is empty, show the full list
    if (search_term === '') {
        unfilteredQueuePaused = false;
        // Move all pending requests back to the main queue
        albumArtQueue = [...unfilteredAlbumArtQueue, ...albumArtQueue];
        unfilteredAlbumArtQueue = [];

        // Use setTimeout to ensure that clearing and displaying
        // happen after all previously canceled requests
        searchTimeout = setTimeout(() => {
            ClearList();
            ParseSongList(originalSongListINI);
            if (!albumArtFetchInProgress && albumArtQueue.length > 0) {
                processAlbumArtQueue();
            }
        }, 20); // Short delay for stabilization
        return;
    }

    // If there is a search query
    unfilteredQueuePaused = true;

    // Start the search with a delay
    searchTimeout = setTimeout(() => {
        // Parse the original list for searching
        const fullSongList = ParseListINI(originalSongListINI);

        // Filter songs
        const searchResults = fullSongList.filter(song => 
            song.title.toLowerCase().includes(search_term) ||
            song.artist.toLowerCase().includes(search_term) ||
            (song.album && song.album.toLowerCase().includes(search_term))
        );

        // Clear and display the results
        ClearList();
        searchResults.forEach((song, index) => {
            AddSongToList(song, index);
        });
        AddSongCountToList(searchResults.length);

        // Start loading album art
        if (!albumArtFetchInProgress && albumArtQueue.length > 0) {
            processAlbumArtQueue();
        }
    }, 400); // Debounce delay
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions