-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5733911
commit 0cb370d
Showing
8 changed files
with
142 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'renterd': minor | ||
--- | ||
|
||
Files can now be sorted by name and health, ascending or descending. |
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,5 @@ | ||
--- | ||
'@siafoundation/react-renterd': minor | ||
--- | ||
|
||
useObjectDirectory now supports sortBy and sortDir. |
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,60 @@ | ||
'use client' | ||
|
||
import { intersection } from 'lodash' | ||
import { useCallback, useMemo } from 'react' | ||
import useLocalStorageState from 'use-local-storage-state' | ||
|
||
type Params<ColumnId extends string, SortField extends string> = { | ||
enabledColumns: ColumnId[] | ||
defaultSortField?: SortField | ||
sortOptions?: { id: SortField }[] | ||
} | ||
|
||
export function useSorting<ColumnId extends string, SortField extends string>( | ||
scope: string, | ||
params: Params<ColumnId, SortField> | ||
) { | ||
const { defaultSortField, sortOptions, enabledColumns } = params | ||
|
||
const [sortField, setSortField] = useLocalStorageState<SortField>( | ||
`${scope}/sortField`, | ||
{ | ||
defaultValue: defaultSortField, | ||
} | ||
) | ||
|
||
const [sortDirection, setSortDirection] = useLocalStorageState< | ||
'desc' | 'asc' | ||
>(`${scope}/sortDirection`, { | ||
defaultValue: 'desc', | ||
}) | ||
|
||
const toggleSort = useCallback( | ||
(field: SortField) => { | ||
if (sortField !== field) { | ||
setSortField(field) | ||
setSortDirection('asc') | ||
return | ||
} | ||
setSortDirection((dir) => (dir === 'desc' ? 'asc' : 'desc')) | ||
}, | ||
[sortField, setSortField, setSortDirection] | ||
) | ||
|
||
const sortableColumns = useMemo(() => { | ||
if (!sortOptions) { | ||
return [] | ||
} | ||
const sortFieldIds = sortOptions.map((o) => o.id) | ||
return intersection(sortFieldIds, enabledColumns as string[]) as SortField[] | ||
}, [sortOptions, enabledColumns]) | ||
|
||
return { | ||
toggleSort, | ||
setSortDirection, | ||
setSortField, | ||
sortableColumns, | ||
sortField, | ||
sortDirection, | ||
} | ||
} |
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