mgsloan / todoist-shortcuts

Browser extension which adds comprehensive keyboard shortcuts to Todoist, beyond what is offered by Todoist itself
MIT License
313 stars 23 forks source link

Auto-Hide Favorites With 0 Tasks #195

Open jakelfreeman opened 2 years ago

jakelfreeman commented 2 years ago

Posting this as a potential enhancement that I coded for myself a few months ago. I was tired of looking at filters in my favorites list that didn't have any tasks, so created a code snippet to auto-hide them when the last task is completed and have them show up again as soon as they contain a task. I'm a self-taught, noobie coder so I'm sure this is far from elegant, but figured I'd share the concept in case anyone wants to improve upon it and/or include it in future releases of this extension:

function initiate_hide_filters(){
    var param = "AuiqUdaGFITJcNpaWKvZFA==";
    var escapedParam = param.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g,'\\$1');

    const targetNode = document.querySelectorAll(".sidebar_expansion_panel,.expansion_panel,.expansion_panel--expanded")[0];

    const config = { attributes: false, childList: true, subtree: true };

    const callback = function(mutationsList, observer) {
        var favContainer = $(".sidebar_expansion_panel.expansion_panel.expansion_panel--expanded")[0];
        $(favContainer).find("li").each(function(){ 
            var container = $(this).find($("."+escapedParam)); 
            if(container.length==0){ $(this).hide(); }else{ $(this).show(); }
        });
        if(AUTO_HIDE_EMPTY_LABELS){
            var labelContainer = $(".sidebar_expansion_panel.expansion_panel.expansion_panel--expanded")[1];
            $(labelContainer).find("li").not(".FilterListItemSortable").each(function(){ 
                var labelCounter = $(this).find(".counter_count"); 
                if(labelCounter.length==0){ $(this).hide(); }else{ $(this).show(); }
            });
        }
    };

    const observer = new MutationObserver(callback);

    observer.observe(targetNode, config);
    var favContainer = $(".sidebar_expansion_panel.expansion_panel.expansion_panel--expanded")[0];
    $(favContainer).find("li").not(".FilterListItemSortable").each(function(){ 
        var container = $(this).find($("."+escapedParam)); 
        if(container.length==0){ $(this).hide(); }else{ $(this).show(); }
    });
    if(AUTO_HIDE_EMPTY_LABELS){
        var labelContainer = $(".sidebar_expansion_panel.expansion_panel.expansion_panel--expanded")[1];
        $(labelContainer).find("li").each(function(){ 
            var labelCounter = $(this).find(".counter_count"); 
            if(labelCounter.length==0){ $(this).hide(); }else{ $(this).show(); }
        });
    }
}

Then, to get it to run post-load, I just used:

$(document).ready(function(){ setTimeout(initiate_hide_filters, 3000); });
mgsloan commented 2 years ago

Nice! I definitely would like to auto-hide my favorite filters that have no tasks.

One trickiness with adding such functionality is that so far todoist-shortcuts has no options. So, would need to add an options page. Which isn't /that/ hard, but still I feel a bit of hesitance to do so. Might revisit this in the future!