Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter by whole file path instead of file name #176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/js/components/folder/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,28 @@ class Folder extends React.Component {
this.state = {}
}

getHighlight ({ nodeLabel, filter, index }) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try and move this to the shared lib.js but it didn't work because of the React stuff, so settled for just having a copy in Folder

const prefix = nodeLabel.substring(0, index)
const middle = nodeLabel.substring(index, index + filter.length)
const suffix = nodeLabel.substring(index + filter.length)
return (
<>
{prefix}
{middle ? <span className='github-pr-file-highlight-filter'>{middle}</span> : null}
{suffix}
</>
)
}

render () {
const { children, nodeLabel, isViewed } = this.props
const { children, nodeLabel, isViewed, filter } = this.props

const index = filter ? (nodeLabel.toLowerCase() || '').indexOf(filter.toLowerCase()) : -1
const displayName = (index === -1) ? nodeLabel : this.getHighlight({ nodeLabel, filter, index })

const display = (
<div>
{nodeLabel}
{displayName}
{isViewed
? (
<svg className='github-pr-file-checkmark octicon octicon-check' viewBox='0 0 12 16' version='1.1' width='12' height='16' aria-hidden='true'>
Expand Down
10 changes: 6 additions & 4 deletions src/js/components/tree/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fullScreenStorageKey = '__better_github_pr_full_screen'
class Tree extends React.Component {
constructor (props) {
super(props)
this.onReloadTree = this.props.reloadTree
this.handleReloadTree = this.props.reloadTree

this.handleClose = this.handleClose.bind(this)
this.onScroll = this.onScroll.bind(this)
Expand Down Expand Up @@ -174,12 +174,14 @@ class Tree extends React.Component {
}

render () {
const { root, filter, show, visibleElement } = this.state
const { filter, show, visibleElement } = this.state

if (!show) {
return null
}

const filtered = createFileTree(filter).tree
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally I didn't have this here, but when the tree was reloaded for a reason other than the filter changing (e.g. sync button pressed, or "viewed" checkbox clicked) it would add back folders that should not be visible.

Not sure if this is the best way to address that, but it seems to work fine.


return (
<div>
<div className='_better_github_pr_resizer' ref={node => { this.resizer = node }} />
Expand All @@ -189,11 +191,11 @@ class Tree extends React.Component {
onFullWidth={this.handleFullWidth}
onOptions={this.handleOptions}
onClose={this.handleClose}
onReloadTree={this.onReloadTree}
onReloadTree={this.handleReloadTree}
berzniz marked this conversation as resolved.
Show resolved Hide resolved
/>
<div className='file-container'>
<div>
{root.list.map(node => (
{filtered.list.map(node => (
<span key={node.nodeLabel}>
{createTree({ ...node, visibleElement, filter })}
</span>
Expand Down
7 changes: 6 additions & 1 deletion src/js/createTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ export const createTree = ({ nodeLabel, list, href, hasComments, isDeleted, diff
const rawChildren = list.map(node => createTree({ ...node, visibleElement, filter }))

return (
<Folder key={nodeLabel} nodeLabel={nodeLabel} isViewed={rawChildren.every(child => child.props.isViewed)}>
<Folder
key={nodeLabel}
nodeLabel={nodeLabel}
isViewed={rawChildren.every(child => child.props.isViewed)}
filter={filter}
>
{rawChildren.map(node => (
<span key={node.key}>
{node}
Expand Down
4 changes: 2 additions & 2 deletions src/js/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ export const createFileTree = (filter = EMPTY_FILTER) => {
diffElements: []
}

files.forEach(({ parts, href }) => {
files.forEach(({ title, parts, href }) => {
let location = tree
if (filterItem(parts[parts.length - 1], filter)) {
if (filterItem(title, filter)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the main change, I haven't made it optional but I'm happy to if you'd like

parts.forEach((part, index) => {
let node = location.list.find(node => node.nodeLabel === part)
if (!node) {
Expand Down