Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
add: QRコードを生成する
Browse files Browse the repository at this point in the history
  • Loading branch information
Sea10wood committed Aug 10, 2024
1 parent f80e349 commit 3d0dcdb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Route, Routes } from "react-router-dom";
import QRCodePage from "./pages/QRCodeGenerator";
import Home from "./pages/home";
import Shooter from "./pages/shooter";
import Yatai from "./pages/yatai";
Expand All @@ -10,6 +11,7 @@ const AppRoutes = () => {
<Route path="/" element={<Home />} />
<Route path="/shooter" element={<Shooter />} />
<Route path="/yatai" element={<Yatai />} />
<Route path="/qrcode" element={<QRCodePage />} />
</Routes>
</>
);
Expand Down
22 changes: 22 additions & 0 deletions src/components/QRCodeGenerator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import QRCode from "qrcode.react";
import { useLocation } from "react-router-dom";
import { generateUUID } from "../utils/uuid";

type QRCodeGeneratorProps = {
size?: number;
};

const QRCodeGenerator: React.FC<QRCodeGeneratorProps> = ({ size = 128 }) => {
const location = useLocation();
const baseUrl = `${window.location.origin}${location.pathname}`;
const roomId = generateUUID();
const url = `${baseUrl}?room_id=${roomId}`;

return (
<div>
<QRCode value={url} size={size} renderAs="svg" />
</div>
);
};

export default QRCodeGenerator;
11 changes: 11 additions & 0 deletions src/pages/QRCodeGenerator/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import QRCodeGenerator from "../../components/QRCodeGenerator";

const QRCodePage: React.FC = () => {
return (
<div>
<QRCodeGenerator size={256} />
</div>
);
};

export default QRCodePage;
19 changes: 19 additions & 0 deletions src/utils/queryParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function generateQueryParams(
params: Record<string, string | number>,
): string {
const searchParams = new URLSearchParams();
// biome-ignore lint/complexity/noForEach: <explanation>
Object.entries(params).forEach(([key, value]) => {
searchParams.append(key, String(value));
});
return searchParams.toString();
}

export function appendQueryParamsToUrl(
baseUrl: string,
params: Record<string, string | number>,
): string {
const queryString = generateQueryParams(params);
const separator = baseUrl.includes("?") ? "&" : "?";
return `${baseUrl}${separator}${queryString}`;
}

0 comments on commit 3d0dcdb

Please sign in to comment.