diff --git a/cmd/server/main.go b/cmd/server/main.go index 94715fb..11d8736 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -339,20 +339,41 @@ func main() { // Bookshelf route (protected) - new default for logged-in users protected.GET("/bookshelf", func(c echo.Context) error { - userID := c.Get("user_id").(string) - userEmail := c.Get("user_email").(string) - userUsername := c.Get("user_username").(string) - userRole := c.Get("user_role").(string) + user := c.Get("user").(database.Users) + userUUID := uuid.UUID(user.ID.Bytes) - user := templates.User{ - ID: userID, - Email: userEmail, - Username: userUsername, - Role: userRole, + // Fetch libraries server-side for SSR + librariesData, err := libraryHandler.GetUserVisibleLibrariesData(c.Request().Context(), pgtype.UUID{Bytes: userUUID, Valid: true}) + if err != nil { + return c.HTML(http.StatusInternalServerError, "Error loading libraries") } + // Convert to template format + libraries := make([]templates.LibraryData, len(librariesData)) + for i, lib := range librariesData { + libUUID := uuid.UUID(lib.ID.Bytes) + description := "" + if lib.Description.Valid { + description = lib.Description.String + } + libraries[i] = templates.LibraryData{ + ID: libUUID.String(), + Name: lib.Name, + Description: description, + TypeName: lib.TypeName, + } + } + + userTemplate := templates.User{ + ID: userUUID.String(), + Email: user.Email, + Username: user.Username, + Role: user.Role, + } + + // Render template WITH libraries data (SSR) var buf bytes.Buffer - err := templates.BookShelf(user).Render(c.Request().Context(), &buf) + err = templates.BookShelf(userTemplate, libraries).Render(c.Request().Context(), &buf) if err != nil { return err } diff --git a/templates/bookshelf.templ b/templates/bookshelf.templ index 82e1f74..282545a 100644 --- a/templates/bookshelf.templ +++ b/templates/bookshelf.templ @@ -1,6 +1,6 @@ package templates -templ BookShelf(user User) { +templ BookShelf(user User, libraries []LibraryData) { @@ -15,14 +15,20 @@ templ BookShelf(user User) { @Header(user, "/bookshelf") -
+
@@ -57,21 +63,43 @@ templ BookShelf(user User) { ") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "

Loading your library...

📚

No Books Yet

Select a library to view your collection

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/templates/types.go b/templates/types.go index 9e635fb..04b5a3d 100644 --- a/templates/types.go +++ b/templates/types.go @@ -38,6 +38,13 @@ type BookData struct { CoverImagePath string } +type LibraryData struct { + ID string + Name string + Description string + TypeName string +} + type DeviceData struct { ID string DeviceName string