Enhance API and frontend components for character management and map visibility

- Updated API documentation to include `ownedByMe` field in character responses, indicating if a character was last updated by the current user's tokens.
- Modified MapView component to track and display the live status based on user-owned characters.
- Enhanced map data handling to exclude hidden maps for non-admin users and improved character icon representation on the map.
- Refactored character data structures to support new properties and ensure accurate rendering in the frontend.
This commit is contained in:
2026-03-01 17:21:15 +03:00
parent 6a6977ddff
commit 7bdaa6bfcc
9 changed files with 82 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
package handlers
import (
"context"
"encoding/json"
"io"
"log/slog"
@@ -26,7 +27,8 @@ func (h *Handlers) ClientRouter(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusUnauthorized)
return
}
_ = username
ctx = context.WithValue(ctx, app.ClientUsernameKey, username)
req = req.WithContext(ctx)
switch matches[2] {
case "locate":

View File

@@ -2,6 +2,7 @@ package handlers
import (
"net/http"
"time"
"github.com/andyleap/hnh-map/internal/app"
)
@@ -22,6 +23,17 @@ func (h *Handlers) APIConfig(rw http.ResponseWriter, req *http.Request) {
JSON(rw, http.StatusOK, config)
}
// CharacterResponse is the API shape for a character, including ownedByMe for the current session.
type CharacterResponse struct {
Name string `json:"name"`
ID int `json:"id"`
Map int `json:"map"`
Position app.Position `json:"position"`
Type string `json:"type"`
Updated time.Time `json:"updated,omitempty"`
OwnedByMe bool `json:"ownedByMe"`
}
// APIGetChars handles GET /map/api/v1/characters.
func (h *Handlers) APIGetChars(rw http.ResponseWriter, req *http.Request) {
ctx := req.Context()
@@ -35,7 +47,19 @@ func (h *Handlers) APIGetChars(rw http.ResponseWriter, req *http.Request) {
return
}
chars := h.Map.GetCharacters()
JSON(rw, http.StatusOK, chars)
out := make([]CharacterResponse, 0, len(chars))
for _, c := range chars {
out = append(out, CharacterResponse{
Name: c.Name,
ID: c.ID,
Map: c.Map,
Position: c.Position,
Type: c.Type,
Updated: c.Updated.UTC(),
OwnedByMe: c.Username == s.Username,
})
}
JSON(rw, http.StatusOK, out)
}
// APIGetMarkers handles GET /map/api/v1/markers.