NextJS 메인페이지 구현 with Copilot
VSCode의 Copilot을 이용해 필요한 UI를 구현해봅니다.
요구사항
•
RedirectURL을 입력할 수 있는 TextField
•
입력한 RedirectURL을 shortPath로 변환하는 요청 트리거 역할의 Button
1. page.tsx
프롬프트
단축 URL 서비스를 만들거야.
redirectURL을 입력받을 수 있는 TextField와 서버로 요청을 보낼 수 있는 버튼이 있는 메인페이지를 만들어줘.
단축URL을 만드는 API 정보
URL: https://zksnirpbgoppgoprlfdw.supabase.co/functions/v1/api/shortPath/v1/shortPath
body는 { "redirectURL" : "URL" }
Response는 {
"shortPath": "rH8unM"
}
이렇게 생겼어.
Plain Text
복사
•
src/app/page.tsx
"use client";
import { useState } from "react";
import Image from "next/image";
export default function Home() {
const [redirectURL, setRedirectURL] = useState("");
const [shortenedURL, setShortenedURL] = useState("");
const handleSubmit = async () => {
if (!redirectURL) {
alert("URL을 입력해주세요.");
return;
}
try {
const response = await fetch(
"https://zksnirpbgoppgoprlfdw.supabase.co/functions/v1/api/shortPath/v1/shortPath",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ redirectURL }),
}
);
if (!response.ok) {
throw new Error("URL 단축에 실패했습니다.");
}
const data = await response.json();
setShortenedURL(`${window.location.origin}/${data.shortPath}`);
} catch (error) {
console.error(error);
alert("오류가 발생했습니다. 다시 시도해주세요.");
}
};
return (
<div className="flex flex-col items-center justify-center min-h-screen p-8">
<h1 className="text-2xl font-bold mb-4">단축 URL 생성기</h1>
<input
type="text"
placeholder="단축할 URL을 입력하세요"
value={redirectURL}
onChange={(e) => setRedirectURL(e.target.value)}
className="border border-gray-300 rounded px-4 py-2 w-full max-w-md mb-4"
/>
<button
onClick={handleSubmit}
className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
>
단축 URL 생성
</button>
{shortenedURL && (
<div className="mt-4">
<p>단축된 URL:</p>
<a
href={shortenedURL}
target="_blank"
rel="noopener noreferrer"
className="text-blue-500 underline"
>
{shortenedURL}
</a>
</div>
)}
</div>
);
}
TypeScript
복사
단축할 URL을 입력하고 “단축 URL 생성” 버튼을 누르면 Supabase에서 구현했던 shortURL 생성 API를 호출하고 그 결과값을 보여줍니다.
이제 외부에는 {도메인}/{shortPath} 형태로 URL을 공유하게 됩니다.
다음 해야할 작업은 {도메인}/{shortPath} 에 접근했을때 원래의 redirectURL로 이동시켜주는 작업이 필요합니다.
끝.