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

Implement yasunorization #152

Merged
merged 3 commits into from
Nov 2, 2024
Merged
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
20 changes: 19 additions & 1 deletion packages/web/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import {
useMatches,
useNavigate,
useRouteLoaderData,
useSearchParams,
} from "@remix-run/react";
import "@mantine/core/styles.css";
import { useDisclosure, useHeadroom } from "@mantine/hooks";
import { motion } from "framer-motion";
import { useMemo } from "react";
import IconGitHubLogo from "~icons/tabler/brand-github";
import IconGraph from "~icons/tabler/graph";
import { YasunoriSpotlight } from "./components/yasunori-spotlight";
Expand All @@ -47,6 +49,21 @@ export const links: LinksFunction = () => [
},
];

/** yasunorized された yasunori を取得する
* URLクリエにyasunori=<not yasunori>をつけると、
* 先頭4文字を利用して Xxxxnori という文字列にします。
*/
function useYasunorizedYasunori() {
const [searchParams] = useSearchParams();
const yasunori = searchParams.get("yasunori");
return useMemo(() => {
if (!yasunori) return "Yasunori";
const firstChar = yasunori.slice(0, 1);
const secondChar = yasunori.slice(1, 4);
return `${firstChar.toUpperCase()}${secondChar}nori`;
}, [yasunori]);
}

export function Layout({ children }: { children: React.ReactNode }) {
const data = useRouteLoaderData<IndexLoader>("routes/_index");
const matches = useMatches();
Expand All @@ -56,6 +73,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
const pinned = useHeadroom({ fixedAt: 120 });
const [opened, { toggle, close }] = useDisclosure();
const navigate = useNavigate();
const yasunorizedYasunori = useYasunorizedYasunori();
return (
<html lang="ja">
<head>
Expand Down Expand Up @@ -119,7 +137,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
repeat: Number.POSITIVE_INFINITY,
}}
>
Yasunori
{yasunorizedYasunori}
</motion.span>
</Button>
</Group>
Expand Down
21 changes: 19 additions & 2 deletions packages/web/app/routes/_index/loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { AppLoadContext, LoaderFunctionArgs } from "@remix-run/cloudflare";
import { http, HttpResponse } from "msw";
import { mockServer } from "../../../mocks/server";
import { indexLoader } from "./loader";

describe("indexLoader", () => {
test("returns data", async () => {
const result = await indexLoader();
const loaderFunctionArgs: LoaderFunctionArgs = {
request: new Request("https://example.com", {
method: "GET",
}),
params: {},
context: {} as AppLoadContext,
};
const result = await indexLoader(loaderFunctionArgs);
expect(result.length).toBe(12);
expect(result.at(-1)).toStrictEqual({
at: "vim-jp radioお便り",
Expand All @@ -26,6 +34,15 @@ describe("indexLoader", () => {
});
}),
);
expect(indexLoader()).rejects.toEqual(new Response(null, { status: 404 }));
const loaderFunctionArgs: LoaderFunctionArgs = {
request: new Request("https://example.com", {
method: "GET",
}),
params: {},
context: {} as AppLoadContext,
};
expect(indexLoader(loaderFunctionArgs)).rejects.toEqual(
new Response(null, { status: 404 }),
);
});
});
6 changes: 4 additions & 2 deletions packages/web/app/routes/_index/loader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { LoaderFunction } from "@remix-run/cloudflare";
import { fetchAwesomeYasunori } from "~/shared/fetch-awesome-yasunori";

export const indexLoader = (async () => {
const data = await fetchAwesomeYasunori();
export const indexLoader = (async ({ request }) => {
const url = new URL(request.url);
const yasunori = url.searchParams.get("yasunori") ?? undefined;
const data = await fetchAwesomeYasunori(yasunori ? { yasunori } : undefined);
if (!data) {
throw new Response(null, { status: 404 });
}
Expand Down
8 changes: 6 additions & 2 deletions packages/web/app/shared/fetch-awesome-yasunori.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { yasunoriApiClient } from "./yasunori-api";

export async function fetchAwesomeYasunori() {
const res = await yasunoriApiClient.awesome.$get();
export async function fetchAwesomeYasunori(
query?: { yasunori: string } | undefined,
) {
const res = await yasunoriApiClient.awesome.$get(
query?.yasunori ? { query: { yasunori: query.yasunori } } : undefined,
);
if (!res.ok) {
return null;
}
Expand Down