2025-06-26 18:40:33 +08:00
|
|
|
import { useAsyncEffect, useDebounce, useKeyPress, useUnmount } from "ahooks";
|
2025-08-05 18:08:00 +08:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
2025-06-26 18:40:33 +08:00
|
|
|
import { CircleCheck, FolderDown, Loader } from "lucide-react";
|
|
|
|
|
import clsx from "clsx";
|
2025-06-29 13:32:07 +08:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
|
|
|
|
import { useSearchStore } from "@/stores/searchStore";
|
2025-11-02 10:59:29 +08:00
|
|
|
import { installExtensionError, parseSearchQuery } from "@/utils";
|
2025-06-29 13:32:07 +08:00
|
|
|
import platformAdapter from "@/utils/platformAdapter";
|
|
|
|
|
import SearchEmpty from "../Common/SearchEmpty";
|
2025-06-26 18:40:33 +08:00
|
|
|
import ExtensionDetail from "./ExtensionDetail";
|
|
|
|
|
import { useShortcutsStore } from "@/stores/shortcutsStore";
|
|
|
|
|
import { useAppStore } from "@/stores/appStore";
|
|
|
|
|
import { platform } from "@/utils/platform";
|
|
|
|
|
|
|
|
|
|
export interface SearchExtensionItem {
|
|
|
|
|
id: string;
|
|
|
|
|
created: string;
|
|
|
|
|
updated: string;
|
|
|
|
|
name: string;
|
|
|
|
|
description: string;
|
|
|
|
|
icon: string;
|
|
|
|
|
type: string;
|
|
|
|
|
category: string;
|
|
|
|
|
tags?: string[];
|
|
|
|
|
platforms: string[];
|
|
|
|
|
developer: {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
avatar: string;
|
|
|
|
|
twitter_handle?: string;
|
|
|
|
|
github_handle?: string;
|
|
|
|
|
location?: string;
|
|
|
|
|
website?: string;
|
|
|
|
|
bio?: string;
|
|
|
|
|
};
|
|
|
|
|
contributors: {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
avatar: string;
|
|
|
|
|
}[];
|
|
|
|
|
url: {
|
|
|
|
|
code: string;
|
|
|
|
|
download: string;
|
|
|
|
|
};
|
|
|
|
|
version: {
|
|
|
|
|
number: string;
|
|
|
|
|
};
|
|
|
|
|
screenshots: {
|
|
|
|
|
title?: string;
|
|
|
|
|
url: string;
|
|
|
|
|
}[];
|
|
|
|
|
action: {
|
|
|
|
|
exec: string;
|
|
|
|
|
args: string[];
|
|
|
|
|
};
|
|
|
|
|
enabled: boolean;
|
|
|
|
|
stats: {
|
|
|
|
|
installs: number;
|
|
|
|
|
views: number;
|
|
|
|
|
};
|
|
|
|
|
checksum: string;
|
2025-08-05 18:08:00 +08:00
|
|
|
installed?: boolean;
|
2025-06-26 18:40:33 +08:00
|
|
|
commands?: Array<{
|
|
|
|
|
type: string;
|
|
|
|
|
name: string;
|
|
|
|
|
icon: string;
|
|
|
|
|
description: string;
|
|
|
|
|
action: {
|
|
|
|
|
exec: string;
|
|
|
|
|
args: string[];
|
|
|
|
|
};
|
|
|
|
|
}>;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-05 18:08:00 +08:00
|
|
|
const ExtensionStore = ({ extensionId }: { extensionId?: string }) => {
|
2025-06-26 18:40:33 +08:00
|
|
|
const {
|
|
|
|
|
searchValue,
|
|
|
|
|
selectedExtension,
|
|
|
|
|
setSelectedExtension,
|
|
|
|
|
installingExtensions,
|
|
|
|
|
setInstallingExtensions,
|
|
|
|
|
setUninstallingExtensions,
|
|
|
|
|
visibleExtensionDetail,
|
|
|
|
|
setVisibleExtensionDetail,
|
|
|
|
|
visibleContextMenu,
|
|
|
|
|
setVisibleContextMenu,
|
|
|
|
|
} = useSearchStore();
|
|
|
|
|
const debouncedSearchValue = useDebounce(searchValue);
|
|
|
|
|
const [list, setList] = useState<SearchExtensionItem[]>([]);
|
|
|
|
|
const { modifierKey } = useShortcutsStore();
|
|
|
|
|
const { addError } = useAppStore();
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const unlisten1 = platformAdapter.listenEvent("install-extension", () => {
|
|
|
|
|
handleInstall();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const unlisten2 = platformAdapter.listenEvent("uninstall-extension", () => {
|
|
|
|
|
handleUnInstall();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
unlisten1.then((fn) => fn());
|
|
|
|
|
unlisten2.then((fn) => fn());
|
|
|
|
|
};
|
|
|
|
|
}, [selectedExtension]);
|
|
|
|
|
|
2025-08-05 18:08:00 +08:00
|
|
|
const handleExtensionDetail = useCallback(async () => {
|
|
|
|
|
try {
|
|
|
|
|
const detail = await platformAdapter.invokeBackend<SearchExtensionItem>(
|
|
|
|
|
"extension_detail",
|
|
|
|
|
{
|
|
|
|
|
id: extensionId,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
setSelectedExtension(detail);
|
|
|
|
|
setVisibleExtensionDetail(true);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
addError(String(error));
|
|
|
|
|
}
|
|
|
|
|
}, [extensionId, installingExtensions]);
|
|
|
|
|
|
2025-06-26 18:40:33 +08:00
|
|
|
useAsyncEffect(async () => {
|
2025-08-05 18:08:00 +08:00
|
|
|
if (extensionId) {
|
|
|
|
|
return handleExtensionDetail();
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-26 18:40:33 +08:00
|
|
|
const result = await platformAdapter.invokeBackend<SearchExtensionItem[]>(
|
|
|
|
|
"search_extension",
|
|
|
|
|
{
|
|
|
|
|
queryParams: parseSearchQuery({
|
2025-06-27 09:36:20 +08:00
|
|
|
query: debouncedSearchValue.trim(),
|
2025-06-26 18:40:33 +08:00
|
|
|
filters: {
|
|
|
|
|
platforms: [platform()],
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-10-11 10:33:09 +08:00
|
|
|
// console.log("search_extension", result);
|
2025-06-26 18:40:33 +08:00
|
|
|
|
|
|
|
|
setList(result ?? []);
|
|
|
|
|
|
|
|
|
|
setSelectedExtension(result?.[0]);
|
2025-08-05 18:08:00 +08:00
|
|
|
}, [debouncedSearchValue, extensionId]);
|
2025-06-26 18:40:33 +08:00
|
|
|
|
|
|
|
|
useUnmount(() => {
|
|
|
|
|
setSelectedExtension(void 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useKeyPress(
|
|
|
|
|
"enter",
|
|
|
|
|
() => {
|
|
|
|
|
if (visibleContextMenu) return;
|
|
|
|
|
|
|
|
|
|
if (visibleExtensionDetail) {
|
|
|
|
|
return handleInstall();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setVisibleExtensionDetail(true);
|
|
|
|
|
},
|
|
|
|
|
{ exactMatch: true }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useKeyPress(
|
|
|
|
|
`${modifierKey}.enter`,
|
|
|
|
|
() => {
|
2025-07-21 18:17:30 +08:00
|
|
|
if (visibleContextMenu || visibleExtensionDetail) {
|
2025-06-26 18:40:33 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleInstall();
|
|
|
|
|
},
|
|
|
|
|
{ exactMatch: true }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useKeyPress(["uparrow", "downarrow"], (_, key) => {
|
|
|
|
|
if (visibleContextMenu || visibleExtensionDetail) return;
|
|
|
|
|
|
|
|
|
|
const index = list.findIndex((item) => item.id === selectedExtension?.id);
|
|
|
|
|
const length = list.length;
|
|
|
|
|
|
|
|
|
|
if (length <= 1) return;
|
|
|
|
|
|
|
|
|
|
let nextIndex = index;
|
|
|
|
|
|
|
|
|
|
if (key === "uparrow") {
|
|
|
|
|
nextIndex = nextIndex > 0 ? nextIndex - 1 : length - 1;
|
|
|
|
|
} else {
|
|
|
|
|
nextIndex = nextIndex < length - 1 ? nextIndex + 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setSelectedExtension(list[nextIndex]);
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-21 18:17:30 +08:00
|
|
|
const toggleInstall = (extension: SearchExtensionItem) => {
|
|
|
|
|
if (!extension) return;
|
2025-06-26 18:40:33 +08:00
|
|
|
|
2025-07-21 18:17:30 +08:00
|
|
|
const { id, installed } = extension;
|
2025-06-26 18:40:33 +08:00
|
|
|
|
|
|
|
|
setList((prev) => {
|
|
|
|
|
return prev.map((item) => {
|
|
|
|
|
if (item.id === id) {
|
2025-07-21 18:17:30 +08:00
|
|
|
return { ...item, installed: !installed };
|
2025-06-26 18:40:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-21 18:17:30 +08:00
|
|
|
const { selectedExtension } = useSearchStore.getState();
|
|
|
|
|
|
2025-06-26 18:40:33 +08:00
|
|
|
if (selectedExtension?.id === id) {
|
|
|
|
|
setSelectedExtension({
|
|
|
|
|
...selectedExtension,
|
2025-07-21 18:17:30 +08:00
|
|
|
installed: !installed,
|
2025-06-26 18:40:33 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleInstall = async () => {
|
2025-07-21 18:17:30 +08:00
|
|
|
const { selectedExtension, installingExtensions } =
|
|
|
|
|
useSearchStore.getState();
|
|
|
|
|
|
2025-06-26 18:40:33 +08:00
|
|
|
if (!selectedExtension) return;
|
|
|
|
|
|
|
|
|
|
const { id, name, installed } = selectedExtension;
|
|
|
|
|
|
2025-07-21 18:17:30 +08:00
|
|
|
if (installed || installingExtensions.includes(id)) return;
|
2025-06-26 18:40:33 +08:00
|
|
|
|
2025-07-21 18:17:30 +08:00
|
|
|
try {
|
2025-06-26 18:40:33 +08:00
|
|
|
setInstallingExtensions(installingExtensions.concat(id));
|
|
|
|
|
|
2025-07-21 18:17:30 +08:00
|
|
|
await platformAdapter.invokeBackend("install_extension_from_store", {
|
|
|
|
|
id,
|
|
|
|
|
});
|
2025-06-26 18:40:33 +08:00
|
|
|
|
2025-07-21 18:17:30 +08:00
|
|
|
toggleInstall(selectedExtension);
|
2025-06-26 18:40:33 +08:00
|
|
|
|
|
|
|
|
addError(
|
|
|
|
|
`${name} ${t("extensionStore.hints.installationCompleted")}`,
|
|
|
|
|
"info"
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
2025-11-02 10:59:29 +08:00
|
|
|
installExtensionError(String(error));
|
2025-06-26 18:40:33 +08:00
|
|
|
} finally {
|
2025-07-21 18:17:30 +08:00
|
|
|
const { installingExtensions } = useSearchStore.getState();
|
|
|
|
|
|
2025-06-26 18:40:33 +08:00
|
|
|
setInstallingExtensions(
|
|
|
|
|
installingExtensions.filter((item) => item !== id)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleUnInstall = async () => {
|
2025-07-21 18:17:30 +08:00
|
|
|
const { selectedExtension, uninstallingExtensions } =
|
|
|
|
|
useSearchStore.getState();
|
|
|
|
|
|
2025-06-26 18:40:33 +08:00
|
|
|
if (!selectedExtension) return;
|
|
|
|
|
|
|
|
|
|
const { id, name, installed, developer } = selectedExtension;
|
|
|
|
|
|
2025-07-21 18:17:30 +08:00
|
|
|
if (!installed || uninstallingExtensions.includes(id)) return;
|
2025-06-26 18:40:33 +08:00
|
|
|
|
2025-07-21 18:17:30 +08:00
|
|
|
try {
|
2025-06-26 18:40:33 +08:00
|
|
|
setUninstallingExtensions(uninstallingExtensions.concat(id));
|
|
|
|
|
|
|
|
|
|
await platformAdapter.invokeBackend("uninstall_extension", {
|
|
|
|
|
developer: developer.id,
|
|
|
|
|
extensionId: id,
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-21 18:17:30 +08:00
|
|
|
toggleInstall(selectedExtension);
|
2025-06-26 18:40:33 +08:00
|
|
|
|
|
|
|
|
addError(
|
|
|
|
|
`${name} ${t("extensionStore.hints.uninstallationCompleted")}`,
|
|
|
|
|
"info"
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
addError(String(error), "error");
|
|
|
|
|
} finally {
|
2025-07-21 18:17:30 +08:00
|
|
|
const { uninstallingExtensions } = useSearchStore.getState();
|
|
|
|
|
|
2025-06-26 18:40:33 +08:00
|
|
|
setUninstallingExtensions(
|
|
|
|
|
uninstallingExtensions.filter((item) => item !== id)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-06-29 13:32:07 +08:00
|
|
|
<div className="h-full text-sm p-4 overflow-auto custom-scrollbar">
|
2025-06-26 18:40:33 +08:00
|
|
|
{visibleExtensionDetail ? (
|
|
|
|
|
<ExtensionDetail
|
|
|
|
|
onInstall={handleInstall}
|
|
|
|
|
onUninstall={handleUnInstall}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{list.length > 0 ? (
|
|
|
|
|
list.map((item) => {
|
|
|
|
|
const { id, icon, name, description, stats, installed } = item;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={id}
|
|
|
|
|
className={clsx(
|
2025-06-27 14:18:32 +08:00
|
|
|
"flex justify-between gap-4 h-[40px] px-2 rounded-lg cursor-pointer text-[#333] dark:text-[#d8d8d8] transition",
|
2025-06-26 18:40:33 +08:00
|
|
|
{
|
|
|
|
|
"bg-black/10 dark:bg-white/15":
|
|
|
|
|
selectedExtension?.id === id,
|
|
|
|
|
}
|
|
|
|
|
)}
|
|
|
|
|
onMouseOver={() => {
|
|
|
|
|
setSelectedExtension(item);
|
|
|
|
|
}}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setVisibleExtensionDetail(true);
|
|
|
|
|
}}
|
|
|
|
|
onContextMenu={(event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
setVisibleContextMenu(true);
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-06-27 14:18:32 +08:00
|
|
|
<div className="flex items-center gap-2 overflow-hidden">
|
2025-06-26 18:40:33 +08:00
|
|
|
<img src={icon} className="size-[20px]" />
|
2025-06-27 14:18:32 +08:00
|
|
|
<span className="whitespace-nowrap">{name}</span>
|
|
|
|
|
<span className="truncate text-[#999]">{description}</span>
|
2025-06-26 18:40:33 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
{installed && (
|
|
|
|
|
<CircleCheck className="size-4 text-green-500" />
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{installingExtensions.includes(item.id) && (
|
|
|
|
|
<Loader className="size-4 text-blue-500 animate-spin" />
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div className="flex items-center gap-1 text-[#999]">
|
|
|
|
|
<FolderDown className="size-4" />
|
|
|
|
|
<span>{stats.installs}</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
) : (
|
|
|
|
|
<div className="flex justify-center items-center h-full">
|
|
|
|
|
<SearchEmpty />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ExtensionStore;
|