Skip to content

Commit

Permalink
Enhance bookmarks status call flexibility:
Browse files Browse the repository at this point in the history
- Added `getBookmarksCount` function
- Added optional `skip` and `take` params to the `getBookmarksStatus` function
- Updated loader to use `Promise.all`
- Updated `getStatus` with better error responses
- Updated `StatusText` with truncation styles
  • Loading branch information
michaelschwobe committed Dec 12, 2023
1 parent 739a701 commit 62a0289
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 16 deletions.
9 changes: 8 additions & 1 deletion app/components/status-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ export const StatusText = forwardRef<
React.ComponentPropsWithoutRef<"span">
>(({ children, className, ...props }, ref) => {
return (
<span {...props} className={cn("text-xs", className)} ref={ref}>
<span
{...props}
className={cn(
"block max-w-[180px] truncate px-3 text-left text-xs",
className,
)}
ref={ref}
>
{typeof children === "string" ? children : "Pending"}
</span>
);
Expand Down
29 changes: 26 additions & 3 deletions app/components/table-columns-bookmarks-status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export const columnsBookmarksStatus: ColumnDef<BookmarksStatusItem>[] = [
to={getValue()}
target="_blank"
rel="noopener noreferrer"
variant="ghost"
size="sm"
variant="ghost"
className={cn(
"flex w-full justify-start overflow-hidden",
"max-w-[75vw] sm:max-w-[45vw]",
Expand All @@ -101,13 +101,36 @@ export const columnsBookmarksStatus: ColumnDef<BookmarksStatusItem>[] = [
header: () => (
<>
<Icon type="info" className="mx-auto" />
<span className="sr-only">Cause</span>
<span className="sr-only">Status Text</span>
</>
),
cell: ({ getValue }) => <StatusText>{getValue()}</StatusText>,
footer: ({ column }) => column.id,
}),

// @ts-expect-error - see comment above
columnHelper.accessor("id", {
id: "edit",
enableSorting: false,
header: () => (
<>
<Icon type="pencil" className="mx-auto" />
<span className="sr-only">Edit</span>
</>
),
cell: ({ getValue }) => (
<LinkButton
to={`/bookmarks/${getValue()}/edit`}
size="sm-icon"
variant="ghost"
>
<Icon type="pencil" />
<span className="sr-only">Edit bookmark</span>
</LinkButton>
),
footer: ({ column }) => column.id,
}),

// @ts-expect-error - see comment above
columnHelper.accessor("id", {
id: "delete",
Expand All @@ -122,8 +145,8 @@ export const columnsBookmarksStatus: ColumnDef<BookmarksStatusItem>[] = [
<ButtonDelete
formAction={`/bookmarks/${getValue()}/edit`}
label="bookmark"
variant="ghost-danger"
size="sm-icon"
variant="ghost-danger"
/>
),
footer: ({ column }) => column.id,
Expand Down
18 changes: 17 additions & 1 deletion app/models/bookmark.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ export async function getBookmarks({
});
}

/**
* If changing this, also double-check the same value in:
* - `/app/routes/bookmarks.status.tsx`
*/
export async function getBookmarksCount() {
return await prisma.bookmark.count();
}

/**
* If changing this, also double-check the same value in:
* - `/app/utils/bookmark-exports.server.ts`
Expand Down Expand Up @@ -96,10 +104,18 @@ export async function getBookmarksLatest({ take = 3 }: { take?: number } = {}) {
* If changing this, also double-check the same value in:
* - `/app/routes/bookmarks.status.tsx`
*/
export async function getBookmarksStatus() {
export async function getBookmarksStatus({
skip,
take,
}: {
skip?: number | null;
take?: number | null;
} = {}) {
return await prisma.bookmark.findMany({
select: { id: true, url: true },
orderBy: [{ createdAt: "desc" }, { url: "asc" }],
...(take ? { take } : {}),
...(skip ? { skip } : {}),
});
}

Expand Down
6 changes: 4 additions & 2 deletions app/routes/bookmarks._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export async function loader({ request }: LoaderFunctionArgs) {
const count = bookmarks.length;
const data = await getFavicons(bookmarks.slice(skip, skip + take));

const hasData = data.length > 0;
const hasPagination = count > take;

const paginationSearchParams = toPaginationSearchParams({
searchParams,
take,
Expand All @@ -58,8 +61,6 @@ export async function loader({ request }: LoaderFunctionArgs) {
take,
total: count,
});
const hasData = data.length > 0;
const hasPagination = count > take;

return json({
count,
Expand Down Expand Up @@ -109,6 +110,7 @@ export default function BookmarksIndexPage() {
<LinkButton
to={`${USER_LOGIN_ROUTE}?redirectTo=/bookmarks/status`}
size="md-icon"
reloadDocument
>
<Icon type="shield" />
<span className="sr-only">Bookmarks status</span>
Expand Down
18 changes: 12 additions & 6 deletions app/routes/bookmarks.status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import { TableSelectable } from "~/components/table-selectable";
import { Badge } from "~/components/ui/badge";
import { H1 } from "~/components/ui/h1";
import { Icon } from "~/components/ui/icon";
import { getBookmarksStatus } from "~/models/bookmark.server";
import {
getBookmarksCount,
getBookmarksStatus,
} from "~/models/bookmark.server";
import { requireUserId } from "~/utils/auth.server";
import { cn, formatMetaTitle } from "~/utils/misc";
import {
Expand All @@ -31,14 +34,19 @@ export async function loader({ request }: LoaderFunctionArgs) {
const { searchParams } = new URL(request.url);
const { skip, take } = parsePaginationSearchParams({ searchParams });

const bookmarks = await getBookmarksStatus();
const count = bookmarks.length;
const data = await getStatuses(bookmarks.slice(skip, skip + take), 60000 * 2);
const [bookmarks, count] = await Promise.all([
getBookmarksStatus({ skip, take }),
getBookmarksCount(),
]);
const data = await getStatuses(bookmarks, 5_000);

const countOk = data.filter((el) => el._meta.ok).length;
const countNotOk = Math.abs(data.length - countOk);
const risk = countNotOk > 5 ? 2 : countNotOk > 0 ? 1 : 0;

const hasData = data.length > 0;
const hasPagination = count > take;

const paginationSearchParams = toPaginationSearchParams({
searchParams,
take,
Expand All @@ -49,8 +57,6 @@ export async function loader({ request }: LoaderFunctionArgs) {
take,
total: count,
});
const hasData = data.length > 0;
const hasPagination = count > take;

return json({
count,
Expand Down
7 changes: 4 additions & 3 deletions app/utils/status.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ export async function getStatus(input: string, timeout: number) {
method: "HEAD",
signal: AbortSignal.timeout(timeout),
});
return { ok, status, statusText };
const message = statusText.length > 0 ? statusText : undefined;
return { ok, status, statusText: message ?? "Unknown" };
} catch (error) {
if (error instanceof Error) {
const name = error.name.length > 0 ? error.name : undefined;
const message = error.message.length > 0 ? error.message : undefined;
const status =
name === "TimeoutError" ? 408 : name === "AbortError" ? 504 : 500;
const statusText = name ?? message ?? "Unknown Error";
const statusText = name ?? message ?? "Unknown";
return { ok: false, status, statusText };
} else {
return { ok: false, status: 500, statusText: "Uncaught Error" };
return { ok: false, status: 500, statusText: "Uncaught" };
}
}
}
Expand Down

0 comments on commit 62a0289

Please sign in to comment.