Lass E-Mails ueber die Faecher finden und halte Kapitel sowie das Menue in der Akte an ihrem Ort.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3465ed57f6
commit
67d48378c9
11 files changed
+696
-160
No files matched your search
+138
-26
@@ -1,6 +1,7 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
import { api } from "../api.js";
|
||||
import { formatDate } from "./content.jsx";
|
||||
import { ErrorNote } from "./widgets.jsx";
|
||||
|
||||
export function Sources() {
|
||||
@@ -197,8 +198,13 @@ export function PaperlessPicker({ context, placement, onDone }) {
|
||||
export function MailPicker({ context, placement, onDone }) {
|
||||
const [accounts, setAccounts] = useState([]);
|
||||
const [accountId, setAccountId] = useState("");
|
||||
const [folders, setFolders] = useState([]);
|
||||
const [mailbox, setMailbox] = useState("");
|
||||
const [draft, setDraft] = useState("");
|
||||
const [query, setQuery] = useState("");
|
||||
const [items, setItems] = useState([]);
|
||||
const [nextBefore, setNextBefore] = useState(null);
|
||||
const [loading, setLoading] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
@@ -211,18 +217,94 @@ export function MailPicker({ context, placement, onDone }) {
|
||||
.catch((err) => setError(err.message));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!accountId) return undefined;
|
||||
let alive = true;
|
||||
setFolders([]);
|
||||
setMailbox("");
|
||||
setItems([]);
|
||||
setError("");
|
||||
setLoading("Fächer werden gelesen …");
|
||||
api(`/api/v1/sources/imap/mailboxes?connector_id=${accountId}`)
|
||||
.then((data) => {
|
||||
if (!alive) return;
|
||||
const list = data.items || [];
|
||||
setFolders(list);
|
||||
const preferred = list.find((item) => item.selectable && item.name.toUpperCase() === "INBOX")
|
||||
|| list.find((item) => item.selectable);
|
||||
setMailbox(preferred ? preferred.name : "");
|
||||
if (!preferred) setLoading("");
|
||||
})
|
||||
.catch((err) => {
|
||||
if (!alive) return;
|
||||
setLoading("");
|
||||
setError(err.message);
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [accountId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!accountId || !mailbox) return undefined;
|
||||
let alive = true;
|
||||
setQuery("");
|
||||
setDraft("");
|
||||
setItems([]);
|
||||
setNextBefore(null);
|
||||
setError("");
|
||||
setLoading("Nachrichten werden gelesen …");
|
||||
const params = new URLSearchParams({ connector_id: accountId, mailbox });
|
||||
api(`/api/v1/sources/imap/messages?${params}`)
|
||||
.then((data) => {
|
||||
if (!alive) return;
|
||||
setItems(data.items || []);
|
||||
setNextBefore(data.next_before ?? null);
|
||||
setLoading("");
|
||||
})
|
||||
.catch((err) => {
|
||||
if (!alive) return;
|
||||
setLoading("");
|
||||
setError(err.message);
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [accountId, mailbox]);
|
||||
|
||||
async function search(event) {
|
||||
event.preventDefault();
|
||||
if (!accountId) {
|
||||
setError("Lege zuerst ein E-Mail-Konto unter Quellen an.");
|
||||
return;
|
||||
}
|
||||
if (!accountId || !mailbox) return;
|
||||
setError("");
|
||||
setLoading("Nachrichten werden gelesen …");
|
||||
const params = new URLSearchParams({ connector_id: accountId, mailbox });
|
||||
if (draft.trim()) params.set("q", draft.trim());
|
||||
try {
|
||||
const data = await api(`/api/v1/sources/imap/messages?connector_id=${accountId}&q=${encodeURIComponent(query)}`);
|
||||
setItems(data.items);
|
||||
const data = await api(`/api/v1/sources/imap/messages?${params}`);
|
||||
setQuery(draft.trim());
|
||||
setItems(data.items || []);
|
||||
setNextBefore(data.next_before ?? null);
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} finally {
|
||||
setLoading("");
|
||||
}
|
||||
}
|
||||
|
||||
async function older() {
|
||||
if (!nextBefore) return;
|
||||
setError("");
|
||||
setLoading("Ältere Nachrichten werden gelesen …");
|
||||
const params = new URLSearchParams({ connector_id: accountId, mailbox, before: String(nextBefore) });
|
||||
if (query) params.set("q", query);
|
||||
try {
|
||||
const data = await api(`/api/v1/sources/imap/messages?${params}`);
|
||||
setItems((current) => [...current, ...(data.items || [])]);
|
||||
setNextBefore(data.next_before ?? null);
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} finally {
|
||||
setLoading("");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,6 +319,7 @@ export function MailPicker({ context, placement, onDone }) {
|
||||
external_id: item.id,
|
||||
context,
|
||||
...(placement ? { placement } : {}),
|
||||
...(item.document_on ? { dates: { document_on: item.document_on, sent_on: null, received_on: null } } : {}),
|
||||
...(item.message_from ? { detail: { message_from: item.message_from } } : {}),
|
||||
},
|
||||
});
|
||||
@@ -248,28 +331,57 @@ export function MailPicker({ context, placement, onDone }) {
|
||||
|
||||
return (
|
||||
<div className="stack">
|
||||
<form onSubmit={search} className="stack">
|
||||
<label>
|
||||
Konto
|
||||
<select value={accountId} onChange={(event) => setAccountId(event.target.value)}>
|
||||
{accounts.length ? null : <option value="">Kein Konto</option>}
|
||||
{accounts.map((item) => (
|
||||
<option key={item.id} value={item.id}>{item.name}</option>
|
||||
<label>
|
||||
Konto
|
||||
<select value={accountId} onChange={(event) => setAccountId(event.target.value)}>
|
||||
{accounts.length ? null : <option value="">Kein Konto</option>}
|
||||
{accounts.map((item) => (
|
||||
<option key={item.id} value={item.id}>{item.name}</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{!accounts.length ? <p className="muted">Lege zuerst ein E-Mail-Konto unter <Link to="/sources">Quellen</Link> an.</p> : null}
|
||||
<div className="mailbox">
|
||||
<nav className="mailbox__folders" aria-label="Fächer">
|
||||
{folders.map((folder) => (
|
||||
folder.selectable ? (
|
||||
<button
|
||||
key={folder.name}
|
||||
type="button"
|
||||
className={folder.name === mailbox ? "" : "quiet"}
|
||||
style={{ paddingLeft: 12 + folder.depth * 14 }}
|
||||
aria-current={folder.name === mailbox ? "true" : undefined}
|
||||
onClick={() => setMailbox(folder.name)}
|
||||
>
|
||||
{folder.label}
|
||||
</button>
|
||||
) : (
|
||||
<span key={folder.name} className="mailbox__group" style={{ paddingLeft: 12 + folder.depth * 14 }}>{folder.label}</span>
|
||||
)
|
||||
))}
|
||||
</nav>
|
||||
<div className="mailbox__messages">
|
||||
<form onSubmit={search} className="inline">
|
||||
<input value={draft} onChange={(event) => setDraft(event.target.value)} placeholder="Im gewählten Fach suchen" aria-label="Im gewählten Fach suchen" />
|
||||
<button type="submit" className="quiet" disabled={!mailbox || !!loading}>Suchen</button>
|
||||
</form>
|
||||
<ErrorNote error={error} />
|
||||
{accountId && !loading && !mailbox && !error ? <p className="muted">Dieses Konto hat keine lesbaren Fächer.</p> : null}
|
||||
{loading ? <p className="muted">{loading}</p> : null}
|
||||
{!loading && mailbox && !items.length ? (
|
||||
<p className="muted">{query ? "In diesem Fach gibt es dazu keine Nachricht." : "In diesem Fach gibt es keine Nachricht."}</p>
|
||||
) : null}
|
||||
<div className="mail-list">
|
||||
{items.map((item) => (
|
||||
<button key={`${mailbox}:${item.uid || item.id}`} type="button" className="mail-row" onClick={() => link(item)}>
|
||||
<span>{formatDate(item.document_on)}</span>
|
||||
<strong>{item.title}</strong>
|
||||
<span>{item.message_from || "Ohne Absender"}</span>
|
||||
</button>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<div className="inline">
|
||||
<input value={query} onChange={(event) => setQuery(event.target.value)} placeholder="Betreff oder Absender" />
|
||||
<button type="submit">Abrufen</button>
|
||||
</div>
|
||||
{nextBefore ? <button type="button" className="quiet" onClick={older} disabled={!!loading}>Ältere laden</button> : null}
|
||||
</div>
|
||||
</form>
|
||||
<ErrorNote error={error} />
|
||||
<div className="stack">
|
||||
{items.map((item) => (
|
||||
<button key={item.id} type="button" className="quiet" onClick={() => link(item)}>
|
||||
{item.title}{item.message_from ? ` · ${item.message_from}` : ""}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user