| 1 | import { RangeInFile } from "core"; |
| 2 | import { findUriInDirs, getUriPathBasename } from "core/util/uri"; |
| 3 | import { useContext } from "react"; |
| 4 | import { v4 as uuidv4 } from "uuid"; |
| 5 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 6 | import FileIcon from "../FileIcon"; |
| 7 | import { ToolTip } from "../gui/Tooltip"; |
| 8 | |
| 9 | interface FilenameLinkProps { |
| 10 | rif: RangeInFile; |
| 11 | } |
| 12 | |
| 13 | function FilenameLink({ rif }: FilenameLinkProps) { |
| 14 | const ideMessenger = useContext(IdeMessengerContext); |
| 15 | |
| 16 | function onClick() { |
| 17 | ideMessenger.post("showLines", { |
| 18 | filepath: rif.filepath, |
| 19 | startLine: rif.range.start.line, |
| 20 | endLine: rif.range.end.line, |
| 21 | }); |
| 22 | } |
| 23 | |
| 24 | const id = uuidv4(); |
| 25 | |
| 26 | let relPathOrBasename = ""; |
| 27 | try { |
| 28 | const { relativePathOrBasename } = findUriInDirs( |
| 29 | rif.filepath, |
| 30 | window.workspacePaths ?? [], |
| 31 | ); |
| 32 | relPathOrBasename = relativePathOrBasename; |
| 33 | } catch (e) { |
| 34 | return <span>{getUriPathBasename(rif.filepath)}</span>; |
| 35 | } |
| 36 | |
| 37 | return ( |
| 38 | <> |
| 39 | <span |
| 40 | data-tooltip-id={id} |
| 41 | data-tooltip-delay-show={500} |
| 42 | 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" |
| 43 | onClick={onClick} |
| 44 | > |
| 45 | <FileIcon filename={rif.filepath} height="20px" width="20px" /> |
| 46 | <span className="mb-0.5 align-baseline underline underline-offset-2"> |
| 47 | {getUriPathBasename(rif.filepath)} |
| 48 | </span> |
| 49 | </span> |
| 50 | <ToolTip id={id} place="top"> |
| 51 | {"/" + relPathOrBasename} |
| 52 | </ToolTip> |
| 53 | </> |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | export default FilenameLink; |