Fix: Restore library stats functionality and remove debug logging

- Fix usePlexLibraries composable to return stats and details
  - Updated loadLibraries signature to match PlexSettings usage
  - Now accepts: sections, authToken, serverUrl, username, fetchFn
  - Returns: { stats, details } object instead of updating refs
  - Added watchtime calculation from Tautulli API

- Update processLibrarySection to work with passed parameters
  - Accept stats and details objects instead of using refs
  - Accept serverUrl and fetchLibraryDetailsFn as parameters
  - No longer depends on composable internal state

- Remove all debug console.log statements
  - Clean up usePlexAuth composable (removed 13 debug logs)
  - Clean up PlexSettings component (removed 9 debug logs)
  - Keep only error logging for troubleshooting

Library stats now display correctly after authentication ✓
Build size reduced by removing debug code
This commit is contained in:
2026-02-27 17:58:38 +01:00
parent 15b6c571d0
commit 5e73b73783
3 changed files with 93 additions and 98 deletions

View File

@@ -1,5 +1,3 @@
import { ref } from "vue";
import { usePlexApi } from "./usePlexApi";
import {
processLibraryItem,
calculateGenreStats,
@@ -7,88 +5,133 @@ import {
} from "@/utils/plexHelpers";
export function usePlexLibraries() {
const { plexServerUrl, fetchLibrarySections, fetchLibraryDetails } =
usePlexApi();
const loading = ref(false);
const libraryStats = ref({
movies: 0,
shows: 0,
music: 0,
watchtime: 0
});
const libraryDetails = ref<any>({
movies: {
total: 0,
recentlyAdded: [],
genres: [],
totalDuration: "0 hours"
},
shows: {
total: 0,
recentlyAdded: [],
genres: [],
totalEpisodes: 0,
totalDuration: "0 hours"
},
music: { total: 0, recentlyAdded: [], genres: [], totalTracks: 0 }
});
async function loadLibraries(authToken: string) {
loading.value = true;
async function loadLibraries(
sections: any[],
authToken: string,
serverUrl: string,
username: string,
fetchLibraryDetailsFn: any
) {
// Reset stats
libraryStats.value = { movies: 0, shows: 0, music: 0, watchtime: 0 };
const stats = { movies: 0, shows: 0, music: 0, watchtime: 0 };
const details: any = {
movies: {
total: 0,
recentlyAdded: [],
genres: [],
totalDuration: "0 hours"
},
shows: {
total: 0,
recentlyAdded: [],
genres: [],
totalEpisodes: 0,
totalDuration: "0 hours"
},
music: { total: 0, recentlyAdded: [], genres: [], totalTracks: 0 }
};
try {
const sections = await fetchLibrarySections(authToken);
for (const section of sections) {
const type = section.type;
const key = section.key;
if (type === "movie") {
await processLibrarySection(authToken, key, "movies");
await processLibrarySection(
authToken,
serverUrl,
key,
"movies",
stats,
details,
fetchLibraryDetailsFn
);
} else if (type === "show") {
await processLibrarySection(authToken, key, "shows");
await processLibrarySection(
authToken,
serverUrl,
key,
"shows",
stats,
details,
fetchLibraryDetailsFn
);
} else if (type === "artist") {
await processLibrarySection(authToken, key, "music");
await processLibrarySection(
authToken,
serverUrl,
key,
"music",
stats,
details,
fetchLibraryDetailsFn
);
}
}
// Calculate watchtime from Tautulli if username provided
if (username) {
try {
const TAUTULLI_API_KEY = "28494032b47542278fe76c6ccd1f0619";
const TAUTULLI_BASE_URL = "http://plex.schleppe:8181/api/v2";
const url = `${TAUTULLI_BASE_URL}?apikey=${TAUTULLI_API_KEY}&cmd=get_history&user=${encodeURIComponent(
username
)}&length=8000`;
const response = await fetch(url);
if (response.ok) {
const data = await response.json();
const history = data.response?.data?.data || [];
const totalMs = history.reduce(
(sum: number, item: any) => sum + (item.duration || 0) * 1000,
0
);
stats.watchtime = Math.round(totalMs / (1000 * 60 * 60));
}
} catch (error) {
console.error("[PlexLibraries] Error fetching watchtime:", error);
}
}
return { stats, details };
} catch (error) {
console.error("[PlexLibraries] Error loading libraries:", error);
throw error;
} finally {
loading.value = false;
}
}
async function processLibrarySection(
authToken: string,
serverUrl: string,
sectionKey: string,
libraryType: string
libraryType: string,
stats: any,
details: any,
fetchLibraryDetailsFn: any
) {
try {
const data = await fetchLibraryDetails(authToken, sectionKey);
const data = await fetchLibraryDetailsFn(
authToken,
serverUrl,
sectionKey
);
if (!data) return;
const totalCount = data.all.MediaContainer?.size || 0;
// Update stats
if (libraryType === "movies") {
libraryStats.value.movies += totalCount;
stats.movies += totalCount;
} else if (libraryType === "shows") {
libraryStats.value.shows += totalCount;
stats.shows += totalCount;
} else if (libraryType === "music") {
libraryStats.value.music += totalCount;
stats.music += totalCount;
}
// Process recently added items
const recentItems = data.recentMetadata
.slice(0, 5)
.map((item: any) =>
processLibraryItem(item, libraryType, authToken, plexServerUrl.value)
processLibraryItem(item, libraryType, authToken, serverUrl)
);
// Calculate stats
@@ -96,7 +139,7 @@ export function usePlexLibraries() {
const durations = calculateDuration(data.metadata, libraryType);
// Update library details
libraryDetails.value[libraryType] = {
details[libraryType] = {
total: totalCount,
recentlyAdded: recentItems,
genres,
@@ -112,9 +155,6 @@ export function usePlexLibraries() {
}
return {
loading,
libraryStats,
libraryDetails,
loadLibraries
};
}