Skip to content

Commit

Permalink
misc(Table): allow to open row link in new tab
Browse files Browse the repository at this point in the history
  • Loading branch information
ansmonjol committed Jan 8, 2025
1 parent 9bdab19 commit b308381
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 63 deletions.
16 changes: 7 additions & 9 deletions src/components/creditNote/CreditNotesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,13 @@ const CreditNotesTable = ({

return actions
}}
onRowAction={(creditNote) => {
navigate(
generatePath(CUSTOMER_INVOICE_CREDIT_NOTE_DETAILS_ROUTE, {
customerId: creditNote?.invoice?.customer?.id as string,
invoiceId: creditNote?.invoice?.id as string,
creditNoteId: creditNote?.id as string,
}),
)
}}
onRowActionLink={(creditNote) =>
generatePath(CUSTOMER_INVOICE_CREDIT_NOTE_DETAILS_ROUTE, {
customerId: creditNote?.invoice?.customer?.id as string,
invoiceId: creditNote?.invoice?.id as string,
creditNoteId: creditNote?.id as string,
})
}
columns={[
{
key: 'totalAmountCents',
Expand Down
14 changes: 6 additions & 8 deletions src/components/customers/CustomerInvoicesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,12 @@ export const CustomerInvoicesList: FC<CustomerInvoicesListProps> = ({
isLoading={isLoading}
hasError={hasError}
data={invoiceData?.collection ?? []}
onRowAction={({ id }) =>
navigate(
generatePath(CUSTOMER_INVOICE_DETAILS_ROUTE, {
customerId,
invoiceId: id,
tab: CustomerInvoiceDetailsTabsOptionsEnum.overview,
}),
)
onRowActionLink={({ id }) =>
generatePath(CUSTOMER_INVOICE_DETAILS_ROUTE, {
customerId,
invoiceId: id,
tab: CustomerInvoiceDetailsTabsOptionsEnum.overview,
})
}
placeholder={{
errorState: {
Expand Down
23 changes: 18 additions & 5 deletions src/components/designSystem/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TableRowProps,
} from '@mui/material'
import { MouseEvent, PropsWithChildren, ReactNode, useRef } from 'react'
import { useNavigate } from 'react-router-dom'

import { Button, IconName, Popper, Skeleton, Tooltip, Typography } from '~/components/designSystem'
import { GenericPlaceholder, GenericPlaceholderProps } from '~/components/GenericPlaceholder'
Expand Down Expand Up @@ -76,7 +77,7 @@ interface TableProps<T> {
emptyState?: Partial<GenericPlaceholderProps>
errorState?: Partial<GenericPlaceholderProps>
}
onRowAction?: (item: T) => void
onRowActionLink?: (item: T) => string
actionColumn?: (item: T) => Array<ActionItem<T> | null> | ReactNode
actionColumnTooltip?: (item: T) => string
rowDataTestId?: (item: T) => string
Expand Down Expand Up @@ -333,7 +334,7 @@ export const Table = <T extends DataItem>({
containerSize = 48,
rowSize = 48,
placeholder,
onRowAction,
onRowActionLink,
actionColumn,
actionColumnTooltip,
rowDataTestId,
Expand All @@ -343,19 +344,20 @@ export const Table = <T extends DataItem>({
const maxSpaceColumns = countMaxSpaceColumns(filteredColumns)
const tableRef = useRef<HTMLTableElement>(null)
const { translate } = useInternationalization()
const navigate = useNavigate()

const { onKeyDown } = useListKeysNavigation({
getElmId: (i) => `${TABLE_ID}-row-${i}`,
navigate: (id) => {
const item = data.find((dataItem) => dataItem.id === id)

if (item) {
onRowAction?.(item)
onRowActionLink?.(item)
}
},
})

const isClickable = !!onRowAction && !isLoading
const isClickable = !!onRowActionLink && !isLoading
const shouldDisplayActionColumn =
!!actionColumn &&
(data.length > 0
Expand All @@ -382,6 +384,8 @@ export const Table = <T extends DataItem>({
const colSpan = filteredColumns.length + (shouldDisplayActionColumn ? 1 : 0)

const handleRowClick = (e: MouseEvent<HTMLTableRowElement>, item: T) => {
if (!onRowActionLink) return

// Prevent row action when clicking on button or link in cell
if (e.target instanceof HTMLAnchorElement || e.target instanceof HTMLButtonElement) {
return
Expand All @@ -397,8 +401,17 @@ export const Table = <T extends DataItem>({
if (e.target instanceof HTMLElement) {
const actionColumnButton = e.target.closest('button')?.dataset.id

const hasSideKeyPressed = e.metaKey || e.ctrlKey

// Make sure anything other than the action column button is clicked
if (actionColumnButton !== ACTION_COLUMN_ID) {
onRowAction?.(item)
const link = onRowActionLink(item)

if (hasSideKeyPressed) {
window.open(link, '_blank')
} else {
navigate(link)
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/designSystem/__tests__/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('Table', () => {

await prepare({
props: {
onRowAction: (row: any) => onRow(row),
onRowActionLink: (row: any) => onRow(row),
actionColumn: () => [
{
title: 'Edit',
Expand Down Expand Up @@ -136,7 +136,7 @@ describe('Table', () => {
await prepare({
props: {
actionColumn: (row: any) => <Button onClick={onClick(row)}>Click me</Button>,
onRowAction: (row: any) => onRow(row),
onRowActionLink: (row: any) => onRow(row),
},
})

Expand Down
16 changes: 7 additions & 9 deletions src/components/invoices/InvoicesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,13 @@ const InvoicesList = ({
),
},
]}
onRowAction={(invoice) => {
navigate(
generatePath(CUSTOMER_INVOICE_DETAILS_ROUTE, {
customerId: invoice?.customer?.id,
invoiceId: invoice.id,
tab: CustomerInvoiceDetailsTabsOptionsEnum.overview,
}),
)
}}
onRowActionLink={(invoice) =>
generatePath(CUSTOMER_INVOICE_DETAILS_ROUTE, {
customerId: invoice?.customer?.id,
invoiceId: invoice.id,
tab: CustomerInvoiceDetailsTabsOptionsEnum.overview,
})
}
placeholder={{
errorState: variables?.searchTerm
? {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/AddOnsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const AddOnsList = () => {
rowSize={72}
isLoading={isLoading}
hasError={!!error}
onRowAction={({ id }) => navigate(generatePath(ADD_ON_DETAILS_ROUTE, { addOnId: id }))}
onRowActionLink={({ id }) => generatePath(ADD_ON_DETAILS_ROUTE, { addOnId: id })}
rowDataTestId={(addOn) => `${addOn.name}`}
columns={[
{
Expand Down
4 changes: 2 additions & 2 deletions src/pages/BillableMetricsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ const BillableMetricsList = () => {
rowSize={72}
isLoading={isLoading}
hasError={!!error}
onRowAction={({ id }) =>
navigate(generatePath(UPDATE_BILLABLE_METRIC_ROUTE, { billableMetricId: id }))
onRowActionLink={({ id }) =>
generatePath(UPDATE_BILLABLE_METRIC_ROUTE, { billableMetricId: id })
}
columns={[
{
Expand Down
2 changes: 1 addition & 1 deletion src/pages/CouponsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const CouponsList = () => {
rowSize={72}
isLoading={isLoading}
hasError={!!error}
onRowAction={({ id }) => navigate(generatePath(COUPON_DETAILS_ROUTE, { couponId: id }))}
onRowActionLink={({ id }) => generatePath(COUPON_DETAILS_ROUTE, { couponId: id })}
rowDataTestId={(addOn) => `${addOn.name}`}
columns={[
{
Expand Down
7 changes: 2 additions & 5 deletions src/pages/CustomersList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { gql } from '@apollo/client'
import { useRef } from 'react'
import { generatePath, useNavigate } from 'react-router-dom'
import { generatePath } from 'react-router-dom'

import {
AddCustomerDrawer,
Expand Down Expand Up @@ -56,7 +56,6 @@ gql`
`

const CustomersList = () => {
const navigate = useNavigate()
const { translate } = useInternationalization()
const { hasPermissions } = usePermissions()
const { formatTimeOrgaTZ } = useOrganizationInfos()
Expand Down Expand Up @@ -115,9 +114,7 @@ const CustomersList = () => {
default: 16,
md: 48,
}}
onRowAction={({ id }) =>
navigate(generatePath(CUSTOMER_DETAILS_ROUTE, { customerId: id }))
}
onRowActionLink={({ id }) => generatePath(CUSTOMER_DETAILS_ROUTE, { customerId: id })}
columns={[
{
key: 'displayName',
Expand Down
12 changes: 5 additions & 7 deletions src/pages/PlansList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,11 @@ const PlansList = () => {
isLoading={isLoading}
hasError={!!error}
rowDataTestId={(plan) => `${plan.name}`}
onRowAction={({ id }) =>
navigate(
generatePath(PLAN_DETAILS_ROUTE, {
planId: id,
tab: PlanDetailsTabsOptionsEnum.overview,
}),
)
onRowActionLink={({ id }) =>
generatePath(PLAN_DETAILS_ROUTE, {
planId: id,
tab: PlanDetailsTabsOptionsEnum.overview,
})
}
columns={[
{
Expand Down
4 changes: 2 additions & 2 deletions src/pages/__devOnly/DesignSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ const DesignSystem = () => {
content: (row) => row.date,
},
]}
onRowAction={(item) => alert(`You clicked on ${item.id}`)}
onRowActionLink={(item) => `you clicked on ${item.id}`}
actionColumn={(currentItem) => [
currentItem.amount > 1000
? {
Expand Down Expand Up @@ -706,7 +706,7 @@ const DesignSystem = () => {
),
},
]}
onRowAction={(item) => alert(`You clicked on ${item.id}`)}
onRowActionLink={(item) => `you clicked on ${item.id}`}
/>
</Block>
</Container>
Expand Down
7 changes: 2 additions & 5 deletions src/pages/developers/Webhooks.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { gql } from '@apollo/client'
import { useRef, useState } from 'react'
import { generatePath, useNavigate } from 'react-router-dom'
import { generatePath } from 'react-router-dom'

import { Button, Table, Tooltip, Typography } from '~/components/designSystem'
import {
Expand Down Expand Up @@ -55,7 +55,6 @@ gql`
`

const Webhooks = () => {
const navigate = useNavigate()
const { translate } = useInternationalization()
const [showOrganizationHmac, setShowOrganizationHmac] = useState<boolean>(false)
const createDialogRef = useRef<CreateWebhookDialogRef>(null)
Expand Down Expand Up @@ -208,9 +207,7 @@ const Webhooks = () => {
),
},
]}
onRowAction={({ id }) => {
navigate(generatePath(WEBHOOK_LOGS_ROUTE, { webhookId: id }))
}}
onRowActionLink={({ id }) => generatePath(WEBHOOK_LOGS_ROUTE, { webhookId: id })}
actionColumnTooltip={() => translate('text_6256de3bba111e00b3bfa51b')}
actionColumn={(webhook) => {
return [
Expand Down
12 changes: 5 additions & 7 deletions src/pages/settings/EmailSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,11 @@ const EmailSettings = () => {
containerSize={{ default: 0 }}
rowSize={72}
data={EMAIL_SCENARIOS}
onRowAction={({ setting }) => {
navigate(
generatePath(EMAILS_SCENARIO_CONFIG_ROUTE, {
type: setting,
}),
)
}}
onRowActionLink={({ setting }) =>
generatePath(EMAILS_SCENARIO_CONFIG_ROUTE, {
type: setting,
})
}
columns={[
{
key: 'id',
Expand Down

0 comments on commit b308381

Please sign in to comment.