4e91c2acdf
git-subtree-dir: sample-front-end git-subtree-split: 0881bf1c045118585100360b2c47594cd94b89f1
128 lines
3.9 KiB
TypeScript
128 lines
3.9 KiB
TypeScript
|
|
import { Playlist, ServerType, ApiResponse, PlexServerConnection, PlexConnectionSettings, PlexLibrary } from '../types';
|
|
import { MOCK_LOCAL_PLAYLISTS, MOCK_CLOUD_PLAYLISTS } from './mockData';
|
|
|
|
const SIMULATE_DELAY_MS = 800;
|
|
|
|
// Mock available libraries on a server
|
|
const MOCK_LIBRARIES: PlexLibrary[] = [
|
|
{ id: 'lib1', title: 'Music (Flac)', type: 'artist' },
|
|
{ id: 'lib2', title: 'MP3 Collection', type: 'artist' },
|
|
{ id: 'lib3', title: 'Soundtracks', type: 'artist' },
|
|
{ id: 'lib4', title: 'Audiobooks', type: 'artist' }
|
|
];
|
|
|
|
// Helper to simulate network request or call actual API
|
|
const fetchPlaylists = async (type: ServerType): Promise<Playlist[]> => {
|
|
// In a real Docker environment with FastAPI, you would do:
|
|
// const response = await fetch(`/api/playlists/${type.toLowerCase()}`);
|
|
// const data = await response.json();
|
|
// return data;
|
|
|
|
// Mocking for UI demonstration
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
if (type === ServerType.LOCAL) {
|
|
resolve([...MOCK_LOCAL_PLAYLISTS]);
|
|
} else {
|
|
resolve([...MOCK_CLOUD_PLAYLISTS]);
|
|
}
|
|
}, SIMULATE_DELAY_MS);
|
|
});
|
|
};
|
|
|
|
const fetchServerStatus = async (): Promise<PlexServerConnection> => {
|
|
// Mocking server status
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
// 90% chance of success for demo
|
|
const isSuccess = Math.random() > 0.1;
|
|
if (isSuccess) {
|
|
resolve({
|
|
isConnected: true,
|
|
name: 'Home Media Server',
|
|
ip: '192.168.1.105',
|
|
port: 32400,
|
|
libraryName: 'Music (Flac)'
|
|
});
|
|
} else {
|
|
resolve({
|
|
isConnected: false
|
|
});
|
|
}
|
|
}, SIMULATE_DELAY_MS);
|
|
});
|
|
};
|
|
|
|
const authenticatePlex = async (settings: PlexConnectionSettings): Promise<{ token: string, serverInfo: PlexServerConnection }> => {
|
|
return new Promise((resolve, reject) => {
|
|
setTimeout(() => {
|
|
// Simulate validation
|
|
if (!settings.address) {
|
|
reject(new Error("Server address is required"));
|
|
return;
|
|
}
|
|
|
|
// If user provided username/password, mock a token generation
|
|
let token = settings.token;
|
|
if (!token && settings.username && settings.password) {
|
|
token = "MOCK_TOKEN_XYZ_999";
|
|
} else if (!token) {
|
|
reject(new Error("Token or Username/Password required"));
|
|
return;
|
|
}
|
|
|
|
// Success response with libraries
|
|
resolve({
|
|
token: token,
|
|
serverInfo: {
|
|
isConnected: true,
|
|
name: 'My Plex Server',
|
|
ip: settings.address,
|
|
port: parseInt(settings.port) || 32400,
|
|
libraryName: MOCK_LIBRARIES[0].title, // Default to first library
|
|
libraries: MOCK_LIBRARIES
|
|
}
|
|
});
|
|
}, 1500);
|
|
});
|
|
}
|
|
|
|
export const apiService = {
|
|
getPlaylists: async (serverType: ServerType): Promise<ApiResponse<Playlist[]>> => {
|
|
try {
|
|
const data = await fetchPlaylists(serverType);
|
|
return { data, status: 'success' };
|
|
} catch (error) {
|
|
console.error(`Error fetching ${serverType} playlists:`, error);
|
|
return { data: [], status: 'error', message: 'Failed to fetch playlists' };
|
|
}
|
|
},
|
|
|
|
getServerStatus: async (): Promise<ApiResponse<PlexServerConnection>> => {
|
|
try {
|
|
const data = await fetchServerStatus();
|
|
return { data, status: 'success' };
|
|
} catch (error) {
|
|
return {
|
|
data: { isConnected: false },
|
|
status: 'error',
|
|
message: 'Failed to connect to server'
|
|
};
|
|
}
|
|
},
|
|
|
|
connectToPlex: async (settings: PlexConnectionSettings): Promise<ApiResponse<{ token: string, serverInfo: PlexServerConnection }>> => {
|
|
try {
|
|
const data = await authenticatePlex(settings);
|
|
return { data, status: 'success', message: 'Connected successfully' };
|
|
} catch (error: any) {
|
|
return {
|
|
data: { token: '', serverInfo: { isConnected: false } },
|
|
status: 'error',
|
|
message: error.message || 'Connection failed'
|
|
};
|
|
}
|
|
}
|
|
};
|