Files
knur-app/components/notes-editor.tsx

50 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

2026-06-22 11:32:27 +02:00
"use client";
import { useRef, useState, useTransition } from "react";
type Props = {
initialNotes: string;
onSave: (notes: string) => Promise<void>;
};
export function NotesEditor({ initialNotes, onSave }: Props) {
const [value, setValue] = useState(initialNotes);
const [saved, setSaved] = useState(false);
const [isPending, startTransition] = useTransition();
const savedTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
const dirty = value !== initialNotes;
function handleSave() {
startTransition(async () => {
await onSave(value);
if (savedTimer.current) clearTimeout(savedTimer.current);
setSaved(true);
savedTimer.current = setTimeout(() => setSaved(false), 2000);
});
}
return (
2026-06-22 15:19:38 +02:00
<div className="flex flex-col gap-2 rounded-2xl border border-white/10 bg-surface/55 shadow-lg shadow-black/10 backdrop-blur-md p-4">
2026-06-22 11:32:27 +02:00
<label className="text-sm font-medium text-fg/60">Notatki</label>
<textarea
value={value}
onChange={(e) => { setValue(e.target.value); setSaved(false); }}
placeholder="Dodaj notatki do tego treningu…"
rows={3}
2026-06-22 15:19:38 +02:00
className="w-full resize-none rounded border border-white/10 bg-bg px-3 py-2 text-sm text-fg placeholder:text-fg/30 focus:border-accent focus:outline-none"
2026-06-22 11:32:27 +02:00
/>
<div className="flex items-center justify-end gap-3">
{saved && <span className="text-xs text-fg/40">Zapisano</span>}
<button
onClick={handleSave}
disabled={!dirty || isPending}
2026-06-22 15:19:38 +02:00
className="btn btn-primary px-3 py-1.5 text-xs"
2026-06-22 11:32:27 +02:00
>
{isPending ? "Zapisuję…" : "Zapisz"}
</button>
</div>
</div>
);
}