This repository was archived by the owner on Mar 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathContentContext.tsx
More file actions
99 lines (87 loc) · 3.25 KB
/
Copy pathContentContext.tsx
File metadata and controls
99 lines (87 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React, { createContext, useContext, useMemo, ReactNode, useState, useCallback } from 'react'
import posthog from 'posthog-js'
import { useChatContext } from './ChatContext'
import { useFileContext } from './FileContext'
import { getFilesInDirectory, getNextAvailableFileNameGivenBaseName } from '@/lib/file'
interface ContentContextType {
showEditor: boolean
setShowEditor: (showEditor: boolean) => void
openContent: (
pathOrChatID: string,
optionalContentToWriteOnCreate?: string,
dontUpdateChatHistory?: boolean,
scrollToPosition?: number,
) => void
currentOpenFileOrChatID: string | null
createUntitledNote: (parentFileOrDirectory?: string) => void
}
const ContentContext = createContext<ContentContextType | undefined>(undefined)
export const useContentContext = (): ContentContextType => {
const context = useContext(ContentContext)
if (context === undefined) {
throw new Error('useContentContext must be used within a ContentProvider')
}
return context
}
interface ContentProviderProps {
children: ReactNode
}
export const ContentProvider: React.FC<ContentProviderProps> = ({ children }) => {
const [showEditor, setShowEditor] = useState(false)
const [currentOpenFileOrChatID, setCurrentOpenFileOrChatID] = useState<string | null>(null)
const { allChatsMetadata, openNewChat } = useChatContext()
const {
vaultFilesFlattened: flattenedFiles,
openOrCreateFile,
addToNavigationHistory,
selectedDirectory,
} = useFileContext()
const openContent = React.useCallback(
async (
pathOrChatID: string,
optionalContentToWriteOnCreate?: string,
dontUpdateChatHistory?: boolean,
scrollToPosition?: number,
) => {
if (!pathOrChatID) return
const chatMetadata = allChatsMetadata.find((chat) => chat.id === pathOrChatID)
if (chatMetadata) {
openNewChat(pathOrChatID)
} else {
setShowEditor(true)
openOrCreateFile(pathOrChatID, optionalContentToWriteOnCreate, scrollToPosition)
}
setCurrentOpenFileOrChatID(pathOrChatID)
if (!dontUpdateChatHistory) {
addToNavigationHistory(pathOrChatID)
}
},
[allChatsMetadata, openNewChat, openOrCreateFile, addToNavigationHistory],
)
const createUntitledNote = useCallback(
async (parentDirectory?: string) => {
const directoryToMakeFileIn =
parentDirectory || selectedDirectory || (await window.electronStore.getVaultDirectoryForWindow())
const filesInDirectory = await getFilesInDirectory(directoryToMakeFileIn, flattenedFiles)
const fileName = getNextAvailableFileNameGivenBaseName(
filesInDirectory.map((file) => file.name),
'Untitled',
)
const finalPath = await window.path.join(directoryToMakeFileIn, fileName)
openContent(finalPath, `## `)
posthog.capture('created_new_note_from_new_note_modal')
},
[selectedDirectory, flattenedFiles, openContent],
)
const ContentContextMemo = useMemo(
() => ({
showEditor,
setShowEditor,
openContent,
currentOpenFileOrChatID,
createUntitledNote,
}),
[showEditor, openContent, currentOpenFileOrChatID, createUntitledNote],
)
return <ContentContext.Provider value={ContentContextMemo}>{children}</ContentContext.Provider>
}