summaryrefslogtreecommitdiff
path: root/components/document-lists/vendor-doc-list-client.tsx
blob: 4aa00ad798254c2a08e8bd3790b57b8b0c711397 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"use client"
import * as React from "react"
import { useRouter, useParams } from "next/navigation"

import DocumentContainer from "@/components/documents/document-container"
import { ProjectInfo, ProjectSwitcher } from "@/components/documents/project-swicher"
import { InformationButton } from "@/components/information/information-button"
import { useTranslation } from "@/i18n/client"
interface VendorDocumentsClientProps {
    projects: ProjectInfo[]
    children: React.ReactNode
}

export default function VendorDocumentListClient({
    projects,
    children,
}: VendorDocumentsClientProps) {
    const router = useRouter()
    const params = useParams()
    const lng = (params?.lng as string) || "ko"
    const { t } = useTranslation(lng, "engineering")
    
    // Get the contractId from route parameters
    const contractIdFromUrl = React.useMemo(() => {
        if (params?.contractId) {
            const contractId = Array.isArray(params.contractId) 
                ? params.contractId[0] 
                : params.contractId
            return Number(contractId)
        }
        return null
    }, [params])
    
    // Use the URL contractId as the selected contract
    const [selectedContractId, setSelectedContractId] = React.useState<number | null>(
        contractIdFromUrl
    )
    
    // projectType 상태 추가
    const [projectType, setProjectType] = React.useState<string>("plant")
    
    // Update selectedContractId when URL changes
    React.useEffect(() => {
        if (contractIdFromUrl) {
            setSelectedContractId(contractIdFromUrl)
        }
    }, [contractIdFromUrl])

    // Handle contract selection
    function handleSelectContract(projectId: number, contractId: number) {
        const foundProjectType = projects.find(v => v.projectId === projectId)?.projectType || "ship"
        
        setSelectedContractId(contractId)
        setProjectType("plant")
        
        // Navigate to the contract's documents page
        router.push(`/${lng}/partners/document-list-only/${contractId}?projectType=plant`)
    }
    
    return (
        <>
            {/* 상단 영역: 제목 왼쪽 / ProjectSwitcher 오른쪽 */}
            <div className="flex items-center justify-between">
                {/* 왼쪽: 타이틀 & 설명 */}
                <div>
                    <div className="flex items-center gap-2">
                        <h2 className="text-2xl font-bold tracking-tight">{t("vendorDocuments.title")}</h2>
                        <InformationButton pagePath="partners/document-list" />
                    </div>
                    <p className="text-muted-foreground">
                        {projectType === "ship" 
                            ? t("vendorDocuments.shipDescription")
                            : t("vendorDocuments.plantDescription")
                        }
                    </p>
                </div>
                
                {/* 오른쪽: ProjectSwitcher */}
                <ProjectSwitcher
                    isCollapsed={false}
                    projects={projects}
                    selectedContractId={selectedContractId}
                    onSelectContract={handleSelectContract}
                />
            </div>
            
            {/* 문서 목록/테이블 영역 */}
            <section className="overflow-hidden rounded-[0.5rem] border bg-background shadow p-5">
                {children}
            </section>
        </>
    )
}