repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
SheetFilter.astroAstro173 lines7.1 KiBraw download
---
import Icon from './Icon.astro';
import SheetFilterBars from './SheetFilterBars.astro';
import type { SheetFacets } from '../lib/sheets';

interface Group {
  key: string;
  label: string;
  count: number;
  accent: string;
}

interface Props {
  /** One entry per row in the list below — the same facets the rows carry,
   *  so the panel's opening numbers and the script's agree. */
  rows: SheetFacets[];
  /** Completes "Search …": `this shelf`, `all sheets`, `#kerberos`. */
  scope: string;
  accent?: string;
  /** The list's own grouping — sections on a shelf, shelves elsewhere —
   *  drawn as the second split bar. Omit for a flat list. */
  groups?: Group[];
  groupLabel?: string;
  /** Tags every row shares (the shelf's own name, the tag page's tag): a
   *  chip that cannot narrow anything is noise. */
  hideTags?: string[];
}
const { rows, scope, accent = 'love', groups = [], groupLabel = 'Sections', hideTags = [] } = Astro.props;

/**
 * The filter panel, in the main site's category-page grammar: a search field
 * with a live count, the list's tags as chips, and a stats block whose bars
 * are the same filters drawn as proportions. Everything in it that shows a
 * number is a button — pressing a bar narrows the list just as its chip does.
 *
 * The markup here is the panel at rest. scripts/sheet-filter.ts binds it,
 * and from then on owns every count, width and pressed state; the
 * `data-sf-*` attributes are the contract between the two.
 */
