Files

20 lines
963 B
TypeScript

import { useCrdtText, useLiveField, type ExactReference } from "@quixos/camino-react";
import { interfaces } from "./gen/client.js";
/** Clear only the submitted text, preserving a newer collaborative draft. */
export function usePrompt(reference: ExactReference, path: "name" | "change" | "followup" | "generate") {
const operation = interfaces.ReadingNavigation.prompts.edit;
const draft = useCrdtText({ key: reference.object + "/prompt/" + path, operation, reference, path: [path] });
const field = useLiveField({ key: reference.object + "/prompt-clear/" + path, operation, reference });
return {
...draft,
clearSubmitted(expected: string) {
const current = field.current();
if (current.status !== "ready") return;
const snapshot = current.binding.getSnapshot();
if (snapshot.value[path] === expected)
current.binding.edit((editor) => editor.spliceText([path], 0, expected.length, ""), snapshot);
},
};
}