Skip to content

refactor(console): Fragmentize sidebar and co-locate scripts with templates#52

Merged
mojatter merged 2 commits intomainfrom
refactor/console-htmx
Apr 10, 2026
Merged

refactor(console): Fragmentize sidebar and co-locate scripts with templates#52
mojatter merged 2 commits intomainfrom
refactor/console-htmx

Conversation

@mojatter
Copy link
Copy Markdown
Owner

@mojatter mojatter commented Apr 10, 2026

Summary

  • Extract the sidebar bucket list and empty state into reusable sub-templates (buckets/list.html, empty.html), included via {{template}} in index.html.
  • Bucket create/delete now return htmx fragments instead of full-page redirects, so the sidebar updates in-place.
  • Introduce a {{define "scripts:<name>"}} convention that keeps JavaScript co-located with its template file while preventing re-execution on htmx swaps. A new GET /scripts endpoint renders all registered script blocks once at page load.
  • Add RegisterTemplateWithScripts to register both a template and its scripts block in one call.

Changes

Sidebar fragmentization

  • handleCreateBucket returns the list fragment directly (was: 302 → /).
  • handleDeleteBucket returns the list fragment + OOB swap to reset #main-content (was: HX-Redirect: /).
  • handleObjects redirects non-htmx requests to / instead of duplicating Buckets.Names() + index.html render.

Script co-location pattern

  • Fragment-owned functions (openPreview, setViewMode, initGalleryView, …) moved from index.html into a {{define "scripts:buckets/objects"}} block at the bottom of objects.html.
  • index.html loads them once via <div hx-get="/scripts" hx-trigger="load">.
  • htmx:afterSettle listener stays in index.html (layout) to avoid duplicate registration.

Test plan

  • go test ./... passes
  • golangci-lint run ./... clean
  • Manual: create/delete bucket updates sidebar without full reload
  • Manual: preview modal works after navigating between buckets
  • Manual: gallery/list view toggle persists across navigation

… fragment

Move preview content generation from client-side JavaScript DOM
construction to a server-side Go template (buckets/preview.html).
The new GET /buckets/{name}/preview/{object...} endpoint returns an
HTML fragment with the appropriate media element and metadata table,
loaded via htmx.ajax(). This removes ~50 lines of JS (formatBytes,
escapeHtml, file-type classification, fetch-based DOM building) while
keeping client-only concerns (modal show/hide, view toggle, gallery
lazy load) in JavaScript.
@mojatter mojatter force-pushed the refactor/console-htmx branch from dbe1ba6 to 07289ab Compare April 10, 2026 10:33
…-page reloads

Extract the sidebar bucket list and empty state into reusable
sub-templates (buckets/list.html, empty.html) included via
{{template}} in index.html.

- handleCreateBucket now returns the list fragment directly instead
  of redirecting to /, so the sidebar updates in-place via htmx.
- handleDeleteBucket returns the list fragment plus an OOB swap that
  resets #main-content to the empty state.
- handleObjects no longer renders index.html for non-htmx requests;
  it redirects to / instead, removing the duplicated Buckets.Names()
  call and the Buckets field from the objects data struct.

Move fragment-owned scripts into {{define "scripts:<name>"}} blocks
inside each template file. A new GET /scripts endpoint renders all
registered script blocks once at page load via hx-trigger="load".
This keeps scripts co-located with their HTML while preventing
re-execution on htmx swaps.

RegisterTemplateWithScripts registers both the template and its
scripts block in one call, eliminating dual-maintenance.
@mojatter mojatter force-pushed the refactor/console-htmx branch from 07289ab to aca8cd8 Compare April 10, 2026 10:35
@mojatter mojatter merged commit 85ca07a into main Apr 10, 2026
2 checks passed
@mojatter mojatter deleted the refactor/console-htmx branch April 10, 2026 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant