π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { useCallback, useEffect, useState } from 'react';
import { useLocation } from 'react-router';
import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
const COMMENT_QUERY = 'comment';
const COMMENT_THREAD_QUERY = 'commentThread';
/** Why a deep-link target could not be landed on. */
export type DocumentCommentFocusMissReason = 'failed' | 'missing';
export interface DocumentCommentFocus {
/** The comment to land on and highlight; equals `rootCommentId` for roots. */
commentId: string;
rootCommentId: string;
/**
* Whether landing scrolls the card into view. A notification deep link must
* bring its target on screen; a pick made in the body must not move the
* reader's viewport (the comment list lives below the document).
*/
scroll: boolean;
/** Changes on every deep link so the same comment can be focused twice in a row. */
token: number;
}
/**
* Turns a notification deep link (`?commentThread=<root>&comment=<id>`) into a
* focus target for the comment list and strips the query so a reload or a
* shared URL doesn't replay it. The list itself is responsible for paging
* for landing on the target β see `DocumentComments` / `Thread` β and calls
* `clearFocus` (thread gone) or `focusRoot` (only the reply is gone).
*/
export const useDocumentCommentDeepLink = (documentId: string) => {
const location = useLocation();
const navigate = useWorkspaceAwareNavigate();
const [focus, setFocus] = useState<(DocumentCommentFocus & { documentId: string }) | undefined>(
undefined,
);
useEffect(() => {
const searchParams = new URLSearchParams(location.search);
const rootCommentId = searchParams.get(COMMENT_THREAD_QUERY);
if (!rootCommentId) return;
const commentId = searchParams.get(COMMENT_QUERY) ?? rootCommentId;
setFocus((current) => ({
commentId,
documentId,
rootCommentId,
scroll: true,
token: (current?.token ?? 0) + 1,
}));
searchParams.delete(COMMENT_QUERY);
searchParams.delete(COMMENT_THREAD_QUERY);
const search = searchParams.toString();
navigate(`${location.pathname}${search ? `?${search}` : ''}${location.hash}`, {
replace: true,
});
}, [documentId, location.hash, location.pathname, location.search, navigate]);
const clearFocus = useCallback(() => setFocus(undefined), []);
/**
* Pin a thread picked from the body whose card is not loaded yet β the same
* pinning a notification deep link gets, minus the URL round trip and minus
* the scroll: pointing at a sentence must not throw the reader to the list.
*/
const focusThread = useCallback(
(rootCommentId: string) =>
setFocus((current) => ({
commentId: rootCommentId,
documentId,
rootCommentId,
scroll: false,
token: (current?.token ?? 0) + 1,
})),
[documentId],
);
/** Fall back to the thread root when the linked reply itself is gone. */
const focusRoot = useCallback(
() =>
setFocus(
(current) =>
current && { ...current, commentId: current.rootCommentId, token: current.token + 1 },
),
[],
);
// A focus target belongs to the document it was opened on.
return {
clearFocus,
focus: focus?.documentId === documentId ? focus : undefined,
focusRoot,
focusThread,
};
};