Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/list #953

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions app/components/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { tw } from "~/utils/tw";
import { EmptyState } from "./empty-state";

import { ListHeader } from "./list-header";
import type { ListItemProps } from "./list-item";
import { ListItem } from "./list-item";
import { Pagination } from "./pagination";
import { Table } from "../table";
Expand All @@ -17,15 +18,14 @@ export const List = ({
ItemComponent,
headerChildren,
hideFirstHeaderColumn = false,
navigate,
className,
customEmptyStateContent,
link,
onRowClick,
}: {
ItemComponent: any;
headerChildren?: ReactNode;
hideFirstHeaderColumn?: boolean;
/** Function to be passed if the rows of the table should navigate */
navigate?: (id: string) => void;
className?: string;
customEmptyStateContent?: {
title: string;
Expand All @@ -34,6 +34,8 @@ export const List = ({
newButtonContent: string;
buttonProps?: any;
};
link?: ListItemProps["link"];
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please add some comments on the type definition so we can see them when we hover the prop in vscode. Same for onRowClick.

onRowClick?: ListItemProps["onClick"];
}) => {
const { items } = useLoaderData<IndexResponse>();
const hasItems = items?.length > 0;
Expand All @@ -56,7 +58,12 @@ export const List = ({
/>
<tbody>
{items.map((item) => (
<ListItem item={item} key={item.id} navigate={navigate}>
<ListItem
item={item}
key={item.id}
link={link}
onClick={onRowClick}
>
<ItemComponent item={item} />
</ListItem>
))}
Expand Down
36 changes: 26 additions & 10 deletions app/components/list/list-item.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
import { tw } from "~/utils/tw";
import { Link, type LinkProps } from "@remix-run/react";

export interface ListItemData {
id: string;
}

export type ListItemProps = {
item: ListItemData;
children: React.ReactNode;
link?: ((itemId: string) => LinkProps) | null;
onClick?: ((itemId: string) => void) | null;
};

export const ListItem = ({
item,
children,
navigate,
}: {
item: ListItemData;
children: React.ReactNode;
navigate?: (id: string) => void;
}) => (
link = null,
onClick,
}: ListItemProps) => (
<tr
key={item.id}
onClick={navigate ? () => navigate(item.id) : undefined}
className={tw("hover:bg-gray-50", navigate ? "cursor-pointer" : "")}
className="hover:bg-gray-50"
onClick={() => {
onClick && onClick(item.id);
}}
>
{children}
{link ? (
<Link
{...link(item.id)}
className="m-0 contents w-full border p-0 align-middle"
>
{children}
</Link>
) : (
children
)}
</tr>
);
4 changes: 1 addition & 3 deletions app/routes/_layout+/admin-dashboard+/users.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { User } from "@prisma/client";
import type { LoaderFunctionArgs } from "@remix-run/node";
import { redirect, json } from "@remix-run/node";
import { useNavigate } from "@remix-run/react";
import { ErrorContent } from "~/components/errors";
import type { HeaderData } from "~/components/layout/header/types";
import { List } from "~/components/list";
Expand Down Expand Up @@ -57,7 +56,6 @@ export async function loader({ context, request }: LoaderFunctionArgs) {
}

export default function Area51() {
const navigate = useNavigate();
return (
<div>
<h1>Admin dashboard</h1>
Expand All @@ -67,7 +65,7 @@ export default function Area51() {
</Filters>
<List
ItemComponent={ListUserContent}
navigate={(itemId) => navigate(`../${itemId}`)}
link={(itemId) => ({ to: `../${itemId}` })}
/>
</div>
</div>
Expand Down
5 changes: 2 additions & 3 deletions app/routes/_layout+/assets._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from "@remix-run/node";
import { json } from "@remix-run/node";
import type { ShouldRevalidateFunctionArgs } from "@remix-run/react";
import { useLoaderData, useNavigate } from "@remix-run/react";
import { useLoaderData } from "@remix-run/react";
import { redirect } from "react-router";
import { AssetImage } from "~/components/assets/asset-image";
import { AssetStatusBadge } from "~/components/assets/asset-status-badge";
Expand Down Expand Up @@ -248,7 +248,6 @@ export const meta: MetaFunction<typeof loader> = ({ data }) => [
];

export default function AssetIndexPage() {
const navigate = useNavigate();
const hasFiltersToClear = useSearchParamHasValue(
"category",
"tag",
Expand Down Expand Up @@ -352,7 +351,7 @@ export default function AssetIndexPage() {
</Filters>
<List
ItemComponent={ListAssetContent}
navigate={(itemId) => navigate(itemId)}
link={(itemId) => ({ to: itemId })}
className=" overflow-x-visible md:overflow-x-auto"
headerChildren={
<>
Expand Down
2 changes: 1 addition & 1 deletion app/routes/_layout+/bookings.$bookingId.add-assets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export default function AddAssetsToNewBooking() {
<List
ItemComponent={RowComponent}
/** Clicking on the row will add the current asset to the atom of selected assets */
navigate={(assetId) => {
onRowClick={(assetId) => {
setSelectedAssets((selectedAssets) =>
selectedAssets.includes(assetId)
? selectedAssets.filter((id) => id !== assetId)
Expand Down
5 changes: 2 additions & 3 deletions app/routes/_layout+/bookings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BookingStatus, OrganizationRoles } from "@prisma/client";
import type { MetaFunction, LoaderFunctionArgs } from "@remix-run/node";
import { json } from "@remix-run/node";
import type { ShouldRevalidateFunction } from "@remix-run/react";
import { Link, Outlet, useMatches, useNavigate } from "@remix-run/react";
import { Link, Outlet, useMatches } from "@remix-run/react";
import { AvailabilityBadge } from "~/components/booking/availability-label";
import { StatusFilter } from "~/components/booking/status-filter";
import { ErrorContent } from "~/components/errors";
Expand Down Expand Up @@ -155,7 +155,6 @@ export type RouteHandleWithName = {
};

export default function BookingsIndexPage() {
const navigate = useNavigate();
const matches = useMatches();

const currentRoute: RouteHandleWithName = matches[matches.length - 1];
Expand Down Expand Up @@ -188,7 +187,7 @@ export default function BookingsIndexPage() {
/>
<List
ItemComponent={ListAssetContent}
navigate={(id) => navigate(id)}
link={(id) => ({ to: id })}
className=" overflow-x-visible md:overflow-x-auto"
headerChildren={
<>
Expand Down
2 changes: 1 addition & 1 deletion app/routes/_layout+/locations.$locationId.add-assets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export default function AddAssetsToLocation() {
<List
ItemComponent={RowComponent}
/** Clicking on the row will add the current asset to the atom of selected assets */
navigate={(assetId) => {
onRowClick={(assetId) => {
setSelectedAssets((selectedAssets) =>
selectedAssets.includes(assetId)
? selectedAssets.filter((id) => id !== assetId)
Expand Down
5 changes: 2 additions & 3 deletions app/routes/_layout+/locations.$locationId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
LoaderFunctionArgs,
MetaFunction,
} from "@remix-run/node";
import { useLoaderData, useNavigate } from "@remix-run/react";
import { useLoaderData } from "@remix-run/react";
import mapCss from "maplibre-gl/dist/maplibre-gl.css?url";
import { z } from "zod";
import { AssetImage } from "~/components/assets/asset-image";
Expand Down Expand Up @@ -169,7 +169,6 @@ export async function action({ context, request, params }: ActionFunctionArgs) {

export default function LocationPage() {
const { location, mapData } = useLoaderData<typeof loader>();
const navigate = useNavigate();

return (
<div>
Expand Down Expand Up @@ -267,7 +266,7 @@ export default function LocationPage() {
</Filters>
<List
ItemComponent={ListAssetContent}
navigate={(itemId) => navigate(`/assets/${itemId}`)}
link={(itemId) => ({ to: `/assets/${itemId}` })}
headerChildren={
<>
<Th className="hidden md:table-cell">Category</Th>
Expand Down
4 changes: 1 addition & 3 deletions app/routes/_layout+/locations._index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Asset, Image as ImageDataType, Location } from "@prisma/client";
import { json } from "@remix-run/node";
import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
import { useNavigate } from "@remix-run/react";

import Header from "~/components/layout/header";
import type { HeaderData } from "~/components/layout/header/types";
Expand Down Expand Up @@ -87,7 +86,6 @@ export const meta: MetaFunction<typeof loader> = ({ data }) => [
];

export default function LocationsIndexPage() {
const navigate = useNavigate();
return (
<>
<Header>
Expand All @@ -105,7 +103,7 @@ export default function LocationsIndexPage() {
<Filters />
<List
ItemComponent={ListItemContent}
navigate={(itemId) => navigate(itemId)}
link={(itemId) => ({ to: itemId })}
headerChildren={
<>
<Th className="hidden md:table-cell">Assets</Th>
Expand Down
2 changes: 1 addition & 1 deletion app/routes/qr+/$qrId_.link-existing-asset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export default function QrLinkExisting() {
<List
ItemComponent={RowComponent}
/** Clicking on the row will add the current asset to the atom of selected assets */
navigate={handleSelectAsset}
onRowClick={handleSelectAsset}
customEmptyStateContent={{
title: "You haven't added any assets yet.",
text: "What are you waiting for? Create your first asset now!",
Expand Down
Loading