263 lines
8.4 KiB
TypeScript
263 lines
8.4 KiB
TypeScript
"use client";
|
|
|
|
import { useMemo, useState } from "react";
|
|
import { Trans, useTranslation } from "react-i18next";
|
|
import { IconRefresh } from "@tabler/icons-react";
|
|
import { Button } from "@kandev/ui/button";
|
|
import { Input } from "@kandev/ui/input";
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@kandev/ui/select";
|
|
import { useMarketplace } from "@/hooks/domains/plugins/use-marketplace";
|
|
import type { CatalogQuery } from "@/lib/api/domains/marketplace-api";
|
|
import type { MarketplaceCatalog, MarketplaceEntry, MarketplaceSource } from "@/lib/types/plugins";
|
|
import { MarketplaceEntryRow } from "./marketplace-entry-row";
|
|
import { MarketplaceSourcesDialog } from "./marketplace-sources-dialog";
|
|
|
|
const ALL_CATEGORIES = "__all__";
|
|
|
|
type MarketplaceBrowserProps = {
|
|
/** Installs a plugin from its package URL; resolves when the install settles. The result is unused here — failures already toast. */
|
|
onInstallUrl: (url: string) => Promise<{ ok: boolean; error?: string }>;
|
|
canManage?: boolean;
|
|
};
|
|
|
|
/** The "Browse" tab: search/filter/sort the catalog and install from it. */
|
|
export function MarketplaceBrowser({ onInstallUrl, canManage = true }: MarketplaceBrowserProps) {
|
|
const [text, setText] = useState("");
|
|
const [category, setCategory] = useState<string>(ALL_CATEGORIES);
|
|
const [sort, setSort] = useState<CatalogQuery["sort"]>("stars");
|
|
const [sourcesOpen, setSourcesOpen] = useState(false);
|
|
const [installingId, setInstallingId] = useState<string | null>(null);
|
|
|
|
// Category is filtered client-side (not sent to the server): otherwise a
|
|
// category-narrowed catalog would shrink the category dropdown to just the
|
|
// selected value, trapping the user. Search + sort stay server-side.
|
|
const { catalog, loading, error, reload, softReload } = useMarketplace({
|
|
q: text.trim() || undefined,
|
|
sort,
|
|
});
|
|
const categories = useCatalogCategories(catalog);
|
|
const visible =
|
|
category === ALL_CATEGORIES
|
|
? catalog.plugins
|
|
: catalog.plugins.filter((entry) => entry.categories.includes(category));
|
|
|
|
const install = async (entry: MarketplaceEntry) => {
|
|
setInstallingId(entry.id);
|
|
try {
|
|
await onInstallUrl(entry.package_url);
|
|
softReload();
|
|
} finally {
|
|
setInstallingId(null);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<MarketplaceToolbar
|
|
text={text}
|
|
onText={setText}
|
|
category={category}
|
|
onCategory={setCategory}
|
|
categories={categories}
|
|
sort={sort}
|
|
onSort={setSort}
|
|
onManageSources={() => setSourcesOpen(true)}
|
|
onRefresh={reload}
|
|
canManage={canManage}
|
|
/>
|
|
|
|
<DegradedSourcesBanner catalog={catalog} />
|
|
|
|
<MarketplaceList
|
|
entries={visible}
|
|
loading={loading}
|
|
error={error}
|
|
filtered={Boolean(text.trim() || category !== ALL_CATEGORIES)}
|
|
installingId={installingId}
|
|
onInstall={install}
|
|
canManage={canManage}
|
|
/>
|
|
|
|
{canManage && (
|
|
<MarketplaceSourcesDialog
|
|
open={sourcesOpen}
|
|
sources={catalog.sources}
|
|
onOpenChange={setSourcesOpen}
|
|
onChanged={reload}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function useCatalogCategories(catalog: MarketplaceCatalog): string[] {
|
|
return useMemo(() => {
|
|
const set = new Set<string>();
|
|
for (const entry of catalog.plugins) {
|
|
for (const cat of entry.categories) set.add(cat);
|
|
}
|
|
return Array.from(set).sort();
|
|
}, [catalog.plugins]);
|
|
}
|
|
|
|
type ToolbarProps = {
|
|
text: string;
|
|
onText: (v: string) => void;
|
|
category: string;
|
|
onCategory: (v: string) => void;
|
|
categories: string[];
|
|
sort: CatalogQuery["sort"];
|
|
onSort: (v: CatalogQuery["sort"]) => void;
|
|
onManageSources: () => void;
|
|
onRefresh: () => void;
|
|
canManage: boolean;
|
|
};
|
|
|
|
function MarketplaceToolbar(props: ToolbarProps) {
|
|
const { t } = useTranslation();
|
|
return (
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<Input
|
|
placeholder={t("plugins:searchPlugins")}
|
|
value={props.text}
|
|
onChange={(e) => props.onText(e.target.value)}
|
|
className="max-w-xs"
|
|
data-testid="marketplace-search"
|
|
/>
|
|
<Select value={props.category} onValueChange={props.onCategory}>
|
|
<SelectTrigger className="w-40" data-testid="marketplace-category">
|
|
<SelectValue placeholder={t("plugins:category")} />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value={ALL_CATEGORIES}>{t("plugins:allCategories")}</SelectItem>
|
|
{props.categories.map((cat) => (
|
|
<SelectItem key={cat} value={cat}>
|
|
{cat}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
<Select value={props.sort} onValueChange={(v) => props.onSort(v as CatalogQuery["sort"])}>
|
|
<SelectTrigger className="w-36" data-testid="marketplace-sort">
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{/* The `value`s are the catalog API's sort tokens, not copy. */}
|
|
<SelectItem value="stars">{t("plugins:sortMostStars")}</SelectItem>
|
|
<SelectItem value="recent">{t("plugins:sortRecentlyUpdated")}</SelectItem>
|
|
<SelectItem value="name">{t("plugins:sortName")}</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
{props.canManage && (
|
|
<div className="ml-auto flex items-center gap-2">
|
|
<Button variant="secondary" onClick={props.onRefresh} className="cursor-pointer">
|
|
<IconRefresh className="h-4 w-4" />
|
|
{t("plugins:refresh")}
|
|
</Button>
|
|
<Button
|
|
variant="outline"
|
|
onClick={props.onManageSources}
|
|
className="cursor-pointer"
|
|
data-testid="marketplace-manage-sources"
|
|
>
|
|
{t("plugins:sources")}
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function DegradedSourcesBanner({ catalog }: { catalog: MarketplaceCatalog }) {
|
|
const degraded = catalog.sources.filter((s) => s.enabled && s.healthy === false);
|
|
if (degraded.length === 0) return null;
|
|
return (
|
|
<div
|
|
data-testid="marketplace-degraded-sources"
|
|
className="rounded-lg border border-amber-500/40 bg-amber-500/10 p-3 text-sm text-amber-700 dark:text-amber-400 space-y-1"
|
|
>
|
|
{degraded.map((s) => (
|
|
<DegradedSourceLine key={s.id} source={s} />
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* The source name comes from its index.json and `detail` is the backend's own
|
|
* network diagnostic, so both are interpolated as values rather than written
|
|
* into the message.
|
|
*/
|
|
function DegradedSourceLine({ source }: { source: MarketplaceSource }) {
|
|
const detail = source.error ? `: ${source.error}` : "";
|
|
return (
|
|
<div>
|
|
<Trans i18nKey="plugins:sourceUnreachableDetail" values={{ name: source.name, detail }}>
|
|
<span className="font-medium">{source.name}</span> is unreachable
|
|
{detail} - its plugins are hidden.
|
|
</Trans>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
type ListProps = {
|
|
entries: MarketplaceEntry[];
|
|
loading: boolean;
|
|
error: string | null;
|
|
filtered: boolean;
|
|
installingId: string | null;
|
|
onInstall: (entry: MarketplaceEntry) => void;
|
|
canManage: boolean;
|
|
};
|
|
|
|
function MarketplaceList({
|
|
entries,
|
|
loading,
|
|
error,
|
|
filtered,
|
|
installingId,
|
|
onInstall,
|
|
canManage,
|
|
}: ListProps) {
|
|
const { t } = useTranslation();
|
|
if (error) {
|
|
return (
|
|
<div className="rounded-lg border border-destructive/40 bg-destructive/5 p-6 text-sm text-destructive">
|
|
{error}
|
|
</div>
|
|
);
|
|
}
|
|
if (loading && entries.length === 0) {
|
|
return (
|
|
<div className="rounded-lg border border-dashed border-border/70 p-6 text-sm text-muted-foreground">
|
|
{t("plugins:loadingMarketplace")}
|
|
</div>
|
|
);
|
|
}
|
|
if (entries.length === 0) {
|
|
return (
|
|
<div className="rounded-xl border border-dashed border-border/70 p-10 text-center text-sm text-muted-foreground">
|
|
{filtered ? t("plugins:noPluginsMatchSearch") : t("plugins:noPluginsAvailable")}
|
|
</div>
|
|
);
|
|
}
|
|
return (
|
|
<div className="space-y-3">
|
|
<p className="text-xs text-muted-foreground">
|
|
{t("plugins:pluginsAvailable", { count: entries.length })}
|
|
</p>
|
|
<div className="grid gap-3 md:grid-cols-2">
|
|
{entries.map((entry) => (
|
|
<MarketplaceEntryRow
|
|
key={`${entry.source_id}:${entry.id}`}
|
|
entry={entry}
|
|
busy={installingId === entry.id}
|
|
onInstall={onInstall}
|
|
canManage={canManage}
|
|
/>
|
|
))}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|