Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | import { Col, Row, Spinner } from "@canonical/react-components";
import {
BundleCard,
CharmCard,
LoadingCard,
} from "@canonical/store-components";
import { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import { ICharm } from "../shared/types";
type DiscourseTopic = {
id: string;
url: string;
title: string;
post: { blurb: string };
};
type Documentation = {
path: string;
domain: string;
title: string;
blocks: { title: string; content: string }[];
};
function App() {
const search = new URLSearchParams(window.location.search).get("q");
const [loading, setLoading] = useState(true);
const [term, setTerm] = useState(search || "");
const [results, setResults] = useState({
charms: [],
bundles: [],
docs: [],
topics: [],
});
useEffect(() => {
if (!search) return;
fetch(`/all-search.json?q=${search}&limit=4`)
.then((response) => response.json())
.then((data) => {
setResults(data);
setLoading(false);
});
}, []);
const { charms, bundles, docs, topics } = results;
return (
<>
<section id="search-docs" className="p-strip is-shallow">
<div className="row">
{search ? (
<h1 className="p-heading--2">Search results for "{search}"</h1>
) : (
<h1>Search Charmhub</h1>
)}
<form
className="p-search-box u-no-margin--bottom"
action="/all-search"
>
<input
value={term}
onChange={(e) => setTerm(e.target.value)}
type="search"
className="p-search-box__input"
name="q"
placeholder="Search Charmhub"
required
/>
<button type="reset" className="p-search-box__reset">
<i className="p-icon--close">Reset</i>
</button>
<button type="submit" className="p-search-box__button">
<i className="p-icon--search">Search</i>
</button>
</form>
</div>
</section>
{search && (
<div className="row">
<section className="p-section">
<h3>
<a href={`/?type=charm&q=${search}`}>Charms ›</a>
</h3>
<Row>
{loading ? (
[...Array(4)].map((_, i) => (
<Col size={3} style={{ marginBottom: "1.5rem" }} key={i}>
<LoadingCard />
</Col>
))
) : charms.length ? (
<>
<p>
Showing the top {charms.length} results for "{search}"
</p>
{charms.map((charm: ICharm) => (
<Col
size={3}
style={{ marginBottom: "1.5rem" }}
key={charm?.package?.name}
>
<CharmCard data={charm} />
</Col>
))}
</>
) : (
<p>No charms matching this search</p>
)}
</Row>
</section>
<section className="p-section">
<h3>
<a href={`/?type=bundle&q=${search}`}>Bundles ›</a>
</h3>
<Row>
{loading ? (
[...Array(4)].map((_, i) => (
<Col size={3} style={{ marginBottom: "1.5rem" }} key={i}>
<LoadingCard />
</Col>
))
) : bundles.length ? (
<>
<p>
Showing the top {bundles.length} results for "{search}"
</p>
{bundles.map((bundle: ICharm) => (
<Col
size={3}
style={{ marginBottom: "1.5rem" }}
key={bundle?.package?.name}
>
<BundleCard data={bundle} />
</Col>
))}
</>
) : (
<p>No bundles matching this search</p>
)}
</Row>
</section>
<section className="p-section">
<h3>
<a href={`https://juju.is/docs/search?q=${search}`}>
Documentation ›
</a>
</h3>
<div>
{loading ? (
<Spinner />
) : docs.length ? (
docs.map((doc: Documentation) => (
<Col size={12} key={doc.path}>
<h5>
{doc.path ? (
<a
href={doc.domain + doc.path}
target="_blank"
rel="noreferrer"
>
{doc.title}
</a>
) : (
doc.title
)}
</h5>
<p>{doc?.blocks?.[0].content.substring(0, 300) + "..."}</p>
</Col>
))
) : (
<p>No matching documentation pages</p>
)}
</div>
</section>
<section className="p-section">
<h3>
<a href={`https://discourse.charmhub.io/search?q=${search}`}>
Forum posts ›
</a>
</h3>
{loading ? (
<Spinner />
) : topics.length ? (
topics.map((topic: DiscourseTopic) => (
<Col size={12} key={topic.id}>
<h5>
<a
href={`https://discourse.charmhub.io/t/${topic.id}`}
target="_blank"
rel="noreferrer"
>
{topic.title}
</a>
</h5>
<p>{topic?.post?.blurb}</p>
</Col>
))
) : (
<p>No matching posts</p>
)}
</section>
</div>
)}
</>
);
}
const container = document.getElementById("main-content");
const root = createRoot(container as HTMLElement);
root.render(<App />);
|