Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

3.1 KB
Raw Blame
import { i18n } from "@/lib/i18n";
import type { BoardData, BoardTask } from "@/lib/api/types";
import { isOverdue } from "@/lib/format";
import { STATUS_ARCHIVED, STATUS_PLANNED } from "@/lib/style";

export const UNASSIGNED = "__unassigned__";

export type BoardFilters = {
  query: string;
  assignees: string[];
  priorities: string[];
  labels: string[];
  overdueOnly: boolean;
};

export const emptyFilters: BoardFilters = {
  query: "",
  assignees: [],
  priorities: [],
  labels: [],
  overdueOnly: false,
};

export function filterCount(f: BoardFilters) {
  return (
    f.assignees.length +
    f.priorities.length +
    f.labels.length +
    (f.overdueOnly ? 1 : 0)
  );
}

export function matches(f: BoardFilters, task: BoardTask): boolean {
  const q = f.query.trim();
  if (q) {
    const inTitle = task.title.toLowerCase().includes(q.toLowerCase());
    const inNumber =
      String(task.number ?? "") ===
      q.replace(/^#/, "").replace(/^[A-Za-z]+-/, "");
    if (!inTitle && !inNumber) return false;
  }
  if (f.assignees.length) {
    const key = task.assigneeId ?? task.userId ?? UNASSIGNED;
    if (!f.assignees.includes(key)) return false;
  }
  if (
    f.priorities.length &&
    !f.priorities.includes(task.priority ?? "no-priority")
  )
    return false;
  if (
    f.labels.length &&
    !(task.labels ?? []).some((l) => f.labels.includes(l.name))
  )
    return false;
  if (f.overdueOnly && !isOverdue(task.dueDate)) return false;
  return true;
}

/** One swipeable page of the board: a workflow column, or the virtual planned/archived lists. */
export type Lane = {
  status: string;
  name: string;
  icon?: string | null;
  isFinal: boolean;
  tasks: BoardTask[];
  total: number;
};

export function lanes(
  data: BoardData,
  f: BoardFilters,
  planned: boolean,
  archived: boolean,
): Lane[] {
  const cols = data.columns ?? [];
  const out: Lane[] = [];
  if (planned)
    out.push({
      status: STATUS_PLANNED,
      name: i18n.t("mobile:board.planned"),
      isFinal: false,
      tasks: data.plannedTasks.filter((t) => matches(f, t)),
      total: data.plannedTasks.length,
    });
  cols.forEach((col) => {
    const tasks = col.tasks ?? [];
    out.push({
      status: col.slug,
      name: col.name,
      icon: col.icon,
      isFinal: !!col.isFinal,
      tasks: tasks.filter((t) => matches(f, t)),
      total: tasks.length,
    });
  });
  if (archived)
    out.push({
      status: STATUS_ARCHIVED,
      name: i18n.t("mobile:board.archived"),
      isFinal: false,
      tasks: data.archivedTasks.filter((t) => matches(f, t)),
      total: data.archivedTasks.length,
    });
  return out;
}

/**
 * A position after every task in `status`. The status endpoint keeps a moved task's old position,
 * which can drop it anywhere in the new column, so moves go through reorder with this instead.
 */
export function endPosition(data: BoardData, status: string): number {
  const tasks =
    status === STATUS_PLANNED
      ? data.plannedTasks
      : status === STATUS_ARCHIVED
        ? data.archivedTasks
        : (data.columns.find((col) => col.slug === status)?.tasks ?? []);
  return tasks.reduce((max, t) => Math.max(max, (t.position ?? 0) + 1), 0);
}