Merge pull request 'client: improve search' (#244) from search into main

Reviewed-on: FoundKeyGang/FoundKey#244
This commit is contained in:
Norm 2022-11-20 03:54:30 +00:00
commit 4952e29ac8
10 changed files with 209 additions and 134 deletions

View file

@ -167,7 +167,6 @@ general: "General"
wallpaper: "Wallpaper"
setWallpaper: "Set wallpaper"
removeWallpaper: "Remove wallpaper"
searchWith: "Search: {q}"
youHaveNoLists: "You don't have any lists"
followConfirm: "Are you sure that you want to follow {name}?"
proxyAccount: "Proxy account"

View file

@ -50,6 +50,8 @@ export default define(meta, paramDef, async (ps, me) => {
if (ps.userId) {
query.andWhere('note.userId = :userId', { userId: ps.userId });
} else if (ps.host) {
query.andWhere('note.userHost = :host', { host: ps.host });
} else if (ps.channelId) {
query.andWhere('note.channelId = :channelId', { channelId: ps.channelId });
}

View file

@ -30,8 +30,8 @@ import * as sound from '@/scripts/sound';
import { $i, refreshAccount, login, updateAccount, signout } from '@/account';
import { defaultStore, ColdDeviceStorage } from '@/store';
import { fetchInstance, instance } from '@/instance';
import { mainRouter } from '@/router';
import { makeHotkey } from '@/scripts/hotkey';
import { search } from '@/scripts/search';
import { deviceKind } from '@/scripts/device-kind';
import { initializeSw } from '@/scripts/initialize-sw';
import { reloadChannel } from '@/scripts/unison-reload';
@ -332,7 +332,9 @@ import { getAccountFromId } from '@/scripts/get-account-from-id';
'd': (): void => {
defaultStore.set('darkMode', !defaultStore.state.darkMode);
},
's': search,
's': (): void => {
mainRouter.push('/search');
},
};
if ($i) {

View file

@ -1,7 +1,6 @@
import { computed, ref, reactive } from 'vue';
import { $i } from '@/account';
import { mainRouter } from '@/router';
import { search } from '@/scripts/search';
import * as os from '@/os';
import { i18n } from '@/i18n';
import { ui } from '@/config';
@ -49,7 +48,7 @@ export const menuDef = reactive({
search: {
title: 'search',
icon: 'fas fa-search',
action: () => search(),
to: '/search',
},
lists: {
title: 'lists',

View file

@ -11,57 +11,19 @@
<div v-else-if="tab === 'remoteUsers'">
<XUsers origin="remote"/>
</div>
<div v-else-if="tab === 'search'">
<div class="_isolated">
<MkInput v-model="searchQuery" :debounce="true" type="search">
<template #prefix><i class="fas fa-search"></i></template>
<template #label>{{ i18n.ts.searchUser }}</template>
</MkInput>
<MkRadios v-model="searchOrigin">
<option value="combined">{{ i18n.ts.all }}</option>
<option value="local">{{ i18n.ts.local }}</option>
<option value="remote">{{ i18n.ts.remote }}</option>
</MkRadios>
</div>
<XUserList v-if="searchQuery" ref="searchEl" class="_gap" :pagination="searchPagination"/>
</div>
</div>
</MkStickyContainer>
</template>
<script lang="ts" setup>
import { computed, watch } from 'vue';
import { computed } from 'vue';
import XFeatured from './explore.featured.vue';
import XUsers from './explore.users.vue';
import MkFolder from '@/components/ui/folder.vue';
import MkInput from '@/components/form/input.vue';
import MkRadios from '@/components/form/radios.vue';
import { definePageMetadata } from '@/scripts/page-metadata';
import { i18n } from '@/i18n';
import XUserList from '@/components/user-list.vue';
const props = defineProps<{
tag?: string;
}>();
let tab = $ref('featured');
let tagsEl = $ref<InstanceType<typeof MkFolder>>();
let searchQuery = $ref(null);
let searchOrigin = $ref('combined');
watch(() => props.tag, () => {
if (tagsEl) tagsEl.toggleContent(props.tag == null);
});
const searchPagination = {
endpoint: 'users/search' as const,
limit: 10,
params: computed(() => searchQuery ? {
query: searchQuery,
origin: searchOrigin,
} : null),
};
const headerTabs = $computed(() => [{
key: 'featured',
@ -75,9 +37,6 @@ const headerTabs = $computed(() => [{
key: 'remoteUsers',
icon: 'fas fa-users',
title: i18n.ts.remote,
}, {
key: 'search',
title: i18n.ts.search,
}]);
definePageMetadata(computed(() => ({

View file

@ -1,34 +1,215 @@
<template>
<MkStickyContainer>
<template #header><MkPageHeader/></template>
<template #header><MkPageHeader v-model:tab="tab" :tabs="headerTabs"/></template>
<MkSpacer :content-max="800">
<XNotes ref="notes" :pagination="pagination"/>
<MkFolder>
<template #header>{{ i18n.ts.search }}</template>
<MkInput v-model="query" class="input" tabindex="1" @keydown="keydown">
<template #prefix><i class="fas fa-magnifying-glass"></i></template>
<template v-if="tab === 'users'" #label>{{ i18n.ts.username }}</template>
<template v-if="tab === 'all'" #caption>Try entering a URL or user handle!</template>
</MkInput>
<template v-if="tab === 'notes'">
<MkSelect v-model="author" class="input">
<template #label>{{ i18n.ts.author }}</template>
<option value="all">{{ i18n.ts.all }}</option>
<option value="self">{{ i18n.ts.you }}</option>
<option value="user">{{ i18n.ts.user }}</option>
<option value="local">{{ i18n.ts.local }}</option>
<option value="host">{{ i18n.ts.instance }}</option>
</MkSelect>
<MkButton v-if="author === 'user'" @click="selectUser" full class="input">
<template v-if="user == null">{{ i18n.ts.selectUser }}</template>
<template v-else>
<MkAvatar :user="user" class="avatar"/>
<MkAcct :user="user"/>
</template>
</MkButton>
<MkInput v-if="author === 'host'" v-model="host" class="input">
<template #prefix>@</template>
<template #label>{{ i18n.ts.host }}</template>
</MkInput>
</template>
<template v-if="tab === 'users'">
<MkSelect v-model="origin" class="input">
<template #label>{{ i18n.ts.instance }}</template>
<option value="combined">{{ i18n.ts.all }}</option>
<option value="local">{{ i18n.ts.local }}</option>
<option value="remote">{{ i18n.ts.remote }}</option>
</MkSelect>
</template>
<MkButton @click="search()" primary :disabled="!canSearch" class="input">
<i class="fas fa-magnifying-glass"></i> {{ i18n.ts.search }}
</MkButton>
</MkFolder>
<MkFolder v-if="tab === 'all' || tab === 'users'">
<template #header>{{ i18n.ts.users }}</template>
<XUserList v-if="userPagination" :pagination="userPagination"/>
</MkFolder>
<MkFolder v-if="tab === 'all' || tab === 'notes'">
<template #header>{{ i18n.ts.notes }}</template>
<XNotes v-if="notePagination" :pagination="notePagination"/>
</MkFolder>
</MkSpacer>
</MkStickyContainer>
</template>
<script lang="ts" setup>
import { computed } from 'vue';
import { computed, watch } from 'vue';
import XNotes from '@/components/notes.vue';
import XUserList from '@/components/user-list.vue';
import MkButton from '@/components/ui/button.vue';
import MkInput from '@/components/form/input.vue';
import MkSelect from '@/components/form/select.vue';
import MkFolder from '@/components/ui/folder.vue';
import { $i } from '@/account';
import { i18n } from '@/i18n';
import * as os from '@/os';
import { mainRouter } from '@/router';
import { definePageMetadata } from '@/scripts/page-metadata';
const props = defineProps<{
query: string;
channel?: string;
}>();
const headerTabs = [{
key: 'all',
title: i18n.ts.all,
}, {
key: 'notes',
title: i18n.ts.notes,
}, {
key: 'users',
title: i18n.ts.users,
}];
const pagination = {
let tab: 'all' | 'notes' | 'users' = $ref('all');
let query: string = $ref('');
let author: 'all' | 'self' | 'user' | 'local' | 'host' = $ref('all');
let user = $ref(null);
let host: string = $ref('');
let origin: 'combined' | 'local' | 'remote' = $ref('combined');
let notePagination = $ref(null);
let userPagination = $ref(null);
watch($$(tab), () => {
userPagination = null;
notePagination = null;
});
let canSearch = $computed(() =>
query !== ''
&&
(
tab !== 'notes'
||
(
author === 'all'
|| author === 'self'
|| author === 'local'
|| author === 'host' && host !== ''
|| author === 'user' && user != null
)
)
);
function selectUser() {
os.selectUser().then(selectedUser => user = selectedUser);
}
async function search(): void {
switch (tab) {
case 'all': {
query = query.trim();
// process special query strings
if (query.startsWith('@') && !query.includes(' ')) {
mainRouter.push('/' + q);
} else if (query.startsWith('#')) {
mainRouter.push('/tags/' + encodeURIComponent(query.slice(1)));
} else if (query.startsWith('https://')) {
const promise = os.api('ap/show', {
uri: query,
});
os.promiseDialog(promise, null, null, i18n.ts.fetchingAsApObject);
const res = await promise;
if (res.type === 'User') {
mainRouter.push(`/@${res.object.username}@${res.object.host}`);
} else if (res.type === 'Note') {
mainRouter.push(`/notes/${res.object.id}`);
}
} else {
notePagination = {
endpoint: 'notes/search' as const,
limit: 10,
params: computed(() => ({
query: props.query,
channelId: props.channel,
})),
};
params: { query },
};
origin = 'combined';
userPagination = {
endpoint: 'users/search' as const,
limit: 4,
params: {
query,
origin,
},
};
}
break;
}
case 'notes': {
const params = { query };
switch (author) {
case 'self':
params.userId = $i.id;
break;
case 'user':
params.userId = user.id;
break;
case 'local':
params.host = null;
break;
case 'host':
params.host = host;
break;
}
notePagination = {
endpoint: 'notes/search' as const,
limit: 10,
params,
};
userPagination = null;
break;
}
case 'users': {
notePagination = null;
userPagination = {
endpoint: 'users/search' as const,
limit: 10,
params: {
query,
origin,
},
};
break;
}
}
}
function keydown(evt: KeyboardEvent): void {
if (evt.key === 'Enter' && canSearch) {
evt.preventDefault();
evt.stopPropagation();
search();
}
}
definePageMetadata(computed(() => ({
title: i18n.t('searchWith', { q: props.query }),
title: i18n.ts.search,
icon: 'fas fa-search',
})));
</script>
<style lang="scss" scoped>
.input {
margin-top: 16px;
}
</style>

View file

@ -73,10 +73,6 @@ export const routes = [{
}, {
path: '/search',
component: page(() => import('./pages/search.vue')),
query: {
q: 'query',
channel: 'channel',
},
}, {
path: '/authorize-follow',
component: page(() => import('./pages/follow.vue')),

View file

@ -1,63 +0,0 @@
import * as os from '@/os';
import { i18n } from '@/i18n';
import { mainRouter } from '@/router';
export async function search() {
const { canceled, result: query } = await os.inputText({
title: i18n.ts.search,
});
if (canceled || query == null || query === '') return;
const q = query.trim();
if (q.startsWith('@') && !q.includes(' ')) {
mainRouter.push(`/${q}`);
return;
}
if (q.startsWith('#')) {
mainRouter.push(`/tags/${encodeURIComponent(q.substr(1))}`);
return;
}
// like 2018/03/12
if (/^[0-9]{4}\/[0-9]{2}\/[0-9]{2}/.test(q.replace(/-/g, '/'))) {
const date = new Date(q.replace(/-/g, '/'));
// 日付しか指定されてない場合、例えば 2018/03/12 ならユーザーは
// 2018/03/12 のコンテンツを「含む」結果になることを期待するはずなので
// 23時間59分進める(そのままだと 2018/03/12 00:00:00 「まで」の
// 結果になってしまい、2018/03/12 のコンテンツは含まれない)
if (q.replace(/-/g, '/').match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{2}$/)) {
date.setHours(23, 59, 59, 999);
}
// TODO
//v.$root.$emit('warp', date);
os.alert({
icon: 'fas fa-history',
iconOnly: true, autoClose: true,
});
return;
}
if (q.startsWith('https://')) {
const promise = os.api('ap/show', {
uri: q,
});
os.promiseDialog(promise, null, null, i18n.ts.fetchingAsApObject);
const res = await promise;
if (res.type === 'User') {
mainRouter.push(`/@${res.object.username}@${res.object.host}`);
} else if (res.type === 'Note') {
mainRouter.push(`/notes/${res.object.id}`);
}
return;
}
mainRouter.push(`/search?q=${encodeURIComponent(q)}`);
}

View file

@ -50,7 +50,6 @@ import { ComputedRef, onMounted, provide } from 'vue';
import XHeader from './header.vue';
import XKanban from './kanban.vue';
import { host, instanceName } from '@/config';
import { search } from '@/scripts/search';
import * as os from '@/os';
import MkPagination from '@/components/ui/pagination.vue';
import XSigninDialog from '@/components/signin-dialog.vue';
@ -87,7 +86,9 @@ const keymap = $computed(() => {
if (ColdDeviceStorage.get('syncDeviceDarkMode')) return;
defaultStore.set('darkMode', !defaultStore.state.darkMode);
},
's': search,
's': () => {
mainRouter.push('/search');
},
};
});

View file

@ -18,7 +18,7 @@
</button>
</div>
<div class="right">
<button class="_button search" @click="search()"><i class="fas fa-search icon"></i><span>{{ i18n.ts.search }}</span></button>
<MkA to="/search" class="link search" active-class="active"><i class="fas fa-search icon"></i><span>{{ i18n.ts.search }}</span></MkA>
<button class="_buttonPrimary signup" @click="signup()">{{ i18n.ts.signup }}</button>
<button class="_button login" @click="signin()">{{ i18n.ts.login }}</button>
</div>
@ -46,7 +46,6 @@ import { onMounted, ref, Ref } from 'vue';
import XSigninDialog from '@/components/signin-dialog.vue';
import XSignupDialog from '@/components/signup-dialog.vue';
import * as os from '@/os';
import { search } from '@/scripts/search';
import { i18n } from '@/i18n';
defineProps<{