Skip to content

Commit

Permalink
Update to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
jlesquembre committed Mar 29, 2023
1 parent 666872f commit a78f333
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 148 deletions.
20 changes: 0 additions & 20 deletions js-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions js-client/package.json
Expand Up @@ -20,8 +20,6 @@
"@shoelace-style/shoelace": "^2.3.0",
"cytoscape": "^3.23.0",
"cytoscape-dagre": "^2.5.0",
"idb": "^7.1.1",
"idb-keyval": "^6.2.0",
"lit": "^2.6.1"
},
"devDependencies": {
Expand Down
50 changes: 50 additions & 0 deletions js-client/src/api.ts
@@ -0,0 +1,50 @@
const API_URL = __API_URL__;

export interface Pkg {
pname: string;
outputPath: string;
}

export interface Cursor {
direction: number;
row_id: string;
}

export interface PackagesResponse {
new_cursor: Cursor;
packages: Pkg[];
}

export async function getPackages({
search = "",
limit = 10,
cursor = null,
}): Promise<PackagesResponse> {
const response = await fetch(`${API_URL}/packages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
search_predicate: search,
limit,
cursor,
}),
});
const queryResult = await response.json();
return queryResult;
}

export async function getGraph(pkgName: string) {
const response = await fetch(`${API_URL}/gremlin`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `g.V().filter{it.get().value('pname').matches('${pkgName}')}.repeat(outE().otherV().simplePath()).times(2).path().by('pname').by(label)`,
}),
});
return await response.json();
}
18 changes: 2 additions & 16 deletions js-client/src/app-main.ts
Expand Up @@ -6,24 +6,10 @@ import "./nix-search.ts";

import dagre from "cytoscape-dagre";
import cytoscape from "cytoscape";
import { getGraph } from "./api";

const API_URL = __API_URL__;
cytoscape.use(dagre);

async function getGraph(pkgName: string) {
const response = await fetch(`${API_URL}/gremlin`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `g.V().filter{it.get().value('pname').matches('${pkgName}')}.repeat(outE().otherV().simplePath()).until(__.not(outE().simplePath())).path().by('pname').by(label)`,
}),
});
return await response.json();
}

function renderCyGraph(graphData: any, container: HTMLElement) {
console.log(graphData);
const data = graphData.cyto["graph-data"];
Expand Down Expand Up @@ -75,7 +61,7 @@ export class AppMain extends LitElement {
display: grid;
gap: 10px;
padding: 2rem;
grid-template-columns: 200px 1fr;
grid-template-columns: min(25%, 300px) 1fr;
}
#cy-container {
border: 1px solid var(--sl-panel-border-color);
Expand Down
59 changes: 33 additions & 26 deletions js-client/src/nix-search.ts
Expand Up @@ -8,51 +8,50 @@ import "@shoelace-style/shoelace/dist/components/input/input.js";
import "@shoelace-style/shoelace/dist/components/spinner/spinner.js";
import "@shoelace-style/shoelace/dist/components/alert/alert.js";
import "@shoelace-style/shoelace/dist/components/icon/icon.js";

// This is the type of the message event send to the web worker
// We can only send message events, see
// https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
// We add a type to filter between events
interface Operation {
// TODO only search is used
type: "search" | "graph";
data: string[];
}
import { classMap } from "lit/directives/class-map.js";
import { getPackages } from "./api";
import type { Cursor, Pkg } from "./api";

export interface ClickItemPayload {
name: string;
}

type EventInput = Event & { target: HTMLInputElement };

// TODO Add pagination
@customElement("nix-search")
export class NixSearch extends LitElement {
worker = new Worker(new URL("./packages", import.meta.url), {
type: "module",
});
@state()
pkgs: Pkg[] = [];

@state()
pkgs: string[] = [];
cursor?: Cursor;

@state()
loading = true;

@state()
selectedPkg?: string;

searchInput = "";

constructor() {
super();
this.worker.addEventListener("message", (msg: MessageEvent<Operation>) => {
const { type, data } = msg.data;
if (type === "search") {
this.pkgs = data;
this.loading = false;
}
});
// trigger a request to get an initial list of packages displayed
this.worker.postMessage({ type: "search", data: "" });
this.getPackages();
}

async getPackages(search = "", limit = 10) {
const { packages, new_cursor } = await getPackages({ search, limit });
this.pkgs = packages;
this.cursor = new_cursor;
this.loading = false;
}

updateSearchQuery(ev: EventInput) {
this.loading = true;
this.worker.postMessage({ type: "search", data: ev.target.value });
this.searchInput = ev.target.value;
this.getPackages(this.searchInput);
}

render() {
Expand Down Expand Up @@ -80,10 +79,13 @@ export class NixSearch extends LitElement {
return html`
<sl-menu>
${this.pkgs.map(
(pkg) =>
({ pname }) =>
html`
<sl-menu-item @click=${this.clickPackageHandler} value=${pkg}
>${pkg}</sl-menu-item
<sl-menu-item
@click=${this.clickPackageHandler}
value=${pname}
class=${classMap({ selected: this.selectedPkg === pname })}
>${pname}</sl-menu-item
>
`
)}
Expand All @@ -93,6 +95,8 @@ export class NixSearch extends LitElement {

private clickPackageHandler(ev: EventInput) {
const name = ev.target.value.trim();
this.selectedPkg = name;

if (name) {
const options = {
detail: { name },
Expand All @@ -111,6 +115,9 @@ export class NixSearch extends LitElement {
sl-input {
margin-bottom: 1em;
}
.selected {
background-color: var(--sl-color-primary-200);
}
`;
}

Expand Down
84 changes: 0 additions & 84 deletions js-client/src/packages.ts

This file was deleted.

0 comments on commit a78f333

Please sign in to comment.