Skip to content

Commit

Permalink
Merge pull request #303 from acelaya-forks/feature/react-router-7
Browse files Browse the repository at this point in the history
Update to react-router 7
  • Loading branch information
acelaya authored Dec 9, 2024
2 parents b329752 + 4842f02 commit 2d71020
Show file tree
Hide file tree
Showing 33 changed files with 5,392 additions and 8,703 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.react-router/
build
node_modules
coverage
4 changes: 2 additions & 2 deletions app/auth/auth-helper.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SessionStorage } from '@remix-run/node';
import { redirect } from '@remix-run/node';
import type { SessionStorage } from 'react-router';
import { redirect } from 'react-router';
import type { Authenticator } from 'remix-auth';
import { CREDENTIALS_STRATEGY } from './auth.server';
import type { SessionData } from './session-context';
Expand Down
2 changes: 1 addition & 1 deletion app/auth/session.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createCookieSessionStorage } from '@remix-run/node';
import { createCookieSessionStorage } from 'react-router';
import { env, isProd } from '../utils/env.server';
import type { SessionData } from './session-context';

Expand Down
2 changes: 1 addition & 1 deletion app/common/MainHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
faCogs,
} from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Link, useLocation } from '@remix-run/react';
import { useToggle } from '@shlinkio/shlink-frontend-kit';
import type { FC } from 'react';
import { Link, useLocation } from 'react-router';
import { Collapse, Nav, Navbar, NavbarBrand, NavbarToggler, NavItem, NavLink } from 'reactstrap';
import { useSession } from '../auth/session-context';
import { ShlinkLogo } from './ShlinkLogo';
Expand Down
6 changes: 3 additions & 3 deletions app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { LoaderFunctionArgs } from '@remix-run/node';
import { Links, Meta, Outlet, Scripts, useLoaderData } from '@remix-run/react';
import type { Theme } from '@shlinkio/shlink-frontend-kit';
import { getSystemPreferredTheme } from '@shlinkio/shlink-frontend-kit';
import { useEffect, useState } from 'react';
import type { LoaderFunctionArgs } from 'react-router';
import { Links, Meta, Outlet, Scripts, useLoaderData } from 'react-router';
import { AuthHelper } from './auth/auth-helper.server';
import { SessionProvider } from './auth/session-context';
import { MainHeader } from './common/MainHeader';
Expand All @@ -23,7 +23,7 @@ export async function loader(
: authHelper.getSession(request, `/login?redirect-to=${encodeURIComponent(pathname)}`)
);

const settings = sessionData && await settingsService.userSettings(sessionData.userId);
const settings = sessionData && (await settingsService.userSettings(sessionData.userId));

return { sessionData, settings };
}
Expand Down
17 changes: 17 additions & 0 deletions app/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { RouteConfig } from '@react-router/dev/routes';
import { index, route } from '@react-router/dev/routes';

// eslint-disable-next-line no-restricted-exports
export default [
index('./routes/_index/route.tsx'),
route('/login', './routes/login.tsx'),
route('/logout', './routes/logout.ts'),
route('/settings/*', './routes/settings.$.tsx'),

// RPC-style proxy for Shlink API
route('/server/:serverId/shlink-api/:method', './routes/server.$serverId.shlink-api.$method.ts'),
// ShlinkWebComponent wrapper
route('/server/:serverId/*', './routes/server.$serverId.$.tsx'),
// Saves tag colors
route('/server/:serverId/tags/colors', './routes/server.$serverId.tags.colors.ts'),
] satisfies RouteConfig;
2 changes: 1 addition & 1 deletion app/routes/_index/NoServers.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { faPlus, faUpRightFromSquare } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Link } from '@remix-run/react';
import type { FC } from 'react';
import { ExternalLink } from 'react-external-link';
import { Link } from 'react-router';
import { Button } from 'reactstrap';

export const NoServers: FC = () => (
Expand Down
2 changes: 1 addition & 1 deletion app/routes/_index/ServersList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { faChevronRight } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Link } from '@remix-run/react';
import clsx from 'clsx';
import type { FC } from 'react';
import { Link } from 'react-router';
import type { Server } from '../../entities/Server';

