Skip to content

Commit 6235d56

Browse files
Add a stub search UI (#1370)
1 parent 75dbd06 commit 6235d56

File tree

8 files changed

+285
-33
lines changed

8 files changed

+285
-33
lines changed

package-lock.json

Lines changed: 86 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/catalog-server/src/lib/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {readFile} from 'fs/promises';
88
import {fileURLToPath} from 'url';
99

1010
import Koa from 'koa';
11+
import cors from '@koa/cors';
1112
import Router from '@koa/router';
1213
import {
1314
getGraphQLParameters,
@@ -163,6 +164,7 @@ export const makeServer = async () => {
163164
`;
164165
});
165166

167+
app.use(cors());
166168
app.use(router.routes());
167169
app.use(router.allowedMethods());
168170
return app;

packages/site-client/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
]
2424
},
2525
"build:dev": {
26-
"command": "find src -name \"*.ts\" | xargs esbuild --format=esm --target=es2022 --color --outdir=lib",
26+
"command": "find src -name \"*.ts\" | xargs esbuild --color --outdir=lib",
2727
"files": [
2828
"src/**/*.ts"
2929
],
@@ -33,7 +33,7 @@
3333
"clean": "if-file-deleted"
3434
},
3535
"build:prod": {
36-
"command": "esbuild --format=esm --target=es2022 --color --outdir=bundled --bundle --splitting --minify src/entrypoints/*.ts",
36+
"command": "esbuild --color --outdir=bundled --bundle --splitting --minify src/entrypoints/*.ts",
3737
"files": [
3838
"src/**/*.ts"
3939
],
@@ -66,6 +66,9 @@
6666
}
6767
},
6868
"dependencies": {
69+
"@urql/core": "^3.0.4",
70+
"@webcomponents/catalog-api": "^0.0.0",
71+
"graphql": "^16.6.0",
6972
"lit": "^2.3.1",
7073
"lit-analyzer": "^1.2.1"
7174
}

packages/site-client/src/components/wco-catalog-search.ts

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,87 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import {html, css, LitElement} from 'lit';
8-
import {customElement} from 'lit/decorators.js';
9-
10-
@customElement('wco-catalog-search')
11-
export class WCOCatalogSearch extends LitElement {
12-
static styles = css`
13-
:host {
14-
display: flex;
15-
align-items: center;
16-
}
17-
`;
18-
19-
render() {
20-
return html`
21-
Search: <input>
22-
`;
23-
}
24-
}
25-
26-
declare global {
27-
interface HTMLElementTagNameMap {
28-
'wco-catalog-search': WCOCatalogSearch;
29-
}
30-
}
31-
7+
import {html, css, LitElement} from 'lit';
8+
import {customElement, query, state} from 'lit/decorators.js';
9+
import {gql, createClient, defaultExchanges} from '@urql/core';
10+
import type {CustomElement} from '@webcomponents/catalog-api/lib/schema.js';
11+
12+
import './wco-element-card.js';
13+
14+
const client = createClient({
15+
// TODO (justinfagnani): get this URL from server
16+
url: 'http://localhost:6451/graphql',
17+
exchanges: defaultExchanges,
18+
});
19+
20+
const elementsQuery = gql`
21+
query Elements($query: String) {
22+
elements(query: $query, limit: 16) {
23+
tagName
24+
package
25+
version
26+
className
27+
}
28+
}
29+
`;
30+
31+
@customElement('wco-catalog-search')
32+
export class WCOCatalogSearch extends LitElement {
33+
static styles = css`
34+
:host {
35+
display: flex;
36+
flex-direction: column;
37+
align-items: center;
38+
}
39+
40+
div {
41+
display: grid;
42+
grid-template-columns: repeat(4, 200px);
43+
grid-template-rows: auto;
44+
grid-auto-columns: 200px;
45+
grid-auto-rows: 200px;
46+
gap: 8px;
47+
}
48+
`;
49+
50+
@query('input')
51+
private _search!: HTMLInputElement;
52+
53+
@state()
54+
private _elements: Array<CustomElement> | undefined;
55+
56+
render() {
57+
return html`
58+
<p>Search: <input id="search" @change=${this._onChange} /></p>
59+
<div>
60+
${this._elements?.map(
61+
(e) => html`<wco-element-card .element=${e}></wco-element-card>`
62+
)}
63+
</div>
64+
`;
65+
}
66+
67+
protected firstUpdated() {
68+
// TODO (justinfagnani): we may want to use a router (and write the search
69+
// to the URL) but this is easy for now.
70+
const urlQuery = new URLSearchParams(window.location.search).get('query');
71+
if (urlQuery) {
72+
this._search.value = urlQuery;
73+
this._onChange();
74+
}
75+
}
76+
77+
async _onChange() {
78+
const searchText = this._search.value;
79+
const result = await client
80+
.query(elementsQuery, {query: searchText})
81+
.toPromise();
82+
this._elements = result.data?.elements;
83+
}
84+
}
85+
86+
declare global {
87+
interface HTMLElementTagNameMap {
88+
'wco-catalog-search': WCOCatalogSearch;
89+
}
90+
}

0 commit comments

Comments
 (0)