~james/core

core/web/src/lib/components/ui/markdownSuggestions.ts -rw-r--r-- 3.5 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import type { BobbinContext } from "#lib/api/client.ts";
import { INVALID_HANDLE } from "#lib/api/identity.ts";
import { listIssues } from "#lib/api/issue.ts";
import { listPulls } from "#lib/api/records.ts";
import { searchActorsTypeahead } from "#lib/api/search.ts";
import { didOf } from "#lib/api/syntax.ts";
import { rkeyFromUri } from "#lib/api/uri.ts";
import type { TicketState } from "#lib/components/repo/tickets/TicketStatePill.svelte";
import type { MentionPerson } from "#lib/components/ui/mentionPeople.ts";

export interface UserSuggestion {
	kind: "user";
	key: string;
	did: string;
	handle: string;
	insert: string;
}

export interface TicketSuggestion {
	kind: "ticket";
	key: string;
	isPull: boolean;
	state: TicketState;
	title: string;
	rkey: string;
	createdAt: string;
	insert: string;
}

export type Suggestion = UserSuggestion | TicketSuggestion;

export interface TicketRepo {
	did: string;
	url: string;
}

const SUGGESTION_LIMIT = 8;
const RECOMMENDATION_LIMIT = 5;
const TICKET_FETCH_LIMIT = 100;

export const suggestUsers = async (
	ctx: BobbinContext,
	query: string,
	signal: AbortSignal
): Promise<UserSuggestion[]> => {
	if (query === "") return [];
	const { actors } = await searchActorsTypeahead(
		ctx,
		{ q: query, limit: SUGGESTION_LIMIT },
		{ signal }
	);
	return actors.map((actor) => userSuggestion(actor.did, actor.handle));
};

const userSuggestion = (did: string, handle: string): UserSuggestion => ({
	kind: "user",
	key: did,
	did,
	handle,
	insert: `@${handle} `
});

export const recommendUsers = (
	people: MentionPerson[],
	exclude: string | undefined
): UserSuggestion[] => {
	const seen = new Set(exclude ? [exclude] : []);
	const picked: UserSuggestion[] = [];
	for (const { did, handle } of people) {
		if (!handle || handle === INVALID_HANDLE || seen.has(did)) continue;
		seen.add(did);
		picked.push(userSuggestion(did, handle));
		if (picked.length === RECOMMENDATION_LIMIT) break;
	}
	return picked;
};

export const recommendPeople = async (
	known: MentionPerson[],
	members: () => Promise<MentionPerson[]>,
	exclude: string | undefined
): Promise<UserSuggestion[]> => {
	const participants = recommendUsers(known, exclude);
	if (participants.length >= RECOMMENDATION_LIMIT) return participants;
	const others = await members().catch(() => []);
	return recommendUsers([...known, ...others], exclude);
};

const ticket = (
	repo: TicketRepo,
	isPull: boolean,
	item: { uri: string; state: TicketState; value: { title: { text: string }; createdAt: string } }
): TicketSuggestion => {
	const rkey = rkeyFromUri(item.uri);
	return {
		kind: "ticket",
		key: item.uri,
		isPull,
		state: item.state,
		title: item.value.title.text,
		rkey,
		createdAt: item.value.createdAt,
		insert: `[#${rkey}](${repo.url}/${isPull ? "pulls" : "issues"}/${rkey}) `
	};
};

export const loadTickets = async (
	ctx: BobbinContext,
	repo: TicketRepo
): Promise<TicketSuggestion[]> => {
	const [issues, pulls] = await Promise.all([
		listIssues(ctx, didOf(repo.did), { limit: TICKET_FETCH_LIMIT }),
		listPulls(ctx, repo.did, { limit: TICKET_FETCH_LIMIT })
	]);
	return [
		...issues.items.map((item) => ticket(repo, false, item)),
		...pulls.items.map((item) => ticket(repo, true, item))
	].sort((a, b) => b.createdAt.localeCompare(a.createdAt));
};

export const filterTickets = (tickets: TicketSuggestion[], query: string): TicketSuggestion[] => {
	const needle = query.toLowerCase();
	return tickets
		.filter(
			(ticket) =>
				ticket.rkey.startsWith(needle) || ticket.title.toLowerCase().includes(needle)
		)
		.slice(0, SUGGESTION_LIMIT);
};