Is it possible to hide a custom view if the query result is empty?

A drastic way (use with caution) would be to add this code inside file custom.js:

function processNoResults(block) {
    const classList = block.classList;
    if (classList.contains("hidden")) return;

    function hideNoResults(div) {
        if (div.textContent === "No matched result") classList.add("hidden");
    }
    block.querySelectorAll(".custom-query-results .text-sm").forEach(hideNoResults);
}

const noResultsObserver = new MutationObserver(() => {
    document.querySelectorAll(".ls-block").forEach(processNoResults);
});

noResultsObserver.observe(document.getElementById("main-content-container"), {
    attributes: true,
    subtree: true,
    attributeFilter: ["class"],
});