<commit_before>import h3 from "../h3.js";
import Page from "../controls/Page.js";
import { getItems, getItem, deleteItem } from "../services/api.js";
import { getIcon, getObject } from "../services/utils.js";
import ActionBar from "../controls/ActionBar.js";
import TabNav from "../controls/TabNav.js";
import MasterDetail from "../controls/MasterDetail.js";
import Paginator from "../controls/Paginator.js";
import octicon from "../services/octicon.js";

const loadItems = async (collection) => {
  const result = await getItems(collection, h3.state.query);
  h3.dispatch("items/set", result.results);
  h3.dispatch("total/set", result.total);
};

const setup = async (state) => {
  const collection = h3.route.parts.collection || "notes";
  const selection = h3.route.parts.id || "";
  let item;
  const oldPage = h3.state.page;
  h3.dispatch("search/set", h3.route.params.q);
  h3.dispatch("page/set", parseInt(h3.route.parts.page || 1));
  if (
    h3.state.collection !== collection ||
    h3.state.page !== oldPage ||
    h3.route.params.reload ||
    h3.route.params.q
  ) {
    await loadItems(collection);
    h3.dispatch("collection/set", collection);
  }
  if (selection) {
    item = await getItem(collection, selection);
  }
  h3.dispatch("item/set", item);
  h3.dispatch("selection/set", selection);
  h3.dispatch("loading/clear");
};

