Repository navigation
feat: paginate and search the guild directory #1600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up(): void | ||
| { | ||
| Schema::ensureExtensionExists('pg_trgm'); | ||
|
|
||
| Schema::table('guilds', function (Blueprint $table): void { | ||
| $table->rawIndex('translate(lower(handle COLLATE "pg_c_utf8"), \'ς\', \'σ\') gin_trgm_ops', 'guilds_lower_handle_trigram_index')->algorithm('gin'); | ||
| $table->rawIndex('translate(lower(name COLLATE "pg_c_utf8"), \'ς\', \'σ\') gin_trgm_ops', 'guilds_lower_name_trigram_index')->algorithm('gin'); | ||
| $table->rawIndex('translate(lower(description COLLATE "pg_c_utf8"), \'ς\', \'σ\') gin_trgm_ops', 'guilds_lower_description_trigram_index')->algorithm('gin'); | ||
| }); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { Head, Link, usePage } from '@inertiajs/react'; | ||
| import { Plus } from 'lucide-react'; | ||
| import { Form, Head, Link, usePage } from '@inertiajs/react'; | ||
| import { ChevronLeft, ChevronRight, Plus, Search } from 'lucide-react'; | ||
| import { useState } from 'react'; | ||
| import { show } from '@/actions/App/Http/Controllers/GuildController'; | ||
| import { index, show } from '@/actions/App/Http/Controllers/GuildController'; | ||
| import { GuildDialog } from '@/components/guild-dialog'; | ||
| import { GuildIcon } from '@/components/guild-icon'; | ||
| import { | ||
|
|
@@ -11,11 +11,25 @@ import { | |
| import { Button } from '@/components/ui/button'; | ||
| import AppLayout from '@/layouts/app-layout'; | ||
|
|
||
| export default function Index() { | ||
| export default function Index({ | ||
| query, | ||
| guildPage, | ||
| }: { | ||
| query: string; | ||
| guildPage: { | ||
| data: string[]; | ||
| current_page: number; | ||
| last_page: number; | ||
| total: number; | ||
| }; | ||
| }) { | ||
| const { guilds } = usePage().props; | ||
| const [order] = useState(() => guilds?.map((guild) => guild.id) ?? []); | ||
| const ranked = guilds?.toSorted( | ||
| (a, b) => order.indexOf(a.id) - order.indexOf(b.id), | ||
| const [search, setSearch] = useState({ query, value: query }); | ||
| if (search.query !== query) { | ||
| setSearch({ query, value: query }); | ||
| } | ||
| const ranked = guildPage.data.flatMap( | ||
| (id) => guilds?.find((guild) => guild.id === id) ?? [], | ||
|
Comment on lines
+31
to
+32
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in f9c5b014. History/cached navigation and persisted pageshow revalidate guildPage and guilds together on the directory; membership-only refreshes remain metadata-only. Browser checks confirmed Back picks up a guild created while away and prefetched navigation catches up IDs and totals. |
||
| ); | ||
|
|
||
| return ( | ||
|
|
@@ -27,6 +41,59 @@ export default function Index() { | |
| Public groups, biggest first. Join to see their commits in | ||
| Following. | ||
| </p> | ||
| <Form | ||
| {...index.form()} | ||
| options={{ | ||
| only: ['guildPage', 'query', 'guilds'], | ||
| preserveState: true, | ||
| preserveScroll: true, | ||
| }} | ||
| role="search" | ||
| className="mt-4" | ||
| > | ||
| {({ processing, errors }) => ( | ||
| <> | ||
| <div className="flex gap-2"> | ||
| <div className="relative min-w-0 flex-1"> | ||
| <Search | ||
| aria-hidden | ||
| className="pointer-events-none absolute top-1/2 left-3 size-4 -translate-y-1/2 text-muted-foreground" | ||
| /> | ||
| <input | ||
| type="search" | ||
| name="q" | ||
| value={search.value} | ||
| onChange={(event) => | ||
| setSearch({ | ||
| query, | ||
| value: event.target.value, | ||
| }) | ||
| } | ||
| maxLength={100} | ||
| aria-label="Search guilds" | ||
| aria-describedby="guild-search-help" | ||
| placeholder="Search guilds" | ||
| className="h-10 w-full rounded-md border bg-background pr-3 pl-9 text-base outline-none placeholder:text-muted-foreground focus-visible:border-ring md:text-sm" | ||
| /> | ||
| </div> | ||
| <Button | ||
| type="submit" | ||
| disabled={processing} | ||
| className="h-10 min-w-20" | ||
| > | ||
| Search | ||
| </Button> | ||
| </div> | ||
| <p | ||
| id="guild-search-help" | ||
| className="mt-2 min-h-5 text-xs text-muted-foreground" | ||
| > | ||
| {errors.q ?? | ||
| 'Search names, #handles, and descriptions.'} | ||
| </p> | ||
| </> | ||
| )} | ||
| </Form> | ||
| </header> | ||
| <div className="flex items-center justify-between gap-3 border-b px-4 py-3 sm:px-6"> | ||
| <p className="text-sm text-muted-foreground"> | ||
|
|
@@ -39,9 +106,94 @@ export default function Index() { | |
| </Button> | ||
| </GuildDialog> | ||
| </div> | ||
| {guilds?.length ? ( | ||
| <nav | ||
| aria-label="Guild pages" | ||
| className="flex h-14 items-center justify-between gap-2 border-b px-4 sm:px-6" | ||
| > | ||
| <p className="text-sm text-muted-foreground" aria-live="polite"> | ||
| <span className="tabular-nums">{guildPage.total}</span>{' '} | ||
| {guildPage.total === 1 ? 'guild' : 'guilds'} | ||
| </p> | ||
| <div className="flex items-center gap-2"> | ||
| {guildPage.current_page > 1 ? ( | ||
| <Button | ||
| asChild | ||
| variant="outline" | ||
| size="icon" | ||
| className="size-9" | ||
| > | ||
| <Link | ||
| prefetch | ||
| href={index({ | ||
| query: { | ||
| q: query, | ||
| page: guildPage.current_page - 1, | ||
| }, | ||
| })} | ||
| only={['guildPage', 'query', 'guilds']} | ||
| preserveState | ||
| preserveScroll | ||
| aria-label="Previous guild page" | ||
| > | ||
| <ChevronLeft aria-hidden className="size-4" /> | ||
| </Link> | ||
| </Button> | ||
| ) : ( | ||
| <Button | ||
| variant="outline" | ||
| size="icon" | ||
| className="size-9" | ||
| disabled | ||
| aria-label="Previous guild page" | ||
| > | ||
| <ChevronLeft aria-hidden className="size-4" /> | ||
| </Button> | ||
| )} | ||
| <span | ||
| className="w-24 text-center text-sm text-muted-foreground tabular-nums" | ||
| aria-live="polite" | ||
| > | ||
| Page {guildPage.current_page} of {guildPage.last_page} | ||
| </span> | ||
| {guildPage.current_page < guildPage.last_page ? ( | ||
| <Button | ||
| asChild | ||
| variant="outline" | ||
| size="icon" | ||
| className="size-9" | ||
| > | ||
| <Link | ||
| prefetch | ||
| href={index({ | ||
| query: { | ||
| q: query, | ||
| page: guildPage.current_page + 1, | ||
| }, | ||
| })} | ||
| only={['guildPage', 'query', 'guilds']} | ||
| preserveState | ||
| preserveScroll | ||
| aria-label="Next guild page" | ||
| > | ||
| <ChevronRight aria-hidden className="size-4" /> | ||
| </Link> | ||
| </Button> | ||
| ) : ( | ||
| <Button | ||
| variant="outline" | ||
| size="icon" | ||
| className="size-9" | ||
| disabled | ||
| aria-label="Next guild page" | ||
| > | ||
| <ChevronRight aria-hidden className="size-4" /> | ||
| </Button> | ||
| )} | ||
| </div> | ||
| </nav> | ||
| {ranked.length ? ( | ||
| <ul className="grid grid-cols-1 gap-3 p-4 min-[540px]:grid-cols-2 sm:p-6"> | ||
| {ranked?.map((guild) => ( | ||
| {ranked.map((guild) => ( | ||
| <li | ||
| key={guild.id} | ||
| className="flex min-w-0 flex-col gap-3 rounded-lg border bg-card p-4" | ||
|
|
@@ -83,7 +235,21 @@ export default function Index() { | |
| </ul> | ||
| ) : ( | ||
| <p className="p-6 text-sm text-muted-foreground"> | ||
| No guilds yet. Create the first one. | ||
| {query !== '' | ||
| ? 'No guilds found on this page. Try another keyword or return to the first page.' | ||
| : guildPage.current_page > 1 | ||
| ? 'No guilds on this page. Return to the first page.' | ||
| : 'No guilds yet. Create the first one.'} | ||
| {(query !== '' || guildPage.current_page > 1) && ( | ||
| <Link | ||
| prefetch | ||
| href={index()} | ||
| only={['guildPage', 'query', 'guilds']} | ||
| className="mt-2 block text-primary hover:underline" | ||
| > | ||
| Browse all guilds | ||
| </Link> | ||
| )} | ||
| </p> | ||
| )} | ||
| </> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cancel restore reloads when a later search or page visit supersedes them.
Restore
?q=rust, delay this reload, then submitlaraveland let that search finish first. Inertia 3.11.0 sends reloads asynchronously. A partial visit on the same path does not cancel them. (raw.githubusercontent.com) The reviewed lockfile selects that version. (raw.githubusercontent.com)The delayed reload passes Inertia's component/path guard and replaces
guildPagewith rust results whilequeryremainslaravel. The response guard does not compare query strings. (raw.githubusercontent.com)Retain each reload's cancellation token and cancel superseded restore work. Keep the reload active when no later visit supersedes it.
Based on learnings, older async work must not overwrite newer state.
Also applies to: 63-63
Source: Learnings