jellytau/src/lib/api/conversions.ts

103 lines
3.3 KiB
TypeScript

/**
* Rust-based conversion utilities via Tauri commands
*
* These functions provide centralized conversion logic in Rust.
* All conversions are handled by the Rust backend for consistency.
*/
import { invoke } from '@tauri-apps/api/core';
/**
* Format time in seconds to MM:SS display string
* @param seconds - Time in seconds
* @returns Formatted string like "3:45" or "12:09"
*/
export async function formatTime(seconds: number): Promise<string> {
return invoke('format_time_seconds', { seconds });
}
/**
* Format time in seconds to HH:MM:SS or MM:SS display string
* Automatically chooses format based on duration
* @param seconds - Time in seconds
* @returns Formatted string like "1:23:45" or "3:45"
*/
export async function formatTimeLong(seconds: number): Promise<string> {
return invoke('format_time_seconds_long', { seconds });
}
/**
* Convert Jellyfin ticks to seconds
* @param ticks - Time in Jellyfin ticks (10,000,000 ticks = 1 second)
* @returns Time in seconds
*/
export async function ticksToSeconds(ticks: number): Promise<number> {
return invoke('convert_ticks_to_seconds', { ticks });
}
/**
* Calculate progress percentage from position and duration
* @param position - Current position in seconds
* @param duration - Total duration in seconds
* @returns Progress as percentage (0.0 to 100.0)
*/
export async function calculateProgress(position: number, duration: number): Promise<number> {
return invoke('calc_progress', { position, duration });
}
/**
* Convert percentage volume (0-100) to normalized (0.0-1.0)
* @param percent - Volume as percentage (0 to 100)
* @returns Normalized volume (0.0 to 1.0)
*/
export async function percentToVolume(percent: number): Promise<number> {
return invoke('convert_percent_to_volume', { percent });
}
/**
* Synchronous time formatting utilities for high-frequency UI updates
*
* These are kept in TypeScript for performance reasons when formatting
* needs to happen frequently (e.g., position updates every 100ms).
*
* For less frequent conversions, prefer the async Rust-based functions above.
*/
/**
* Format time in seconds to MM:SS display string (synchronous)
* @param seconds - Time in seconds
* @returns Formatted string like "3:45" or "12:09"
*/
export function formatTimeSync(seconds: number): string {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs.toString().padStart(2, '0')}`;
}
/**
* Format time in seconds to HH:MM:SS or MM:SS display string (synchronous)
* @param seconds - Time in seconds
* @returns Formatted string like "1:23:45" or "3:45"
*/
export function formatTimeLongSync(seconds: number): string {
const hours = Math.floor(seconds / 3600);
const mins = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
if (hours > 0) {
return `${hours}:${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
return `${mins}:${secs.toString().padStart(2, '0')}`;
}
/**
* Calculate progress percentage (synchronous)
* @param position - Current position in seconds
* @param duration - Total duration in seconds
* @returns Progress as percentage (0 to 100)
*/
export function calculateProgressSync(position: number, duration: number): number {
if (duration <= 0) return 0;
return Math.min(100, Math.max(0, (position / duration) * 100));
}