Skip to content

Commit

Permalink
on second thought, we def don't need a top-level app/pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo committed Feb 23, 2024
1 parent bf9e4ff commit cc9f952
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
26 changes: 0 additions & 26 deletions app/pagination/index.tsx → app/components/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*
* Copyright Oxide Computer Company
*/
import { useState } from 'react'
import tunnel from 'tunnel-rat'

import {
Expand All @@ -30,28 +29,3 @@ export function Pagination({ inline = false, ...props }: PaginationProps) {
}

Pagination.Target = Tunnel.Out

type PageToken = string | undefined

export function usePagination() {
const [prevPages, setPrevPages] = useState<PageToken[]>([])
const [currentPage, setCurrentPage] = useState<PageToken>()

const goToPrevPage = () => {
const prevPage = prevPages.pop()
setCurrentPage(prevPage)
setPrevPages(prevPages)
}

const goToNextPage = (nextPageToken: string) => {
setPrevPages([...prevPages, currentPage])
setCurrentPage(nextPageToken)
}

return {
currentPage,
goToNextPage,
goToPrevPage,
hasPrev: prevPages.length > 0,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { act, renderHook } from '@testing-library/react'
import { describe, expect, it } from 'vitest'

import { usePagination } from '../index.tsx'
import { usePagination } from './use-pagination'

describe('usePagination', () => {
it('starts with empty state', () => {
Expand Down
33 changes: 33 additions & 0 deletions app/hooks/use-pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import { useState } from 'react'

type PageToken = string | undefined

export function usePagination() {
const [prevPages, setPrevPages] = useState<PageToken[]>([])
const [currentPage, setCurrentPage] = useState<PageToken>()

const goToPrevPage = () => {
const prevPage = prevPages.pop()
setCurrentPage(prevPage)
setPrevPages(prevPages)
}

const goToNextPage = (nextPageToken: string) => {
setPrevPages([...prevPages, currentPage])
setCurrentPage(nextPageToken)
}

return {
currentPage,
goToNextPage,
goToPrevPage,
hasPrev: prevPages.length > 0,
}
}
2 changes: 1 addition & 1 deletion app/layouts/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Outlet } from 'react-router-dom'
import { SkipLinkTarget } from '@oxide/ui'
import { classed } from '@oxide/util'

import { Pagination } from '~/pagination'
import { Pagination } from '~/components/Pagination'
import { PageActionsTarget } from 'app/components/PageActions'
import { useScrollRestoration } from 'app/hooks/use-scroll-restoration'

Expand Down
3 changes: 2 additions & 1 deletion app/table/QueryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
import { EmptyMessage, TableEmptyBox } from '@oxide/ui'
import { invariant, isOneOf } from '@oxide/util'

import { Pagination, usePagination } from '~/pagination'
import { Pagination } from '~/components/Pagination'
import { usePagination } from '~/hooks/use-pagination'

import { DefaultCell } from './cells'
import { getActionsCol, getMultiSelectCol, getSelectCol, type MakeActions } from './columns'
Expand Down

0 comments on commit cc9f952

Please sign in to comment.