Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions internal/api/handlers/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2058,16 +2058,45 @@ func (h *LibraryHandler) HandleListUnmatchedItems(w http.ResponseWriter, r *http
}
}

// Optional case-insensitive search across title, library name, type, and
// status. Applied server-side so it spans the whole table, not just the
// current page. The displayed folder still comes from the lateral join below,
// but the search predicate checks every membership so multi-library items are
// found when any linked library name matches.
search := strings.TrimSpace(q.Get("q"))
filter := ""
filterArgs := []any{}
if search != "" {
filterArgs = append(filterArgs, "%"+search+"%")
filter = ` AND (
mi.title ILIKE $1
OR mi.type ILIKE $1
OR mi.status ILIKE $1
OR EXISTS (
SELECT 1
FROM media_item_libraries search_mil
JOIN media_folders search_f ON search_f.id = search_mil.media_folder_id
WHERE search_mil.content_id = mi.content_id
AND search_f.name ILIKE $1
)
)`
}

countSQL := `
SELECT COUNT(*)
FROM media_items mi
WHERE mi.status IN ('unmatched', 'pending', 'ambiguous')`
countSQL += filter

var total int
if err := h.pool.QueryRow(r.Context(),
`SELECT COUNT(*) FROM media_items WHERE status IN ('unmatched', 'pending', 'ambiguous')`,
).Scan(&total); err != nil {
if err := h.pool.QueryRow(r.Context(), countSQL, filterArgs...).Scan(&total); err != nil {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
slog.Error("counting unmatched items", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to count unmatched items")
return
}

rows, err := h.pool.Query(r.Context(), `
listArgs := append(append([]any{}, filterArgs...), limit, offset)
listSQL := fmt.Sprintf(`
SELECT mi.content_id, mi.title, mi.year, mi.type, mi.status,
COALESCE(lib.folder_id, 0),
COALESCE(lib.folder_name, '')
Expand All @@ -2079,10 +2108,12 @@ func (h *LibraryHandler) HandleListUnmatchedItems(w http.ResponseWriter, r *http
WHERE mil.content_id = mi.content_id
LIMIT 1
) lib ON true
WHERE mi.status IN ('unmatched', 'pending', 'ambiguous')
WHERE mi.status IN ('unmatched', 'pending', 'ambiguous')%s
ORDER BY mi.title ASC, mi.content_id ASC
LIMIT $1 OFFSET $2
`, limit, offset)
LIMIT $%d OFFSET $%d
`, filter, len(filterArgs)+1, len(filterArgs)+2)

rows, err := h.pool.Query(r.Context(), listSQL, listArgs...)
if err != nil {
slog.Error("listing unmatched items", "error", err)
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list unmatched items")
Expand Down
113 changes: 65 additions & 48 deletions web/src/components/MatchItemDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Badge } from "@/components/ui/badge";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import type { FileVersion, ItemDetail, MatchCandidate } from "@/api/types";
import MediaLocations from "@/components/MediaLocations";
import { useSearchItemMatchCandidates, useApplyItemMatch } from "@/hooks/queries/items";
Expand Down Expand Up @@ -170,55 +171,71 @@ export default function MatchItemDialog({ item, open, onOpenChange }: MatchItemD
{candidates.length > 0 && (
<div className="flex min-h-0 min-w-0 flex-1 flex-col gap-2 overflow-hidden">
<Label className="shrink-0">Results</Label>
<div className="overlay-scroll min-h-0 flex-1 space-y-1 overflow-y-auto overscroll-contain pr-1 pb-1">
{candidates.map((candidate, index) => {
const candidateKey = Object.entries(candidate.provider_ids)
.map(([k, v]) => `${k}-${v}`)
.join("_");
return (
<button
key={`${candidateKey}-${index}`}
type="button"
className={cn(
"flex w-full min-w-0 items-start gap-3 rounded-lg border p-3 text-left transition-colors",
selectedCandidate === candidate
? "border-primary bg-primary/5"
: "border-border hover:bg-muted/50",
)}
onClick={() => setSelectedCandidate(candidate)}
data-testid="match-candidate"
>
{candidate.image_url ? (
<img
src={candidate.image_url}
alt=""
className="h-16 w-11 shrink-0 rounded object-cover"
/>
) : (
<div className="bg-muted h-16 w-11 shrink-0 rounded" />
)}
<div className="min-w-0 flex-1">
<div className="truncate text-sm font-medium">{candidate.title}</div>
<div className="text-muted-foreground text-xs">
{candidate.year ? candidate.year : ""}
<TooltipProvider delayDuration={150}>
<div className="overlay-scroll min-h-0 flex-1 space-y-1 overflow-y-auto overscroll-contain pr-1 pb-1">
{candidates.map((candidate, index) => {
const candidateKey = Object.entries(candidate.provider_ids)
.map(([k, v]) => `${k}-${v}`)
.join("_");
return (
<button
key={`${candidateKey}-${index}`}
type="button"
className={cn(
"flex w-full min-w-0 items-start gap-3 rounded-lg border p-3 text-left transition-colors",
selectedCandidate === candidate
? "border-primary bg-primary/5"
: "border-border hover:bg-muted/50",
)}
onClick={() => setSelectedCandidate(candidate)}
data-testid="match-candidate"
>
{candidate.image_url ? (
<Tooltip>
<TooltipTrigger asChild>
<img
src={candidate.image_url}
alt=""
className="h-24 w-16 shrink-0 cursor-zoom-in rounded object-cover"
/>
</TooltipTrigger>
<TooltipContent
side="right"
className="border-border/60 overflow-hidden border bg-transparent p-0 shadow-xl"
>
<img
src={candidate.image_url}
alt={candidate.title}
className="h-72 w-48 rounded-md object-cover"
/>
</TooltipContent>
</Tooltip>
) : (
<div className="bg-muted h-24 w-16 shrink-0 rounded" />
)}
<div className="min-w-0 flex-1">
<div className="truncate text-sm font-medium">{candidate.title}</div>
<div className="text-muted-foreground text-xs">
{candidate.year ? candidate.year : ""}
</div>
<div className="mt-1 flex min-w-0 flex-wrap gap-1">
{candidate.sources.map((source) => (
<Badge key={source} variant="outline" className="text-[10px]">
{source}
</Badge>
))}
{candidate.sources.length > 1 && (
<Badge variant="secondary" className="text-[10px]">
{candidate.sources.length} sources agree
</Badge>
)}
</div>
</div>
<div className="mt-1 flex min-w-0 flex-wrap gap-1">
{candidate.sources.map((source) => (
<Badge key={source} variant="outline" className="text-[10px]">
{source}
</Badge>
))}
{candidate.sources.length > 1 && (
<Badge variant="secondary" className="text-[10px]">
{candidate.sources.length} sources agree
</Badge>
)}
</div>
</div>
</button>
);
})}
</div>
</button>
);
})}
</div>
</TooltipProvider>
</div>
)}

Expand Down
9 changes: 6 additions & 3 deletions web/src/hooks/queries/admin/libraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,16 @@ export function useRefreshLibraryMetadata() {

const UNMATCHED_PAGE_SIZE = 10;

export function useUnmatchedLibraryItems(page = 0) {
export function useUnmatchedLibraryItems(page = 0, search = "") {
const offset = page * UNMATCHED_PAGE_SIZE;
const trimmed = search.trim();
return useQuery({
queryKey: adminKeys.unmatchedItems(page),
queryKey: adminKeys.unmatchedItems(page, trimmed),
queryFn: () =>
api<UnmatchedLibraryItemsResponse>(
`/libraries/unmatched-items?limit=${UNMATCHED_PAGE_SIZE}&offset=${offset}`,
`/libraries/unmatched-items?limit=${UNMATCHED_PAGE_SIZE}&offset=${offset}${
trimmed ? `&q=${encodeURIComponent(trimmed)}` : ""
}`,
).then((d) => d ?? { items: [], total: 0 }),
staleTime: ADMIN_STALE_TIME,
placeholderData: (prev) => prev,
Expand Down
4 changes: 2 additions & 2 deletions web/src/hooks/queries/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,9 @@ export const adminKeys = {
pluginRepositories: () => ["admin", "plugins", "repositories"] as const,
pluginCatalog: () => ["admin", "plugins", "catalog"] as const,
pluginInstallations: () => ["admin", "plugins", "installations"] as const,
unmatchedItems: (page?: number) =>
unmatchedItems: (page?: number, search?: string) =>
page != null
? (["admin", "libraries", "unmatchedItems", page] as const)
? (["admin", "libraries", "unmatchedItems", page, search ?? ""] as const)
: (["admin", "libraries", "unmatchedItems"] as const),
itemImages: (id: string) => ["admin", "items", id, "images"] as const,
buildInfo: () => ["admin", "system", "buildInfo"] as const,
Expand Down
Loading