Skip to content

Commit

Permalink
add index to each items to avoid command selecting more than one elem…
Browse files Browse the repository at this point in the history
…ents with the same name
  • Loading branch information
HarryYu02 committed Apr 26, 2024
1 parent 17e60ab commit f8f0a24
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/components/BookmarkList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ const BookmarkList = ({
}) => {
return (
<>
{bookmarkData.map((bookmark) => {
{bookmarkData.map((bookmark, index) => {
return (
<ListItem
key={`bookmark-${bookmark.id}`}
type="bookmark"
item={bookmark}
keywords={[bookmark.url ?? "", "bookmarks"]}
index={index}
/>
);
})}
Expand Down
34 changes: 19 additions & 15 deletions src/components/CommandActions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button } from "./ui/button";
import { MdDelete } from "react-icons/md";
import { MdDelete, MdFolderCopy } from "react-icons/md";
import {
Command,
CommandInput,
Expand Down Expand Up @@ -87,22 +87,26 @@ const CommandActions = ({
}}
className="flex items-center gap-2"
>
<MdDelete className="size-4" />
<MdDelete className="size-4 p-0" />
Close Tab
</CommandItem>
{/* TODO: Duplicate item name */}
{/* <CommandItem
onSelect={() => {
chrome.tabs
.duplicate(target.id ?? chrome.tabs.TAB_ID_NONE)
.catch((error) => {
console.log(error);
});
window.close();
}}
>
Copy Tab
</CommandItem> */}
<CommandItem
onSelect={() => {
chrome.tabs
.duplicate(
target.id ??
chrome.tabs.TAB_ID_NONE
)
.catch((error) => {
console.log(error);
});
window.close();
}}
className="flex items-center gap-2"
>
<MdFolderCopy className="size-4 p-0" />
Copy Tab
</CommandItem>
</>
) : target && itemType === "bookmark" ? (
<>
Expand Down
13 changes: 10 additions & 3 deletions src/components/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ interface CmdItem {

type ListItemProps = (BookmarkItem | TabItem | CmdItem) & {
forceMount?: boolean;
index: number;
};

const ListItem = ({ type, item, forceMount, keywords }: ListItemProps) => {
const ListItem = ({
type,
item,
forceMount,
keywords,
index,
}: ListItemProps) => {
const onSelectHandler = () => {
switch (type) {
case "tab":
Expand Down Expand Up @@ -66,9 +73,9 @@ const ListItem = ({ type, item, forceMount, keywords }: ListItemProps) => {
className="flex w-full items-center justify-between"
onSelect={onSelectHandler}
value={
type === "command"
(type === "command"
? `command-${item.name}`
: `${type}-${item.title}`
: `${type}-${item.title}`) + `-${index}`
}
forceMount={forceMount}
keywords={keywords}
Expand Down
1 change: 1 addition & 0 deletions src/components/SearchListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const SearchListItem = ({ searchText }: SearchListItemProps) => {
},
}}
keywords={[]}
index={0}
/>
);
};
Expand Down
8 changes: 5 additions & 3 deletions src/components/SearchingList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ const SearchingList = () => {
commandList?.scrollTo(0, 0);
}, [searchText]);

const item = currentItem.indexOf("-");
const itemType = currentItem.slice(0, item) as ItemType;
const itemValue = currentItem.slice(item + 1);
const prefixIndex = currentItem.indexOf("-");
const suffixIndex = currentItem.lastIndexOf("-");
const itemType = currentItem.slice(0, prefixIndex) as ItemType;
const itemValue = currentItem.slice(prefixIndex + 1, suffixIndex);
// const itemIdentifier = currentItem.slice(suffixIndex + 1);

return (
<Command
Expand Down
4 changes: 2 additions & 2 deletions src/components/TabList.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import useTabs from "@/hooks/useTabs";
import ListItem from "./ListItem";

const TabList = ({ tabData }: { tabData: chrome.tabs.Tab[] }) => {
return (
<>
{tabData.map((tab) => {
{tabData.map((tab, index) => {
return (
<ListItem
key={`tab-${tab.id}`}
type="tab"
item={tab}
keywords={[tab.url ?? "", "tabs"]}
index={index}
/>
);
})}
Expand Down
1 change: 1 addition & 0 deletions src/components/ThemeListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const ThemeListItem = ({ targetTheme }: { targetTheme: Theme }) => {
},
}}
keywords={[`${targetTheme} mode`]}
index={targetTheme.length}
/>
);
};
Expand Down

0 comments on commit f8f0a24

Please sign in to comment.