Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f554999
git 연결
Namgyeon Jan 19, 2025
9b6a5ca
feat: 기본기능 구현
Namgyeon Jan 19, 2025
5569432
feat: favicon추가, font:Pretendard 적용
Namgyeon Jan 20, 2025
2fab846
feat: 검색기능 추가
Namgyeon Jan 20, 2025
fba2e36
chors: 배포 코드추가
Namgyeon Jan 20, 2025
8acaf03
chors: 배포 코드추가2
Namgyeon Jan 20, 2025
7cfb7c7
feat: props nickname 기본값 추가
Namgyeon Jan 20, 2025
6becf40
배포 코드 수정
Namgyeon Jan 20, 2025
a71f491
배포 코드 수정
Namgyeon Jan 20, 2025
ed284bb
배포 코드 수정
Namgyeon Jan 20, 2025
badd979
배포 코드 수정
Namgyeon Jan 21, 2025
eac3d41
배포 오류 수정을 위해 파일경로 수정
Namgyeon Jan 21, 2025
b8f1f6e
배포 오류 수정을 위해 파일경로 수정
Namgyeon Jan 21, 2025
c9c3229
배포 오류 수정
Namgyeon Jan 21, 2025
4e00daa
feat: 서버사이드 렌더링으로 변경
Namgyeon Jan 21, 2025
f4de0eb
feat: AllPosts SSR로 변경
Namgyeon Jan 21, 2025
38b936a
feat: api요청에 URLSearchParams를 사용
Namgyeon Jan 22, 2025
6f7bdb6
feat: OrderByValue type alias 추가
Namgyeon Jan 22, 2025
979b2ac
feat: baseURL 상수화
Namgyeon Jan 22, 2025
a0c4627
feat: 에러 핸들링 수정
Namgyeon Jan 22, 2025
75419fb
feat: 불필요한 상태 제거
Namgyeon Jan 22, 2025
895e82a
refactor: 콤포넌트 prop 가독성, 유지보수 개선
Namgyeon Jan 23, 2025
a09eae4
refactor: 불필요한 useState삭제, textMapper 객체를 활용하여 기능 유지
Namgyeon Jan 23, 2025
bbaa9f2
refactor: useMediaQuery hooks를 따로 구분하여 가독성 개선
Namgyeon Jan 23, 2025
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
39 changes: 39 additions & 0 deletions next/src/hooks/useMediaQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useState, useEffect } from "react";
// 479, 767, 1023

type MediaQuery = {
isMobile: boolean;
isTablet: boolean;
isDesktop: boolean;
};

export function useMediaQuery(): MediaQuery {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 커스텀 훅 사용하신거 상당히 좋습니다.

조금만 더 정리해본다면, 지금 상황의 경우, pageSize 가 객체로 데이터를 주다보니 결국 활용하는 입장에서 이 결과를 또 다시 어디ㅓㄴ가 풀어 사용해야할텐데요.
그냥 이런 경우 직관적으로 풀어서 필드 하나씩 때려주는것도 괜찮은 방법이에요.

개인적으로 사용하는 코드 참고 차원에서 첨부드립니다

"use client";

import { useEffect, useState } from "react";

function getDevice(): "mobile" | "tablet" | "desktop" | null {
  if (typeof window === "undefined") return null;

  return window.matchMedia("(min-width: 1024px)").matches
    ? "desktop"
    : window.matchMedia("(min-width: 640px)").matches
      ? "tablet"
      : "mobile";
}

function getDeminsions() {
  if (typeof window === "undefined") return null;

  return { width: window.innerWidth, height: window.innerHeight };
}

export function useMediaQuery() {
  const [device, setDevice] = useState<"mobile" | "tablet" | "desktop" | null>(
    getDevice(),
  );

  const [dimensions, setDimensions] = useState<{
    width: number;
    height: number;
  } | null>(getDeminsions());

  useEffect(() => {
    const checkDevice = () => {
      setDevice(getDevice());
      setDimensions(getDeminsions());
    };

    checkDevice();

    window.addEventListener("resize", checkDevice);
    return () => {
      window.removeEventListener("resize", checkDevice);
    };
  }, []);

  return {
    device,
    width: dimensions?.width,
    height: dimensions?.height,
    isMobile: device === "mobile",
    isTablet: device === "tablet",
    isDesktop: device === "desktop",
  };
}

const [pageSize, setPageSize] = useState({
isMobile: false,
isTablet: false,
isDesktop: false,
});

const updatePageSize = () => {
const width = window.innerWidth;

setPageSize({
isMobile: width < 480,
isTablet: width >= 480 && width < 768,
isDesktop: width >= 768,
});
};
// 추후 디바운스 활용해서 최적화 도전(공부필요).
useEffect(() => {
updatePageSize();

const handleResize = () => updatePageSize();
window.addEventListener("resize", handleResize);

return () => {
window.removeEventListener("resize", handleResize);
};
}, []);

return pageSize;
}
29 changes: 7 additions & 22 deletions next/src/pages/boards/components/BestPosts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,22 @@ import BestPost from "./BestPost";
import styles from "./BestPosts.module.css";
import { useEffect, useState } from "react";
import fetchPosts from "@/lib/fetch-posts";
import { useMediaQuery } from "@/hooks/useMediaQuery";

interface BestPostProps {
initialBestPosts: Post[];
}

// 479, 767, 1023

export default function BestPosts({ initialBestPosts }: BestPostProps) {
const { isMobile, isTablet, isDesktop } = useMediaQuery();
const [pageSize, setPageSize] = useState(3);
const [bestPosts, setBestPosts] = useState<Post[]>(initialBestPosts);

const pageSizeCheck = () => {
const pageWidth = window.innerWidth;
if (pageWidth < 480) {
setPageSize(1);
} else if (pageWidth >= 480 && pageWidth < 768) {
setPageSize(2);
} else {
setPageSize(3);
}
};

useEffect(() => {
pageSizeCheck();

const handleResize = () => pageSizeCheck();
window.addEventListener("resize", handleResize);

return () => {
window.removeEventListener("resize", handleResize);
};
}, []);
if (isMobile) setPageSize(1);
if (isTablet) setPageSize(2);
if (isDesktop) setPageSize(3);
}, [isMobile, isTablet, isDesktop]);

useEffect(() => {
const fetchBestPosts = async () => {
Expand All @@ -43,6 +27,7 @@ export default function BestPosts({ initialBestPosts }: BestPostProps) {
};
fetchBestPosts();
}, [pageSize]);

return (
<div className={styles.container}>
<h3 className={styles.title}>베스트 게시글</h3>
Expand Down
Loading