1
1
import React , { useState , useEffect } from 'react' ;
2
2
import { SidebarRightIcon } from '@navikt/aksel-icons' ;
3
+ import { xpArchiveConfig } from '@common/shared/siteConfigs' ;
3
4
import { Button , Detail , Heading , Label } from '@navikt/ds-react' ;
4
5
import { useFetchContent } from '../hooks/useFetchContent' ;
5
6
import { useAppState } from '../context/appState/useAppState' ;
@@ -17,6 +18,11 @@ const getDefaultView = (isWebpage: boolean, hasAttachment: boolean): ViewVariant
17
18
return undefined ;
18
19
} ;
19
20
21
+ const updateContentUrl = ( nodeId : string , locale : string , versionId ?: string ) => {
22
+ const newUrl = `${ xpArchiveConfig . basePath } /${ nodeId } /${ locale } /${ versionId ?? '' } ` ;
23
+ window . history . pushState ( { } , '' , newUrl ) ;
24
+ } ;
25
+
20
26
export const Content = ( ) => {
21
27
const { selectedContentId, selectedLocale, selectedVersion, setSelectedVersion } =
22
28
useAppState ( ) ;
@@ -36,6 +42,16 @@ export const Content = () => {
36
42
versionId : selectedVersion ?? '' ,
37
43
} ) ;
38
44
45
+ useEffect ( ( ) => {
46
+ if ( selectedVersion ) {
47
+ updateContentUrl ( selectedContentId ?? '' , selectedLocale , selectedVersion ) ;
48
+ } else if ( data ?. versions ?. [ 0 ] ) {
49
+ const latestVersionId = data . versions [ 0 ] . versionId ;
50
+ setSelectedVersion ( latestVersionId ) ;
51
+ updateContentUrl ( selectedContentId ?? '' , selectedLocale , latestVersionId ) ;
52
+ }
53
+ } , [ data , selectedContentId , selectedLocale , selectedVersion ] ) ;
54
+
39
55
const isWebpage = ! ! data ?. html && ! data . json . attachment ;
40
56
const hasAttachment = ! ! data ?. json . attachment ;
41
57
const [ selectedView , setSelectedView ] = useState < ViewVariant | undefined > (
@@ -47,6 +63,15 @@ export const Content = () => {
47
63
setSelectedView ( getDefaultView ( isWebpage , hasAttachment ) ) ;
48
64
} , [ isWebpage , hasAttachment , selectedContentId ] ) ;
49
65
66
+ const getVersionDisplay = ( ) => {
67
+ if ( selectedVersion && data ?. versions ) {
68
+ return formatTimestamp (
69
+ data . versions . find ( ( v ) => v . versionId === selectedVersion ) ?. timestamp ?? ''
70
+ ) ;
71
+ }
72
+ return 'Laster...' ;
73
+ } ;
74
+
50
75
if ( ! selectedContentId ) {
51
76
return < EmptyState /> ;
52
77
}
@@ -75,14 +100,7 @@ export const Content = () => {
75
100
iconPosition = { 'right' }
76
101
onClick = { ( ) => setIsVersionPanelOpen ( true ) }
77
102
>
78
- { selectedVersion && data ?. versions
79
- ? formatTimestamp (
80
- data . versions . find ( ( v ) => v . versionId === selectedVersion )
81
- ?. timestamp ?? ''
82
- )
83
- : data ?. versions ?. [ 0 ]
84
- ? `${ formatTimestamp ( data . versions [ 0 ] . timestamp , true ) } (Siste versjon)`
85
- : 'Laster...' }
103
+ { getVersionDisplay ( ) }
86
104
</ Button >
87
105
< VersionSelector
88
106
versions = { data ?. versions || [ ] }
0 commit comments