-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Spaces] Space Avatar with selector in main Kibana menu (#18609)
Adds Space Avatar with selector in main Kibana menu
- Loading branch information
Showing
28 changed files
with
806 additions
and
875 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export function getSpaceUrlContext(basePath = '/', defaultContext = null) { | ||
// Look for `/s/space-url-context` in the base path | ||
const matchResult = basePath.match(/\/s\/([a-z0-9\-]+)/); | ||
|
||
if (!matchResult || matchResult.length === 0) { | ||
return defaultContext; | ||
} | ||
|
||
// Ignoring first result, we only want the capture group result at index 1 | ||
const [, urlContext = defaultContext] = matchResult; | ||
|
||
return urlContext; | ||
} | ||
|
||
export function stripSpaceUrlContext(basePath = '/') { | ||
const currentSpaceUrlContext = getSpaceUrlContext(basePath); | ||
|
||
let basePathWithoutSpace; | ||
if (currentSpaceUrlContext) { | ||
const indexOfSpaceContext = basePath.indexOf(`/s/${currentSpaceUrlContext}`); | ||
|
||
const startsWithSpace = indexOfSpaceContext === 0; | ||
|
||
if (startsWithSpace) { | ||
basePathWithoutSpace = '/'; | ||
} else { | ||
basePathWithoutSpace = basePath.substring(0, indexOfSpaceContext); | ||
} | ||
} else { | ||
basePathWithoutSpace = basePath; | ||
} | ||
|
||
if (basePathWithoutSpace.endsWith('/')) { | ||
return basePathWithoutSpace.substr(0, -1); | ||
} | ||
|
||
return basePathWithoutSpace; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { stripSpaceUrlContext, getSpaceUrlContext } from './spaces_url_parser'; | ||
|
||
test('it removes the space url context from the base path when the space is not at the root', () => { | ||
const basePath = `/foo/s/my-space`; | ||
expect(stripSpaceUrlContext(basePath)).toEqual('/foo'); | ||
}); | ||
|
||
test('it removes the space url context from the base path when the space is the root', () => { | ||
const basePath = `/s/my-space`; | ||
expect(stripSpaceUrlContext(basePath)).toEqual(''); | ||
}); | ||
|
||
test(`it doesn't change base paths without a space url context`, () => { | ||
const basePath = `/this/is/a-base-path/ok`; | ||
expect(stripSpaceUrlContext(basePath)).toEqual(basePath); | ||
}); | ||
|
||
test('it accepts no parameters', () => { | ||
expect(stripSpaceUrlContext()).toEqual(''); | ||
}); | ||
|
||
test('it remove the trailing slash', () => { | ||
expect(stripSpaceUrlContext('/')).toEqual(''); | ||
}); | ||
|
||
test('it identifies the space url context', () => { | ||
const basePath = `/this/is/a/crazy/path/s/my-awesome-space-lives-here`; | ||
expect(getSpaceUrlContext(basePath)).toEqual('my-awesome-space-lives-here'); | ||
}); | ||
|
||
test('it handles base url without a space url context', () => { | ||
const basePath = `/this/is/a/crazy/path/s`; | ||
expect(getSpaceUrlContext(basePath)).toEqual(null); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export class SpacesManager { | ||
constructor(httpAgent, chrome) { | ||
this._httpAgent = httpAgent; | ||
this._baseUrl = chrome.addBasePath(`/api/spaces/v1`); | ||
} | ||
|
||
async getSpaces() { | ||
return await this._httpAgent | ||
.get(`${this._baseUrl}/spaces`) | ||
.then(response => response.data); | ||
} | ||
|
||
async getSpace(id) { | ||
return await this._httpAgent | ||
.get(`${this._baseUrl}/space/${id}`); | ||
} | ||
|
||
async createSpace(space) { | ||
return await this._httpAgent | ||
.post(`${this._baseUrl}/space`, space); | ||
} | ||
|
||
async updateSpace(space) { | ||
return await this._httpAgent | ||
.put(`${this._baseUrl}/space/${space.id}?overwrite=true`, space); | ||
} | ||
|
||
async deleteSpace(space) { | ||
return await this._httpAgent | ||
.delete(`${this._baseUrl}/space/${space.id}`); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
x-pack/plugins/spaces/public/views/components/space_card.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { | ||
EuiCard, | ||
EuiText | ||
} from '@elastic/eui'; | ||
import './space_card.less'; | ||
|
||
export const SpaceCard = (props) => { | ||
const { | ||
space, | ||
onClick | ||
} = props; | ||
|
||
return ( | ||
<EuiCard | ||
className="spaceCard" | ||
title={renderSpaceTitle(space)} | ||
description={renderSpaceDescription(space)} | ||
onClick={onClick} | ||
/> | ||
); | ||
}; | ||
|
||
function renderSpaceTitle(space) { | ||
return ( | ||
<div className="spaceCardTitle"> | ||
<EuiText><h3>{space.name}</h3></EuiText> | ||
</div> | ||
); | ||
} | ||
|
||
function renderSpaceDescription(space) { | ||
return space.description; | ||
} |
9 changes: 9 additions & 0 deletions
9
x-pack/plugins/spaces/public/views/components/space_card.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.spaceCard, .euiCard.euiCard--isClickable.spaceCard { | ||
width: 310px; | ||
height: 230px; | ||
min-height: 200px; | ||
} | ||
|
||
.spaceCardDescription { | ||
margin-bottom: 10px; | ||
} |
32 changes: 32 additions & 0 deletions
32
x-pack/plugins/spaces/public/views/components/space_card.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { shallow, mount } from 'enzyme'; | ||
import { SpaceCard } from './space_card'; | ||
|
||
test('it renders without crashing', () => { | ||
const space = { | ||
name: 'space name', | ||
description: 'space description' | ||
}; | ||
|
||
shallow(<SpaceCard space={space} />); | ||
}); | ||
|
||
test('it is clickable', () => { | ||
const space = { | ||
name: 'space name', | ||
description: 'space description' | ||
}; | ||
|
||
const clickHandler = jest.fn(); | ||
|
||
const wrapper = mount(<SpaceCard space={space} onClick={clickHandler} />); | ||
wrapper.simulate('click'); | ||
|
||
expect(clickHandler).toHaveBeenCalledTimes(1); | ||
}); |
58 changes: 58 additions & 0 deletions
58
x-pack/plugins/spaces/public/views/components/space_cards.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { Component, Fragment } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import chrome from 'ui/chrome'; | ||
import { SpaceCard } from './space_card'; | ||
import { stripSpaceUrlContext } from '../../../common/spaces_url_parser'; | ||
import { | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
EuiSpacer, | ||
} from '@elastic/eui'; | ||
import { chunk } from 'lodash'; | ||
|
||
export class SpaceCards extends Component { | ||
render() { | ||
const maxSpacesPerRow = 3; | ||
const rows = chunk(this.props.spaces, maxSpacesPerRow); | ||
|
||
return ( | ||
<Fragment> | ||
{ | ||
rows.map((row, idx) => ( | ||
<Fragment key={idx}> | ||
<EuiFlexGroup gutterSize="l" justifyContent="spaceEvenly"> | ||
{row.map(this.renderSpace)} | ||
</EuiFlexGroup> | ||
<EuiSpacer /> | ||
</Fragment> | ||
)) | ||
} | ||
</Fragment> | ||
|
||
); | ||
} | ||
|
||
renderSpace = (space) => ( | ||
<EuiFlexItem key={space.id} grow={false}> | ||
<SpaceCard space={space} onClick={this.createSpaceClickHandler(space)} /> | ||
</EuiFlexItem> | ||
); | ||
|
||
createSpaceClickHandler = (space) => { | ||
return () => { | ||
const baseUrlWithoutSpace = stripSpaceUrlContext(chrome.getBasePath()); | ||
|
||
window.location = `${baseUrlWithoutSpace}/s/${space.urlContext}`; | ||
}; | ||
}; | ||
} | ||
|
||
SpaceCards.propTypes = { | ||
spaces: PropTypes.array.isRequired, | ||
}; |
46 changes: 46 additions & 0 deletions
46
x-pack/plugins/spaces/public/views/components/space_cards.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { shallow, mount } from 'enzyme'; | ||
import { SpaceCards } from './space_cards'; | ||
import { EuiFlexGroup } from '@elastic/eui'; | ||
import { SpaceCard } from './space_card'; | ||
|
||
test('it renders without crashing', () => { | ||
const space = { | ||
id: 'space-id', | ||
name: 'space name', | ||
description: 'space description' | ||
}; | ||
|
||
shallow(<SpaceCards spaces={[space]} />); | ||
}); | ||
|
||
test('it renders spaces in groups of 3', () => { | ||
function buildSpace(name) { | ||
return { | ||
id: `id-${name}`, | ||
name, | ||
description: `desc-${name}` | ||
}; | ||
} | ||
|
||
const spaces = [ | ||
buildSpace(1), | ||
buildSpace(2), | ||
buildSpace(3), | ||
buildSpace(4) | ||
]; | ||
|
||
const wrapper = mount(<SpaceCards spaces={spaces} />); | ||
|
||
const groups = wrapper.find(EuiFlexGroup); | ||
expect(groups).toHaveLength(2); | ||
|
||
expect(groups.at(0).find(SpaceCard)).toHaveLength(3); | ||
expect(groups.at(1).find(SpaceCard)).toHaveLength(1); | ||
}); |
Oops, something went wrong.