|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { useFetchContent } from '../hooks/useFetchContent'; |
| 3 | +import { useAppState } from '../context/appState/useAppState'; |
| 4 | +import { ViewSelector, ViewVariant } from 'client/viewSelector/ViewSelector'; |
| 5 | +import { VersionSelector } from 'client/versionSelector/VersionSelector'; |
| 6 | +import { ContentView } from '../contentView/ContentView'; |
| 7 | + |
| 8 | +import style from './Content.module.css'; |
| 9 | + |
| 10 | +const getDefaultView = (isWebpage: boolean, hasAttachment: boolean): ViewVariant => { |
| 11 | + if (isWebpage) return 'html'; |
| 12 | + if (hasAttachment) return 'filepreview'; |
| 13 | + return 'json'; |
| 14 | +}; |
| 15 | + |
| 16 | +export const Content = () => { |
| 17 | + const { selectedContentId, selectedLocale, selectedVersion } = useAppState(); |
| 18 | + |
| 19 | + const fetchId = selectedVersion?.nodeId || selectedContentId; |
| 20 | + |
| 21 | + const { data, isLoading } = useFetchContent({ |
| 22 | + id: fetchId || '', |
| 23 | + locale: selectedLocale, |
| 24 | + versionId: selectedVersion?.versionId ?? undefined, |
| 25 | + }); |
| 26 | + |
| 27 | + const isWebpage = !!data?.html && !data.json.attachment; |
| 28 | + const hasAttachment = !!data?.json.attachment; |
| 29 | + const [selectedView, setSelectedView] = useState<ViewVariant>( |
| 30 | + getDefaultView(isWebpage, hasAttachment) |
| 31 | + ); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + setSelectedView(getDefaultView(isWebpage, hasAttachment)); |
| 35 | + }, [isWebpage, hasAttachment]); |
| 36 | + |
| 37 | + return ( |
| 38 | + <> |
| 39 | + {selectedContentId ? ( |
| 40 | + <div className={style.content}> |
| 41 | + <div className={style.top}> |
| 42 | + <ViewSelector |
| 43 | + selectedView={selectedView} |
| 44 | + setSelectedView={setSelectedView} |
| 45 | + hasAttachment={hasAttachment} |
| 46 | + isWebpage={isWebpage} |
| 47 | + /> |
| 48 | + <VersionSelector versions={data?.versions || []} /> |
| 49 | + </div> |
| 50 | + <ContentView selectedView={selectedView} isLoading={isLoading} data={data} /> |
| 51 | + </div> |
| 52 | + ) : null} |
| 53 | + </> |
| 54 | + ); |
| 55 | +}; |
0 commit comments