mirror of
https://github.com/outline/outline.git
synced 2026-11-30 17:10:36 +03:00
* feat: Allow document links to suppress view recording Adds a suppressView query parameter that prevents a view from being recorded when a document is opened, removing it from the URL afterwards so that copied links record views as normal. * refactor: Use useConsumeQueryParam to read suppressView Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { useRef, useEffect, useState } from "react";
|
|
import useConsumeQueryParam from "~/hooks/useConsumeQueryParam";
|
|
import type Document from "~/models/Document";
|
|
import { SuppressViewParam } from "~/utils/suppressView";
|
|
import { isTruthyQueryValue } from "~/utils/urls";
|
|
|
|
const MARK_AS_VIEWED_AFTER = 3 * 1000;
|
|
type Props = {
|
|
document: Document;
|
|
children?: React.JSX.Element;
|
|
};
|
|
|
|
function MarkAsViewed(props: Props) {
|
|
const { document, children } = props;
|
|
const viewTimeout = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
const suppress = isTruthyQueryValue(useConsumeQueryParam(SuppressViewParam));
|
|
|
|
// Only the document that was linked to is suppressed, not any navigated to
|
|
// afterwards.
|
|
const [suppressedDocumentId] = useState(() =>
|
|
suppress ? document.id : undefined
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (document.id === suppressedDocumentId) {
|
|
return;
|
|
}
|
|
|
|
viewTimeout.current = setTimeout(async () => {
|
|
const view = await document.view();
|
|
|
|
if (view) {
|
|
document.updateLastViewed(view);
|
|
}
|
|
}, MARK_AS_VIEWED_AFTER);
|
|
|
|
return () => {
|
|
if (viewTimeout.current) {
|
|
clearTimeout(viewTimeout.current);
|
|
}
|
|
};
|
|
}, [document, suppressedDocumentId]);
|
|
|
|
return children || null;
|
|
}
|
|
|
|
export default MarkAsViewed;
|