Skip to content

Commit

Permalink
feat: agent version in footer
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksasiriski committed Jun 20, 2024
1 parent cd77b65 commit ae403a6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/lib/components/footer/main.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
<script>
/**
* @typedef {object} Props
* @property {string} apiVersion
*/
/** @type {Props} */
let { apiVersion } = $props();
const apiVersionWOSemicolon = $derived(apiVersion.replace(/\;$/, ''));
</script>

<footer class="h-[10lvh] w-full border-t-2 border-neutral-100 dark:border-neutral-700">
<div class="h-full flex place-content-center items-center">
<div class="h-full flex flex-col place-content-center items-center">
<a
class="text-hearchco-primary dark:text-hearchco-secondary font-bold hover:underline"
href="https://github.com/hearchco"
>
Source code
</a>
<p class="text-neutral-600 dark:text-neutral-300 font-light">
Agent version: {apiVersionWOSemicolon}
</p>
</div>
</footer>
37 changes: 37 additions & 0 deletions src/lib/functions/api/fetchversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { error } from '@sveltejs/kit';
import { createApiUrl } from '$lib/functions/api/createurl.js';

/**
* @param {typeof fetch} [fetcher]
* @returns {Promise<string>}
*/
export async function fetchVersion(fetcher = fetch) {
/** @type {URL} */
let apiUrl;
try {
apiUrl = createApiUrl('versionz');
} catch (/** @type {any} */ err) {
// Internal Server Error
throw error(500, `Failed to create API URL: ${err.message}`);
}

/** @type {Response} */
let response;
try {
response = await fetcher(apiUrl, {
method: 'GET', // POST doesn't cache on CDN
headers: {
Accept: 'text/plain',
'Accept-Encoding': 'gzip, deflate, br'
}
});
} catch (/** @type {any} */ err) {
// Bad Gateway
throw error(502, `Failed to fetch version: ${err.message}`);
}

/** @type {string} */
const version = await response.text();

return version;
}
9 changes: 9 additions & 0 deletions src/routes/+layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { fetchVersion } from '$lib/functions/api/fetchversion';

/** @type {import('./$types').LayoutLoad} */
export async function load({ fetch }) {
const apiVersion = await fetchVersion(fetch);
return {
apiVersion: apiVersion
};
}
5 changes: 4 additions & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import Preconnect from '$lib/components/preconnect/main.svelte';
import ThemeToggle from '$lib/components/themetoggle/main.svelte';
import Footer from '$lib/components/footer/main.svelte';
let { data } = $props();
const apiVersion = $derived(data.apiVersion);
</script>

<Preconnect />
Expand All @@ -15,4 +18,4 @@
<slot />
</main>

<Footer />
<Footer {apiVersion} />
1 change: 0 additions & 1 deletion src/routes/search/+page.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export async function load({ url, fetch }) {

return {
browser: browser,
apiVersion: resp.version,
query: queryWithoutCategory,
currentPage: currentPage,
maxPages: maxPages,
Expand Down
2 changes: 0 additions & 2 deletions src/routes/search/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
let { data } = $props();
const javascript = $derived(data.browser);
const query = $derived(data.query);
const title = $derived(
query === ''
Expand All @@ -18,7 +17,6 @@
? `${query} | Hearchco Search`
: `${query.slice(0, 15)}... | Hearchco Search`
);
const currentPage = $derived(data.currentPage);
// const maxPages = $derived(data.maxPages);
const category = $derived(data.category);
Expand Down

0 comments on commit ae403a6

Please sign in to comment.