From 88f32708c3801c5be413641be092db6fcbe5b013 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 22 Mar 2017 04:09:09 +0100 Subject: [PATCH] Web UI support for the new omnisearch --- .../javascripts/components/actions/search.jsx | 8 ++- .../components/reducers/search.jsx | 58 +++++++++++-------- .../components/reducers/statuses.jsx | 2 + app/views/api/v1/search/index.rabl | 4 +- 4 files changed, 43 insertions(+), 29 deletions(-) diff --git a/app/assets/javascripts/components/actions/search.jsx b/app/assets/javascripts/components/actions/search.jsx index ceb0e4a0c..e4af716ee 100644 --- a/app/assets/javascripts/components/actions/search.jsx +++ b/app/assets/javascripts/components/actions/search.jsx @@ -18,11 +18,13 @@ export function clearSearchSuggestions() { }; }; -export function readySearchSuggestions(value, accounts) { +export function readySearchSuggestions(value, { accounts, hashtags, statuses }) { return { type: SEARCH_SUGGESTIONS_READY, value, - accounts + accounts, + hashtags, + statuses }; }; @@ -32,7 +34,7 @@ export function fetchSearchSuggestions(value) { return; } - api(getState).get('/api/v1/accounts/search', { + api(getState).get('/api/v1/search', { params: { q: value, resolve: true, diff --git a/app/assets/javascripts/components/reducers/search.jsx b/app/assets/javascripts/components/reducers/search.jsx index d835ef268..1767be2c6 100644 --- a/app/assets/javascripts/components/reducers/search.jsx +++ b/app/assets/javascripts/components/reducers/search.jsx @@ -11,28 +11,38 @@ const initialState = Immutable.Map({ suggestions: [] }); -const normalizeSuggestions = (state, value, accounts) => { - let newSuggestions = [ - { +const normalizeSuggestions = (state, value, accounts, hashtags, statuses) => { + let newSuggestions = []; + + if (accounts.length > 0) { + newSuggestions.push({ title: 'account', items: accounts.map(item => ({ type: 'account', id: item.id, value: item.acct })) + }); + } + + if (value.indexOf('@') === -1 && value.indexOf(' ') === -1 || hashtags.length > 0) { + let hashtagItems = hashtags.map(item => ({ + type: 'hashtag', + id: item, + value: `#${item}` + })); + + if (value.indexOf('@') === -1 && value.indexOf(' ') === -1 && hashtags.indexOf(value) === -1) { + hashtagItems.unshift({ + type: 'hashtag', + id: value, + value: `#${value}` + }); } - ]; - if (value.indexOf('@') === -1 && value.indexOf(' ') === -1) { newSuggestions.push({ title: 'hashtag', - items: [ - { - type: 'hashtag', - id: value, - value: `#${value}` - } - ] + items: hashtagItems }); } @@ -44,17 +54,17 @@ const normalizeSuggestions = (state, value, accounts) => { export default function search(state = initialState, action) { switch(action.type) { - case SEARCH_CHANGE: - return state.set('value', action.value); - case SEARCH_SUGGESTIONS_READY: - return normalizeSuggestions(state, action.value, action.accounts); - case SEARCH_RESET: - return state.withMutations(map => { - map.set('suggestions', []); - map.set('value', ''); - map.set('loaded_value', ''); - }); - default: - return state; + case SEARCH_CHANGE: + return state.set('value', action.value); + case SEARCH_SUGGESTIONS_READY: + return normalizeSuggestions(state, action.value, action.accounts, action.hashtags, action.statuses); + case SEARCH_RESET: + return state.withMutations(map => { + map.set('suggestions', []); + map.set('value', ''); + map.set('loaded_value', ''); + }); + default: + return state; } }; diff --git a/app/assets/javascripts/components/reducers/statuses.jsx b/app/assets/javascripts/components/reducers/statuses.jsx index ce791eab6..1669b8c65 100644 --- a/app/assets/javascripts/components/reducers/statuses.jsx +++ b/app/assets/javascripts/components/reducers/statuses.jsx @@ -32,6 +32,7 @@ import { FAVOURITED_STATUSES_FETCH_SUCCESS, FAVOURITED_STATUSES_EXPAND_SUCCESS } from '../actions/favourites'; +import { SEARCH_SUGGESTIONS_READY } from '../actions/search'; import Immutable from 'immutable'; const normalizeStatus = (state, status) => { @@ -108,6 +109,7 @@ export default function statuses(state = initialState, action) { case NOTIFICATIONS_EXPAND_SUCCESS: case FAVOURITED_STATUSES_FETCH_SUCCESS: case FAVOURITED_STATUSES_EXPAND_SUCCESS: + case SEARCH_SUGGESTIONS_READY: return normalizeStatuses(state, action.statuses); case TIMELINE_DELETE: return deleteStatus(state, action.id, action.references); diff --git a/app/views/api/v1/search/index.rabl b/app/views/api/v1/search/index.rabl index d10ac9e0f..8d1640f2d 100644 --- a/app/views/api/v1/search/index.rabl +++ b/app/views/api/v1/search/index.rabl @@ -1,6 +1,6 @@ object @search -child accounts: :accounts do +child :accounts, object_root: false do extends 'api/v1/accounts/show' end @@ -8,6 +8,6 @@ node(:hashtags) do |search| search.hashtags.map(&:name) end -child statuses: :statuses do +child :statuses, object_root: false do extends 'api/v1/statuses/show' end