Properly debouncing tribute mentions. Fixes #633 (#639)

This commit is contained in:
Dessalines 2022-04-27 16:57:55 -04:00 committed by GitHub
parent 6b53ae7f62
commit 130bfc9c8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 87 deletions

View file

@ -799,7 +799,7 @@ export class Settings extends Component<any, SettingsState> {
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
}, 400), }),
false false
); );
} }
@ -834,7 +834,7 @@ export class Settings extends Component<any, SettingsState> {
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
}, 400), }),
false false
); );
} }

View file

@ -600,7 +600,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
}, 400), }),
false false
); );
} }

View file

@ -778,7 +778,7 @@ export class Search extends Component<any, SearchState> {
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
}, 400), }),
false false
); );
} }
@ -808,7 +808,7 @@ export class Search extends Component<any, SearchState> {
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
}, 400), }),
false false
); );
} }

View file

@ -20,7 +20,6 @@ import {
PrivateMessageView, PrivateMessageView,
RegistrationApplicationView, RegistrationApplicationView,
Search, Search,
SearchResponse,
SearchType, SearchType,
SortType, SortType,
UserOperation, UserOperation,
@ -585,9 +584,9 @@ export function setupTribute() {
let it: PersonTribute = item.original; let it: PersonTribute = item.original;
return `[${it.key}](${it.view.person.actor_id})`; return `[${it.key}](${it.view.person.actor_id})`;
}, },
values: (text: string, cb: (persons: PersonTribute[]) => any) => { values: debounce(async (text: string, cb: any) => {
personSearch(text, (persons: PersonTribute[]) => cb(persons)); cb(await personSearch(text));
}, }),
allowSpaces: false, allowSpaces: false,
autocompleteMode: true, autocompleteMode: true,
// TODO // TODO
@ -602,11 +601,9 @@ export function setupTribute() {
let it: CommunityTribute = item.original; let it: CommunityTribute = item.original;
return `[${it.key}](${it.view.community.actor_id})`; return `[${it.key}](${it.view.community.actor_id})`;
}, },
values: (text: string, cb: any) => { values: debounce(async (text: string, cb: any) => {
communitySearch(text, (communities: CommunityTribute[]) => cb(await communitySearch(text));
cb(communities) }),
);
},
allowSpaces: false, allowSpaces: false,
autocompleteMode: true, autocompleteMode: true,
// TODO // TODO
@ -638,42 +635,16 @@ interface PersonTribute {
view: PersonViewSafe; view: PersonViewSafe;
} }
function personSearch(text: string, cb: (persons: PersonTribute[]) => any) { async function personSearch(text: string): Promise<PersonTribute[]> {
if (text) { let users = (await fetchUsers(text)).users;
let form: Search = { let persons: PersonTribute[] = users.map(pv => {
q: text, let tribute: PersonTribute = {
type_: SearchType.Users, key: `@${pv.person.name}@${hostname(pv.person.actor_id)}`,
sort: SortType.TopAll, view: pv,
listing_type: ListingType.All,
page: 1,
limit: mentionDropdownFetchLimit,
auth: authField(false),
}; };
return tribute;
WebSocketService.Instance.send(wsClient.search(form)); });
return persons;
let personSub = WebSocketService.Instance.subject.subscribe(
msg => {
let res = wsJsonToRes(msg);
if (res.op == UserOperation.Search) {
let data = res.data as SearchResponse;
let persons: PersonTribute[] = data.users.map(pv => {
let tribute: PersonTribute = {
key: `@${pv.person.name}@${hostname(pv.person.actor_id)}`,
view: pv,
};
return tribute;
});
cb(persons);
personSub.unsubscribe();
}
},
err => console.error(err),
() => console.log("complete")
);
} else {
cb([]);
}
} }
interface CommunityTribute { interface CommunityTribute {
@ -681,45 +652,16 @@ interface CommunityTribute {
view: CommunityView; view: CommunityView;
} }
function communitySearch( async function communitySearch(text: string): Promise<CommunityTribute[]> {
text: string, let comms = (await fetchCommunities(text)).communities;
cb: (communities: CommunityTribute[]) => any let communities: CommunityTribute[] = comms.map(cv => {
) { let tribute: CommunityTribute = {
if (text) { key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
let form: Search = { view: cv,
q: text,
type_: SearchType.Communities,
sort: SortType.TopAll,
listing_type: ListingType.All,
page: 1,
limit: mentionDropdownFetchLimit,
auth: authField(false),
}; };
return tribute;
WebSocketService.Instance.send(wsClient.search(form)); });
return communities;
let communitySub = WebSocketService.Instance.subject.subscribe(
msg => {
let res = wsJsonToRes(msg);
if (res.op == UserOperation.Search) {
let data = res.data as SearchResponse;
let communities: CommunityTribute[] = data.communities.map(cv => {
let tribute: CommunityTribute = {
key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
view: cv,
};
return tribute;
});
cb(communities);
communitySub.unsubscribe();
}
},
err => console.error(err),
() => console.log("complete")
);
} else {
cb([]);
}
} }
export function getListingTypeFromProps(props: any): ListingType { export function getListingTypeFromProps(props: any): ListingType {