Skip to content

Commit

Permalink
Properly debouncing tribute mentions. Fixes #633 (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines authored Apr 27, 2022
1 parent 6b53ae7 commit 130bfc9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/shared/components/person/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ export class Settings extends Component<any, SettingsState> {
} catch (err) {
console.error(err);
}
}, 400),
}),
false
);
}
Expand Down Expand Up @@ -834,7 +834,7 @@ export class Settings extends Component<any, SettingsState> {
} catch (err) {
console.log(err);
}
}, 400),
}),
false
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/components/post/post-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
} catch (err) {
console.log(err);
}
}, 400),
}),
false
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/shared/components/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ export class Search extends Component<any, SearchState> {
} catch (err) {
console.error(err);
}
}, 400),
}),
false
);
}
Expand Down Expand Up @@ -808,7 +808,7 @@ export class Search extends Component<any, SearchState> {
} catch (err) {
console.log(err);
}
}, 400),
}),
false
);
}
Expand Down
106 changes: 24 additions & 82 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
PrivateMessageView,
RegistrationApplicationView,
Search,
SearchResponse,
SearchType,
SortType,
UserOperation,
Expand Down Expand Up @@ -585,9 +584,9 @@ export function setupTribute() {
let it: PersonTribute = item.original;
return `[${it.key}](${it.view.person.actor_id})`;
},
values: (text: string, cb: (persons: PersonTribute[]) => any) => {
personSearch(text, (persons: PersonTribute[]) => cb(persons));
},
values: debounce(async (text: string, cb: any) => {
cb(await personSearch(text));
}),
allowSpaces: false,
autocompleteMode: true,
// TODO
Expand All @@ -602,11 +601,9 @@ export function setupTribute() {
let it: CommunityTribute = item.original;
return `[${it.key}](${it.view.community.actor_id})`;
},
values: (text: string, cb: any) => {
communitySearch(text, (communities: CommunityTribute[]) =>
cb(communities)
);
},
values: debounce(async (text: string, cb: any) => {
cb(await communitySearch(text));
}),
allowSpaces: false,
autocompleteMode: true,
// TODO
Expand Down Expand Up @@ -638,88 +635,33 @@ interface PersonTribute {
view: PersonViewSafe;
}

function personSearch(text: string, cb: (persons: PersonTribute[]) => any) {
if (text) {
let form: Search = {
q: text,
type_: SearchType.Users,
sort: SortType.TopAll,
listing_type: ListingType.All,
page: 1,
limit: mentionDropdownFetchLimit,
auth: authField(false),
async function personSearch(text: string): Promise<PersonTribute[]> {
let users = (await fetchUsers(text)).users;
let persons: PersonTribute[] = users.map(pv => {
let tribute: PersonTribute = {
key: `@${pv.person.name}@${hostname(pv.person.actor_id)}`,
view: pv,
};

WebSocketService.Instance.send(wsClient.search(form));

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([]);
}
return tribute;
});
return persons;
}

interface CommunityTribute {
key: string;
view: CommunityView;
}

function communitySearch(
text: string,
cb: (communities: CommunityTribute[]) => any
) {
if (text) {
let form: Search = {
q: text,
type_: SearchType.Communities,
sort: SortType.TopAll,
listing_type: ListingType.All,
page: 1,
limit: mentionDropdownFetchLimit,
auth: authField(false),
async function communitySearch(text: string): Promise<CommunityTribute[]> {
let comms = (await fetchCommunities(text)).communities;
let communities: CommunityTribute[] = comms.map(cv => {
let tribute: CommunityTribute = {
key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
view: cv,
};

WebSocketService.Instance.send(wsClient.search(form));

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([]);
}
return tribute;
});
return communities;
}

export function getListingTypeFromProps(props: any): ListingType {
Expand Down

0 comments on commit 130bfc9

Please sign in to comment.