-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from afspeirs/feat/add-folder-support
feat: add folder support
- Loading branch information
Showing
17 changed files
with
596 additions
and
103 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Disclosure } from '@headlessui/react'; | ||
import { ChevronUpIcon } from '@heroicons/react/24/outline'; | ||
import { useAtomValue } from 'jotai'; | ||
import { useCallback } from 'react'; | ||
import { useRxData, type QueryConstructor } from 'rxdb-hooks'; | ||
|
||
import { type NoteDocType } from '@/api/types'; | ||
import { Button } from '@/components/Button'; | ||
// import { FolderContextMenu } from './FolderContextMenu'; | ||
import { NotesList } from '@/components/NotesList'; | ||
import { notesSearchAtom } from '@/context/notesSearch'; | ||
import { notesSortAtom, notesSortOptions } from '@/context/notesSort'; | ||
import { classNames } from '@/utils/classNames'; | ||
import type { FolderListItemProps } from './types'; | ||
|
||
export function FolderListItem({ | ||
folder, | ||
}: FolderListItemProps) { | ||
const search = useAtomValue(notesSearchAtom); | ||
const sort = useAtomValue(notesSortAtom); | ||
const notesQuery: QueryConstructor<NoteDocType> = useCallback( | ||
(collection) => collection.find({ | ||
selector: { | ||
folder: { | ||
$eq: folder, | ||
}, | ||
text: { | ||
$regex: RegExp(search, 'i'), | ||
}, | ||
}, | ||
sort: notesSortOptions[sort].value, | ||
}), | ||
[folder, search, sort], | ||
); | ||
|
||
const { result: notes, isFetching } = useRxData<NoteDocType>('notes', notesQuery); | ||
return ( | ||
<li | ||
key={folder} | ||
className="group/folder-context-menu relative flex flex-col" | ||
onContextMenu={(event) => event.preventDefault()} | ||
> | ||
<Disclosure> | ||
{({ open }) => ( | ||
<> | ||
<Disclosure.Button | ||
as={Button} | ||
secondaryAction={( | ||
<ChevronUpIcon | ||
className={classNames( | ||
'h-6 w-6 transform transition-transform', | ||
open ? 'rotate-180' : 'rotate-90', | ||
)} | ||
aria-hidden="true" | ||
/> | ||
)} | ||
> | ||
{folder} | ||
</Disclosure.Button> | ||
<Disclosure.Panel | ||
className="w-full pl-8" | ||
> | ||
<NotesList | ||
notes={notes} | ||
isFetching={isFetching} | ||
padding={false} | ||
/> | ||
</Disclosure.Panel> | ||
</> | ||
)} | ||
</Disclosure> | ||
</li> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { useAtom, useAtomValue } from 'jotai'; | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
import { ViewportList } from 'react-viewport-list'; | ||
import { useRxData, type QueryConstructor } from 'rxdb-hooks'; | ||
|
||
import type { NoteDocType } from '@/api/types'; | ||
import { foldersAtom } from '@/context/folders'; | ||
import { notesSearchAtom } from '@/context/notesSearch'; | ||
import { notesSortAtom, notesSortOptions } from '@/context/notesSort'; | ||
import { FolderListItem } from './FolderListItem'; | ||
|
||
export function FolderList() { | ||
const ref = useRef<HTMLUListElement | null>(null); | ||
// const setFolders = useSetAtom(foldersAtom); | ||
const [folders, setFolders] = useAtom(foldersAtom); | ||
const search = useAtomValue(notesSearchAtom); | ||
const sort = useAtomValue(notesSortAtom); | ||
const notesQuery: QueryConstructor<NoteDocType> = useCallback( | ||
(collection) => collection.find({ | ||
selector: { | ||
folder: { | ||
$exists: true, | ||
}, | ||
text: { | ||
$regex: RegExp(search, 'i'), | ||
}, | ||
}, | ||
sort: notesSortOptions[sort].value, | ||
}), | ||
[search, sort], | ||
); | ||
|
||
const { result: notes, isFetching } = useRxData<NoteDocType>('notes', notesQuery); | ||
|
||
useEffect(() => { | ||
const allFolders = notes.map((note) => note.folder ?? '').filter(Boolean); | ||
const newFolders = [...new Set(allFolders)].sort(); | ||
// console.log(newFolders); | ||
setFolders(newFolders); | ||
}, [notes, setFolders]); | ||
|
||
return ( | ||
<ul | ||
role="list" | ||
className="flex flex-col overflow-y-auto overflow-x-hidden mb-1" | ||
ref={ref} | ||
> | ||
{isFetching && ( | ||
<li className="block p-3">Loading...</li> | ||
)} | ||
<ViewportList | ||
viewportRef={ref} | ||
items={folders} | ||
> | ||
{/* TODO: Maybe I should be passing this through to query the database less? */} | ||
{/* // folderNotes={notes.filter((note) => note.folder === folder)} */} | ||
{(folder) => <FolderListItem key={folder} folder={folder} />} | ||
</ViewportList> | ||
</ul> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// import type { RxDocument } from 'rxdb'; | ||
|
||
// import type { NoteDocType } from '@/api/types'; | ||
|
||
export type FolderListItemProps = { | ||
folder: string, | ||
// folderNotes: RxDocument<NoteDocType>[], | ||
}; |
Oops, something went wrong.