• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jcubic / 10xDevs / 18840571689

27 Oct 2025 12:14PM UTC coverage: 23.648% (-1.9%) from 25.594%
18840571689

push

github

jcubic
update workflow

1783 of 8857 branches covered (20.13%)

Branch coverage included in aggregate %.

761 of 1901 relevant lines covered (40.03%)

1814.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/components/notes/NotesContext.tsx
1
'use client';
2

3
import { createContext, useContext, useEffect, useCallback, type ReactNode } from 'react';
4
import { usePathname, useRouter } from 'next/navigation';
5
import type { NoteTreeNode } from '@/types/tree';
6
import type { SaveStatus } from '@/types/notes';
7
import { useNodeSelection } from '@/hooks/useNodeSelection';
8

9
interface NotesContextValue {
10
  notes: NoteTreeNode[];
11
  selectedNoteId: number | null;
12
  saveStatus: SaveStatus;
13
  setNotes: (notes: NoteTreeNode[] | ((prev: NoteTreeNode[]) => NoteTreeNode[])) => void;
14
  setSaveStatus: (status: SaveStatus) => void;
15
  updateNoteContent: (noteId: number, content: string) => void;
16
  updateNoteName: (noteId: number, name: string) => void;
17
  markNoteDirty: (noteId: number, dirty: boolean) => void;
18
  getSelectedNote: () => NoteTreeNode | null;
19
  getNote: (noteId: number) => NoteTreeNode | null;
20
  selectNote: (noteId: number | null) => void;
21
}
22

23
const NotesContext = createContext<NotesContextValue | undefined>(undefined);
×
24

25
export function useNotesContext() {
26
  const context = useContext(NotesContext);
×
27
  if (context === undefined) {
×
28
    throw new Error('useNotesContext must be used within a NotesProvider');
×
29
  }
30
  return context;
×
31
}
32

33
interface NotesProviderProps {
34
  children: ReactNode;
35
  initialNotes?: NoteTreeNode[];
36
  initialSelectedNoteId?: number | null;
37
}
38

39
export function NotesProvider({
40
  children,
41
  initialNotes = [],
×
42
  initialSelectedNoteId = null
×
43
}: NotesProviderProps) {
44
  const pathname = usePathname();
×
45
  const router = useRouter();
×
46

47
  // Use the hook that manages all the state
48
  const {
49
    notes,
50
    selectedNoteId,
51
    saveStatus,
52
    setNotes,
53
    setSaveStatus,
54
    updateSelection,
55
    updateDirtyFlag,
56
    updateNoteContent,
57
    updateNoteName
58
  } = useNodeSelection(initialNotes, initialSelectedNoteId);
×
59

60
  // Sync notes state when initialNotes prop changes (e.g., after redirect)
61
  useEffect(() => {
×
62
    if (initialNotes.length > 0 && notes.length === 0) {
×
63
      setNotes(initialNotes);
×
64
    }
65
  }, [initialNotes, notes.length, setNotes]);
66

67
  const markNoteDirty = updateDirtyFlag;
×
68

69
  const getSelectedNote = useCallback((): NoteTreeNode | null => {
×
70
    if (!selectedNoteId) return null;
×
71
    return notes.find((note) => note.id === selectedNoteId) || null;
×
72
  }, [notes, selectedNoteId]);
73

74
  const getNote = useCallback(
×
75
    (noteId: number): NoteTreeNode | null => {
76
      return notes.find((note) => note.id === noteId) || null;
×
77
    },
78
    [notes]
79
  );
80

81
  // Note selection with Next.js router
82
  const selectNote = useCallback(
×
83
    (noteId: number | null) => {
84
      // Update state immediately
85
      updateSelection(noteId);
×
86

87
      // Navigate using Next.js router
88
      if (noteId === null) {
×
89
        router.push('/');
×
90
      } else {
91
        router.push(`/note/${noteId}`);
×
92
      }
93
    },
94
    [updateSelection, router]
95
  );
96

97
  // Sync URL to state on URL changes
98
  useEffect(() => {
×
99
    const noteMatch = pathname.match(/\/note\/(\d+)/);
×
100
    const urlNoteId = noteMatch ? parseInt(noteMatch[1], 10) : null;
×
101

102
    if (urlNoteId !== null) {
×
103
      updateSelection(urlNoteId);
×
104
    }
105
  }, [pathname, updateSelection]);
106

107
  const value: NotesContextValue = {
×
108
    notes,
109
    selectedNoteId,
110
    saveStatus,
111
    setNotes,
112
    setSaveStatus,
113
    updateNoteContent,
114
    updateNoteName,
115
    markNoteDirty,
116
    getSelectedNote,
117
    getNote,
118
    selectNote
119
  };
120

121
  return <NotesContext.Provider value={value}>{children}</NotesContext.Provider>;
×
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc