SPB Git

Repository Public

actions.ts 3.5 KB 100 lines typescript

1// Actions de l'interface sur « la cible courante » : sélection, sinon fil ouvert, sinon ligne sous le curseur.2import { t } from "../i18n"3import { KW, type EmailFull, type Id } from "../jmap/types"4import { S, folderOfView } from "../state/app"5import { composeFromDraft, composeReply } from "../state/composer"6import { archive, destroyForever, isPermanentDeleteView, markRead, reportJunk, setFlag, setPinned, threadHref, trash } from "../state/mail"7import { navigate } from "../state/session"8import { confirmAsync } from "./kit"910export interface Targets {11  ids: Id[]12  /** vrai : exactement ces messages ; faux : les conversations de ces lignes */13  exact: boolean14}1516export function currentTargets(): Targets | null {17  const s = S.get()18  if (s.selected.size) return { ids: [...s.selected], exact: false }19  if (s.open && s.open.status === "ready" && s.open.emailIds.length) return { ids: s.open.emailIds, exact: true }20  if (s.cursor) return { ids: [s.cursor], exact: false }21  return null22}2324function clearSelection() {25  if (S.get().selected.size) S.set({ selected: new Set() })26}2728export function openRow(id: Id) {29  const s = S.get()30  const e = s.emails[id]31  if (!e) return32  S.set({ cursor: id })33  if (s.view === "drafts" || e.keywords[KW.draft]) void composeFromDraft(id)34  else navigate(threadHref(e.threadId))35}3637export async function doArchive(tg = currentTargets()) {38  if (!tg) return39  clearSelection()40  await archive(tg.ids, tg.exact)41}4243export async function doDelete(tg = currentTargets()) {44  if (!tg) return45  if (isPermanentDeleteView()) {46    const ok = await confirmAsync({ title: t("confirm.deleteForeverTitle"), body: t("confirm.deleteForeverBody", { n: tg.ids.length }), confirmLabel: t("action.deleteForever"), danger: true })47    if (!ok) return48    clearSelection()49    await destroyForever(tg.ids, tg.exact)50    return51  }52  clearSelection()53  await trash(tg.ids, tg.exact)54}5556export async function doJunk(isJunk: boolean, tg = currentTargets()) {57  if (!tg) return58  clearSelection()59  await reportJunk(tg.ids, isJunk, tg.exact)60}6162function anyLacks(ids: Id[], kw: string): boolean {63  const s = S.get()64  return ids.some((id) => !(s.emails[id] ?? s.open?.emails[id])?.keywords[kw])65}6667/** Dans un fil ouvert, favori et épingle portent sur le dernier message qui se trouve dans le dossier affiché (celui que la liste montre). */68function representative(ids: Id[]): Id[] {69  const s = S.get()70  const folder = folderOfView(s.mb ? s.accounts[s.mb] : undefined, s.view)71  const inFolder = folder ? ids.filter((id) => s.open?.emails[id]?.mailboxIds[folder.id]) : []72  return (inFolder.length ? inFolder : ids).slice(-1)73}7475export async function doToggleFlag(tg = currentTargets()) {76  if (!tg) return77  const ids = tg.exact ? representative(tg.ids) : tg.ids78  await setFlag(ids, anyLacks(ids, KW.flagged))79}80export async function doTogglePin(tg = currentTargets()) {81  if (!tg) return82  const ids = tg.exact ? representative(tg.ids) : tg.ids83  await setPinned(ids, anyLacks(ids, KW.pinned))84}85export async function doMarkRead(read: boolean, tg = currentTargets()) {86  if (!tg) return87  await markRead(tg.ids, read)88  if (!read && S.get().open) navigate(threadHref(null))89}9091export function lastOpenEmail(): EmailFull | null {92  const o = S.get().open93  if (!o || o.status !== "ready") return null94  const id = o.emailIds[o.emailIds.length - 1]95  return o.emails[id] ?? null96}97export function doReply(kind: "reply" | "replyAll" | "forward", email: EmailFull | null = lastOpenEmail()) {98  if (email) composeReply(email, kind)99}100
Files main
Menu

Theme