Skip to content

Commit

Permalink
Simplifies search by URL by removing merging of results
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeibbb committed Oct 31, 2024
1 parent 80033c6 commit 60b148f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 77 deletions.
65 changes: 26 additions & 39 deletions src/plus/launchpad/launchpad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ export class LaunchpadCommand extends QuickCommand<State> {
i: LaunchpadItem,
ui: LaunchpadGroup,
topItem: LaunchpadItem | undefined,
alwaysShow: boolean | undefined,
): LaunchpadItemQuickPickItem => {
const buttons = [];

Expand Down Expand Up @@ -452,6 +453,7 @@ export class LaunchpadCommand extends QuickCommand<State> {
i.actionableCategory === 'other' ? '' : `${actionGroupMap.get(i.actionableCategory)![0]} \u2022 `
}${fromNow(i.updatedDate)} by @${i.author!.username}`,

alwaysShow: alwaysShow,
buttons: buttons,
iconPath: i.author?.avatarUrl != null ? Uri.parse(i.author.avatarUrl) : undefined,
item: i,
Expand All @@ -460,7 +462,7 @@ export class LaunchpadCommand extends QuickCommand<State> {
};
};

const getItems = (result: LaunchpadCategorizedResult, treatAllGroupAsExpanded?: boolean) => {
const getItems = (result: LaunchpadCategorizedResult, isSearching?: boolean) => {
const items: (LaunchpadItemQuickPickItem | DirectiveQuickPickItem | ConnectMoreIntegrationsItem)[] = [];

if (result.items?.length) {
Expand All @@ -475,18 +477,21 @@ export class LaunchpadCommand extends QuickCommand<State> {
for (const [ui, groupItems] of uiGroups) {
if (!groupItems.length) continue;

items.push(...buildGroupHeading(ui, groupItems.length));

if (!treatAllGroupAsExpanded && context.collapsed.get(ui)) continue;
if (!isSearching) {
items.push(...buildGroupHeading(ui, groupItems.length));
if (context.collapsed.get(ui)) {
continue;
}
}

items.push(...groupItems.map(i => buildLaunchpadQuickPickItem(i, ui, topItem)));
items.push(...groupItems.map(i => buildLaunchpadQuickPickItem(i, ui, topItem, isSearching)));
}
}

return items;
};

function getItemsAndPlaceholder(treatAllGroupAsExpanded?: boolean) {
function getItemsAndPlaceholder(isSearching?: boolean) {
if (context.result.error != null) {
return {
placeholder: `Unable to load items (${
Expand All @@ -509,7 +514,7 @@ export class LaunchpadCommand extends QuickCommand<State> {

return {
placeholder: 'Choose an item to focus on',
items: getItems(context.result, treatAllGroupAsExpanded),
items: getItems(context.result, isSearching),
};
}

Expand Down Expand Up @@ -539,8 +544,7 @@ export class LaunchpadCommand extends QuickCommand<State> {
};

const { items, placeholder } = getItemsAndPlaceholder();

let groupsHidden = false;
const nonGroupedItems = items.filter(i => !isDirectiveQuickPickItem(i));

const step = createPickStep({
title: context.title,
Expand All @@ -556,32 +560,27 @@ export class LaunchpadCommand extends QuickCommand<State> {
RefreshQuickInputButton,
],
onDidChangeValue: async quickpick => {
const hideGroups = Boolean(quickpick.value?.length);

if (groupsHidden !== hideGroups) {
groupsHidden = hideGroups;
quickpick.items = hideGroups ? items.filter(i => !isDirectiveQuickPickItem(i)) : items;
}
const { value } = quickpick;
const activeLaunchpadItems = quickpick.activeItems.filter(
(i): i is LaunchpadItemQuickPickItem => 'item' in i && !i.alwaysShow,
);
const hideGroups = Boolean(value?.length);
const consideredItems = hideGroups ? nonGroupedItems : items;

let updated = false;
for (const item of quickpick.items) {
for (const item of consideredItems) {
if (item.alwaysShow) {
item.alwaysShow = false;
updated = true;
}
if ('item' in item && item.item?.isSearched) {
updated = true;
}
}
if (updated) {
// Force quickpick to update by changing the items object:
quickpick.items = [...quickpick.items.filter(i => !('item' in i && i.item?.isSearched))];
}

// By doing the following we make sure we operate with the PRs that belong to Launchpad initially.
// Also, when we re-create the array, we make sure that `alwaysShow` updates are applied.
quickpick.items =
updated && quickpick.items === consideredItems ? [...consideredItems] : consideredItems;

const activeLaunchpadItems = quickpick.activeItems.filter(
(i): i is LaunchpadItemQuickPickItem => 'item' in i,
);

if (!value?.length || activeLaunchpadItems.length) {
// Nothing to search or use items that have been found locally
this.updateItemsDebouncer.cancel();
Expand Down Expand Up @@ -622,13 +621,6 @@ export class LaunchpadCommand extends QuickCommand<State> {
}
// Nothing is found above, so let's perform search in the API:
await updateItems(quickpick, value);
quickpick.items.forEach(i => {
if ('item' in i && doesPullRequestSatisfyRepositoryURLIdentity(i.item, prUrlIdentity)) {
i.alwaysShow = true;
}
});
quickpick.items = quickpick.items.filter((i): i is LaunchpadItemQuickPickItem => 'item' in i);
groupsHidden = true;
return true;
},
onDidClickButton: async (quickpick, button) => {
Expand Down Expand Up @@ -1369,12 +1361,7 @@ async function updateContextItems(
options?: { force?: boolean; search?: string },
cancellation?: CancellationToken,
) {
const result = await container.launchpad.getCategorizedItems(options, cancellation);
if (options?.search) {
context.result = container.launchpad.mergeSearchedCategorizedItems(context.result, result);
} else {
context.result = result;
}
context.result = await container.launchpad.getCategorizedItems(options, cancellation);
if (container.telemetry.enabled) {
updateTelemetryContext(context);
}
Expand Down
38 changes: 0 additions & 38 deletions src/plus/launchpad/launchpadProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ export type LaunchpadItem = LaunchpadPullRequest & {
actionableCategory: LaunchpadActionCategory;
suggestedActions: LaunchpadAction[];
openRepository?: OpenRepository;
isSearched?: boolean;

underlyingPullRequest: PullRequest;
};
Expand Down Expand Up @@ -644,43 +643,6 @@ export class LaunchpadProvider implements Disposable {
return repoRemotes;
}

mergeSearchedCategorizedItems(
prsOfTheLaunchpad: LaunchpadCategorizedResult,
prsOfSearch: LaunchpadCategorizedResult | undefined,
): LaunchpadCategorizedResult {
if (prsOfSearch == null || prsOfSearch?.error) {
if (prsOfTheLaunchpad.items == null) {
return prsOfTheLaunchpad;
}
return {
items: prsOfTheLaunchpad.items.filter(i => !i.isSearched),
timings: prsOfTheLaunchpad.timings,
};
}

const result = [];
const idsOfLaunchpadPrs = new Set();
if (prsOfTheLaunchpad.items) {
for (const item of prsOfTheLaunchpad.items) {
if (!item.isSearched) {
idsOfLaunchpadPrs.add(item.id);
result.push(item);
}
}
}
for (const item of prsOfSearch.items) {
if (!idsOfLaunchpadPrs.has(item.id)) {
item.isSearched = true;
result.push(item);
}
}

return {
items: result,
timings: prsOfSearch.timings,
};
}

@gate<LaunchpadProvider['getCategorizedItems']>(o => `${o?.force ?? false}`)
@log<LaunchpadProvider['getCategorizedItems']>({ args: { 0: o => `force=${o?.force}`, 1: false } })
async getCategorizedItems(
Expand Down

0 comments on commit 60b148f

Please sign in to comment.