Skip to content

Commit

Permalink
Supposedly redirect works on client/server
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Dec 18, 2023
1 parent 2facb61 commit 6276d76
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 59 deletions.
4 changes: 2 additions & 2 deletions apps/web/app/components/rangeBarChart.client/bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export default function Bar({
up
? "fill-green-400 stroke-green-400"
: down
? "fill-red-400 stroke-red-400"
: "fill-light stroke-light",
? "fill-red-400 stroke-red-400"
: "fill-light stroke-light",
)}
/>
</>
Expand Down
8 changes: 4 additions & 4 deletions apps/web/app/components/selectField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export default ({
const initialValue = Array.isArray(props.value)
? props.value
: props.value
? [props.value]
: [];
? [props.value]
: [];

if (simple) {
targetOptions = options.length;
Expand All @@ -91,8 +91,8 @@ export default ({
const newValue = Array.isArray(props.value)
? props.value
: props.value
? [props.value]
: [];
? [props.value]
: [];
setValue(newValue);
}, [JSON.stringify(props.value)]);

Expand Down
8 changes: 4 additions & 4 deletions apps/web/app/components/sortParam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export default function SortParam({
sort === name
? invertSort
: sort === invertSort
? name
: defaultOrder === "asc"
? name
: invertSort,
? name
: defaultOrder === "asc"
? name
: invertSort,
}),
}}
className="gap-x inline-flex items-center hover:underline"
Expand Down
6 changes: 0 additions & 6 deletions apps/web/app/lib/auth.server.ts

This file was deleted.

6 changes: 6 additions & 0 deletions apps/web/app/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { redirect } from "@remix-run/node";

export function redirectToAuth({ request }: { request: Request }) {
return redirect(getAuthRedirect({ request }));
}

export function getAuthRedirect({ request }: { request: Request }) {
const location = new URL(request.url);

Expand Down
9 changes: 0 additions & 9 deletions apps/web/app/lib/errors.ts

This file was deleted.

15 changes: 11 additions & 4 deletions apps/web/app/lib/isomorphicLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { makeTRPCClient } from "@peated/server/lib/trpc";
import { type User } from "@peated/server/types";
import config from "@peated/web/config";
import { type ClientLoaderFunctionArgs } from "@remix-run/react";
import { json, type LoaderFunctionArgs } from "@remix-run/server-runtime";
import {
json,
type LoaderFunction,
type LoaderFunctionArgs,
} from "@remix-run/server-runtime";
import { isResponse } from "@remix-run/server-runtime/dist/responses";
import { captureException } from "@sentry/remix";

export type IsomorphicContext = {
Expand All @@ -15,7 +20,9 @@ export type IsomorphicContext = {
isServer: boolean;
};

type DataCallback<T> = (context: IsomorphicContext) => Promise<T>;
type DataCallback<T extends ReturnType<LoaderFunction>> = (
context: IsomorphicContext,
) => Promise<T>;

/**
* Builds a loader which gives access to a uniform context object, using DI to inject
Expand Down Expand Up @@ -43,6 +50,7 @@ export function makeIsomorphicLoader<T>(callback: DataCallback<T>) {
isServer: true,
};
const payload = await callback(context);
if (isResponse(payload)) return payload;
return json(payload);
},
clientLoader: async function clientLoader({
Expand All @@ -61,8 +69,7 @@ export function makeIsomorphicLoader<T>(callback: DataCallback<T>) {
context: { trpc: trpcClient, user: window.REMIX_CONTEXT.user },
isServer: false,
};
const payload = await callback(context);
return payload;
return await callback(context);
},
};
}
6 changes: 0 additions & 6 deletions apps/web/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import LoadingIndicator from "./components/loadingIndicator";
import { default as config } from "./config";
import { ApiProvider } from "./hooks/useApi";
import { ApiUnauthorized } from "./lib/api";
import { Redirect } from "./lib/errors";
import { logError } from "./lib/log";
import { trpc } from "./lib/trpc";

Expand Down Expand Up @@ -201,11 +200,6 @@ export function ErrorBoundary() {
const error = useRouteError();
if (!isRouteErrorResponse(error)) logError(error);

if (error instanceof Redirect) {
location.href = error.path;
return null;
}

if (error instanceof ApiUnauthorized && error.data.name === "invalid_token") {
// need middleware!
// logout();
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/routes/addBottle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { toTitleCase } from "@peated/server/lib/strings";
import type { Entity } from "@peated/server/types";
import BottleForm from "@peated/web/components/bottleForm";
import Spinner from "@peated/web/components/spinner";
import { redirectToAuth } from "@peated/web/lib/auth.server";
import { redirectToAuth } from "@peated/web/lib/auth";
import { trpc } from "@peated/web/lib/trpc";
import { type LoaderFunction, type MetaFunction } from "@remix-run/node";
import { useLocation, useNavigate } from "@remix-run/react";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/routes/addFlight.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useNavigate } from "@remix-run/react";

import FlightForm from "@peated/web/components/flightForm";
import { redirectToAuth } from "@peated/web/lib/auth.server";
import { redirectToAuth } from "@peated/web/lib/auth";
import { trpc } from "@peated/web/lib/trpc";
import { type LoaderFunction, type MetaFunction } from "@remix-run/node";
import type { SitemapFunction } from "remix-sitemap";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/routes/bottles.$bottleId_.addTasting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import SelectField from "@peated/web/components/selectField";
import Spinner from "@peated/web/components/spinner";
import TextAreaField from "@peated/web/components/textAreaField";
import useApi from "@peated/web/hooks/useApi";
import { redirectToAuth } from "@peated/web/lib/auth.server";
import { redirectToAuth } from "@peated/web/lib/auth";
import { toBlob } from "@peated/web/lib/blobs";
import { logError } from "@peated/web/lib/log";
import { trpc } from "@peated/web/lib/trpc";
Expand Down
5 changes: 2 additions & 3 deletions apps/web/app/routes/favorites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { type MetaFunction } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import { type SitemapFunction } from "remix-sitemap";
import invariant from "tiny-invariant";
import { getAuthRedirect } from "../lib/auth";
import { Redirect } from "../lib/errors";
import { redirectToAuth } from "../lib/auth";
import { makeIsomorphicLoader } from "../lib/isomorphicLoader";

export const sitemap: SitemapFunction = () => ({
Expand All @@ -17,7 +16,7 @@ export const sitemap: SitemapFunction = () => ({

export const { loader, clientLoader } = makeIsomorphicLoader(
async ({ request, context: { trpc, user } }) => {
if (!user) throw new Redirect(getAuthRedirect({ request }));
if (!user) return redirectToAuth({ request });

return {
favoriteList: await trpc.collectionBottleList.query({
Expand Down
5 changes: 2 additions & 3 deletions apps/web/app/routes/flights._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import SimpleHeader from "@peated/web/components/simpleHeader";
import type { MetaFunction } from "@remix-run/node";
import { Link, useLoaderData } from "@remix-run/react";
import { type SitemapFunction } from "remix-sitemap";
import { getAuthRedirect } from "../lib/auth";
import { Redirect } from "../lib/errors";
import { redirectToAuth } from "../lib/auth";
import { makeIsomorphicLoader } from "../lib/isomorphicLoader";

export const sitemap: SitemapFunction = () => ({
Expand All @@ -16,7 +15,7 @@ export const sitemap: SitemapFunction = () => ({

export const { loader, clientLoader } = makeIsomorphicLoader(
async ({ request, context: { trpc, user } }) => {
if (!user) throw new Redirect(getAuthRedirect({ request }));
if (!user) return redirectToAuth({ request });

return {
flightList: await trpc.flightList.query(),
Expand Down
23 changes: 11 additions & 12 deletions apps/web/app/routes/friends.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ import Layout from "@peated/web/components/layout";
import ListItem from "@peated/web/components/listItem";
import SimpleHeader from "@peated/web/components/simpleHeader";
import UserAvatar from "@peated/web/components/userAvatar";
import { redirectToAuth } from "@peated/web/lib/auth.server";
import { redirectToAuth } from "@peated/web/lib/auth";
import { trpc } from "@peated/web/lib/trpc";
import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import type { MetaFunction } from "@remix-run/node";
import { Link, useLoaderData } from "@remix-run/react";
import { useState } from "react";
import type { SitemapFunction } from "remix-sitemap";
import { makeIsomorphicLoader } from "../lib/isomorphicLoader";

export const sitemap: SitemapFunction = () => ({
exclude: true,
});

export async function loader({
request,
context: { user, trpc },
}: LoaderFunctionArgs) {
if (!user) return redirectToAuth({ request });
export const { loader, clientLoader } = makeIsomorphicLoader(
async ({ request, context: { trpc, user } }) => {
if (!user) return redirectToAuth({ request });

return json({
friendList: await trpc.friendList.query(),
});
}
return {
friendList: await trpc.friendList.query(),
};
},
);

export const meta: MetaFunction = () => {
return [
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/routes/notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import EmptyActivity from "@peated/web/components/emptyActivity";
import Layout from "@peated/web/components/layout";
import NotificationList from "@peated/web/components/notifications/list";
import Tabs from "@peated/web/components/tabs";
import { redirectToAuth } from "@peated/web/lib/auth.server";
import { redirectToAuth } from "@peated/web/lib/auth";
import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import { Link, useLoaderData, useLocation } from "@remix-run/react";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/routes/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Layout from "@peated/web/components/layout";
import TextField from "@peated/web/components/textField";
import useApi from "@peated/web/hooks/useApi";
import useAuth from "@peated/web/hooks/useAuth";
import { redirectToAuth } from "@peated/web/lib/auth.server";
import { redirectToAuth } from "@peated/web/lib/auth";
import { toBlob } from "@peated/web/lib/blobs";
import { trpc } from "@peated/web/lib/trpc";
import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/routes/tastings.$tastingId_.editImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Layout from "@peated/web/components/layout";
import Spinner from "@peated/web/components/spinner";
import useApi from "@peated/web/hooks/useApi";
import { ApiError } from "@peated/web/lib/api";
import { redirectToAuth } from "@peated/web/lib/auth.server";
import { redirectToAuth } from "@peated/web/lib/auth";
import { toBlob } from "@peated/web/lib/blobs";
import { logError } from "@peated/web/lib/log";
import { useMutation, useQueryClient } from "@tanstack/react-query";
Expand Down

0 comments on commit 6276d76

Please sign in to comment.