MCPcopy Create free account
hub / github.com/churichard/notabase / useOnNoteLinkClick

Function useOnNoteLinkClick

editor/useOnNoteLinkClick.ts:7–98  ·  view source on GitHub ↗
(currentNoteId: string)

Source from the content-addressed store, hash-verified

5import { queryParamToArray } from 'utils/url';
6
7export default function useOnNoteLinkClick(currentNoteId: string) {
8 const router = useRouter();
9 const {
10 query: { stack: stackQuery },
11 } = router;
12 const openNoteIds = useStore((state) => state.openNoteIds);
13 const isPageStackingOn = useStore((state) => state.isPageStackingOn);
14
15 const onClick = useCallback(
16 (noteId: string, stackNote: boolean, highlightedPath?: Path) => {
17 // If stackNote is false, open the note in its own page
18 if (!stackNote) {
19 const hash = highlightedPath ? `0-${highlightedPath}` : undefined;
20 router.push({
21 pathname: '/app/note/[noteId]',
22 query: { ...router.query, noteId },
23 hash,
24 });
25 return;
26 }
27
28 // If the note is already open, scroll it into view
29 const index = openNoteIds.findIndex(
30 (openNoteId) => openNoteId === noteId
31 );
32 if (index > -1) {
33 const noteElement = document.getElementById(openNoteIds[index]);
34 if (noteElement) {
35 const notesContainer = noteElement.parentElement;
36 const noteWidth = noteElement.offsetWidth;
37 notesContainer?.scrollTo({
38 left: noteWidth * index, // We assume all the notes are the same width
39 behavior: 'smooth',
40 });
41 }
42
43 if (highlightedPath) {
44 // Update highlighted path; scrolling is handled in editor
45 router.push({ hash: `${index}-${highlightedPath}` }, undefined, {
46 shallow: true,
47 });
48 }
49
50 return;
51 }
52
53 // If the note is not open, add it to the open notes after currentNoteId
54 const currentNoteIndex = openNoteIds.findIndex(
55 (openNoteId) => openNoteId === currentNoteId
56 );
57 if (currentNoteIndex < 0) {
58 console.error(
59 `Error: current note ${currentNoteId} is not in open notes`
60 );
61 return;
62 }
63
64 const newNoteIndex = currentNoteIndex + 1;

Callers 5

SidebarNoteLinkFunction · 0.85
BacklinkNoteBranchFunction · 0.85
BacklinkMatchLeafFunction · 0.85
NoteLinkElementFunction · 0.85
BlockRefElementFunction · 0.85

Calls 1

queryParamToArrayFunction · 0.90

Tested by

no test coverage detected