<commit_before>// TODO: popup theme switcher
// TODO: when popup 10 items are deleted but list has more, then bug

declare function require(arg: string): string;

import * as $ from "jquery";
import {Intent, IRecentlyWatched, IRuntimeResponse} from "./common";
import {loadSettings} from "./utils";

let recentlyWatched = $("#recently-watched");
let recentlyWatchedList = $("#recently-watched__list");
let noItemsOverlay = $("#no-items");

// @Event Listener
$(".setting").on("click", () => {
    chrome.runtime.sendMessage({
        intent: Intent.Open_Options,
    });
});

// @Event Listener
loadSettings(["quickLink"]).then(res => {
    $(".logo").on("click", () => {
        chrome.tabs.create({
            url: res.quickLink || "https://9anime.to",
        });
    });
});

// @Event Listener
$(".integrate-mal").on("click", () => {
    chrome.runtime.sendMessage({
        intent: Intent.Open_Options,
        params: {
            goto: "MyAnimeList",
        },
    });
});

function showOverlay() {
    noItemsOverlay.show();
    recentlyWatched.hide();
}

function hideOverlay() {
    noItemsOverlay.hide();
    recentlyWatched.show();
}

function removeListItem(el: HTMLElement, animeId: string): void {
    chrome.runtime.sendMessage({
        animeId,
        intent: Intent.Recently_Watched_Remove,
    }, (r: IRuntimeResponse) => {
        if (r.success) {
            let target = $(el);
            // The data contains the number of items that
            // are currently in the recently watched list.
            // If this hits 0, we show our awesome no item
            // overlay, else just the animation/remove.
            if ((r.data as number) > 0) {
                target.parents(".item").addClass("fade-out");
                setTimeout(() => {
                    target.parents(".item").remove();
                }, 500);
            } else {
                target.parents(".item").remove();
                showOverlay();
            }
        }
    });
}

function generateListItem(item: IRecentlyWatched, visible= true): JQuery<HTMLElement> {
    let episodeSpan = "";
    let url = item.url;
    // If epNum is not present we just show the animeName.
    // If epId is not present, the url wont have the `/epId`
    // part appended at the end.
    if (item.epNum && item.epId) {
        episodeSpan = `<span class="label">E${item.epNum}</span>`;
        url += `/${item.epId}`;
    }
    let display = visible ? "flex" : "none";
    let listItem = $(
        `<div class="item" style="display: ${display}">
                    <div class="item-body">
                        <span class="name">${item.animeName}</span>
                            ${episodeSpan}
                    </div>
                    <img src="images/remove-icon.png" class="remove" alt="Remove Item">
                </div>`,
    );
    listItem.find(".item-body").on("click", () => {
        chrome.tabs.create({
            url,
        });
    });
    listItem.find(".remove").on("click", e => {
        removeListItem(e.currentTarget, item.animeId);
    });
    return listItem;
}
<commit_msg>ref: cleanup<commit_after>// TODO: popup theme switcher
// TODO: when popup 10 items are deleted but list has more, then bug
// FIXME: sanitize the values from the localStorage before binding to popup (AMO advisory)

declare function require(arg: string): string;

import * as $ from "jquery";
import {Intent, IRecentlyWatched, IRuntimeResponse} from "./common";
import {loadSettings} from "./utils";

let recentlyWatched = $("#recently-watched");
let recentlyWatchedList = $("#recently-watched__list");
let noItemsOverlay = $("#no-items");

// @Event Listener
$(".setting").on("click", () => {
    chrome.runtime.sendMessage({
        intent: Intent.Open_Options,
    });
});

// @Event Listener
loadSettings(["quickLink"]).then(res => {
    $(".logo").on("click", () => {
        chrome.tabs.create({
            url: res.quickLink || "https://9anime.to",
        });
    });
});

// @Event Listener
$(".integrate-mal").on("click", () => {
    chrome.runtime.sendMessage({
        intent: Intent.Open_Options,
        params: {
            goto: "MyAnimeList",
        },
    });
});

function showOverlay() {
    noItemsOverlay.show();
    recentlyWatched.hide();
}

function hideOverlay() {
    noItemsOverlay.hide();
    recentlyWatched.show();
}

function removeListItem(el: HTMLElement, animeId: string): void {
    chrome.runtime.sendMessage({
        animeId,
        intent: Intent.Recently_Watched_Remove,
    }, (r: IRuntimeResponse) => {
        if (r.success) {
            let target = $(el);
            // The data contains the number of items that
            // are currently in the recently watched list.
            // If this hits 0, we show our awesome no item
            // overlay, else just the animation/remove.
            if ((r.data as number) > 0) {
                target.parents(".item").addClass("fade-out");
                setTimeout(() => {
                    target.parents(".item").remove();
                }, 500);
            } else {
                target.parents(".item").remove();
                showOverlay();
            }
        }
    });
}

function generateListItem(item: IRecentlyWatched, visible= true): JQuery<HTMLElement> {
    let episodeSpan = "";
    let url = item.url;
    // If epNum is not present we just show the animeName.
    // If epId is not present, the url wont have the `/epId`
    // part appended at the end.
    if (item.epNum && item.epId) {
        episodeSpan = `<span class="label">E${item.epNum}</span>`;
        url += `/${item.epId}`;
    }
    let display = visible ? "flex" : "none";
    let listItem = $(
        `<div class="item" style="display: ${display}">
            <div class="item-body">
                <span class="name">${item.animeName}</span>
                    ${episodeSpan}
            </div>
            <img src="images/remove-icon.png" class="remove" alt="Remove Item">
        </div>`,
    );
    listItem.find(".item-body").on("click", () => {
        chrome.tabs.create({
            url,
        });
    });
    listItem.find(".remove").on("click", e => {
        removeListItem(e.currentTarget, item.animeId);
    });
    return listItem;
}

{'fraction_non_alphanumeric': 0.16177409061901724, 'fraction_numerical': 0.0025526483726866626, 'mean_word_length': 2.1597782258064515, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 41, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2681958', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1817, 'n_words': 440}