-
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 #259 from afspeirs/feat/add-notes-list-to-home-page
add NotesList to HomePage
- Loading branch information
Showing
18 changed files
with
240 additions
and
177 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,72 +1,31 @@ | ||
import { Disclosure } from '@headlessui/react'; | ||
import { useAtomValue } from 'jotai'; | ||
import { ChevronUpIcon, FolderClosedIcon, FolderOpenIcon } from 'lucide-react'; | ||
import { useCallback } from 'react'; | ||
import { useRxData } from 'rxdb-hooks'; | ||
import { FolderClosedIcon } from 'lucide-react'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
|
||
import type { NoteDocType, NoteQuery } from '@/api/types'; | ||
import { Button } from '@/components/Button'; | ||
import { NotesList } from '@/components/NotesList'; // eslint-disable-line import/no-cycle | ||
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: NoteQuery = 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); | ||
const [searchParams] = useSearchParams(); | ||
const searchParamsFolder = searchParams.get('folder'); | ||
|
||
return ( | ||
<li | ||
key={folder} | ||
className="group/folder-context-menu relative flex flex-col" | ||
onContextMenu={(event) => event.preventDefault()} | ||
> | ||
<Disclosure> | ||
{({ open }) => ( | ||
<> | ||
<Disclosure.Button | ||
as={Button} | ||
Icon={open ? FolderOpenIcon : FolderClosedIcon} | ||
secondaryAction={( | ||
<ChevronUpIcon | ||
className={classNames( | ||
'size-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} | ||
/> | ||
</Disclosure.Panel> | ||
</> | ||
)} | ||
</Disclosure> | ||
<Button | ||
active={folder === searchParamsFolder} | ||
href={{ | ||
pathname: '/', | ||
search: `folder=${folder}`, | ||
}} | ||
Icon={FolderClosedIcon} | ||
> | ||
{folder} | ||
</Button> | ||
</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,40 @@ | ||
import { useRef } from 'react'; | ||
import { ViewportList } from 'react-viewport-list'; | ||
|
||
import { classNames } from '@/utils/classNames'; | ||
import { FolderListItem } from './FolderListItem'; | ||
import { FolderListProps } from './types'; | ||
|
||
export function FolderList({ | ||
folders, | ||
isFetching, | ||
padding, | ||
}: FolderListProps) { | ||
const ref = useRef<HTMLUListElement | null>(null); | ||
|
||
return ( | ||
<ul | ||
role="list" | ||
className={classNames( | ||
'flex flex-col h-full overflow-y-auto overflow-x-hidden', | ||
padding ? 'p-2' : '', | ||
)} | ||
ref={ref} | ||
> | ||
{isFetching && ( | ||
<li className="block p-3 sm:py-2">Loading...</li> | ||
)} | ||
{!isFetching && folders?.length === 0 && folders.length === 0 && ( | ||
<li className="block p-3 sm:py-2">No folders found</li> | ||
)} | ||
<ViewportList | ||
viewportRef={ref} | ||
items={folders} | ||
> | ||
{(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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
export type FolderListProps = { | ||
folders: string[], | ||
isFetching: boolean, | ||
padding?: boolean, | ||
}; | ||
|
||
export type FolderListItemProps = { | ||
folder: string, | ||
}; |
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
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
Oops, something went wrong.