Skip to content

Commit e873f6d

Browse files
committed
improved search
1 parent b0c7dca commit e873f6d

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/extension/codemic.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ class CodeMic {
10781078
}
10791079
case t.Screen.Welcome: {
10801080
const current = await this.getSessionListingOfDefaultVscWorkspace();
1081-
const recent = await this.getRecentSessionListings(4);
1081+
const recent = await this.getRecentSessionListings();
10821082
this.welcome = {
10831083
sessions: _.compact([current, ...recent]),
10841084
loadingFeatured: true,
@@ -1214,7 +1214,7 @@ class CodeMic {
12141214
}
12151215
}
12161216

1217-
async getRecentSessionListings(limit: number): Promise<t.SessionListing[]> {
1217+
async getRecentSessionListings(limit?: number): Promise<t.SessionListing[]> {
12181218
function getTimestamp(history: t.SessionHistory) {
12191219
return (history && lib.getSessionHistoryItemLastOpenTimestamp(history)) || '';
12201220
}
@@ -1223,7 +1223,7 @@ class CodeMic {
12231223
const recent: t.SessionListing[] = [];
12241224
for (const history of orderedHistory) {
12251225
try {
1226-
if (recent.length === limit) break;
1226+
if (limit !== undefined && recent.length === limit) break;
12271227

12281228
const head = await Session.Core.readLocalHead(history.workspace);
12291229
if (head?.id === history.id) {
@@ -1352,6 +1352,8 @@ class CodeMic {
13521352
publication: this.publications.get(s.head.id),
13531353
}));
13541354
sessions = lib.searchSessions(sessions, this.searchQuery);
1355+
sessions = lib.limitRecentSessions(sessions, 4);
1356+
13551357
welcome = {
13561358
searchQuery: this.searchQuery,
13571359
sessions,

src/lib/lib.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -716,21 +716,58 @@ export function searchSessions(sessions: t.SessionUIListing[], searchQuery: stri
716716
if (!searchQueryNorm) return sessions;
717717

718718
const tokens = searchQueryNorm.split(/\s+/);
719-
return sessions.filter(listing => doFieldsIncludeTokens(getFields(listing.head)));
719+
720+
let pairs = sessions.map(listing => ({
721+
listing,
722+
score:
723+
((getTokensMatchScore(getFields(listing.head)) *
724+
(listing.publication ? listing.publication.likes * 20 + listing.publication.views : 1)) /
725+
10) *
726+
(listing.head.isClip ? 1 : 2),
727+
}));
728+
pairs = _.filter(pairs, 'score');
729+
pairs = _.orderBy(pairs, 'score', 'desc');
730+
return _.map(pairs, 'listing');
720731

721732
function getFields(head: t.SessionHead): string[] {
722733
return [
723734
`@${head.author ?? ''}/${head.handle}`,
724735
head.title,
736+
head.toc.map(item => item.title).join(' '),
725737
head.description,
726-
...head.toc.map(item => item.title),
727738
].map(str => str?.toLowerCase());
728739
}
729740

730-
function doFieldsIncludeTokens(fields: string[]) {
731-
return tokens.every(token => doFieldsIncludeToken(fields, token));
741+
function getTokensMatchScore(fields: string[]): number {
742+
let score = 0;
743+
for (const token of tokens) {
744+
let tokenMatchScore = getTokenMatchScore(fields, token);
745+
if (!tokenMatchScore) return 0; // all tokens must match
746+
score += tokenMatchScore;
747+
}
748+
return score;
749+
}
750+
function getTokenMatchScore(fields: string[], token: string): number {
751+
let score = 0;
752+
for (let i = 0; i < fields.length; i++) {
753+
if (fields[i].includes(token)) {
754+
score += (fields.length - i) ** 2 * (token.length / fields[i].length);
755+
}
756+
}
757+
758+
return score;
732759
}
733-
function doFieldsIncludeToken(fields: string[], token: string) {
734-
return fields.some(field => field.includes(token));
760+
}
761+
762+
export function limitRecentSessions(sessions: t.SessionUIListing[], limit: number): t.SessionUIListing[] {
763+
let res = [];
764+
let count = 0;
765+
for (const listing of sessions) {
766+
if (listing.group === 'recent') {
767+
if (count >= limit) continue;
768+
count++;
769+
}
770+
res.push(listing);
735771
}
772+
return res;
736773
}

0 commit comments

Comments
 (0)