Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/components/account/config/search/entries.ts
5537 views
1
/*
2
* This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import type { IconName } from "@cocalc/frontend/components/icon";
7
8
import { search_match, search_split } from "@cocalc/util/misc";
9
10
interface Info0 {
11
path: string;
12
desc: string;
13
title: string;
14
icon?: IconName | "ai";
15
}
16
17
export interface Info extends Info0 {
18
search: string;
19
}
20
21
interface Info1 extends Info0 {
22
search?: string | object;
23
}
24
25
const searchInfo: { [path: string]: Info } = {};
26
27
export function register(info: Info1) {
28
const search = (
29
info.desc +
30
" " +
31
info.path +
32
" " +
33
info.title +
34
" " +
35
JSON.stringify(info.search ?? "")
36
).toLowerCase();
37
searchInfo[info.path] = { ...info, search };
38
}
39
40
export function search(s: string, allowEmpty?: boolean): Info[] {
41
const v = search_split(s.toLowerCase().trim());
42
const result: Info[] = [];
43
if (v.length == 0 && !allowEmpty) return result;
44
for (const path in searchInfo) {
45
if (
46
v.length == 0 ||
47
(searchInfo[path].search && search_match(searchInfo[path].search, v))
48
) {
49
result.push(searchInfo[path]);
50
}
51
}
52
return result;
53
}
54
55