fix: Fixed issue where table view would not sort correctly outside of Files view

This commit is contained in:
Aman Harwara
2023-01-18 23:43:51 +05:30
parent 51b8aca8d7
commit 066a0b4c65
2 changed files with 44 additions and 46 deletions

View File

@@ -373,6 +373,7 @@ const ContentListView = forwardRef<HTMLDivElement, Props>(
navigationController={navigationController} navigationController={navigationController}
notesController={notesController} notesController={notesController}
historyModalController={historyModalController} historyModalController={historyModalController}
itemListController={itemListController}
/> />
) : ( ) : (
<ContentList <ContentList

View File

@@ -1,5 +1,4 @@
import { WebApplication } from '@/Application/Application' import { WebApplication } from '@/Application/Application'
import { PrefDefaults } from '@/Constants/PrefDefaults'
import { FilesController } from '@/Controllers/FilesController' import { FilesController } from '@/Controllers/FilesController'
import { formatDateForContextMenu } from '@/Utils/DateUtils' import { formatDateForContextMenu } from '@/Utils/DateUtils'
import { getIconForFileType } from '@/Utils/Items/Icons/getIconForFileType' import { getIconForFileType } from '@/Utils/Items/Icons/getIconForFileType'
@@ -8,11 +7,11 @@ import {
FileItem, FileItem,
SortableItem, SortableItem,
PrefKey, PrefKey,
ApplicationEvent,
FileBackupRecord, FileBackupRecord,
SystemViewId, SystemViewId,
DecryptedItemInterface, DecryptedItemInterface,
SNNote, SNNote,
TagMutator,
} from '@standardnotes/snjs' } from '@standardnotes/snjs'
import { useState, useEffect, useCallback, useMemo, useRef } from 'react' import { useState, useEffect, useCallback, useMemo, useRef } from 'react'
import { FileItemActionType } from '../AttachedFilesPopover/PopoverFileItemAction' import { FileItemActionType } from '../AttachedFilesPopover/PopoverFileItemAction'
@@ -37,6 +36,7 @@ import { NotesController } from '@/Controllers/NotesController/NotesController'
import { HistoryModalController } from '@/Controllers/NoteHistory/HistoryModalController' import { HistoryModalController } from '@/Controllers/NoteHistory/HistoryModalController'
import { useItemLinks } from '@/Hooks/useItemLinks' import { useItemLinks } from '@/Hooks/useItemLinks'
import { ItemLink } from '@/Utils/Items/Search/ItemLink' import { ItemLink } from '@/Utils/Items/Search/ItemLink'
import { ItemListController } from '@/Controllers/ItemList/ItemListController'
const ContextMenuCell = ({ const ContextMenuCell = ({
items, items,
@@ -176,7 +176,7 @@ const ItemLinksCell = ({
) )
} }
const ItemNameCell = ({ item }: { item: DecryptedItemInterface }) => { const ItemNameCell = ({ item, hideIcon }: { item: DecryptedItemInterface; hideIcon: boolean }) => {
const application = useApplication() const application = useApplication()
const [backupInfo, setBackupInfo] = useState<FileBackupRecord | undefined>(undefined) const [backupInfo, setBackupInfo] = useState<FileBackupRecord | undefined>(undefined)
const isItemFile = item instanceof FileItem const isItemFile = item instanceof FileItem
@@ -196,11 +196,13 @@ const ItemNameCell = ({ item }: { item: DecryptedItemInterface }) => {
return ( return (
<div className="flex items-center gap-3 whitespace-normal"> <div className="flex items-center gap-3 whitespace-normal">
<span className="relative"> <span className="relative">
{isItemFile ? ( {!hideIcon ? (
getFileIconComponent(getIconForFileType(item.mimeType), 'w-6 h-6 flex-shrink-0') isItemFile ? (
) : ( getFileIconComponent(getIconForFileType(item.mimeType), 'w-6 h-6 flex-shrink-0')
<Icon type={noteIcon} className={`text-accessory-tint-${noteIconTint}`} /> ) : (
)} <Icon type={noteIcon} className={`text-accessory-tint-${noteIconTint}`} />
)
) : null}
{backupInfo && ( {backupInfo && (
<div <div
className="absolute bottom-1 right-1 translate-x-1/2 translate-y-1/2 rounded-full bg-default text-success" className="absolute bottom-1 right-1 translate-x-1/2 translate-y-1/2 rounded-full bg-default text-success"
@@ -261,6 +263,7 @@ type Props = {
navigationController: NavigationController navigationController: NavigationController
notesController: NotesController notesController: NotesController
historyModalController: HistoryModalController historyModalController: HistoryModalController
itemListController: ItemListController
} }
const ContentTableView = ({ const ContentTableView = ({
@@ -272,51 +275,45 @@ const ContentTableView = ({
navigationController, navigationController,
notesController, notesController,
historyModalController, historyModalController,
itemListController,
}: Props) => { }: Props) => {
const listHasFiles = items.some((item) => item instanceof FileItem) const listHasFiles = items.some((item) => item instanceof FileItem)
const getSortByPreference = useCallback(() => { const { sortBy, sortDirection } = itemListController.displayOptions
const globalPrefValue = application.getPreference(PrefKey.SortNotesBy, PrefDefaults[PrefKey.SortNotesBy]) const sortReversed = sortDirection === 'asc'
const filesViewPrefValue = application.getPreference(PrefKey.SystemViewPreferences)?.[SystemViewId.Files]?.sortBy const { hideDate, hideEditorIcon: hideIcon, hideTags } = itemListController.webDisplayOptions
return filesViewPrefValue ?? globalPrefValue
}, [application])
const getSortReversedPreference = useCallback(() => {
const globalPrefValue = application.getPreference(PrefKey.SortNotesReverse, PrefDefaults[PrefKey.SortNotesReverse])
const filesViewPrefValue = application.getPreference(PrefKey.SystemViewPreferences)?.[SystemViewId.Files]
?.sortReverse
return filesViewPrefValue ?? globalPrefValue
}, [application])
const [sortBy, setSortBy] = useState<keyof SortableItem>(() => getSortByPreference())
const [sortReversed, setSortReversed] = useState(() => getSortReversedPreference())
useEffect(() => {
return application.addEventObserver(async (event) => {
if (event === ApplicationEvent.PreferencesChanged) {
setSortBy(getSortByPreference())
setSortReversed(getSortReversedPreference())
}
})
}, [application, getSortByPreference, getSortReversedPreference])
const onSortChange = useCallback( const onSortChange = useCallback(
async (sortBy: keyof SortableItem, sortReversed: boolean) => { async (sortBy: keyof SortableItem, sortReversed: boolean) => {
const systemViewPreferences = application.getPreference(PrefKey.SystemViewPreferences) || {} const selectedTag = navigationController.selected
const filesViewPreferences = systemViewPreferences[SystemViewId.Files] || {}
await application.setPreference(PrefKey.SystemViewPreferences, { if (!selectedTag) {
...systemViewPreferences, return
[SystemViewId.Files]: { }
...filesViewPreferences,
if (selectedTag.uuid === SystemViewId.Files) {
const systemViewPreferences = application.getPreference(PrefKey.SystemViewPreferences) || {}
const filesViewPreferences = systemViewPreferences[SystemViewId.Files] || {}
await application.setPreference(PrefKey.SystemViewPreferences, {
...systemViewPreferences,
[SystemViewId.Files]: {
...filesViewPreferences,
sortBy,
sortReverse: sortReversed,
},
})
}
await application.mutator.changeAndSaveItem<TagMutator>(selectedTag, (mutator) => {
mutator.preferences = {
...mutator.preferences,
sortBy, sortBy,
sortReverse: sortReversed, sortReverse: sortReversed,
}, }
}) })
}, },
[application], [application, navigationController.selected],
) )
const [contextMenuItem, setContextMenuItem] = useState<DecryptedItemInterface | undefined>(undefined) const [contextMenuItem, setContextMenuItem] = useState<DecryptedItemInterface | undefined>(undefined)
@@ -331,7 +328,7 @@ const ContentTableView = ({
{ {
name: 'Name', name: 'Name',
sortBy: 'title', sortBy: 'title',
cell: (item) => <ItemNameCell item={item} />, cell: (item) => <ItemNameCell item={item} hideIcon={hideIcon} />,
}, },
{ {
name: 'Upload date', name: 'Upload date',
@@ -339,7 +336,7 @@ const ContentTableView = ({
cell: (item) => { cell: (item) => {
return formatDateForContextMenu(item.created_at) return formatDateForContextMenu(item.created_at)
}, },
hidden: isSmallBreakpoint, hidden: isSmallBreakpoint || hideDate,
}, },
{ {
name: 'Size', name: 'Size',
@@ -351,11 +348,11 @@ const ContentTableView = ({
}, },
{ {
name: 'Attached to', name: 'Attached to',
hidden: isSmallBreakpoint || isMediumBreakpoint || isLargeBreakpoint, hidden: isSmallBreakpoint || isMediumBreakpoint || isLargeBreakpoint || hideTags,
cell: (item) => <AttachedToCell item={item} />, cell: (item) => <AttachedToCell item={item} />,
}, },
], ],
[isLargeBreakpoint, isMediumBreakpoint, isSmallBreakpoint, listHasFiles], [hideDate, hideIcon, hideTags, isLargeBreakpoint, isMediumBreakpoint, isSmallBreakpoint, listHasFiles],
) )
const getRowId = useCallback((item: DecryptedItemInterface) => item.uuid, []) const getRowId = useCallback((item: DecryptedItemInterface) => item.uuid, [])