import { DndContext, DragEndEvent, PointerSensor, useSensor, useSensors } from '@dnd-kit/core' import { arrayMove, SortableContext, verticalListSortingStrategy } from '@dnd-kit/sortable' import { IS_PLATFORM, useFlag, useParams } from 'common' import { ProjectUsageSection as ProjectUsageSectionV1 } from 'components/interfaces/Home/ProjectUsageSection' import { SortableSection } from 'components/interfaces/ProjectHome/SortableSection' import { TopSection } from 'components/interfaces/ProjectHome/TopSection' import { ScaffoldContainer, ScaffoldSection } from 'components/layouts/Scaffold' import dayjs from 'dayjs' import { useLocalStorage } from 'hooks/misc/useLocalStorage' import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject' import { usePHFlag } from 'hooks/ui/useFlag' import { PROJECT_STATUS } from 'lib/constants' import { useTrack } from 'lib/telemetry/track' import { useEffect, useRef } from 'react' import { useAppStateSnapshot } from 'state/app-state' import { cn } from 'ui' import { AdvisorSection } from './AdvisorSection' import { ConnectSection } from './ConnectSection' import type { ConnectSectionVariant } from './ConnectSection.config' import { CustomReportSection } from './CustomReportSection' import { type GettingStartedState } from './GettingStarted/GettingStarted.types' import { GettingStartedSection } from './GettingStarted/GettingStartedSection' import { DEFAULT_SECTION_ORDER, mergeSectionOrder, getSectionVisibility } from './Home.utils' import { ProjectUsageSection as ProjectUsageSectionV2 } from './ProjectUsageSection' export const ProjectHome = () => { const { enableBranching } = useParams() const snap = useAppStateSnapshot() const { data: project } = useSelectedProjectQuery() const track = useTrack() const showHomepageUsageV2 = useFlag('newHomepageUsageV2') const connectSectionVariant = usePHFlag('connectSection') const isMatureProject = dayjs(project?.inserted_at).isBefore(dayjs().subtract(10, 'day')) const hasShownEnableBranchingModalRef = useRef(false) const isPaused = project?.status === PROJECT_STATUS.INACTIVE const isComingUp = project?.status === PROJECT_STATUS.COMING_UP const [sectionOrder, setSectionOrder] = useLocalStorage( `home-section-order-${project?.ref || 'default'}`, DEFAULT_SECTION_ORDER ) const [gettingStartedState, setGettingStartedState] = useLocalStorage( `home-getting-started-${project?.ref || 'default'}`, 'empty' ) const UsageSection = showHomepageUsageV2 ? ProjectUsageSectionV2 : ProjectUsageSectionV1 const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 8 } })) const handleDragEnd = (event: DragEndEvent) => { const { active, over } = event if (!over || active.id === over.id) return setSectionOrder((items) => { const oldIndex = items.indexOf(String(active.id)) const newIndex = items.indexOf(String(over.id)) if (oldIndex === -1 || newIndex === -1) return items track('home_section_rows_moved', { section_moved: String(active.id), old_position: oldIndex, new_position: newIndex, }) return arrayMove(items, oldIndex, newIndex) }) } useEffect(() => { if (enableBranching && !hasShownEnableBranchingModalRef.current) { hasShownEnableBranchingModalRef.current = true snap.setShowCreateBranchModal(true) } }, [enableBranching, snap]) useEffect(() => { setSectionOrder(mergeSectionOrder) }, [setSectionOrder]) const { showConnectSection, showGettingStarted } = getSectionVisibility({ connectSectionVariant, isMatureProject, hasProject: !!project, gettingStartedState, }) return (
{!isPaused && ( { if (id === 'connect') return showConnectSection if (id === 'getting-started') return showGettingStarted return true })} strategy={verticalListSortingStrategy} > {sectionOrder.map((id) => { if (IS_PLATFORM && id === 'usage') { return (
) } if (id === 'connect' && showConnectSection) { return ( ) } if (id === 'getting-started' && showGettingStarted) { return ( ) } if (id === 'advisor') { return (
) } if (id === 'custom-report') { return (
) } })}
)}
) }