User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
import { useMemo, useState } from 'react'
|
|
|
|
|
2022-03-25 09:00:48 -07:00
|
|
|
type Direction = 'ascending' | 'descending'
|
|
|
|
|
|
|
|
interface Config {
|
|
|
|
key: string
|
|
|
|
direction: Direction
|
|
|
|
}
|
|
|
|
|
2021-08-17 13:51:02 -07:00
|
|
|
export function useSortableData<T>(
|
|
|
|
items: T[],
|
|
|
|
config = null
|
|
|
|
): { items: T[]; requestSort: any; sortConfig: any } {
|
2022-03-25 09:00:48 -07:00
|
|
|
const [sortConfig, setSortConfig] = useState<Config | null>(config)
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
|
|
|
|
const sortedItems = useMemo(() => {
|
2021-06-05 09:14:34 -07:00
|
|
|
const sortableItems = items ? [...items] : []
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
if (sortConfig !== null) {
|
|
|
|
sortableItems.sort((a, b) => {
|
|
|
|
if (!isNaN(a[sortConfig.key])) {
|
|
|
|
return sortConfig.direction === 'ascending'
|
|
|
|
? a[sortConfig.key] - b[sortConfig.key]
|
|
|
|
: b[sortConfig.key] - a[sortConfig.key]
|
|
|
|
}
|
|
|
|
if (a[sortConfig.key] < b[sortConfig.key]) {
|
|
|
|
return sortConfig.direction === 'ascending' ? -1 : 1
|
|
|
|
}
|
|
|
|
if (a[sortConfig.key] > b[sortConfig.key]) {
|
|
|
|
return sortConfig.direction === 'ascending' ? 1 : -1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return sortableItems
|
|
|
|
}, [items, sortConfig])
|
|
|
|
|
|
|
|
const requestSort = (key) => {
|
2022-03-25 09:00:48 -07:00
|
|
|
let direction: Direction = 'ascending'
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
if (
|
|
|
|
sortConfig &&
|
|
|
|
sortConfig.key === key &&
|
|
|
|
sortConfig.direction === 'ascending'
|
|
|
|
) {
|
|
|
|
direction = 'descending'
|
|
|
|
}
|
|
|
|
setSortConfig({ key, direction })
|
|
|
|
}
|
|
|
|
|
|
|
|
return { items: sortedItems, requestSort, sortConfig }
|
|
|
|
}
|