2018-08-27 15:27:09 +00:00
|
|
|
import * as React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { Dispatch } from 'redux';
|
|
|
|
|
2018-08-29 00:17:08 +00:00
|
|
|
import { Button, MenuItem } from '@blueprintjs/core';
|
|
|
|
import { IconNames } from '@blueprintjs/icons';
|
2018-08-29 01:52:21 +00:00
|
|
|
import { IItemRendererProps, ItemPredicate, Select } from '@blueprintjs/select';
|
2018-08-27 15:27:09 +00:00
|
|
|
|
2018-09-01 17:24:05 +00:00
|
|
|
import { selectAndLoadInstance } from '../redux/actions';
|
2018-08-27 15:27:09 +00:00
|
|
|
import { IAppState, IInstance } from '../redux/types';
|
|
|
|
|
|
|
|
interface IInstanceSearchProps {
|
2018-08-29 01:52:21 +00:00
|
|
|
currentInstanceName: string | null;
|
2018-08-27 15:27:09 +00:00
|
|
|
instances?: IInstance[];
|
2018-09-01 17:24:05 +00:00
|
|
|
selectAndLoadInstance: (instanceName: string) => void;
|
2018-08-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 00:17:08 +00:00
|
|
|
const InstanceSelect = Select.ofType<IInstance>();
|
|
|
|
|
2018-08-27 15:27:09 +00:00
|
|
|
class InstanceSearchImpl extends React.Component<IInstanceSearchProps> {
|
2018-08-27 21:31:27 +00:00
|
|
|
|
2018-08-27 15:27:09 +00:00
|
|
|
public render() {
|
|
|
|
return (
|
2018-08-29 00:17:08 +00:00
|
|
|
<InstanceSelect
|
2018-09-01 17:54:13 +00:00
|
|
|
items={this.props.instances || []}
|
2018-08-27 15:27:09 +00:00
|
|
|
itemRenderer={this.itemRenderer}
|
|
|
|
onItemSelect={this.onItemSelect}
|
2018-08-29 00:17:08 +00:00
|
|
|
itemPredicate={this.itemPredicate}
|
|
|
|
disabled={!this.props.instances}
|
2018-08-29 01:52:21 +00:00
|
|
|
initialContent={this.renderInitialContent()}
|
|
|
|
noResults={this.renderNoResults()}
|
|
|
|
popoverProps={{popoverClassName: "fediverse-instance-search-popover"}}
|
2018-08-29 00:17:08 +00:00
|
|
|
>
|
|
|
|
<Button
|
|
|
|
icon={IconNames.SELECTION}
|
|
|
|
rightIcon={IconNames.CARET_DOWN}
|
2018-08-29 01:52:21 +00:00
|
|
|
text={this.props.currentInstanceName || ("Select an instance")}
|
2018-08-29 00:17:08 +00:00
|
|
|
disabled={!this.props.instances}
|
|
|
|
/>
|
|
|
|
</InstanceSelect>
|
|
|
|
);
|
2018-08-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 01:52:21 +00:00
|
|
|
private renderInitialContent = () => {
|
|
|
|
return (
|
|
|
|
<MenuItem disabled={true} text={"Start typing"} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private renderNoResults = () => {
|
|
|
|
return (
|
|
|
|
<MenuItem disabled={true} text={"Keep typing"} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private itemRenderer = (item: IInstance, itemProps: IItemRendererProps) => {
|
|
|
|
if (!itemProps.modifiers.matchesPredicate) {
|
2018-08-29 00:17:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<MenuItem
|
|
|
|
text={item.name}
|
|
|
|
key={item.name}
|
2018-08-29 01:52:21 +00:00
|
|
|
active={itemProps.modifiers.active}
|
|
|
|
onClick={itemProps.handleClick}
|
2018-08-29 00:17:08 +00:00
|
|
|
/>
|
|
|
|
);
|
2018-08-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 00:17:08 +00:00
|
|
|
private itemPredicate: ItemPredicate<IInstance> = (query, item, index) => {
|
2018-08-29 01:52:21 +00:00
|
|
|
if (!item.name || query.length < 4) {
|
2018-08-29 00:17:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return item.name.toLowerCase().indexOf(query.toLowerCase()) >= 0;
|
2018-08-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 21:31:27 +00:00
|
|
|
private onItemSelect = (item: IInstance, event?: React.SyntheticEvent<HTMLElement>) => {
|
2018-09-01 17:24:05 +00:00
|
|
|
this.props.selectAndLoadInstance(item.name);
|
2018-08-27 15:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state: IAppState) => ({
|
2018-09-01 17:24:05 +00:00
|
|
|
currentInstanceName: state.currentInstance.currentInstanceName,
|
2018-08-27 15:27:09 +00:00
|
|
|
instances: state.data.instances,
|
|
|
|
})
|
|
|
|
const mapDispatchToProps = (dispatch: Dispatch) => ({
|
2018-09-01 17:24:05 +00:00
|
|
|
selectAndLoadInstance: (instanceName: string) => dispatch(selectAndLoadInstance(instanceName) as any),
|
2018-08-27 15:27:09 +00:00
|
|
|
})
|
|
|
|
export const InstanceSearch = connect(mapStateToProps, mapDispatchToProps)(InstanceSearchImpl)
|