2025-08-21 11:08:33 +03:00
|
|
|
import { useEffect, useRef, useState } from "preact/hooks";
|
2025-08-21 10:05:53 +03:00
|
|
|
import { t } from "../services/i18n";
|
|
|
|
|
import FormTextBox from "./react/FormTextBox";
|
2025-08-21 10:53:59 +03:00
|
|
|
import { useNoteContext, useNoteProperty, useSpacedUpdate } from "./react/hooks";
|
2025-08-21 10:05:53 +03:00
|
|
|
import protected_session_holder from "../services/protected_session_holder";
|
|
|
|
|
import server from "../services/server";
|
2025-08-21 10:34:30 +03:00
|
|
|
import "./note_title.css";
|
2025-08-21 11:08:33 +03:00
|
|
|
import { isLaunchBarConfig } from "../services/utils";
|
2025-08-20 23:53:13 +03:00
|
|
|
|
2025-08-20 21:50:06 +03:00
|
|
|
export default function NoteTitleWidget() {
|
2025-08-21 11:08:33 +03:00
|
|
|
const { note, noteId, componentId, viewScope, noteContext } = useNoteContext();
|
|
|
|
|
const title = useNoteProperty(note, "title", componentId);
|
2025-08-21 10:44:58 +03:00
|
|
|
const isProtected = useNoteProperty(note, "isProtected");
|
2025-08-21 10:53:59 +03:00
|
|
|
const newTitle = useRef("");
|
2025-08-21 10:05:53 +03:00
|
|
|
|
2025-08-21 11:08:33 +03:00
|
|
|
const [ isReadOnly, setReadOnly ] = useState<boolean>(false);
|
|
|
|
|
const [ navigationTitle, setNavigationTitle ] = useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const isReadOnly = note === null
|
|
|
|
|
|| note === undefined
|
|
|
|
|
|| (note.isProtected && !protected_session_holder.isProtectedSessionAvailable())
|
|
|
|
|
|| isLaunchBarConfig(note.noteId)
|
|
|
|
|
|| viewScope?.viewMode !== "default";
|
|
|
|
|
setReadOnly(isReadOnly);
|
|
|
|
|
}, [ note?.noteId, note?.isProtected, viewScope?.viewMode ]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (isReadOnly) {
|
|
|
|
|
noteContext?.getNavigationTitle().then(setNavigationTitle);
|
|
|
|
|
}
|
|
|
|
|
}, [isReadOnly]);
|
|
|
|
|
|
2025-08-21 10:05:53 +03:00
|
|
|
const spacedUpdate = useSpacedUpdate(async () => {
|
|
|
|
|
if (!note) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
protected_session_holder.touchProtectedSessionIfNecessary(note);
|
2025-08-21 10:53:59 +03:00
|
|
|
await server.put<void>(`notes/${noteId}/title`, { title: newTitle.current }, componentId);
|
2025-08-21 10:05:53 +03:00
|
|
|
});
|
|
|
|
|
|
2025-08-20 21:50:06 +03:00
|
|
|
return (
|
2025-08-21 10:34:30 +03:00
|
|
|
<div className="note-title-widget">
|
2025-08-21 10:05:53 +03:00
|
|
|
<FormTextBox
|
|
|
|
|
autocomplete="off"
|
2025-08-21 11:08:33 +03:00
|
|
|
currentValue={(!isReadOnly ? title : navigationTitle) ?? ""}
|
2025-08-21 10:05:53 +03:00
|
|
|
placeholder={t("note_title.placeholder")}
|
2025-08-21 10:34:30 +03:00
|
|
|
className={`note-title ${isProtected ? "protected" : ""}`}
|
2025-08-21 10:05:53 +03:00
|
|
|
tabIndex={100}
|
2025-08-21 11:08:33 +03:00
|
|
|
readOnly={isReadOnly}
|
2025-08-21 10:05:53 +03:00
|
|
|
onChange={(newValue) => {
|
2025-08-21 10:53:59 +03:00
|
|
|
newTitle.current = newValue;
|
2025-08-21 10:05:53 +03:00
|
|
|
spacedUpdate.scheduleUpdate();
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2025-08-21 10:34:30 +03:00
|
|
|
</div>
|
2025-08-20 21:50:06 +03:00
|
|
|
);
|
|
|
|
|
}
|