const Home = () => {
  const add = () => h3.navigateTo(`/${h3.state.collection}/add`);
  const cancelAction = () => h3.dispatch("alert/clear") || h3.redraw();
  const deleteAction = async () => {
    const result = await deleteItem(h3.state.collection, h3.state.selection);
    if (result) {
      h3.dispatch("alert/clear");
      h3.navigateTo(`/${h3.state.collection}`);
      await loadItems(h3.state.collection);
    }
    h3.redraw();
  };
  const actions = [
    {
      onclick: () => h3.navigateTo(`/${h3.state.collection}/add`),
      icon: "plus",
      label: "Add",
    },
    {
      onclick: () =>
        h3.navigateTo(`/${h3.state.collection}/${h3.state.selection}/edit`),
      icon: "pencil",
      label: "Edit",
      disabled: !h3.state.selection,
    },
    {
      onclick: () => {
        const confirm = {
          type: "warn",
          buttonType: "danger",
          label: "Yes, delete!",
          action: deleteAction,
          cancelAction,
          message: `Do you really want to delete ${getObject(
            h3.state.item.id
          )} '${h3.state.item.data.title}'?!`,
        };
        h3.dispatch("alert/set", confirm);
        h3.redraw();
      },
      icon: "trashcan",
      label: "Delete",
      disabled: !h3.state.selection,
    },
    {
      onclick: () => {
        const textArea = document.createElement("textarea");
        textArea.value = h3.state.item.data.text || h3.state.item.data.code;
        document.body.appendChild(textArea);
        textArea.select();
        document.execCommand("Copy");
        textArea.remove();
      },
      icon: "clippy",
      label: "Copy",
      disabled: !h3.state.selection,
    },
  ];
  const tabnav = {
    title: "Main Navigation",
    extras: ActionBar(actions),
    tabs: [
      {
        title: "Notes",
        onclick: async () => {
          if (!h3.route.path.match(/^\/notes/)) {
            h3.navigateTo("/notes");
          }
        },
        selected: h3.state.collection === "notes",
        icon: getIcon("notes"),
      },
      {
        title: "Snippets",
        onclick: async () => {
          if (!h3.route.path.match(/^\/snippet/)) {
            h3.navigateTo("/snippets");
          }
        },
        selected: h3.state.collection === "snippets",
        icon: getIcon("snippets"),
      },
    ],
  };
  const page = parseInt(h3.route.parts.page || 1);
  const totalPages = Math.ceil(h3.state.total / h3.state.query.limit);
  const content = h3("div.content.d-flex.flex-1.flex-column", [
    TabNav(tabnav),
    h3(
      "div.top-info-bar.d-flex.flex-row.flex-justify-between.flex-items-center",
      [
        h3("div", [
          h3("span", `Total ${h3.state.collection}: `),
          h3("strong", String(h3.state.total)),
          h3.state.query.search &&
            h3("span", [
              h3("span", { $html: " &middot; Searching for: " }),
              h3("strong", h3.state.query.search),
              h3(
                "button.btn.btn-invisible",
                {
                  onclick: () => h3.navigateTo(h3.route.path, { reload: true }),
                },
                [octicon("x"), "Clear"]
              ),
            ]),
        ]),

        totalPages > 1 &&
          Paginator({
            current: page,
            total: totalPages,
            callback: (n) => h3.navigateTo(`/${h3.state.collection}/${n}`),
          }),
      ]
    ),
    MasterDetail({<commit_msg>Improvements.<commit_after>import h3 from "../h3.js";
import Page from "../controls/Page.js";
import { getItems, getItem, deleteItem } from "../services/api.js";
import { getIcon, getObject } from "../services/utils.js";
import ActionBar from "../controls/ActionBar.js";
import TabNav from "../controls/TabNav.js";
import MasterDetail from "../controls/MasterDetail.js";
import Paginator from "../controls/Paginator.js";
import octicon from "../services/octicon.js";

const loadItems = async (collection) => {
  const result = await getItems(collection, h3.state.query);
  h3.dispatch("items/set", result.results);
  h3.dispatch("total/set", result.total);
};

const setup = async (state) => {
  const collection = h3.route.parts.collection || "notes";
  const selection = h3.route.parts.id || "";
  let item;
  const oldPage = h3.state.page;
  h3.dispatch("search/set", h3.route.params.q);
  h3.dispatch("page/set", parseInt(h3.route.parts.page || 1));
  if (
    h3.state.collection !== collection ||
    h3.state.page !== oldPage ||
    h3.route.params.reload ||
    h3.route.params.q
  ) {
    await loadItems(collection);
    h3.dispatch("collection/set", collection);
  }
  if (selection) {
    item = await getItem(collection, selection);
  }
  h3.dispatch("item/set", item);
  h3.dispatch("selection/set", selection);
  h3.dispatch("loading/clear");
};

const Home = () => {
  const add = () => h3.navigateTo(`/${h3.state.collection}/add`);
  const cancelAction = () => h3.dispatch("alert/clear") || h3.redraw();
  const deleteAction = async () => {
    const result = await deleteItem(h3.state.collection, h3.state.selection);
    if (result) {
      h3.dispatch("alert/clear");
      h3.navigateTo(`/${h3.state.collection}`);
      await loadItems(h3.state.collection);
    }
    h3.redraw();
  };
  const actions = [
    {
      onclick: () => h3.navigateTo(`/${h3.state.collection}/add`),
      icon: "plus",
      label: "Add",
    },
    {
      onclick: () =>
        h3.navigateTo(`/${h3.state.collection}/${h3.state.selection}/edit`),
      icon: "pencil",
      label: "Edit",
      disabled: !h3.state.selection,
    },
    {
      onclick: () => {
        const confirm = {
          type: "warn",
          buttonType: "danger",
          label: "Yes, delete!",
          action: deleteAction,
          cancelAction,
          message: `Do you really want to delete ${getObject(
            h3.state.item.id
          )} '${h3.state.item.data.title}'?!`,
        };
        h3.dispatch("alert/set", confirm);
        h3.redraw();
      },
      icon: "trashcan",
      label: "Delete",
      disabled: !h3.state.selection,
    },
    {
      onclick: () => {
        const textArea = document.createElement("textarea");
        textArea.value = h3.state.item.data.text || h3.state.item.data.code;
        document.body.appendChild(textArea);
        textArea.select();
        document.execCommand("Copy");
        textArea.remove();
      },
      icon: "clippy",
      label: "Copy",
      disabled: !h3.state.selection,
    },
  ];
  const tabnav = {
    title: "Main Navigation",
    extras: ActionBar(actions),
    tabs: [
      {
        title: "Notes",
        onclick: async () => {
          if (!h3.route.path.match(/^\/notes/)) {
            h3.navigateTo("/notes");
          }
        },
        selected: h3.state.collection === "notes",
        icon: getIcon("notes"),
      },
      {
        title: "Snippets",
        onclick: async () => {
          if (!h3.route.path.match(/^\/snippet/)) {
            h3.navigateTo("/snippets");
          }
        },
        selected: h3.state.collection === "snippets",
        icon: getIcon("snippets"),
      },
    ],
  };
  const page = parseInt(h3.route.parts.page || 1);
  const totalPages = Math.ceil(h3.state.total / h3.state.query.limit);
  const content = h3("div.content.d-flex.flex-1.flex-column", [
    TabNav(tabnav),
    h3(
      "div.top-info-bar.d-flex.flex-row.flex-justify-between.flex-items-center",
      [
        h3("div.d-flex.flex-row.flex-items-center", [
          h3("div.total.d-flex.flex-row.flex-items-center", [
            h3("span", { $html: `Total ${h3.state.collection}:&nbsp;` }),
            h3("strong", String(h3.state.total)),
          ]),
          h3.state.query.search &&
            h3("div-search.d-flex.flex-row.flex-items-center", [
              h3("span", { $html: "&nbsp;&middot;&nbsp;Searching for:&nbsp;" }),
              h3("strong", h3.state.query.search),
              h3(
                "button.btn.btn-invisible",
                {
                  style: "margin-bottom: -1px;",
                  onclick: () => h3.navigateTo(h3.route.path, { reload: true }),
                },
                [octicon("x"), "Clear"]
              ),
            ]),
        ]),

        totalPages > 1 &&
          Paginator({
            current: page,
            total: totalPages,
            callback: (n) => h3.navigateTo(`/${h3.state.collection}/${n}`),
          }),
      ]
    ),
    MasterDetail({
{'fraction_non_alphanumeric': 0.1806176351076798, 'fraction_numerical': 0.015440877691995123, 'mean_word_length': 2.1234137055837565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, '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': '3988399', 'n_tokens_mistral': 3202, 'n_tokens_neox': 3196, 'n_words': 531}