A deployable markdown editor that connects with your self hosted files and lets you edit in a beautiful interface

Fix last_repo persistence: fallback to user_repos table and add debug logging

Backend:
- Add GetMostRecentUserRepo query to get most recent repo from user_repos
- Update GetCurrentUser to check user_repos if users.last_repo is empty
- Add debug logging to track last_repo flow

Frontend:
- Add debug logging to Dashboard useEffect to track repo detection

This ensures users are redirected to their previously selected repo after login,
even if users.last_repo is empty but user_repos has entries.

+63 -2
+26 -1
backend/internal/api/handlers/auth.go
··· 231 231 return 232 232 } 233 233 234 + log.Printf("[DEBUG] GetCurrentUser: userID=%d, last_repo from users table='%s'", userID, user.LastRepo) 235 + 236 + lastRepo := user.LastRepo 237 + 238 + // If last_repo is empty, try to get the most recent repo from user_repos table 239 + if lastRepo == "" { 240 + log.Printf("[DEBUG] last_repo empty, checking user_repos table") 241 + mostRecentRepo, err := h.db.GetMostRecentUserRepo(userID) 242 + if err != nil { 243 + log.Printf("[DEBUG] Error getting most recent repo: %v", err) 244 + } else if mostRecentRepo != nil { 245 + lastRepo = mostRecentRepo.RepoName 246 + log.Printf("[DEBUG] Found repo in user_repos table: %s", lastRepo) 247 + 248 + // Also update the users table for next time 249 + if err := h.db.UpdateUserRepo(userID, lastRepo); err != nil { 250 + log.Printf("[DEBUG] Failed to update users.last_repo: %v", err) 251 + } 252 + } else { 253 + log.Printf("[DEBUG] No repos found in user_repos table") 254 + } 255 + } 256 + 234 257 response := UserResponse{ 235 258 ID: user.ID, 236 259 Username: user.Username, 237 260 AvatarURL: user.AvatarURL, 238 261 Provider: "github", 239 - LastRepo: user.LastRepo, 262 + LastRepo: lastRepo, 240 263 } 264 + 265 + log.Printf("[DEBUG] GetCurrentUser response: ID=%d, LastRepo='%s'", response.ID, response.LastRepo) 241 266 242 267 w.Header().Set("Content-Type", "application/json") 243 268 json.NewEncoder(w).Encode(response)
+26
backend/internal/database/queries.go
··· 334 334 return repos, nil 335 335 } 336 336 337 + // GetMostRecentUserRepo gets the most recently used repo from user_repos table 338 + func (db *DB) GetMostRecentUserRepo(userID int) (*UserRepo, error) { 339 + query := ` 340 + SELECT id, user_id, repo_name, repo_link, last_used_at, created_at, updated_at 341 + FROM user_repos 342 + WHERE user_id = ? 343 + ORDER BY last_used_at DESC 344 + LIMIT 1 345 + ` 346 + 347 + var repo UserRepo 348 + err := db.QueryRow(query, userID).Scan( 349 + &repo.ID, &repo.UserID, &repo.RepoName, &repo.RepoLink, 350 + &repo.LastUsedAt, &repo.CreatedAt, &repo.UpdatedAt, 351 + ) 352 + 353 + if err == sql.ErrNoRows { 354 + return nil, nil 355 + } 356 + if err != nil { 357 + return nil, fmt.Errorf("failed to get most recent user repo: %w", err) 358 + } 359 + 360 + return &repo, nil 361 + } 362 + 337 363 func (db *DB) DeleteBranchState(userID int, repoFullName string) error { 338 364 query := ` 339 365 DELETE FROM branch_states
+11 -1
frontend/src/components/dashboard/DashboardApp.tsx
··· 47 47 48 48 // Check for last_repo or URL params and redirect if needed 49 49 useEffect(() => { 50 - if (userLoading || !user) return; 50 + console.log('[DEBUG] Dashboard useEffect running, userLoading:', userLoading, 'user:', user); 51 + 52 + if (userLoading || !user) { 53 + console.log('[DEBUG] Still loading user or no user, returning early'); 54 + return; 55 + } 51 56 52 57 // Check URL params first 53 58 const urlParams = new URLSearchParams(window.location.search); 54 59 const repoParam = urlParams.get('repo'); 60 + console.log('[DEBUG] URL repo param:', repoParam); 55 61 56 62 if (repoParam) { 57 63 // Parse repo from URL (format: owner/repo) 58 64 const [owner, repo] = repoParam.split('/'); 59 65 if (owner && repo) { 66 + console.log('[DEBUG] Setting repo from URL param:', owner, repo); 60 67 setRepoConfig({ owner, repo, folder: '' }); 61 68 return; 62 69 } 63 70 } 64 71 65 72 // Check user's last_repo 73 + console.log('[DEBUG] Checking user.last_repo:', user.last_repo); 66 74 if (user.last_repo) { 67 75 const [owner, repo] = user.last_repo.split('/'); 68 76 if (owner && repo) { 77 + console.log('[DEBUG] Setting repo from user.last_repo:', owner, repo); 69 78 setRepoConfig({ owner, repo, folder: '' }); 70 79 return; 71 80 } 72 81 } 73 82 74 83 // No repo configured, redirect to select-repo page 84 + console.log('[DEBUG] No repo found, redirecting to /select-repo'); 75 85 window.location.href = '/select-repo'; 76 86 }, [user, userLoading]); 77 87