Skip to content

Commit 1734022

Browse files
committed
feat, fix(analysis): analysis api 대폭 수정, 비로그인 사용자 커뮤니티 조회
1 parent b961852 commit 1734022

File tree

11 files changed

+105
-147
lines changed

11 files changed

+105
-147
lines changed

front/src/app/(dashboard)/analysis/[id]/page.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,24 @@ export default function ResultsPage() {
1212
const params = useParams()
1313
const router = useRouter()
1414
const repoId = Number(params.id)
15-
const user = typeof window !== "undefined" ? JSON.parse(localStorage.getItem("user") || "{}") : null
16-
const userId = user?.id
1715

18-
const { history, result, loading, selectedId, setSelectedId, reload } = useAnalysisResult(userId, repoId)
16+
const { history, result, loading, selectedId, setSelectedId, reload } = useAnalysisResult(repoId)
1917

2018
if (loading)
2119
return <div className="p-8 text-center text-muted-foreground">🕓 분석 결과를 불러오는 중...</div>
2220

2321
if (!history || !result)
2422
return <div className="p-8 text-center text-muted-foreground">⚠️ 분석 데이터를 찾을 수 없습니다.</div>
2523

24+
// ✅ API 응답에서 ownerId 추출
25+
const ownerId = history.repository.ownerId
26+
27+
// ✅ 로그인한 사용자 (권한 체크용)
28+
const currentUser = typeof window !== "undefined"
29+
? JSON.parse(localStorage.getItem("user") || "null")
30+
: null
31+
const currentUserId = currentUser?.id
32+
2633
const radarData = [
2734
{ category: "README", score: (result.readmeScore / 30) * 100 },
2835
{ category: "TEST", score: (result.testScore / 30) * 100 },
@@ -45,7 +52,7 @@ export default function ResultsPage() {
4552
history={history}
4653
selectedId={selectedId}
4754
onSelect={setSelectedId}
48-
userId={userId}
55+
userId={currentUserId}
4956
repoId={repoId}
5057
onDeleted={handleDeleted} />
5158

@@ -58,7 +65,7 @@ export default function ResultsPage() {
5865

5966
{/* 🌐 공개 설정 및 커뮤니티 섹션 */}
6067
<RepositoryPublicSection
61-
userId={history.repository.ownerId}
68+
userId={ownerId}
6269
repoId={repoId}
6370
initialPublic={history.repository.publicRepository} />
6471
</div>

front/src/app/(dashboard)/users/history/page.tsx

Lines changed: 0 additions & 1 deletion
This file was deleted.

front/src/components/analysis/RepositoryPublicSection.tsx

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import { useEffect, useState } from "react"
44
import { Card } from "@/components/ui/card"
5-
import { Button } from "@/components/ui/Button" // ✅ 대소문자 수정
65
import { Switch } from "@/components/ui/switch"
76
import { ShareButton } from "@/components/analysis/ShareButton"
87
import { Globe, Lock, MessageSquare } from "lucide-react"
98
import { useRepositoryPublic } from "@/hooks/analysis/useRepositoryPublic"
109
import { CommentSection } from "@/components/community/CommentSection"
1110
import { analysisApi } from "@/lib/api/analysis"
1211
import type { HistoryResponseDto } from "@/types/analysis"
12+
import { useAuth } from "@/hooks/auth/useAuth"
1313

1414
interface Props {
1515
userId: number
@@ -19,29 +19,18 @@ interface Props {
1919

2020
export function RepositoryPublicSection({ userId, repoId, initialPublic }: Props) {
2121
const { isPublic, togglePublic } = useRepositoryPublic(initialPublic, userId, repoId)
22-
22+
const { user, isAuthed, isInitializing } = useAuth()
23+
const currentUserId = user?.id ?? null
24+
2325
const [analysisResultId, setAnalysisResultId] = useState<number | null>(null)
2426
const [loading, setLoading] = useState(true)
25-
const [currentUserId, setCurrentUserId] = useState<number | null>(null)
26-
27-
useEffect(() => {
28-
const stored = localStorage.getItem("user")
29-
if (stored) {
30-
try {
31-
const parsed = JSON.parse(stored)
32-
setCurrentUserId(Number(parsed.id))
33-
} catch (err) {
34-
console.error("유저 정보 파싱 실패:", err)
35-
}
36-
}
37-
}, [])
3827

3928
useEffect(() => {
40-
if (!userId || !repoId) return
29+
if (!repoId) return
4130

4231
const loadAnalysisId = async () => {
4332
try {
44-
const historyResponse: HistoryResponseDto = await analysisApi.getRepositoryHistory(userId, repoId)
33+
const historyResponse: HistoryResponseDto = await analysisApi.getRepositoryHistory(repoId)
4534
// ✅ 최신 분석 결과 ID 추출
4635
if (Array.isArray(historyResponse.analysisVersions) && historyResponse.analysisVersions.length > 0) {
4736
const latest = historyResponse.analysisVersions[0]
@@ -57,16 +46,16 @@ export function RepositoryPublicSection({ userId, repoId, initialPublic }: Props
5746
}
5847

5948
loadAnalysisId()
60-
}, [userId, repoId])
49+
}, [repoId])
6150

62-
if (currentUserId === null) {
51+
if (isInitializing) {
6352
return <div className="p-6 text-center text-muted-foreground">사용자 정보를 불러오는 중...</div>
6453
}
6554

6655
return (
6756
<>
6857
{/* 🌐 공개 설정 */}
69-
{currentUserId === userId && (
58+
{isAuthed && currentUserId === userId && (
7059
<Card className="mb-8 p-6">
7160
<div className="flex items-center justify-between">
7261
<div className="flex items-center gap-3">
@@ -114,14 +103,7 @@ export function RepositoryPublicSection({ userId, repoId, initialPublic }: Props
114103
) : analysisResultId ? (
115104
<div className="text-muted-foreground text-sm">
116105
{/* ✏️ 댓글 작성 폼 */}
117-
<CommentSection analysisResultId={analysisResultId} memberId={userId} />
118-
119-
{/* 💬 댓글 목록 */}
120-
<div className="border-t pt-6">
121-
{/* 댓글 목록이 CommentSection 안에서 렌더링되거나 별도 CommentList가 있다면 여기에 배치 */}
122-
{/* 예: <CommentList analysisResultId={analysisResultId} /> */}
123-
{/* CommentSection이 이미 작성+조회 포함한다면 그대로 둬도 됨 */}
124-
</div>
106+
<CommentSection analysisResultId={analysisResultId} />
125107
</div>
126108
) : (
127109
<p className="text-muted-foreground text-sm">아직 분석 기록이 없습니다.</p>
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
"use client"
1+
"use client"
22

33
import { useState } from "react"
4+
import { useAuth } from "@/hooks/auth/useAuth"
45
import { CommentList } from "./CommentList"
56
import { CommentForm } from "./CommentForm"
67

78
interface CommentSectionProps {
89
analysisResultId: number
9-
memberId: number
1010
}
1111

12-
export function CommentSection({ analysisResultId, memberId }: CommentSectionProps) {
12+
export function CommentSection({ analysisResultId }: CommentSectionProps) {
13+
const { isAuthed } = useAuth()
1314
const [refreshKey, setRefreshKey] = useState(0)
1415

1516
return (
1617
<div className="mt-6 space-y-6">
1718
{/* ✏️ 댓글 작성 폼 */}
18-
<CommentForm
19-
analysisResultId={analysisResultId}
20-
memberId={memberId}
21-
onCommentAdded={() => setRefreshKey((k) => k + 1)}
22-
/>
19+
{isAuthed ? (
20+
<CommentForm
21+
analysisResultId={analysisResultId}
22+
onCommentAdded={() => setRefreshKey((k) => k + 1)}
23+
/>
24+
) : (
25+
<p className="text-sm text-muted-foreground">로그인 후 댓글을 작성할 수 있어요.</p>
26+
)}
2327

2428
{/* 💬 댓글 목록 */}
2529
<CommentList key={refreshKey} analysisResultId={analysisResultId} />
2630
</div>
2731
)
28-
}
32+
}

front/src/hooks/analysis/useAnalysis.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,11 @@ export function useAnalysis() {
3333
}
3434
}
3535

36-
/** 📦 사용자별 저장소 목록 조회 */
37-
const getUserRepositories = async (userId: number): Promise<RepositoryResponse[]> => {
38-
setIsLoading(true)
39-
setError(null)
40-
41-
try {
42-
const repositories = await analysisApi.getUserRepositories(userId)
43-
return repositories
44-
} catch (err: any) {
45-
const message =
46-
err?.response?.data?.message ||
47-
err?.message ||
48-
"저장소 목록을 불러오는 중 오류가 발생했습니다."
49-
const apiError = new Error(message) as ApiError
50-
setError(apiError)
51-
throw apiError
52-
} finally {
53-
setIsLoading(false)
54-
}
55-
}
56-
5736
/** ❌ 에러 초기화 */
5837
const clearError = () => setError(null)
5938

6039
return {
6140
requestAnalysis,
62-
getUserRepositories,
6341
isLoading,
6442
error,
6543
clearError,

front/src/hooks/analysis/useAnalysisResult.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import { analysisApi } from "@/lib/api/analysis"
55
import type { HistoryResponseDto, AnalysisResultResponseDto } from "@/types/analysis"
66
import { formatDateTimeKST } from "@/lib/utils/formatDate"
77

8-
export function useAnalysisResult(userId?: number, repoId?: number) {
8+
export function useAnalysisResult(repoId?: number) {
99
const [history, setHistory] = useState<HistoryResponseDto | null>(null)
1010
const [selectedId, setSelectedId] = useState<number | null>(null)
1111
const [result, setResult] = useState<AnalysisResultResponseDto | null>(null)
1212
const [loading, setLoading] = useState(true)
1313

1414
const load = useCallback(async () => {
15-
if (!userId || !repoId) return
15+
if (!repoId) return
1616
setLoading(true)
1717

1818
try {
19-
// 1️⃣ 히스토리 불러오기
20-
const data = await analysisApi.getRepositoryHistory(userId, repoId)
19+
// 1️⃣ 히스토리 불러오기 (JWT 자동 처리)
20+
const data = await analysisApi.getRepositoryHistory(repoId)
2121

2222
// 최신순 정렬
2323
const sorted = [...data.analysisVersions].sort(
@@ -39,21 +39,20 @@ export function useAnalysisResult(userId?: number, repoId?: number) {
3939

4040
// 3️⃣ 선택된 분석 결과 불러오기
4141
if (latestId) {
42-
const detail = await analysisApi.getAnalysisDetail(userId, repoId, latestId)
42+
const detail = await analysisApi.getAnalysisDetail(repoId, latestId)
4343
setResult(detail)
4444
} else {
4545
setResult(null)
4646
}
4747
} catch (err) {
4848
console.error("❌ useAnalysisResult load() error:", err)
49+
setHistory(null)
50+
setResult(null)
4951
} finally {
5052
setLoading(false)
5153
}
52-
}, [userId, repoId])
54+
}, [repoId])
5355

54-
/**
55-
* 🎬 최초 로드
56-
*/
5756
useEffect(() => {
5857
load()
5958
}, [load])
@@ -62,12 +61,13 @@ export function useAnalysisResult(userId?: number, repoId?: number) {
6261
* 🔄 특정 버전 선택 시 분석 결과 다시 로드
6362
*/
6463
useEffect(() => {
65-
if (!selectedId || !userId || !repoId) return
64+
if (!selectedId || !repoId || !history) return
65+
6666
;(async () => {
67-
const detail = await analysisApi.getAnalysisDetail(userId, repoId, selectedId)
67+
const detail = await analysisApi.getAnalysisDetail(repoId, selectedId)
6868
setResult(detail)
6969
})()
70-
}, [selectedId, userId, repoId])
70+
}, [selectedId, repoId, history])
7171

7272
return {
7373
history,
@@ -77,4 +77,4 @@ export function useAnalysisResult(userId?: number, repoId?: number) {
7777
setSelectedId,
7878
reload: load, // ✅ 외부에서 다시 불러올 수 있도록 노출
7979
}
80-
}
80+
}

0 commit comments

Comments
 (0)