mirror of
https://github.com/litruv/AudioSort.git
synced 2026-07-24 10:46:03 +10:00
chore: update dependencies and add patch-package for better-sqlite3
feat: implement just-split category filter and enhance metadata parsing fix: improve type imports and variable handling in services
This commit is contained in:
@@ -12,6 +12,15 @@ import { SearchService } from './SearchService';
|
||||
import { WaveFile } from 'wavefile';
|
||||
import { OrganizationService } from './OrganizationService';
|
||||
|
||||
type MusicMetadataParser = (path: string, options?: { duration?: boolean }) => Promise<{
|
||||
format: { duration?: number | null; sampleRate?: number | null; bitsPerSample?: number | null };
|
||||
common: { comment?: unknown[]; genre?: unknown[]; subtitle?: unknown };
|
||||
}>;
|
||||
|
||||
type MusicMetadataNamespace = Partial<{ parseFile: MusicMetadataParser }> & {
|
||||
default?: Partial<{ parseFile: MusicMetadataParser }>;
|
||||
};
|
||||
|
||||
interface CsvCategoryRow {
|
||||
Category: string;
|
||||
SubCategory: string;
|
||||
@@ -32,6 +41,7 @@ export class LibraryService {
|
||||
private metadataSuggestionCache: { authors: Set<string> } | null = null;
|
||||
private readonly waveformPreviewCache = new Map<number, { modifiedAt: number; pointCount: number; samples: number[]; rms: number }>();
|
||||
private offlineAudioContextCtor: (new (channelCount: number, length: number, sampleRate: number) => any) | null = null;
|
||||
private musicMetadataParseFile: MusicMetadataParser | null = null;
|
||||
public constructor(
|
||||
private readonly database: DatabaseService,
|
||||
private readonly settings: SettingsService,
|
||||
@@ -1342,6 +1352,29 @@ export class LibraryService {
|
||||
return value.replace(/\\/g, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves and caches the music-metadata parseFile implementation.
|
||||
*/
|
||||
private async resolveMusicMetadataParser(): Promise<MusicMetadataParser> {
|
||||
if (this.musicMetadataParseFile) {
|
||||
return this.musicMetadataParseFile;
|
||||
}
|
||||
|
||||
const namespace = (await import('music-metadata')) as MusicMetadataNamespace;
|
||||
const candidate = typeof namespace.parseFile === 'function'
|
||||
? namespace.parseFile
|
||||
: namespace.default && typeof namespace.default.parseFile === 'function'
|
||||
? namespace.default.parseFile
|
||||
: null;
|
||||
|
||||
if (!candidate) {
|
||||
throw new Error('music-metadata parseFile not found');
|
||||
}
|
||||
|
||||
this.musicMetadataParseFile = candidate;
|
||||
return candidate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts metadata from the WAV container, falling back to defaults when parsing fails.
|
||||
*/
|
||||
@@ -1353,22 +1386,8 @@ export class LibraryService {
|
||||
categories: string[];
|
||||
}> {
|
||||
try {
|
||||
const mmImport = await import('music-metadata');
|
||||
const mm = (mmImport as { default?: unknown }).default ?? mmImport;
|
||||
|
||||
const loadMusicMetadata = typeof mm === 'object' && mm !== null && 'loadMusicMetadata' in mm
|
||||
? (mm as { loadMusicMetadata: () => Promise<{ parseFile: (path: string, opts?: { duration?: boolean }) => Promise<{
|
||||
format: { duration?: number | null; sampleRate?: number | null; bitsPerSample?: number | null };
|
||||
common: { comment?: unknown[]; genre?: unknown[]; subtitle?: unknown };
|
||||
}> }> }).loadMusicMetadata
|
||||
: null;
|
||||
|
||||
if (!loadMusicMetadata) {
|
||||
throw new Error('music-metadata loadMusicMetadata not found');
|
||||
}
|
||||
|
||||
const musicMetadata = await loadMusicMetadata();
|
||||
const metadata = await musicMetadata.parseFile(filePath, { duration: true });
|
||||
const parseFile = await this.resolveMusicMetadataParser();
|
||||
const metadata = await parseFile(filePath, { duration: true });
|
||||
const infoTags = this.tagService.readInfoTags(filePath);
|
||||
const splitInfoValues = (input: string | undefined): string[] =>
|
||||
input
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import Fuse from 'fuse.js';
|
||||
import Fuse, { type FuseResult, type IFuseOptions } from 'fuse.js';
|
||||
import { AudioFileSummary } from '../../shared/models';
|
||||
import { DatabaseService } from './DatabaseService';
|
||||
import { TagService } from './TagService';
|
||||
@@ -76,7 +76,7 @@ export class SearchService {
|
||||
|
||||
return fuseInstance
|
||||
.search(remainingQuery)
|
||||
.map((result: Fuse.FuseResult<AudioFileSummary>) => result.item)
|
||||
.map((result: FuseResult<AudioFileSummary>) => result.item)
|
||||
.filter((file) => (filters.length === 0 ? true : this.matchesAdvancedFilters(file, filters)));
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ export class SearchService {
|
||||
/**
|
||||
* Provides the standard Fuse configuration used by the service.
|
||||
*/
|
||||
private createFuseOptions(): Fuse.IFuseOptions<AudioFileSummary> {
|
||||
private createFuseOptions(): IFuseOptions<AudioFileSummary> {
|
||||
return {
|
||||
includeScore: true,
|
||||
threshold: 0.35,
|
||||
@@ -114,7 +114,7 @@ export class SearchService {
|
||||
{ name: 'tags', weight: 0.2 },
|
||||
{ name: 'categories', weight: 0.1 }
|
||||
]
|
||||
} satisfies Fuse.IFuseOptions<AudioFileSummary>;
|
||||
} satisfies IFuseOptions<AudioFileSummary>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -163,8 +163,8 @@ export class TagService {
|
||||
.filter((entry) => entry.length > 0);
|
||||
|
||||
const tagText = tagValuesList.length > 0 ? tagValuesList.join('; ') : null;
|
||||
const categoryText = categoryValuesList.length > 0 ? categoryValuesList.join('; ') : null;
|
||||
const primaryCategory = categoryValuesList.at(0) ?? null;
|
||||
const categoryText = categoryValuesList.length > 0 ? categoryValuesList.join('; ') : null;
|
||||
const primaryCategory = categoryValuesList.length > 0 ? categoryValuesList[0] : null;
|
||||
const trimmedTitle = metadata.title?.toString().trim();
|
||||
const effectiveTitle = trimmedTitle && trimmedTitle.length > 0 ? trimmedTitle : null;
|
||||
const trimmedAuthor = metadata.author?.toString().trim();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { useEffect, useMemo, useState, type JSX } from 'react';
|
||||
import FileList from './components/FileList';
|
||||
import FileDetailPanel from './components/FileDetailPanel';
|
||||
import MultiFileEditor from './components/MultiFileEditor';
|
||||
@@ -274,8 +274,7 @@ function App(): JSX.Element {
|
||||
setActiveTab('listen');
|
||||
};
|
||||
|
||||
const handleEditModeSplitComplete = async (created: AudioFileSummary[]) => {
|
||||
await libraryStore.rescan();
|
||||
const handleEditModeSplitComplete = (created: AudioFileSummary[]) => {
|
||||
setActiveTab('listen');
|
||||
if (created.length > 0) {
|
||||
libraryStore.selectFile(created[0].id);
|
||||
@@ -294,6 +293,7 @@ function App(): JSX.Element {
|
||||
categories={library.categories}
|
||||
files={library.files}
|
||||
activeFilter={library.categoryFilter}
|
||||
justSplitIds={library.justSplitFileIds}
|
||||
onSelect={handleCategorySelect}
|
||||
onDropFiles={handleDropFilesToCategory}
|
||||
/>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useMemo } from 'react';
|
||||
import type { AudioFileSummary, CategoryRecord } from '../../../shared/models';
|
||||
import { CATEGORY_FILTER_UNTAGGED, type CategoryFilterValue } from '../stores/LibraryStore';
|
||||
import { CATEGORY_FILTER_JUST_SPLIT, CATEGORY_FILTER_UNTAGGED, type CategoryFilterValue } from '../stores/LibraryStore';
|
||||
import {
|
||||
buildCategorySwatch,
|
||||
createCategoryStyleVars,
|
||||
@@ -11,6 +11,8 @@ export interface CategorySidebarProps {
|
||||
categories: CategoryRecord[];
|
||||
files: AudioFileSummary[];
|
||||
activeFilter: CategoryFilterValue;
|
||||
/** Identifiers of files created during the most recent split action. */
|
||||
justSplitIds: number[];
|
||||
onSelect(filter: CategoryFilterValue): void;
|
||||
onDropFiles?(fileIds: number[], categoryId: string): void;
|
||||
}
|
||||
@@ -37,7 +39,7 @@ function deriveCounts(files: AudioFileSummary[]): {
|
||||
};
|
||||
}
|
||||
|
||||
export function CategorySidebar({ categories, files, activeFilter, onSelect, onDropFiles }: CategorySidebarProps): JSX.Element {
|
||||
export function CategorySidebar({ categories, files, activeFilter, justSplitIds, onSelect, onDropFiles }: CategorySidebarProps): JSX.Element {
|
||||
const { total, untagged, categoryCounts } = deriveCounts(files);
|
||||
const swatchMap = useMemo(() => {
|
||||
const map = new Map<string, ReturnType<typeof buildCategorySwatch>>();
|
||||
@@ -103,6 +105,17 @@ export function CategorySidebar({ categories, files, activeFilter, onSelect, onD
|
||||
<span className="category-sidebar__label">TO TAG</span>
|
||||
<span className="category-sidebar__count">{untagged}</span>
|
||||
</button>
|
||||
{justSplitIds.length > 0 ? (
|
||||
<button
|
||||
type="button"
|
||||
className="category-sidebar__item"
|
||||
data-active={activeFilter === CATEGORY_FILTER_JUST_SPLIT}
|
||||
onClick={() => handleSelect(CATEGORY_FILTER_JUST_SPLIT)}
|
||||
>
|
||||
<span className="category-sidebar__label">JUST SPLIT</span>
|
||||
<span className="category-sidebar__count">{justSplitIds.length}</span>
|
||||
</button>
|
||||
) : null}
|
||||
</div>
|
||||
{sortedGroups.map(([groupName, records]) => {
|
||||
const sortedRecords = records
|
||||
|
||||
@@ -10,7 +10,9 @@ import type {
|
||||
type ChangeListener = () => void;
|
||||
|
||||
export const CATEGORY_FILTER_UNTAGGED = 'untagged' as const;
|
||||
export type CategoryFilterValue = string | typeof CATEGORY_FILTER_UNTAGGED | null;
|
||||
/** Filter key that exposes files generated by the most recent split action. */
|
||||
export const CATEGORY_FILTER_JUST_SPLIT = 'just-split' as const;
|
||||
export type CategoryFilterValue = string | typeof CATEGORY_FILTER_UNTAGGED | typeof CATEGORY_FILTER_JUST_SPLIT | null;
|
||||
|
||||
export interface LibrarySnapshot {
|
||||
initialized: boolean;
|
||||
@@ -25,6 +27,8 @@ export interface LibrarySnapshot {
|
||||
categoryFilter: CategoryFilterValue;
|
||||
lastScan: LibraryScanSummary | null;
|
||||
metadataSuggestionsVersion: number;
|
||||
/** Files created by the most recent split action during this app session. */
|
||||
justSplitFileIds: number[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,7 +47,8 @@ export class LibraryStore extends EventTarget {
|
||||
searchQuery: '',
|
||||
categoryFilter: null,
|
||||
lastScan: null,
|
||||
metadataSuggestionsVersion: 0
|
||||
metadataSuggestionsVersion: 0,
|
||||
justSplitFileIds: []
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -70,7 +75,8 @@ export class LibraryStore extends EventTarget {
|
||||
searchQuery: '',
|
||||
categoryFilter: null,
|
||||
lastScan: null,
|
||||
metadataSuggestionsVersion: nextVersion
|
||||
metadataSuggestionsVersion: nextVersion,
|
||||
justSplitFileIds: []
|
||||
};
|
||||
this.refreshVisibleFiles(this.snapshot.selectedFileId ?? null);
|
||||
}
|
||||
@@ -263,7 +269,10 @@ export class LibraryStore extends EventTarget {
|
||||
};
|
||||
|
||||
let visible: AudioFileSummary[];
|
||||
if (filter === CATEGORY_FILTER_UNTAGGED) {
|
||||
if (filter === CATEGORY_FILTER_JUST_SPLIT) {
|
||||
const justSplitIds = new Set(this.snapshot.justSplitFileIds);
|
||||
visible = files.filter((file) => justSplitIds.has(file.id));
|
||||
} else if (filter === CATEGORY_FILTER_UNTAGGED) {
|
||||
visible = files.filter((file) => file.categories.length === 0);
|
||||
} else if (filter) {
|
||||
visible = files.filter((file) => file.categories.includes(filter));
|
||||
@@ -275,7 +284,8 @@ export class LibraryStore extends EventTarget {
|
||||
...this.snapshot,
|
||||
visibleFiles: visible,
|
||||
selectedFileId,
|
||||
focusedFile
|
||||
focusedFile,
|
||||
justSplitFileIds: this.snapshot.justSplitFileIds
|
||||
};
|
||||
this.emitChange();
|
||||
}
|
||||
@@ -398,21 +408,29 @@ export class LibraryStore extends EventTarget {
|
||||
public async splitFile(fileId: number, segments: SplitSegmentRequest[]): Promise<AudioFileSummary[]> {
|
||||
const created = await window.api.splitFile(fileId, segments);
|
||||
const files = await window.api.listAudioFiles();
|
||||
const { categoryFilter } = this.snapshot;
|
||||
let visible: AudioFileSummary[];
|
||||
if (categoryFilter === CATEGORY_FILTER_UNTAGGED) {
|
||||
visible = files.filter((file) => file.categories.length === 0);
|
||||
} else if (categoryFilter) {
|
||||
visible = files.filter((file) => file.categories.includes(categoryFilter));
|
||||
} else {
|
||||
visible = files.slice();
|
||||
const createdIds = created.map((record) => record.id);
|
||||
const fileLookup = new Map(files.map((record) => [record.id, record] as const));
|
||||
// Prefer fresh copies from the library list so downstream consumers see current metadata.
|
||||
const justSplitRecords: AudioFileSummary[] = createdIds
|
||||
.map((id) => fileLookup.get(id) ?? created.find((record) => record.id === id) ?? null)
|
||||
.filter((record): record is AudioFileSummary => record !== null);
|
||||
|
||||
const primaryId = justSplitRecords.at(0)?.id ?? null;
|
||||
const nextSelection = new Set<number>(justSplitRecords.map((record) => record.id));
|
||||
if (primaryId !== null) {
|
||||
nextSelection.add(primaryId);
|
||||
}
|
||||
|
||||
this.snapshot = {
|
||||
...this.snapshot,
|
||||
files,
|
||||
visibleFiles: visible,
|
||||
metadataSuggestionsVersion: this.snapshot.metadataSuggestionsVersion + 1
|
||||
visibleFiles: justSplitRecords,
|
||||
metadataSuggestionsVersion: this.snapshot.metadataSuggestionsVersion + 1,
|
||||
categoryFilter: CATEGORY_FILTER_JUST_SPLIT,
|
||||
justSplitFileIds: justSplitRecords.map((record) => record.id),
|
||||
selectedFileId: primaryId,
|
||||
selectedFileIds: nextSelection,
|
||||
focusedFile: primaryId !== null ? fileLookup.get(primaryId) ?? justSplitRecords.at(0) ?? null : null
|
||||
};
|
||||
this.emitChange();
|
||||
return created;
|
||||
@@ -462,8 +480,13 @@ export class LibraryStore extends EventTarget {
|
||||
|
||||
private refreshVisibleFiles(preferredId?: number | null, emit = true): void {
|
||||
const { files, categoryFilter } = this.snapshot;
|
||||
const availableIds = new Set(files.map((file) => file.id));
|
||||
const justSplitIds = this.snapshot.justSplitFileIds.filter((id) => availableIds.has(id));
|
||||
let visible: AudioFileSummary[];
|
||||
if (categoryFilter === CATEGORY_FILTER_UNTAGGED) {
|
||||
if (categoryFilter === CATEGORY_FILTER_JUST_SPLIT) {
|
||||
const justSplitSet = new Set(justSplitIds);
|
||||
visible = files.filter((file) => justSplitSet.has(file.id));
|
||||
} else if (categoryFilter === CATEGORY_FILTER_UNTAGGED) {
|
||||
visible = files.filter((file) => file.categories.length === 0);
|
||||
} else if (categoryFilter) {
|
||||
visible = files.filter((file) => file.categories.includes(categoryFilter));
|
||||
@@ -500,7 +523,8 @@ export class LibraryStore extends EventTarget {
|
||||
visibleFiles: visible,
|
||||
selectedFileId: nextSelected,
|
||||
selectedFileIds: nextSelectionSet,
|
||||
focusedFile: nextFocused
|
||||
focusedFile: nextFocused,
|
||||
justSplitFileIds: justSplitIds
|
||||
};
|
||||
|
||||
if (emit) {
|
||||
|
||||
Reference in New Issue
Block a user