Choose whose notes reach your bell
Some checks failed
CI / Lint, Typen, Tests, Build (push) Failing after 5m49s
CI / Migrationen auf leerer Datenbank (push) Successful in 10m24s

The bell is a shared pile: every active HR person sees every open note,
regardless of who wrote it. That was agreed and it stays the default —
this narrows it, it never widens it. You can now untick colleagues whose
notes you do not want to see.

What gets stored is the *exceptions*, not the selection. The difference
shows the day someone new joins HR: had the selection been stored, she
would be invisible to everyone until each person ticked her, and nobody
would notice her follow-ups piling up. This way she is visible from day
one and hiding her is a deliberate act. Same reasoning that made notes a
shared inbox in the first place — the silent gap is worse than a row too
many.

Own notes always come through: `note_mutes` rejects a self-reference, and
the predicate says so again rather than depending on a check constraint
staying put. Notes with no author come through too — hiding one because
nobody knows who wrote it is exactly the loss this list exists to prevent.

The rule lives in lib/notes.ts as one SQL expression because two places
need it: the bell in the header and the "Anstehend" card on the dashboard.
Two copies drift, and then the card counts something the bell does not
show.

No SQL function and no audit row, unlike anything that touches employee
data — this is a personal display preference, and an audit trail recording
every tick would make finding real changes harder. Same pattern as saved
reports and hire drafts, and the owner policy on note_mutes means a row
for someone else cannot be written even with invented values.

The checkbox flips immediately and flips back if saving fails; the list
gets clicked through several at a time and a round trip per tick feels
like hesitation.

Verified: 19 tests, five mutation-checked (or→and, dropping the own-notes
clause, inverting `not exists`, inverting the default, and losing the
email fallback each turn them red). Typecheck, lint, schema drift, 477
tests and the build are clean. Not seen in a browser: login goes through
the company account and the database is unreachable — the migration is
reviewed but has not been run.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-09 20:00:13 +02:00
parent e1b69fb022
commit 99b1df9735
12 changed files with 614 additions and 15 deletions

View File

@@ -1,24 +1,47 @@
"use client";
import { Bell } from "lucide-react";
import { Bell, Users } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useRef, useState } from "react";
import { completeEmployeeNote } from "@/actions/employees";
import { setNotizSichtbarkeit } from "@/actions/notes";
import { useToast } from "@/components/ui/Toast";
import { NOTE_CATEGORY_STYLES } from "@/lib/colors";
import { fmtDate } from "@/lib/format";
import type { OpenNote } from "@/lib/notes";
export type Kollege = { id: string; name: string; sichtbar: boolean };
// Click-outside mechanics borrowed from CountryPicker (useRef + mousedown
// listener) — without its draft-text reset, which has no equivalent here.
export function NotesBell({ notes }: { notes: OpenNote[] }) {
export function NotesBell({ notes, kollegen }: { notes: OpenNote[]; kollegen: Kollege[] }) {
const { showToast } = useToast();
const router = useRouter();
const [open, setOpen] = useState(false);
const [completingId, setCompletingId] = useState<string | null>(null);
const [zeigeEinstellung, setZeigeEinstellung] = useState(false);
// Der Haken springt sofort um und wird zurückgedreht, wenn das Speichern
// scheitert. Ohne das fühlt sich jeder Klick wie eine halbe Sekunde
// Bedenkzeit an — die Liste wird beim Durchgehen mehrfach angetippt.
const [eigen, setEigen] = useState<Record<string, boolean>>({});
const containerRef = useRef<HTMLDivElement>(null);
const sichtbarkeit = (k: Kollege) => eigen[k.id] ?? k.sichtbar;
const ausgeblendet = kollegen.filter((k) => !sichtbarkeit(k)).length;
async function schalte(k: Kollege) {
const neu = !sichtbarkeit(k);
setEigen((v) => ({ ...v, [k.id]: neu }));
const result = await setNotizSichtbarkeit({ kollegeId: k.id, sichtbar: neu });
if (result.success) {
router.refresh();
} else {
setEigen((v) => ({ ...v, [k.id]: !neu }));
showToast(result.error ?? "Fehler beim Speichern.", "error");
}
}
useEffect(() => {
function onClickOutside(e: MouseEvent) {
if (containerRef.current && !containerRef.current.contains(e.target as Node)) {
@@ -54,7 +77,53 @@ export function NotesBell({ notes }: { notes: OpenNote[] }) {
{open && (
<div className="absolute right-0 z-20 mt-2 max-h-[28rem] w-96 overflow-y-auto rounded border border-border bg-white shadow-[var(--shadow-card-hover)]">
<div className="border-b border-border px-4 py-2.5 text-xs font-bold uppercase tracking-wide text-ink-muted">Meine Notizen ({notes.length})</div>
<div className="flex items-center justify-between gap-2 border-b border-border px-4 py-2.5">
<span className="text-xs font-bold uppercase tracking-wide text-ink-muted">
Meine Notizen ({notes.length})
</span>
<button
type="button"
onClick={() => setZeigeEinstellung((v) => !v)}
aria-expanded={zeigeEinstellung}
className={`flex items-center gap-1 rounded px-1.5 py-0.5 text-xs font-semibold hover:bg-surface focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-brand-500 ${
zeigeEinstellung ? "text-brand-700" : "text-ink-muted"
}`}
>
<Users className="h-3.5 w-3.5" />
Kolleg:innen
{/* Nur wenn eingeschränkt: sonst ist die Zahl bei jedem
Aufschlagen zu lesen, ohne je etwas zu bedeuten. */}
{ausgeblendet > 0 && <span className="text-warning-text">({ausgeblendet} aus)</span>}
</button>
</div>
{zeigeEinstellung && (
<div className="border-b border-border bg-surface px-4 py-3">
<p className="mb-2 text-xs text-ink-muted">
Wessen Notizen hier erscheinen. Die eigenen sind immer dabei.
</p>
{kollegen.length === 0 ? (
<p className="py-2 text-sm text-ink-muted">Keine weiteren HR-Kolleg:innen freigeschaltet.</p>
) : (
<ul className="flex flex-col gap-1">
{kollegen.map((k) => (
<li key={k.id}>
<label className="flex cursor-pointer items-center gap-2 rounded px-1 py-1 text-sm hover:bg-white">
<input
type="checkbox"
checked={sichtbarkeit(k)}
onChange={() => schalte(k)}
className="h-4 w-4 rounded border-border text-brand-600 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-brand-500"
/>
<span className={sichtbarkeit(k) ? "text-ink" : "text-ink-muted line-through"}>{k.name}</span>
</label>
</li>
))}
</ul>
)}
</div>
)}
{notes.length === 0 ? (
<p className="px-4 py-6 text-center text-sm text-ink-muted">Keine offenen Notizen.</p>
) : (