44import jetpackAnalytics from '@automattic/jetpack-analytics' ;
55import { useBreakpointMatch , JetpackLogo } from '@automattic/jetpack-components' ;
66import { NavigableRegion , Page } from '@wordpress/admin-ui' ;
7+ import { TabPanel } from '@wordpress/components' ;
78import { useSelect } from '@wordpress/data' ;
8- import { useEffect } from '@wordpress/element' ;
9+ import { useCallback , useEffect , useMemo } from '@wordpress/element' ;
910import { __ } from '@wordpress/i18n' ;
10- import { Outlet , useLocation } from 'react-router' ;
11+ import { Outlet , useLocation , useNavigate } from 'react-router' ;
1112/**
1213 * Internal dependencies
1314 */
1415import useConfigValue from '../../../hooks/use-config-value' ;
1516import EmptySpamButton from '../../components/empty-spam-button' ;
1617import EmptyTrashButton from '../../components/empty-trash-button' ;
1718import ExportResponsesButton from '../../inbox/export-responses' ;
18- import Integrations from '../../integrations' ;
1919import { store as dashboardStore } from '../../store' ;
2020import ActionsDropdownMenu from '../actions-dropdown-menu' ;
2121import CreateFormButton from '../create-form-button' ;
22- import IntegrationsButton from '../integrations-button' ;
2322
2423import './style.scss' ;
2524// eslint-disable-next-line import/no-unresolved -- aliased to the package's built asset in webpack config.
2625import '@wordpress/admin-ui/build-style/style.css' ;
2726const Layout = ( ) => {
2827 const location = useLocation ( ) ;
28+ const navigate = useNavigate ( ) ;
2929 const [ isSm ] = useBreakpointMatch ( 'sm' ) ;
3030
3131 const enableIntegrationsTab = useConfigValue ( 'isIntegrationsEnabled' ) ;
@@ -40,32 +40,78 @@ const Layout = () => {
4040
4141 const isResponsesTrashView = currentStatus . includes ( 'trash' ) ;
4242 const isResponsesSpamView = currentStatus . includes ( 'spam' ) ;
43- const isIntegrationsOpen = location . pathname === '/integrations' ;
4443
4544 useEffect ( ( ) => {
4645 jetpackAnalytics . tracks . recordEvent ( 'jetpack_forms_dashboard_page_view' , {
4746 viewport : isSm ? 'mobile' : 'desktop' ,
4847 } ) ;
4948 } , [ isSm ] ) ;
5049
50+ const tabs = useMemo (
51+ ( ) => [
52+ {
53+ name : 'responses' ,
54+ title : __ ( 'Responses' , 'jetpack-forms' ) ,
55+ } ,
56+ ...( enableIntegrationsTab
57+ ? [ { name : 'integrations' , title : __ ( 'Integrations' , 'jetpack-forms' ) } ]
58+ : [ ] ) ,
59+ ] ,
60+ [ enableIntegrationsTab ]
61+ ) ;
62+
63+ const getCurrentTab = useCallback ( ( ) => {
64+ const path = location . pathname . split ( '/' ) [ 1 ] ;
65+ const validTabNames = tabs . map ( tab => tab . name ) ;
66+
67+ if ( validTabNames . includes ( path ) ) {
68+ return path ;
69+ }
70+
71+ return 'responses' ;
72+ } , [ location . pathname , tabs ] ) ;
73+
74+ const isResponsesTab = getCurrentTab ( ) === 'responses' ;
75+
76+ const handleTabSelect = useCallback (
77+ ( tabName : string ) => {
78+ if ( ! tabName ) {
79+ tabName = 'responses' ;
80+ }
81+
82+ const currentTab = getCurrentTab ( ) ;
83+
84+ if ( currentTab !== tabName ) {
85+ jetpackAnalytics . tracks . recordEvent ( 'jetpack_forms_dashboard_tab_change' , {
86+ tab : tabName ,
87+ viewport : isSm ? 'mobile' : 'desktop' ,
88+ previous_tab : currentTab ,
89+ } ) ;
90+ }
91+
92+ navigate ( {
93+ pathname : `/${ tabName } ` ,
94+ search : tabName === 'responses' ? location . search : '' ,
95+ } ) ;
96+ } ,
97+ [ navigate , location . search , isSm , getCurrentTab ]
98+ ) ;
99+
51100 const headerActions = isSm ? (
52101 < >
53- { isResponsesTrashView && < EmptyTrashButton /> }
54- { isResponsesSpamView && < EmptySpamButton /> }
55- < ActionsDropdownMenu exportData = { { show : true } } />
102+ { isResponsesTab && isResponsesTrashView && < EmptyTrashButton /> }
103+ { isResponsesTab && isResponsesSpamView && < EmptySpamButton /> }
104+ < ActionsDropdownMenu exportData = { { show : isResponsesTab } } />
56105 </ >
57106 ) : (
58- < >
59- { isResponsesTrashView && < EmptyTrashButton /> }
60- { isResponsesSpamView && < EmptySpamButton /> }
107+ < div className = "jp-forms__layout-header-actions" >
108+ { isResponsesTab && < ExportResponsesButton /> }
109+ { isResponsesTab && isResponsesTrashView && < EmptyTrashButton /> }
110+ { isResponsesTab && isResponsesSpamView && < EmptySpamButton /> }
61111 { ! isResponsesTrashView && ! isResponsesSpamView && (
62- < >
63- { enableIntegrationsTab && < IntegrationsButton /> }
64- < CreateFormButton label = { __ ( 'Create form' , 'jetpack-forms' ) } />
65- </ >
112+ < CreateFormButton label = { __ ( 'Create form' , 'jetpack-forms' ) } />
66113 ) }
67- < ExportResponsesButton />
68- </ >
114+ </ div >
69115 ) ;
70116
71117 return (
@@ -82,9 +128,18 @@ const Layout = () => {
82128 className = "admin-ui-page__content"
83129 ariaLabel = { __ ( 'Forms dashboard content' , 'jetpack-forms' ) }
84130 >
85- { ! isLoadingConfig && < Outlet /> }
131+ { ! isLoadingConfig && (
132+ < TabPanel
133+ className = "jp-forms__dashboard-tabs"
134+ tabs = { tabs }
135+ initialTabName = { getCurrentTab ( ) }
136+ onSelect = { handleTabSelect }
137+ key = { getCurrentTab ( ) }
138+ >
139+ { ( ) => < Outlet /> }
140+ </ TabPanel >
141+ ) }
86142 </ NavigableRegion >
87- { isIntegrationsOpen && < Integrations /> }
88143 </ Page >
89144 ) ;
90145} ;
0 commit comments