diff --git a/astro.config.mjs b/astro.config.mjs index 60f8b8a..51933e2 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -19,9 +19,9 @@ export default defineConfig({ }, }, integrations: [preact(), mdx()], - adapter: node({ - mode: 'standalone', - }), + // adapter: node({ + // mode: 'standalone', + // }), output: 'hybrid', outDir: './out/astro', build: { diff --git a/bun.lockb b/bun.lockb index 4e290a1..da3686a 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index bfbe175..d928716 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "astro": "^4.15.11", "better-sqlite3": "^9.4.3", "drizzle-orm": "^0.29.4", + "fuse.js": "^7.0.0", "katex": "^0.16.9", "preact": "^10.19.6", "typescript": "^5.3.3" diff --git a/src/client/Paginate.tsx b/src/client/Paginate.tsx new file mode 100644 index 0000000..993a471 --- /dev/null +++ b/src/client/Paginate.tsx @@ -0,0 +1,31 @@ +import { useComputed, useSignal, type ReadonlySignal } from '@preact/signals' +import type { JSX } from 'preact/jsx-runtime' + +export const ShowMore = ({ + items, + pageSize, + children, +}: { + items: ReadonlySignal + pageSize: number + children: (item: T) => JSX.Element +}) => { + const $shownItems = useSignal(pageSize) + + const $paginatedItems = useComputed(() => { + return items.value.slice(0, $shownItems.value) + }) + + console.log($paginatedItems.value) + + return ( + <> + {$paginatedItems.value.map(children)} +
+ {$shownItems.value < items.value.length && ( + + )} +
+ + ) +} diff --git a/src/client/UtentiPage.tsx b/src/client/UtentiPage.tsx index ebf2e2c..9681719 100644 --- a/src/client/UtentiPage.tsx +++ b/src/client/UtentiPage.tsx @@ -1,5 +1,8 @@ -import { useSignal } from '@preact/signals' +import { useComputed, useSignal } from '@preact/signals' +import Fuse from 'fuse.js' import { useEffect } from 'preact/hooks' +import { trottleDebounce } from './lib/util' +import { ShowMore } from './Paginate' type User = { uid: string @@ -20,37 +23,63 @@ function applyPatches(users: User[]) { } export const UtentiPage = () => { - const utentiSignal = useSignal([]) + const $utentiData = useSignal([]) + const $fuse = useSignal | null>(null) + + const $searchText = useSignal('') + const $searchResults = useComputed(() => + $searchText.value.trim().length > 0 + ? ($fuse.value?.search($searchText.value).map(result => result.item) ?? []) + : $utentiData.value, + ) useEffect(() => { fetch('https://poisson.phc.dm.unipi.it/users.json') .then(response => response.json()) .then(data => { applyPatches(data) - utentiSignal.value = data + + $utentiData.value = data + $fuse.value = new Fuse(data, { + keys: ['gecos'], + }) }) }, []) return ( <>
- {utentiSignal.value.map(user => ( -
-
- person -
-
{user.gecos}
- -
- ))} + {$searchResults.value ? ( + + {poissonUser => ( + console.log(poissonUser), + ( +
+
+ person +
+
{poissonUser.gecos}
+ +
+ ) + )} +
+ ) : ( + <>Nessun risultato + )}
) diff --git a/src/client/lib/hooks.tsx b/src/client/lib/hooks.tsx new file mode 100644 index 0000000..e69de29 diff --git a/src/client/lib/util.ts b/src/client/lib/util.ts new file mode 100644 index 0000000..94997ee --- /dev/null +++ b/src/client/lib/util.ts @@ -0,0 +1,34 @@ +export const trottleDebounce = ( + fn: (...args: T) => R, + delay: number, + options: { leading?: boolean; trailing?: boolean } = {}, +): ((...args: T) => R | undefined) => { + let lastCall = 0 + let lastResult: R | undefined + let lastArgs: T | undefined + let timeout: NodeJS.Timeout | undefined + + const leading = options.leading ?? true + const trailing = options.trailing ?? true + + return (...args: T): R | undefined => { + lastArgs = args + if (leading && Date.now() - lastCall >= delay) { + lastCall = Date.now() + lastResult = fn(...args) + } else { + if (timeout) { + clearTimeout(timeout) + } + timeout = setTimeout(() => { + if (trailing && lastArgs) { + lastCall = Date.now() + lastResult = fn(...lastArgs) + } + }, delay) + } + + console.log(lastResult) + return lastResult + } +} diff --git a/src/content/meta/whats-phc.md b/src/content/meta/whats-phc.md index e60aa17..b936413 100644 --- a/src/content/meta/whats-phc.md +++ b/src/content/meta/whats-phc.md @@ -1,4 +1,5 @@ --- +# Questo documento è utilizzato nella homepage del sito nella card principale title: Cos'è il PHC? --- diff --git a/src/styles/components.scss b/src/styles/components.scss index 5f4a444..73431bb 100644 --- a/src/styles/components.scss +++ b/src/styles/components.scss @@ -594,4 +594,8 @@ max-width: 25rem; } } + + .show-more { + place-self: center; + } } diff --git a/src/styles/pages.scss b/src/styles/pages.scss index ebbd4da..1267f5c 100644 --- a/src/styles/pages.scss +++ b/src/styles/pages.scss @@ -239,6 +239,8 @@ } .utenti { + background: #ffffe4; + main { justify-self: center; @@ -250,6 +252,14 @@ padding: 5rem 0; gap: 5rem; + + .search-result { + background: #ffeabc; + } + + button { + background: #ffd270; + } } }