Skip to content

Commit

Permalink
fix: type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
avitorio committed Jun 25, 2024
1 parent e8ea476 commit 432833b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/outstatic/src/components/DocumentsTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sentenceCase } from 'change-case'
import cookies from 'js-cookie'
import { Settings } from 'lucide-react'
import Link from 'next/link'
import { useState, useCallback, useEffect } from 'react'
import { useState, useCallback, useEffect, ReactNode } from 'react'
import { Button } from '../ui/button'
import {
CaretSortIcon,
Expand Down Expand Up @@ -201,7 +201,7 @@ const cellSwitch = (
className="px-6 py-4 text-base font-semibold text-gray-900"
data-testid="status-cell"
>
{item}
{item as ReactNode}
</td>
)
default:
Expand Down
4 changes: 2 additions & 2 deletions packages/outstatic/src/components/DocumentsTable/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ describe('DocumentsTable', () => {

const sortedAsc = screen
.getAllByTestId('status-cell')
.map((node) => node.textContent.trim())
.map((node) => (node.textContent as string).trim())
expect(sortedAsc).toEqual(['draft', 'published'])

// Click the status column header again to sort descending
fireEvent.click(screen.getByText('Status'))

const sortedDesc = screen
.getAllByTestId('status-cell')
.map((node) => node.textContent.trim())
.map((node) => (node.textContent as string).trim())
expect(sortedDesc).toEqual(['published', 'draft'])
})

Expand Down
4 changes: 2 additions & 2 deletions packages/outstatic/src/components/ui/checkbox-with-label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { Controller, useFormContext, RegisterOptions } from 'react-hook-form'
import { cn } from '@/utils/ui'

interface CheckboxWithLabelProps
extends React.ComponentPropsWithoutRef<typeof Checkbox> {
extends Omit<React.ComponentPropsWithoutRef<typeof Checkbox>, 'type'> {
id: string
label: string
label?: string
registerOptions?: RegisterOptions
helperText?: string
}
Expand Down
15 changes: 9 additions & 6 deletions packages/outstatic/src/utils/hooks/useSortedDocuments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OstDocument } from '@/types/public'

type SortDirection = 'ascending' | 'descending'

type SortConfig = {
export type SortConfig = {
key: keyof OstDocument
direction: SortDirection
}
Expand All @@ -27,18 +27,21 @@ export const useSortedDocuments = (
if (a.status === b.status) {
return a.title.localeCompare(b.title) * multiplier
}
return (a.status === 'published' ? 1 : -1) * multiplier // Fix the sorting logic here
return (a.status === 'published' ? 1 : -1) * multiplier
}

const valueA = a[key]
const valueB = b[key]
const valueA = a[key] as string | number
const valueB = b[key] as string | number

if (typeof valueA === 'string' && typeof valueB === 'string') {
return valueA.localeCompare(valueB) * multiplier
}

if (valueA < valueB) return -1 * multiplier
if (valueA > valueB) return 1 * multiplier
if (typeof valueA === 'number' && typeof valueB === 'number') {
return (valueA - valueB) * multiplier
}

// Fallback for other types
return 0
},
[sortConfig]
Expand Down

2 comments on commit 432833b

@amponce
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch with those type fixes! I didn't get a warning locally @avitorio

@avitorio
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good, I initially didn't see these but running pnpm typecheck surfaced them.
I've mostly just typecasted the values, not proper type fixing tbh 😅
I should add an action to run pnpm typecheck on commit pushes.

Please sign in to comment.