const pad = (n: number) => String(n).padStart(2, '0');
const tally = (values: string[]) => {
  const map = new Map<string, number>();
  for (const v of values) map.set(v, (map.get(v) ?? 0) + 1);
  return [...map.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
};

const hidden = new Set(hideTags.map((t) => t.toLowerCase()));
const tags = tally(rows.flatMap((r) => r.tags)).filter(([t]) => !hidden.has(t));
// A tag used once is a label, not a way to narrow a list: on a long list
// they would bury the chips that do something. Short lists keep them all.
const CHIP_FLOOR = tags.length > 30 ? 2 : 1;
const CHIPS_SHOWN = 12;
const chips = tags.filter(([, n]) => n >= CHIP_FLOOR);
const extra = Math.max(0, chips.length - CHIPS_SHOWN);

const BARS = 6;
const techniques = tags.slice(0, BARS);

const DIFFS = [
  { key: 'beginner', accent: 'foam' },
  { key: 'intermediate', accent: 'gold' },
  { key: 'advanced', accent: 'love' },
].map((d) => ({ ...d, count: rows.filter((r) => r.diff === d.key).length }))
  .filter((d) => d.count > 0);

// The tools the list reaches for most. They sit under the difficulty split
// on a flat list and under the techniques on a grouped one, whichever column
// is the shorter, so the two stay roughly level.
const tools = tally(rows.flatMap((r) => r.tools)).slice(0, 4);
const grouped = groups.length > 1;
---
<section class="sf" id="filter" data-sheet-filter aria-label="Filter sheets" style={`--sf-acc: var(--${accent});`}>
  <div class="sf__bar">
    <label class="sf__search">
      <Icon name="search" />
      <span class="sr-only">Search {scope}</span>
      <input
        type="search" data-sf-q placeholder={`Search ${scope}`}
        autocomplete="off" spellcheck="false" enterkeyhint="search" />
    </label>
    <span class="sf__count" data-sf-count aria-live="polite">{rows.length} shown</span>
  </div>

  {chips.length > 0 && (
    <div class="sf__chips" data-sf-chips>
      {chips.map(([tag, n], i) => (
        <button
          type="button" class="sf-chip" data-sf-tag={tag} aria-pressed="false"
          data-sf-extra={i >= CHIPS_SHOWN ? '' : undefined} hidden={i >= CHIPS_SHOWN}
        >{tag}<span class="sf-chip__n" data-sf-n={`tag:${tag}`}>{pad(n)}</span></button>
      ))}
      {extra > 0 && (
        <button type="button" class="sf-chip sf-chip--aux" data-sf-more={`+ ${extra} more`} aria-expanded="false">
          <span data-sf-more-label>+ {extra} more</span>
        </button>
      )}
      <button type="button" class="sf-chip sf-chip--aux" data-sf-clear hidden>Clear filters ×</button>
    </div>
  )}
  {chips.length === 0 && (
    <div class="sf__chips" data-sf-chips>
      <button type="button" class="sf-chip sf-chip--aux" data-sf-clear hidden>Clear filters ×</button>
    </div>
  )}

  {/* Open in the markup, so it reads in full without JS. On a narrow screen
      the script folds it: there the block is a thousand pixels tall and would
      sit between the search field and its results. */}
  <details class="sf__fold" data-sf-fold open>
  <summary><span>Breakdown</span><span class="sf__fold-mark" aria-hidden="true"></span></summary>
  <div class="sf__stats corners">
    <div class="sf__col">
      {DIFFS.length > 0 && (
        <div class="sf-split">
          <p class="sf__label">Difficulty</p>
          <div class="sf-split__bar" aria-hidden="true">
            {DIFFS.map((d) => (
              <i data-sf-grow={`diff:${d.key}`} data-sf-mark={`diff:${d.key}`} style={`flex-grow:${d.count}; --seg: var(--${d.accent});`}></i>
            ))}
          </div>
          <div class="sf-legend sf-legend--wide">
            {DIFFS.map((d) => (
              <button type="button" class="sf-key" data-sf-diff={d.key} aria-pressed="false" style={`--seg: var(--${d.accent});`}>
                <i aria-hidden="true"></i>{d.key}<span class="sf-key__n" data-sf-n={`diff:${d.key}`}>{pad(d.count)}</span>
              </button>
            ))}
          </div>
        </div>
      )}

      {groups.length > 1 && (
        <div class="sf-split">
          <p class="sf__label">{groupLabel}</p>
          <div class="sf-split__bar" aria-hidden="true">
            {groups.map((g) => (
              <i data-sf-grow={`sec:${g.key}`} data-sf-mark={`sec:${g.key}`} style={`flex-grow:${g.count}; --seg: var(--${g.accent});`}></i>
            ))}
          </div>
          <div class="sf-legend">
            {groups.map((g) => (
              <button type="button" class="sf-key" data-sf-sec={g.key} aria-pressed="false" style={`--seg: var(--${g.accent});`}>
                <i aria-hidden="true"></i>{g.label}<span class="sf-key__n" data-sf-n={`sec:${g.key}`}>{pad(g.count)}</span>
              </button>
            ))}
          </div>
        </div>
      )}

      {!grouped && tools.length > 0 && <SheetFilterBars label="Tools reached for" facet="tool" items={tools} accent="pine" />}
    </div>

    {(techniques.length > 0 || (grouped && tools.length > 0)) && (
      <div class="sf__col">
        {techniques.length > 0 && <SheetFilterBars label="Techniques that recur" facet="tag" items={techniques} />}
        {grouped && tools.length > 0 && <SheetFilterBars label="Tools reached for" facet="tool" items={tools} accent="pine" />}
      </div>
    )}
  </div>
  </details>

  <p class="sf__empty" data-sf-empty hidden>
    <span>No sheets match these filters.</span>
    <button type="button" class="sf-chip sf-chip--aux" data-sf-clear>Clear filters ×</button>
  </p>
</section>

<script>
  import { initSheetFilter } from '../scripts/sheet-filter';

  document.addEventListener('astro:page-load', initSheetFilter);
  if (document.readyState !== 'loading') initSheetFilter();
  else document.addEventListener('DOMContentLoaded', initSheetFilter, { once: true });
</script>