| 1 | import { RangeInFile } from "core"; |
| 2 | import { findUriInDirs, getUriPathBasename } from "core/util/uri"; |
| 3 | import { useContext } from "react"; |
| 4 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 5 | import FileIcon from "../FileIcon"; |
| 6 | import { ToolTip } from "../gui/Tooltip"; |
| 7 | |
| 8 | interface FilenameLinkProps { |
| 9 | rif: RangeInFile; |
| 10 | } |
| 11 | |
| 12 | function FilenameLink({ rif }: FilenameLinkProps) { |
| 13 | const ideMessenger = useContext(IdeMessengerContext); |
| 14 | |
| 15 | function onClick() { |
| 16 | ideMessenger.post("showLines", { |
| 17 | filepath: rif.filepath, |
| 18 | startLine: rif.range.start.line, |
| 19 | endLine: rif.range.end.line, |
| 20 | }); |
| 21 | } |
| 22 | |
| 23 | let relPathOrBasename = ""; |
| 24 | try { |
| 25 | const { relativePathOrBasename } = findUriInDirs( |
| 26 | rif.filepath, |
| 27 | window.workspacePaths ?? [], |
| 28 | ); |
| 29 | relPathOrBasename = relativePathOrBasename; |
| 30 | } catch (e) { |
| 31 | return <span>{getUriPathBasename(rif.filepath)}</span>; |
| 32 | } |
| 33 | |
| 34 | return ( |
| 35 | <ToolTip place="top" content={"/" + relPathOrBasename}> |
| 36 | <span |
| 37 | data-tooltip-delay-show={500} |
| 38 | className="mx-[0.1em] mb-[0.15em] inline-flex cursor-pointer items-center gap-0.5 rounded-md pr-[0.2em] align-middle hover:ring-1" |
| 39 | onClick={onClick} |
| 40 | > |
| 41 | <FileIcon filename={rif.filepath} height="20px" width="20px" /> |
| 42 | <span className="mb-0.5 align-baseline underline underline-offset-2"> |
| 43 | {getUriPathBasename(rif.filepath)} |
| 44 | </span> |
| 45 | </span> |
| 46 | </ToolTip> |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | export default FilenameLink; |