-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Star list doesn't update when starring folder through context menu
- Loading branch information
1 parent
d238311
commit 8623c4a
Showing
6 changed files
with
111 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"use client"; | ||
|
||
import path from "path"; | ||
|
||
import React from "react"; | ||
import { Button } from "@nextui-org/button"; | ||
import { Tooltip } from "@nextui-org/tooltip"; | ||
import { ContextMenuItem, useContextMenu } from "use-context-menu"; | ||
|
||
import { getFolderIcon } from "./explorer-item"; | ||
|
||
import { parseStringPath, useExplorer } from "@/hooks/useExplorer"; | ||
import { useFolder } from "@/hooks/useFolder"; | ||
|
||
interface StarredItemProps { | ||
itemPath: string | ||
} | ||
|
||
const StarredItem: React.FC<StarredItemProps> = ({ itemPath }) => { | ||
const explorer = useExplorer(); | ||
const itemName = path.basename(itemPath); | ||
const folder = useFolder(itemPath.replace(explorer.disk, "")); | ||
|
||
const handleOpen = () => { | ||
explorer.setPath(parseStringPath(itemPath)); | ||
}; | ||
|
||
const handleUnstar = () => { | ||
folder.toggleStar(); | ||
}; | ||
|
||
const { contextMenu, onContextMenu } = useContextMenu( | ||
<> | ||
<ContextMenuItem onSelect={() => handleOpen()}>打开</ContextMenuItem> | ||
<ContextMenuItem onSelect={() => handleUnstar()}>取消星标</ContextMenuItem> | ||
</> | ||
); | ||
|
||
return ( | ||
<div | ||
className="inline-block m-1" | ||
aria-label={itemName}> | ||
<Tooltip content={itemPath}> | ||
<Button | ||
color="default" | ||
variant="flat" | ||
size="sm" | ||
onPress={() => handleOpen()} | ||
onContextMenu={onContextMenu}> | ||
{getFolderIcon(itemName)} | ||
{itemName} | ||
</Button> | ||
</Tooltip> | ||
|
||
{contextMenu} | ||
</div> | ||
); | ||
} | ||
|
||
export default StarredItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useEffect } from "react"; | ||
|
||
import { emitter } from "@/lib/emitter"; | ||
|
||
type EmitterInstance = [string, (...args: any[]) => any]; | ||
|
||
/** | ||
* Create an event listener with `EventEmitter` | ||
* | ||
* @example | ||
* ```ts | ||
* useEmitter([ | ||
* ["foo", () => console.log("bar")] | ||
* ]); | ||
* | ||
* // in somewhere... | ||
* new Emitter().emit("foo"); // bar | ||
* ``` | ||
*/ | ||
export function useEmitter(instances: EmitterInstance[]) { | ||
useEffect(() => { | ||
instances.forEach((instance: EmitterInstance) => { | ||
emitter.on(instance[0], (...args: any[]) => instance[1](...args)); | ||
}); | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useState } from "react"; | ||
|
||
export function useForceUpdate() { | ||
const [, setCount] = useState(0); | ||
|
||
return () => setCount((prev) => prev + 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import EventEmitter from "events"; | ||
|
||
export const emitter = new EventEmitter(); |