generated from edmundhung/remix-cloudflare-template
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathresources.tsx
182 lines (157 loc) · 4.21 KB
/
resources.tsx
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { getSite } from '~/search';
import type { SearchOptions, Resource, User } from '~/types';
function calculateScore(resource: Resource): number {
const timeScore =
(new Date(resource.createdAt).valueOf() -
new Date('2021-12-24T00:00:00.000Z').valueOf()) /
1000 /
3600 /
24;
const bookmarkCount = resource.bookmarkUsers?.length ?? 0;
const bookmarkScore = bookmarkCount > 0 ? Math.log10(bookmarkCount) + 1 : 0;
const viewScore = Math.log10(resource.viewCount ?? 0);
return 1 * timeScore + 10 * bookmarkScore + 1.5 * viewScore;
}
function compareResources(key: string, prev: Resource, next: Resource): number {
let diff = 0;
switch (key) {
case 'timestamp':
diff =
new Date(next.createdAt).valueOf() - new Date(prev.createdAt).valueOf();
break;
case 'bookmarkCount':
diff =
(next.bookmarkUsers?.length ?? 0) - (prev.bookmarkUsers?.length ?? 0);
break;
case 'viewCount':
diff = (next.viewCount ?? 0) - (prev.viewCount ?? 0);
break;
}
return diff;
}
export function search(
list: { [resourceId: string]: Resource },
options: SearchOptions,
): { entries: Resource[]; count: number } {
function match(
wanted: string[],
value: string | string[],
partialMatch = false,
) {
if (wanted.length === 0) {
return true;
}
if (partialMatch || Array.isArray(value)) {
return wanted.every((item) => value.includes(item));
}
return wanted.includes(value);
}
const entries = Object.values(list)
.filter((resource) => {
if (options.includes && !options.includes.includes(resource.id)) {
return false;
}
if (options.excludes && options.excludes.includes(resource.id)) {
return false;
}
const isMatching =
match(options?.list ? [options.list] : [], resource.lists ?? []) &&
match(
options?.keyword ? options.keyword.toLowerCase().split(' ') : [],
`${resource.title} ${resource.description}`.toLowerCase(),
true,
) &&
match(options.author ? [options.author] : [], resource.author ?? '') &&
match(
options.category ? [options.category] : [],
resource.category ?? '',
) &&
match(
options.site ? [options.site] : [],
new URL(resource.url).hostname,
) &&
match(
([] as string[]).concat(
options.platform ?? [],
options.integrations ?? [],
),
resource.integrations ?? [],
);
return isMatching;
})
.sort((prev, next) => {
let diff = 0;
switch (options.sort) {
case 'top':
for (const key of ['bookmarkCount', 'viewCount', 'timestamp']) {
diff = compareResources(key, prev, next);
if (diff !== 0) {
break;
}
}
break;
case 'hot':
diff = calculateScore(next) - calculateScore(prev);
break;
case 'new':
default:
diff = options.includes
? options.includes.indexOf(prev.id) -
options.includes.indexOf(next.id)
: compareResources('timestamp', prev, next);
break;
}
return diff;
});
return {
entries: options.limit ? entries.slice(0, options.limit) : entries,
count: entries.length,
};
}
export function getSuggestions(
list: { [resourceId: string]: Resource },
resource: Resource,
) {
const suggestions: SearchOptions[] = [];
if (resource.category === 'package' && resource.title) {
suggestions.push({
integrations: [resource.title],
sort: 'top',
});
}
if (resource.author) {
suggestions.push({
author: resource.author,
sort: 'top',
});
}
if (resource.category === 'others') {
suggestions.push({
site: getSite(resource.url),
sort: 'top',
});
}
const result = suggestions.map((searchOptions) => ({
entries: search(list, {
...searchOptions,
excludes: [resource.id],
limit: 6,
}).entries,
searchOptions,
}));
return result;
}
export function patchResource(resource: Resource, user: User): Resource {
const isUserBookmarked = user.bookmarked.includes(resource.id) ?? false;
const isResourceBookmarked =
resource.bookmarkUsers?.includes(user.profile.id) ?? false;
if (isUserBookmarked === isResourceBookmarked) {
return resource;
}
return {
...resource,
bookmarkUsers: isUserBookmarked
? [...(resource.bookmarkUsers ?? []), user.profile.id]
: resource.bookmarkUsers?.filter((id) => id !== user.profile.id),
};
}