Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | import React, { useCallback, useEffect, useMemo, useState } from "react"; import { Button } from "ui/buttons/Button"; import { FlexGrow } from "ui/spacing/Spacing"; import API from "renderer/lib/api"; import l10n from "shared/lib/lang/l10n"; import type { PluginMetadata, PluginRepositoryEntry, PluginRepositoryMetadata, } from "lib/pluginManager/types"; import { FlatList } from "ui/lists/FlatList"; import { EntityListItem } from "ui/lists/EntityListItem"; import { Input } from "ui/form/Input"; import { StyledPluginItemRow, StyledPluginManagerWindow, StyledPluginManagerListColumn, StyledPluginManagerSearch, StyledPluginManagerSearchResults, StyledPluginManagerNoResults, StyledPluginManagerRepoForm, StyledPluginManagerRepoBtns, StyledPluginItemRowRepoName, StyledPluginItemRowRepoBtns, StyledPluginItemRowRepoUrl, } from "./style"; import { TextField } from "ui/form/TextField"; import useResizeObserver from "ui/hooks/use-resize-observer"; import { CloseIcon } from "ui/icons/Icons"; import { ConsistentWidthLabel } from "ui/util/ConsistentWidthLabel"; export type RepoItem = { id: string; name: string; plugin: PluginMetadata; repo: PluginRepositoryMetadata; installedVersion?: string; updateAvailable: boolean; }; const protectedIds = ["core"]; type PluginManagerRepoAction = "none" | "add" | "remove"; interface PluginsManagerReposProps { onClose: () => void; } const PluginsManagerRepos = ({ onClose }: PluginsManagerReposProps) => { const [listRef, listSize] = useResizeObserver<HTMLDivElement>(); const [loading, setLoading] = useState(true); const [action, setAction] = useState<PluginManagerRepoAction>("none"); const [repoItems, setRepoItems] = useState<PluginRepositoryEntry[]>([]); const [searchTerm, setSearchTerm] = useState(""); const [selectedId, setSelectedId] = useState(""); const [url, setUrl] = useState(""); const refreshData = useCallback(async () => { const reposList = await API.pluginManager.getPluginRepos(); setRepoItems(reposList); setLoading(false); }, []); useEffect(() => { refreshData(); }, [refreshData]); const filteredRepoItems = useMemo(() => { return repoItems .filter((item) => { const searchKey = `${item.url} ${item.name}`.toLocaleUpperCase(); const search = searchTerm.toLocaleUpperCase(); return searchKey.includes(search); }) .sort((a, b) => { const isCoreA = a.id === "core"; const isCoreB = b.id === "core"; if (isCoreA && !isCoreB) { return -1; } else if (!isCoreA && isCoreB) { return 1; } else { return a.id.localeCompare(b.id); } }); }, [repoItems, searchTerm]); const renderLabel = useCallback( (item: PluginRepositoryEntry) => { return ( <StyledPluginItemRow> <StyledPluginItemRowRepoName>{item.name}</StyledPluginItemRowRepoName> <StyledPluginItemRowRepoUrl>{item.url}</StyledPluginItemRowRepoUrl> <StyledPluginItemRowRepoBtns> {!protectedIds.includes(item.id) && ( <Button disabled={action !== "none"} size="small" title={l10n("FIELD_DELETE_REPOSITORY")} onClick={async () => { Iif (action !== "none") { return; } setAction("remove"); await API.pluginManager.removePluginRepo(item.url); refreshData(); setAction("none"); }} > <CloseIcon /> </Button> )} </StyledPluginItemRowRepoBtns> </StyledPluginItemRow> ); }, [action, refreshData] ); const height = listSize.height ?? 200; useEffect(() => { window.resizeTo(window.innerWidth, 367 + 28); }, []); return ( <StyledPluginManagerWindow> <StyledPluginManagerListColumn> <StyledPluginManagerSearch> <Input type="search" placeholder={l10n("TOOLBAR_SEARCH")} value={searchTerm} onChange={(e) => setSearchTerm(e.currentTarget.value)} /> </StyledPluginManagerSearch> <StyledPluginManagerSearchResults ref={listRef}> {filteredRepoItems.length > 0 ? ( <FlatList selectedId={selectedId} items={filteredRepoItems} setSelectedId={setSelectedId} height={height} children={({ item }) => ( <EntityListItem item={item} type={"custom"} renderLabel={renderLabel} /> )} /> ) : ( <StyledPluginManagerNoResults> {l10n(loading ? "FIELD_LOADING" : "FIELD_NO_RESULTS")} </StyledPluginManagerNoResults> )} </StyledPluginManagerSearchResults> </StyledPluginManagerListColumn> <StyledPluginManagerRepoForm> <TextField name="url" label={l10n("FIELD_REPOSITORY_URL")} placeholder="https://www.example.com/repository.json" value={url} onChange={(e) => setUrl(e.currentTarget.value)} ></TextField> <StyledPluginManagerRepoBtns> <Button disabled={action !== "none"} onClick={async () => { Iif (action !== "none") { return; } setAction("add"); await API.pluginManager.addPluginRepo(url); refreshData(); setAction("none"); setUrl(""); }} > <ConsistentWidthLabel label={ action === "add" ? l10n("FIELD_ADDING") : l10n("FIELD_ADD_REPOSITORY") } possibleValues={[ l10n("FIELD_INSTALLING"), l10n("FIELD_ADD_REPOSITORY"), ]} /> </Button> <FlexGrow /> <Button onClick={onClose}>{l10n("FIELD_CLOSE")}</Button> </StyledPluginManagerRepoBtns> </StyledPluginManagerRepoForm> </StyledPluginManagerWindow> ); }; export default PluginsManagerRepos; |