Files
ThreadNet-Web/src/autocomplete/RoomProvider.js
T

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-07-03 22:15:13 +05:30
import React from 'react';
import AutocompleteProvider from './AutocompleteProvider';
import Q from 'q';
import MatrixClientPeg from '../MatrixClientPeg';
import Fuse from 'fuse.js';
2016-07-03 22:15:13 +05:30
import {TextualCompletion} from './Components';
2016-06-21 05:05:23 +05:30
const ROOM_REGEX = /(?=#)([^\s]*)/g;
let instance = null;
export default class RoomProvider extends AutocompleteProvider {
constructor() {
super(ROOM_REGEX, {
2016-07-03 22:15:13 +05:30
keys: ['displayName', 'userId'],
});
2016-06-21 05:05:23 +05:30
this.fuse = new Fuse([], {
2016-07-03 22:15:13 +05:30
keys: ['name', 'roomId', 'aliases'],
2016-06-21 05:05:23 +05:30
});
}
getCompletions(query: string, selection: {start: number, end: number}) {
let client = MatrixClientPeg.get();
let completions = [];
2016-07-03 22:15:13 +05:30
const {command, range} = this.getCurrentCommand(query, selection);
if (command) {
2016-06-21 05:05:23 +05:30
// the only reason we need to do this is because Fuse only matches on properties
this.fuse.set(client.getRooms().filter(room => !!room).map(room => {
return {
name: room.name,
roomId: room.roomId,
2016-07-03 22:15:13 +05:30
aliases: room.getAliases(),
2016-06-21 05:05:23 +05:30
};
}));
completions = this.fuse.search(command[0]).map(room => {
return {
2016-07-03 22:15:13 +05:30
completion: room.roomId,
component: (
<TextualCompletion title={room.name} subtitle={room.roomId} />
),
range,
};
2016-07-03 22:15:13 +05:30
}).slice(0, 4);
}
return Q.when(completions);
}
getName() {
return 'Rooms';
}
static getInstance() {
2016-07-03 22:15:13 +05:30
if (instance == null) {
instance = new RoomProvider();
2016-07-03 22:15:13 +05:30
}
return instance;
}
}