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'
|
|
|
|
|
2021-06-18 20:07:57 -07:00
|
|
|
export function useSortableData<T>(items: T[], config = null): { items: T[], requestSort: any, sortConfig: any } {
|
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 [sortConfig, setSortConfig] = useState(config)
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
let direction = 'ascending'
|
|
|
|
if (
|
|
|
|
sortConfig &&
|
|
|
|
sortConfig.key === key &&
|
|
|
|
sortConfig.direction === 'ascending'
|
|
|
|
) {
|
|
|
|
direction = 'descending'
|
|
|
|
}
|
|
|
|
setSortConfig({ key, direction })
|
|
|
|
}
|
|
|
|
|
|
|
|
return { items: sortedItems, requestSort, sortConfig }
|
|
|
|
}
|