export type ServersListProps = {
Expand Down
4 changes: 2 additions & 2 deletions app/routes/_index/route.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LoaderFunctionArgs } from '@remix-run/node';
import { useLoaderData } from '@remix-run/react';
import type { LoaderFunctionArgs } from 'react-router';
import { useLoaderData } from 'react-router';
import { AuthHelper } from '../../auth/auth-helper.server';
import { Layout } from '../../common/Layout';
import { serverContainer } from '../../container/container.server';
Expand Down
13 changes: 7 additions & 6 deletions app/routes/login.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/node';
import { redirect } from '@remix-run/node';
import { json } from '@remix-run/node';
import { useActionData } from '@remix-run/react';
import { SimpleCard } from '@shlinkio/shlink-frontend-kit';
import { useId } from 'react';
import type { ActionFunctionArgs, LoaderFunctionArgs } from 'react-router';
import { useActionData } from 'react-router';
import { redirect } from 'react-router';
import { Button, Input } from 'reactstrap';
import { AuthHelper } from '../auth/auth-helper.server';
import { serverContainer } from '../container/container.server';
Expand All @@ -19,7 +18,7 @@ export async function action(
} catch (e: any) {
// TODO Use a more robust way to detect errors
if (INCORRECT_CREDENTIAL_ERROR_PREFIXES.some((prefix) => e.message.startsWith(prefix))) {
return json({ error: true });
return { error: true };
}

throw e;
Expand Down Expand Up @@ -53,7 +52,9 @@ export default function Login() {
<Input id={passwordId} type="password" name="password" required />
</div>
<Button color="primary" type="submit">Login</Button>
{actionData?.error && <div className="text-danger">Username or password are incorrect</div>}
{actionData && 'error' in actionData && actionData.error && (
<div className="text-danger">Username or password are incorrect</div>
)}
</form>
</SimpleCard>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/routes/logout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ActionFunctionArgs } from '@remix-run/node';
import type { ActionFunctionArgs } from 'react-router';
import { AuthHelper } from '../auth/auth-helper.server';
import { serverContainer } from '../container/container.server';

Expand Down
4 changes: 2 additions & 2 deletions app/routes/server.$serverId.$.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { LoaderFunctionArgs } from '@remix-run/node';
import { useLoaderData, useLocation, useParams } from '@remix-run/react';
import type { Settings } from '@shlinkio/shlink-web-component/settings';
import type { ReactNode } from 'react';
import { useEffect, useMemo, useState } from 'react';
import type { LoaderFunctionArgs } from 'react-router';
import { useLoaderData, useLocation, useParams } from 'react-router';
import { ShlinkApiProxyClient } from '../api/ShlinkApiProxyClient.client';
import { AuthHelper } from '../auth/auth-helper.server';
import { serverContainer } from '../container/container.server';
Expand Down
5 changes: 2 additions & 3 deletions app/routes/server.$serverId.shlink-api.$method.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ActionFunctionArgs } from '@remix-run/node';
import { json } from '@remix-run/node';
import type { ShlinkApiClient } from '@shlinkio/shlink-js-sdk';
import { ErrorType } from '@shlinkio/shlink-js-sdk/api-contract';
import type { ActionFunctionArgs } from 'react-router';
import type { ApiClientBuilder } from '../api/apiClientBuilder.server';
import { AuthHelper } from '../auth/auth-helper.server';
import { serverContainer } from '../container/container.server';
Expand Down Expand Up @@ -89,7 +88,7 @@ export async function action(

try {
const response = await apiMethod.bind(client)(...args as Parameters<typeof apiMethod>);
return json(response);
return Response.json(response);
} catch (e) {
console_.error(e);
return problemDetails({
Expand Down
2 changes: 1 addition & 1 deletion app/routes/server.$serverId.tags.colors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ActionFunctionArgs } from '@remix-run/node';
import type { ActionFunctionArgs } from 'react-router';
import { AuthHelper } from '../auth/auth-helper.server';
import { serverContainer } from '../container/container.server';
import { TagsService } from '../tags/TagsService.server';
Expand Down
31 changes: 23 additions & 8 deletions app/routes/settings.$.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/node';
import { useFetcher, useLoaderData } from '@remix-run/react';
import type { Settings as AppSettings } from '@shlinkio/shlink-web-component/settings';
import { ShlinkWebSettings } from '@shlinkio/shlink-web-component/settings';
import { useCallback } from 'react';
import { useCallback, useEffect, useState } from 'react';
import type { ActionFunctionArgs, LoaderFunctionArgs } from 'react-router';
import { Route, Routes } from 'react-router';
import { useFetcher, useLoaderData } from 'react-router';
import { AuthHelper } from '../auth/auth-helper.server';
import { Layout } from '../common/Layout';
import { serverContainer } from '../container/container.server';
Expand Down Expand Up @@ -38,14 +39,28 @@ export default function Settings() {
method: 'POST',
encType: 'application/json',
}), [fetcher]);
const [isClient, setIsClient] = useState(false);

useEffect(() => {
setIsClient(true);
}, []);

return (
<Layout>
<ShlinkWebSettings
settings={settings}
updateSettings={submitSettings}
defaultShortUrlsListOrdering={{}}
/>
{isClient && (
<Routes>
<Route
path="*"
element={(
<ShlinkWebSettings
settings={settings}
updateSettings={submitSettings}
defaultShortUrlsListOrdering={{}}
/>
)}
/>
</Routes>
)}
</Layout>
);
}
4 changes: 4 additions & 0 deletions app/tailwind.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
font-weight: 500;
line-height: 1.2;
}

hr {
@apply my-3
}
}

@tailwind components;
Expand Down
6 changes: 4 additions & 2 deletions app/utils/response.server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { json } from '@remix-run/node';
import type { ProblemDetailsError } from '@shlinkio/shlink-js-sdk/api-contract';

export const empty = () => new Response(null, { status: 204 });

export const problemDetails = (errorPayload: ProblemDetailsError) => json(errorPayload, errorPayload.status);
export const problemDetails = (errorPayload: ProblemDetailsError) => Response.json(
errorPayload,
{ status: errorPayload.status },
);
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ services:
shlink_dashboard_node:
container_name: shlink_dashboard_node
user: 1000:1000
image: node:22.10-alpine
image: node:22.12-alpine
command: /bin/sh -c "cd /home/shlink-dashboard && npm install && node --run start"
volumes:
- ./:/home/shlink-dashboard
Expand Down
Loading

0 comments on commit 2d71020

Please sign in to comment.