Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(search): orderby search results #2794

Merged
merged 3 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions components/views/chat/search/SearchUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ describe('SearchUtil.default.getSearchResultGroupList', () => {
})
})

describe('SearchUtil.default.getSearchOrderTypeList', () => {
test('0', () => {
const result: any = SearchUtil.default.getSearchOrderTypeList()
expect(result).toMatchSnapshot()
})
})

describe('SearchUtil.default.getCommandMeta', () => {
test('0', () => {
const result: any = SearchUtil.default.getCommandMeta(
Expand Down
18 changes: 0 additions & 18 deletions components/views/chat/search/SearchUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
SearchCommandType,
SearchCommandTypeParam,
SearchOption,
SearchOrderType,
SearchQueryItem,
SearchRecommend,
SearchRecommendResultItem,
Expand Down Expand Up @@ -98,22 +97,6 @@ export const searchCommandMetaList = [
},
] as SearchCommandMeta[]

const searchOrderTypeList = [
{
type: SearchOrderType.New,
title: 'New',
},

{
type: SearchOrderType.Old,
title: 'Old',
},
{
type: SearchOrderType.Relevant,
title: 'Relevant',
},
]

const searchResultGroupList = [
{
type: SearchResultGroupType.Messages,
Expand Down Expand Up @@ -142,7 +125,6 @@ const SearchUtil = {
getCommandTypeParams: () => searchCommandTypeParams,
getCommandMetaList: () => searchCommandMetaList,
getSearchResultGroupList: () => searchResultGroupList,
getSearchOrderTypeList: () => searchOrderTypeList,
/**
* @method getCommandMeta DocsTODO
* @description
Expand Down
17 changes: 0 additions & 17 deletions components/views/chat/search/__snapshots__/SearchUtil.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,6 @@ Object {
}
`;

exports[`SearchUtil.default.getSearchOrderTypeList 0 1`] = `
Array [
Object {
"title": "New",
"type": "new",
},
Object {
"title": "Old",
"type": "old",
},
Object {
"title": "Relevant",
"type": "relevant",
},
]
`;

exports[`SearchUtil.default.getSearchResultGroupList 0 1`] = `
Array [
Object {
Expand Down
9 changes: 4 additions & 5 deletions components/views/chat/search/result/Result.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<InteractablesClose class="close" :action="toggle" />
</div>
<!-- <SearchResultGroup
:groupList="groupList"
:groupBy="groupBy"
@toggleGroup="toggleGroupBy"
/> -->
:groupList="groupList"
:groupBy="groupBy"
@toggleGroup="toggleGroupBy"
/> -->
<!-- <div class="search-result-filters">
<div class="container-filter user-options">
<multiselect
Expand Down Expand Up @@ -65,7 +65,6 @@
</div> -->
<SearchResultMeta
:result="result"
:items="orderTypeList"
:orderBy="orderBy"
:page="page"
:perPage="queryOptions.perPage"
Expand Down
24 changes: 10 additions & 14 deletions components/views/chat/search/result/Result.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export default Vue.extend({
data() {
return {
groupList: SearchUtil.getSearchResultGroupList(),
orderTypeList: SearchUtil.getSearchOrderTypeList(),
query: '' as string,
groupBy: SearchResultGroupType.Messages as SearchResultGroupType,
orderBy: SearchOrderType.New as SearchOrderType,
Expand Down Expand Up @@ -76,19 +75,19 @@ export default Vue.extend({
},
watch: {
date: {
handler(newDateValue) {
handler(newValue) {
this.queryOptions = {
...this.queryOptions,
dateRange: {
start: newDateValue,
end: newDateValue,
start: newValue,
end: newValue,
},
}
},
},
},
mounted() {
this.fetchResult(this.searchQuery)
this.fetchResult()
},
methods: {
/**
Expand All @@ -113,33 +112,30 @@ export default Vue.extend({
*/
toggleOrderBy(state: SearchOrderType) {
this.orderBy = state
this.fetchResult()
},
/**
* @method fetchResult DocsTODO
* @description
* @param query
*/
async fetchResult(query: string): Promise<void> {
async fetchResult(): Promise<void> {
this.isLoading = DataStateType.Loading
this.queryOptions = {
...this.queryOptions,
accounts: [...this.friends.all, this.accounts.details],
queryString: query,
queryString: this.searchQuery,
}
this.result = await this.$store.dispatch('textile/searchConversations', {
query: this.queryOptions,
page: this.page,
orderBy: this.orderBy,
})
this.isLoading = DataStateType.Ready
},
async handleClickPaginate(pageNum: number) {
this.isLoading = DataStateType.Loading
handleClickPaginate(pageNum: number) {
this.page = pageNum
this.result = await this.$store.dispatch('textile/searchConversations', {
query: this.queryOptions,
page: this.page,
})
this.isLoading = DataStateType.Ready
this.fetchResult()
},
onChange(value: any) {
this.queryOptions = {
Expand Down
14 changes: 8 additions & 6 deletions components/views/chat/search/result/meta/Meta.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<div class="meta">
<div class="result-count">{{ resultsText }}</div>
<!-- <div class="result-orderby">
<div class="result-orderby">
<div
v-for="item in items"
:class="{'orderby-item': true, 'active': item.type === orderBy}"
@click="$emit('toggleOrder',item.type)"
v-if="result.totalRows"
v-for="item in Object.keys(SearchOrderType)"
class="orderby-item"
:class="{ 'active': SearchOrderType[item] === orderBy }"
@click="$emit('toggleOrder', SearchOrderType[item])"
>
{{item.title}}
{{item}}
</div>
</div> -->
</div>
</div>
5 changes: 3 additions & 2 deletions components/views/chat/search/result/meta/Meta.less
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
}
.result-orderby {
display: flex;
gap: @normal-spacing;
.orderby-item {
&:extend(.font-muted);
cursor: pointer;
padding: 2px 8px;
&.active {
font-weight: bold;
&:extend(.font-secondary);
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions components/views/chat/search/result/meta/Meta.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { SearchOrderType, UISearchResult } from '~/types/search/search'

export default Vue.extend({
props: {
items: {
type: Array,
required: true,
},
result: {
type: Object as PropType<UISearchResult>,
required: true,
Expand All @@ -28,6 +24,7 @@ export default Vue.extend({
},
},
computed: {
SearchOrderType: () => SearchOrderType,
resultsText(): TranslateResult {
// if 0 results
if (this.result.totalRows === 0) {
Expand Down
6 changes: 5 additions & 1 deletion libraries/SatelliteDB/SearchIndex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import MiniSearch, { Options } from 'minisearch'
import type { SearchOptions } from 'minisearch'
import { UISearchResultData } from '~/types/search/search'

export type SearchIndexOptions = {
schema: Options<any>
Expand Down Expand Up @@ -74,7 +75,10 @@ export default class SearchIndex {
this.addAll(records)
}

search(query: string, options?: SearchOptions | undefined) {
search(
query: string,
options?: SearchOptions | undefined,
): UISearchResultData[] {
return this.index
.search(query, options)
.map((doc) => ({ ...doc, ...this.getById(doc[this.idField]) }))
Expand Down
22 changes: 20 additions & 2 deletions store/textile/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import { AccountsError } from '~/store/accounts/types'
import GroupChatsProgram from '~/libraries/Solana/GroupChatsProgram/GroupChatsProgram'
import SolanaManager from '~/libraries/Solana/SolanaManager/SolanaManager'
import { Group } from '~/store/groups/types'
import { UISearchResult, QueryOptions } from '~/types/search/search'
import {
UISearchResult,
QueryOptions,
SearchOrderType,
UISearchResultData,
} from '~/types/search/search'

const getGroupChatProgram = (): GroupChatsProgram => {
const $SolanaManager: SolanaManager = Vue.prototype.$SolanaManager
Expand Down Expand Up @@ -920,9 +925,11 @@ export default {
{
query,
page,
orderBy,
}: {
query: QueryOptions
page: number
orderBy: SearchOrderType
},
): Promise<UISearchResult> {
const { queryString, accounts, dateRange, perPage } = query
Expand All @@ -937,7 +944,7 @@ export default {
await db.initializeSearchIndexes()
}

const result = db.search.conversationMessages.search(
const result: UISearchResultData[] = db.search.conversationMessages.search(
`${queryString}${
startDate && endDate
? ` AND at >= ${startDate} AND at <= ${endDate}`
Expand All @@ -953,6 +960,17 @@ export default {
...match,
user: accounts.find((acct) => acct?.textilePubkey === match.from),
}))
if (orderBy === SearchOrderType.New) {
data.sort((a: UISearchResultData, b: UISearchResultData) => b.at - a.at)
}
if (orderBy === SearchOrderType.Old) {
data.sort((a: UISearchResultData, b: UISearchResultData) => a.at - b.at)
}
if (orderBy === SearchOrderType.Relevant) {
data.sort(
(a: UISearchResultData, b: UISearchResultData) => b.score - a.score,
)
}
return { data: data.slice(skip, perPage * page), totalRows: result?.length }
},
}
24 changes: 17 additions & 7 deletions types/search/search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MatchInfo } from 'minisearch'
import { MessageTypes } from '../textile/mailbox'
import { Friend } from '../ui/friends'
import { User } from '../ui/user'

Expand Down Expand Up @@ -132,14 +133,23 @@ export type SearchParam = {
orderby: SearchOrderType
}

export interface UISearchResultData {
at: number
conversation: string
from: string
id: string
match: MatchInfo
payload: 'ay'
readAt: number
score: number
terms: string[]
to: string
type: MessageTypes
user?: User
}

export interface UISearchResult {
data: {
user?: User
id: any
terms: string[]
score: number
match: MatchInfo
}[]
data: UISearchResultData[]
totalRows: number
}

Expand Down