258 lines
8.1 KiB
TypeScript
258 lines
8.1 KiB
TypeScript
import { useEffect, useState, type CSSProperties } from 'react'
|
|
import { ListChecks, Plus, Trash2, Tags } from 'lucide-react'
|
|
import { api } from './api'
|
|
import { Button } from './components/ui/button'
|
|
import { Input } from './components/ui/input'
|
|
import { Textarea } from './components/ui/textarea'
|
|
import { Field } from './App'
|
|
|
|
export function TagList({ tags = [] }: { tags?: any[] }) {
|
|
if (!tags.length) return null
|
|
return (
|
|
<div className="ticket-tags" aria-label="Tags">
|
|
{tags.map((tag) => (
|
|
<span
|
|
className="ticket-tag"
|
|
key={tag.id}
|
|
style={{ '--tag-color': tag.color } as CSSProperties}
|
|
title={tag.sender ? `Automatisch für ${tag.sender}` : tag.name}
|
|
>
|
|
{tag.name}
|
|
</span>
|
|
))}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export function QueueTagsEditor({ queue, onSave }: any) {
|
|
const [tags, setTags] = useState<any[]>([]),
|
|
[busy, setBusy] = useState(false)
|
|
useEffect(
|
|
() =>
|
|
setTags(
|
|
(queue.tags || []).map((tag: any) => ({
|
|
...tag,
|
|
_key: tag.id,
|
|
sendersText: tag.senders.join('\n'),
|
|
})),
|
|
),
|
|
[queue.id, queue.tags_revision],
|
|
)
|
|
const change = (index: number, key: string, value: string) =>
|
|
setTags((current) => current.map((tag, i) => (i === index ? { ...tag, [key]: value } : tag)))
|
|
return (
|
|
<form
|
|
className="queue-tag-editor"
|
|
onSubmit={async (event) => {
|
|
event.preventDefault()
|
|
setBusy(true)
|
|
try {
|
|
await onSave({
|
|
revision: queue.tags_revision,
|
|
tags: tags.map((tag) => ({
|
|
id: tag.id,
|
|
name: tag.name,
|
|
color: tag.color,
|
|
senders: tag.sendersText
|
|
.split(/[;,\n]/)
|
|
.map((email: string) => email.trim())
|
|
.filter(Boolean),
|
|
})),
|
|
})
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}}
|
|
>
|
|
<h3>
|
|
<Tags size={17} /> Tags & Absender
|
|
</h3>
|
|
<p className="muted">
|
|
Eingehende Mails dieser Absender erhalten automatisch die passenden Tags. Pro Tag sind
|
|
mehrere vollständige E-Mail-Adressen möglich; Groß- und Kleinschreibung spielt keine Rolle.
|
|
</p>
|
|
{!tags.length && <p className="empty">Noch keine Tags für diese Queue.</p>}
|
|
{tags.map((tag, index) => (
|
|
<div className="queue-tag-rule" key={tag._key}>
|
|
<div className="tag-rule-heading">
|
|
<TagList tags={[{ ...tag, id: tag._key, name: tag.name || 'Neuer Tag' }]} />
|
|
<Button
|
|
type="button"
|
|
variant="plain"
|
|
size="sm"
|
|
disabled={busy}
|
|
aria-label={`Tag entfernen ${index + 1}`}
|
|
onClick={() => setTags((current) => current.filter((_, i) => i !== index))}
|
|
>
|
|
<Trash2 size={15} /> Entfernen
|
|
</Button>
|
|
</div>
|
|
<div className="form-columns">
|
|
<Field label="Tagname">
|
|
<Input
|
|
required
|
|
maxLength={80}
|
|
disabled={busy}
|
|
aria-label={`Tagname ${index + 1}`}
|
|
value={tag.name}
|
|
onChange={(event) => change(index, 'name', event.target.value)}
|
|
/>
|
|
</Field>
|
|
<Field label="Tagfarbe">
|
|
<Input
|
|
type="color"
|
|
disabled={busy}
|
|
aria-label={`Tagfarbe ${index + 1}`}
|
|
value={tag.color}
|
|
onChange={(event) => change(index, 'color', event.target.value)}
|
|
/>
|
|
</Field>
|
|
</div>
|
|
<Field label="Absenderadressen">
|
|
<Textarea
|
|
rows={3}
|
|
disabled={busy}
|
|
aria-label={`Absenderadressen ${index + 1}`}
|
|
value={tag.sendersText}
|
|
placeholder={'system@example.de\nmonitoring@example.de'}
|
|
onChange={(event) => change(index, 'sendersText', event.target.value)}
|
|
/>
|
|
</Field>
|
|
<small className="muted">
|
|
Eine Adresse pro Zeile oder mit Komma/Semikolon trennen. Maximal 100 Adressen je Tag.
|
|
Ohne Adressen wird der Tag nicht automatisch vergeben.
|
|
</small>
|
|
</div>
|
|
))}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
disabled={busy || tags.length >= 50}
|
|
onClick={() =>
|
|
setTags((current) => [
|
|
...current,
|
|
{ _key: crypto.randomUUID(), name: '', color: '#8b5cf6', sendersText: '' },
|
|
])
|
|
}
|
|
>
|
|
<Plus size={15} /> Tag hinzufügen
|
|
</Button>
|
|
<p className="muted tag-rules-help">
|
|
Regeln gelten ab der nächsten eingehenden Mail. Vorhandene Zuordnungen bleiben beim
|
|
Bearbeiten erhalten. Ein entfernter Tag wird beim Speichern auch von Tickets entfernt.
|
|
</p>
|
|
<div className="form-footer">
|
|
<Button type="submit" disabled={busy}>
|
|
{busy ? 'Wird gespeichert …' : 'Tags speichern'}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
)
|
|
}
|
|
|
|
export function TicketChecklist({ ticketId, items, editable, onReload }: any) {
|
|
const [text, setText] = useState(''),
|
|
[key, setKey] = useState(crypto.randomUUID()),
|
|
[busy, setBusy] = useState(false),
|
|
[error, setError] = useState('')
|
|
const mutate = async (path: string, method: string, data: any) => {
|
|
setBusy(true)
|
|
setError('')
|
|
try {
|
|
await api(path, method, data)
|
|
await onReload()
|
|
return true
|
|
} catch (e) {
|
|
setError((e as Error).message)
|
|
await onReload()
|
|
return false
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
return (
|
|
<section className="ticket-checklist" aria-label="Checkliste">
|
|
<div className="checklist-heading">
|
|
<h2>
|
|
<ListChecks size={18} /> Checkliste
|
|
</h2>
|
|
<span>
|
|
{items.filter((item: any) => item.done).length} / {items.length}
|
|
</span>
|
|
</div>
|
|
{error && (
|
|
<p className="error" role="alert">
|
|
{error}
|
|
</p>
|
|
)}
|
|
{!items.length && <p className="muted checklist-empty">Noch keine Punkte vorhanden.</p>}
|
|
<ul>
|
|
{items.map((item: any) => (
|
|
<li key={item.id}>
|
|
<label className={item.done ? 'checklist-done' : ''}>
|
|
<input
|
|
type="checkbox"
|
|
checked={item.done}
|
|
disabled={busy || !editable}
|
|
onChange={(event) =>
|
|
mutate(`/tickets/${ticketId}/checklist/${item.id}`, 'PATCH', {
|
|
done: event.target.checked,
|
|
revision: item.revision,
|
|
})
|
|
}
|
|
/>
|
|
<span>{item.text}</span>
|
|
</label>
|
|
{editable && (
|
|
<Button
|
|
type="button"
|
|
variant="plain"
|
|
size="xs"
|
|
disabled={busy}
|
|
aria-label={`Checklistenpunkt löschen: ${item.text}`}
|
|
onClick={() =>
|
|
mutate(`/tickets/${ticketId}/checklist/${item.id}`, 'DELETE', {
|
|
revision: item.revision,
|
|
})
|
|
}
|
|
>
|
|
<Trash2 size={14} />
|
|
</Button>
|
|
)}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
{editable && (
|
|
<form
|
|
className="checklist-add"
|
|
onSubmit={async (event) => {
|
|
event.preventDefault()
|
|
if (!text.trim() || busy) return
|
|
if (await mutate(`/tickets/${ticketId}/checklist`, 'POST', { id: key, text })) {
|
|
setText('')
|
|
setKey(crypto.randomUUID())
|
|
}
|
|
}}
|
|
>
|
|
<Input
|
|
aria-label="Neuer Checklistenpunkt"
|
|
placeholder="Neuer Punkt …"
|
|
maxLength={500}
|
|
value={text}
|
|
disabled={busy}
|
|
onChange={(event) => setText(event.target.value)}
|
|
/>
|
|
<Button
|
|
type="submit"
|
|
size="sm"
|
|
disabled={busy || !text.trim()}
|
|
aria-label="Checklistenpunkt hinzufügen"
|
|
>
|
|
<Plus size={16} />
|
|
</Button>
|
|
</form>
|
|
)}
|
|
</section>
|
|
)
|
|
}
|