fix npm
This commit is contained in:
parent
4ce029bfe3
commit
b4041e6733
|
|
@ -1 +1,14 @@
|
||||||
export const searchPackage = async (search: string) => {};
|
import algoliasearch from "algoliasearch";
|
||||||
|
const algolia = algoliasearch("OFCNCOG2CU", "f54e21fa3a2a0160595bb058179bfb1e");
|
||||||
|
const npm = algolia.initIndex("npm-search");
|
||||||
|
export type AlgoliaResult = {
|
||||||
|
name: string;
|
||||||
|
objectID: string;
|
||||||
|
version: string;
|
||||||
|
_highlightResult: { name: { value: string } };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const searchPackage = async (search: string) => {
|
||||||
|
const res = await npm.search(search);
|
||||||
|
return res.hits as AlgoliaResult[];
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,16 @@ import { useGlobal, useLocal } from "web-utils";
|
||||||
import { EDGlobal } from "../../../logic/ed-global";
|
import { EDGlobal } from "../../../logic/ed-global";
|
||||||
import { npm_page, npm_site } from "../../../../../../../db/db";
|
import { npm_page, npm_site } from "../../../../../../../db/db";
|
||||||
import { EdNpmItems } from "./npm-items";
|
import { EdNpmItems } from "./npm-items";
|
||||||
|
import { AlgoliaResult, searchPackage } from "./npm-algolia";
|
||||||
|
|
||||||
export const EdNpmImport = ({ mode }: { mode: "page" | "site" }) => {
|
export const EdNpmImport = ({ mode }: { mode: "page" | "site" }) => {
|
||||||
const p = useGlobal(EDGlobal, "EDITOR");
|
const p = useGlobal(EDGlobal, "EDITOR");
|
||||||
const local = useLocal({
|
const local = useLocal({
|
||||||
search: "",
|
search: {
|
||||||
|
text: "",
|
||||||
|
result: [] as AlgoliaResult[],
|
||||||
|
options: [] as { label: string; value: string }[],
|
||||||
|
},
|
||||||
status: "init" as "init" | "ready",
|
status: "init" as "init" | "ready",
|
||||||
size: 0,
|
size: 0,
|
||||||
list: [] as (npm_page | npm_site)[],
|
list: [] as (npm_page | npm_site)[],
|
||||||
|
|
@ -50,14 +55,37 @@ export const EdNpmImport = ({ mode }: { mode: "page" | "site" }) => {
|
||||||
unstyled
|
unstyled
|
||||||
classNamePrefix={"sel"}
|
classNamePrefix={"sel"}
|
||||||
placeholder="Search Packages"
|
placeholder="Search Packages"
|
||||||
inputValue={local.search}
|
inputValue={local.search.text}
|
||||||
openMenuOnClick={false}
|
openMenuOnClick={false}
|
||||||
openMenuOnFocus={false}
|
openMenuOnFocus={false}
|
||||||
onInputChange={(text) => {
|
onInputChange={async (text) => {
|
||||||
local.search = text;
|
local.search.text = text;
|
||||||
|
local.search.result = await searchPackage(text);
|
||||||
|
local.search.options = [];
|
||||||
|
for (const r of local.search.result) {
|
||||||
|
local.search.options.push({
|
||||||
|
label: `${r.name} (${r.version})`,
|
||||||
|
value: `${r.name}-><-${r.version}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
local.render();
|
local.render();
|
||||||
}}
|
}}
|
||||||
onChange={() => {}}
|
options={local.search.options}
|
||||||
|
onChange={async (e) => {
|
||||||
|
if (e) {
|
||||||
|
const [name, version] = e.value.split("-><-");
|
||||||
|
if (mode === "page") {
|
||||||
|
await db.npm_page.create({
|
||||||
|
data: { id_page: p.page.cur.id, module: name, version },
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await db.npm_site.create({
|
||||||
|
data: { id_site: p.site.id, module: name, version },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<div className="w-[100px] flex items-center justify-center">
|
<div className="w-[100px] flex items-center justify-center">
|
||||||
{local.status === "init" && <>Loading...</>}
|
{local.status === "init" && <>Loading...</>}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue