Skip to content
Draft
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
81 changes: 47 additions & 34 deletions src/lib/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ export async function promptForMissing<
// ─── Item picker ──────────────────────────────────────────────────────────────

const PICKER_PAGE_SIZE = 20;
const FETCH_MORE = '__fetch_more__';
const NEXT_PAGE = '__next_page__';
const PREV_PAGE = '__prev_page__';
const NONE = '__none__';

export type PickerConfig<T extends { id: string }> = {
Expand Down Expand Up @@ -283,14 +284,16 @@ export async function pickId<T extends { id: string }>(
}

const resend = await requireClient(globalOpts);
const allFetched: T[] = [];

for (;;) {
const cursor = allFetched.at(-1)?.id;
const fetchPage = async (
cursors: readonly (string | undefined)[],
pageIndex: number,
): Promise<string | undefined> => {
const cursor = cursors[pageIndex];
const spinner = createSpinner(
allFetched.length === 0
pageIndex === 0
? `Fetching ${config.resourcePlural}...`
: `Fetching more ${config.resourcePlural}...`,
: `Fetching ${config.resourcePlural} (page ${pageIndex + 1})...`,
globalOpts.quiet,
);

Expand All @@ -305,7 +308,7 @@ export async function pickId<T extends { id: string }>(
return undefined;
}
spinner.fail(`Failed to fetch ${config.resourcePlural}`);
outputError(
return outputError(
{
message: result.error?.message ?? 'Unexpected empty response',
code: 'list_error',
Expand All @@ -314,27 +317,31 @@ export async function pickId<T extends { id: string }>(
);
}

allFetched.push(...result.data.data);
const pageItems = result.data.data;
const hasMore = result.data.has_more ?? false;

const nextCursors =
hasMore && cursors.length === pageIndex + 1
? (() => {
const lastItem = pageItems.at(-1);
return lastItem ? [...cursors, lastItem.id] : cursors;
})()
: cursors;

const displayItems = config.filter
? allFetched.filter(config.filter)
: allFetched;
? pageItems.filter(config.filter)
: pageItems;

if (displayItems.length === 0 && !hasMore && optional) {
if (displayItems.length === 0 && !hasMore && pageIndex === 0 && optional) {
spinner.clear();
return undefined;
}

spinner.stop(
allFetched.length === displayItems.length
? `${config.resourcePlural} fetched`
: `More ${config.resourcePlural} fetched`,
);
spinner.stop(`${config.resourcePlural} fetched`);

if (displayItems.length === 0 && !hasMore) {
if (displayItems.length === 0 && !hasMore && pageIndex === 0) {
p.log.warn(`No ${config.resourcePlural} found.`);
outputError(
return outputError(
{
message: `No ${config.resourcePlural} found.`,
code: 'no_items',
Expand All @@ -343,23 +350,21 @@ export async function pickId<T extends { id: string }>(
);
}

if (displayItems.length === 0 && hasMore && !optional) {
continue;
if (displayItems.length === 0 && config.filter) {
p.log.info(`No matching ${config.resourcePlural} on this page.`);
}

const options: { value: string; label: string; hint?: string }[] =
displayItems.map((item) => ({
const options: { value: string; label: string; hint?: string }[] = [
...(pageIndex > 0
? [{ value: PREV_PAGE, label: '\u2190 Previous page' }]
: []),
...(optional ? [{ value: NONE, label: 'None' }] : []),
...displayItems.map((item) => ({
value: item.id,
...config.display(item),
}));

if (optional) {
options.unshift({ value: NONE, label: 'None' });
}

if (hasMore) {
options.push({ value: FETCH_MORE, label: 'Fetch more...' });
}
})),
...(hasMore ? [{ value: NEXT_PAGE, label: 'Next page \u2192' }] : []),
];

const selected = await p.select({
message: `Select a ${config.resource}`,
Expand All @@ -374,8 +379,16 @@ export async function pickId<T extends { id: string }>(
return undefined;
}

if (selected !== FETCH_MORE) {
return selected;
if (selected === NEXT_PAGE) {
return fetchPage(nextCursors, pageIndex + 1);
}
}

if (selected === PREV_PAGE) {
return fetchPage(nextCursors, pageIndex - 1);
}

return selected;
};

return fetchPage([undefined], 0);
}
Loading