@ -1,32 +1,24 @@
import { css } from '@emotion/css' ;
import React , { FC } from 'react' ;
import React from 'react' ;
import { useAsync } from 'react-use' ;
import { DataFrame , GrafanaTheme2 } from '@grafana/data' ;
import { Card , Icon , Spinner , useStyles2 } from '@grafana/ui' ;
import { DataFrame , NavModel , NavModelItem } from '@grafana/data' ;
import { config } from '@grafana/runtime' ;
import { Alert , Card , Icon , Spinner } from '@grafana/ui' ;
import { Page } from 'app/core/components/Page/Page' ;
import { GrafanaRouteComponentProps } from 'app/core/navigation/types' ;
import { getGrafanaStorage } from './storage' ;
export interface Props extends GrafanaRouteComponentProps < { slug : string } > { }
export const StorageFolderPage : FC < Props > = ( props ) = > {
const slug = props . match . params . slug ;
const styles = useStyles2 ( getStyles ) ;
export function StorageFolderPage ( props : Props ) {
const slug = props . match . params . slug ? ? '' ;
const listing = useAsync ( ( ) : Promise < DataFrame | undefined > = > {
return getGrafanaStorage ( ) . list ( slug ) ;
} , [ slug ] ) ;
let base = document . location . pathname ;
if ( ! base . endsWith ( '/' ) ) {
base += '/' ;
}
let parent = '' ;
const idx = base . lastIndexOf ( '/' , base . length - 2 ) ;
if ( idx > 0 ) {
parent = base . substring ( 0 , idx ) ;
}
const childRoot = slug . length > 0 ? ` g/ ${ slug } / ` : 'g/' ;
const pageNav = getPageNavFromSlug ( slug ) ;
const renderListing = ( ) = > {
if ( listing . value ) {
@ -35,8 +27,10 @@ export const StorageFolderPage: FC<Props> = (props) => {
let name = item ;
const isFolder = name . indexOf ( '.' ) < 0 ;
const isDash = ! isFolder && name . endsWith ( '.json' ) ;
const url = ` ${ childRoot } ${ name } ` ;
return (
< Card key = { name } href = { isFolder || isDash ? base + name : undefined } >
< Card key = { name } href = { isFolder || isDash ? url : undefined } >
< Card.Heading > { name } < / Card.Heading >
< Card.Figure >
< Icon name = { isFolder ? 'folder' : isDash ? 'gf-grid' : 'file-alt' } size = "sm" / >
@ -51,29 +45,37 @@ export const StorageFolderPage: FC<Props> = (props) => {
return < div > ? < / div > ;
} ;
const navModel = getRootContentNavModel ( ) ;
return (
< div className = { styles . wrapper } >
{ slug ? . length > 0 && (
< >
< h1 > { slug } < / h1 >
< Card href = { parent } >
< Card.Heading > { parent } < / Card.Heading >
< Card.Figure >
< Icon name = "arrow-left" size = "sm" / >
< / Card.Figure >
< / Card >
< br / >
< / >
< Page navModel = { navModel } pageNav = { pageNav } >
{ ! config . featureToggles . topnav && (
< div >
< Alert title = "Enable the topnav feature toggle" > This page is designed assuming topnav is enabled < / Alert >
< / div >
) }
{ renderListing ( ) }
< / div >
< / Page >
) ;
} ;
}
export function getPageNavFromSlug ( slug : string ) {
const parts = slug . split ( '/' ) ;
let pageNavs : NavModelItem [ ] = [ ] ;
let url = 'g' ;
let lastPageNav : NavModelItem | undefined ;
for ( let i = 0 ; i < parts . length ; i ++ ) {
url += ` / ${ parts [ i ] } ` ;
pageNavs . push ( { text : parts [ i ] , url , parentItem : lastPageNav } ) ;
lastPageNav = pageNavs [ pageNavs . length - 1 ] ;
}
return lastPageNav ;
}
const getStyles = ( theme : GrafanaTheme2 ) = > ( {
wrapper : css `
margin : 50px ;
` ,
} ) ;
export function getRootContentNavModel ( ) : NavModel {
return { main : { text : 'C:' } , node : { text : 'Content' , url : '/g' } } ;
}
export default StorageFolderPage ;