E526 `restore-file` - Add support for new PR view by maschwenk · Pull Request #8870 · refined-github/refined-github · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@maschwenk
Copy link
Contributor
@maschwenk maschwenk commented Jan 14, 2026

Fixes

Reimplementation of

addressing the maintainer's feedback, trying to sidestep drama. In honesty I don't totally know how to test this.

Changes

  • Added support for the new React-based PR files view while keeping the legacy view handling completely unchanged
  • Use GitHubFileURL helper for URL parsing instead of manual regex
  • Track focused file container via a global variable (focusedFileContainer) for removal after discard operation
  • Use the octicon library (GitCompareIcon) for the menu item icon
  • Handle renamed files by checking React props for RENAMED status with the suggested conditional expression
  • Use handleMenuOpening pattern from more-file-links.tsx for adding menu items

Key differences from previous PR

  1. Legacy code unchanged: The legacy view handling (addLegacyMenuItem, observe) remains exactly as it was
  2. Global file tracking: Following the suggestion to save the file container on menu open for later removal
  3. Cleaner URL parsing: Using GitHubFileURL instead of manual string manipulation
  4. Proper getFilenames function: Lives in restore-file.tsx where it belongs, returns {original, new} as suggested

Test URLs

Fixes: refined-github#8714

- Added support for the new React-based PR files view
- Keep legacy view handling unchanged
- Use GitHubFileURL for URL parsing
- Track focused file container via global variable for removal after discard
- Use octicon library (GitCompareIcon) for the menu item icon
- Handle renamed files by checking React props for RENAMED status

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@github-actions github-actions bot added the bug label Jan 14, 2026
@maschwenk maschwenk marked this pull request as ready for review January 14, 2026 14:53
10BC0
maschwenk and others added 2 commits January 14, 2026 12:03
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The menu is created fresh on each click, so items can't already exist.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Comment on lines 107 to 127
// New React view: get filenames from the View File link and React props
const fileAnchor = menuItem
.closest('ul')!
.querySelector<HTMLAnchorElement>('li:has(svg.octicon-eye) a')!;

const fileUrl = new GitHubFileURL(fileAnchor.href);
const newFileName = fileUrl.filePath;

// Check if the file was renamed by looking at the React props
const reactPropsElement = $('[data-target="react-app.embeddedData"]');
const reactProps = JSON.parse(reactPropsElement.textContent);

const diffContents = reactProps.payload.diffContents.find(
(dc: {path: string}) => dc.path === newFileName,
);

const originalFileName = diffContents?.status === 'RENAMED'
? diffContents.oldTreeEntry.path
: newFileName;

return {original: originalFileName, new: newFileName};
Copy link
Member
@SunsetTechuila SunsetTechuila Jan 14, 2026

Choose a reason for hiding this comment

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

You can do:

const [originalFileName, newFileName = originalFileName] = $('.Link--primary span:not(.sr-only)', focusedFileContainer).textContent.split('  ')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

94a7d16

done

Copy link
Member
@SunsetTechuila SunsetTechuila Jan 15, 2026

Choose a reason for hiding this comment

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

The variant I previously suggested works only for renamed files and doesn't account for invisible LRM chars.

Here is a better one:

	const fileNameElement = $('[class^="DiffFileHeader-module__file-name"]', focusedFileContainer);
	const span = $optional('span:not(.sr-only)', fileNameElement);
	const [originalFileName, newFileName = originalFileName] = (span ?? fileNameElement)
		// eslint-disable-next-line unicorn/prefer-string-replace-all -- Invisible char
		.textContent.split('  ').map(text => text.replaceAll(/\u200E/g, ''));

Use focusedFileContainer to get filenames directly from the file header,
matching the pattern used in the legacy view.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

function getFilenames(menuItem: HTMLElement): {original: string; new: string} {
// Legacy view: get filenames from the data-path and Link--primary elements
// TODO: Drop in June 2026
Copy link
Member
@SunsetTechuila SunsetTechuila Jan 15, 2026

Choose a reason for hiding this comment

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

This is probably premature. The policy is to keep support for removed UI for 6 months. The React-based view is still in preview and could remain there for a couple more months. The same applies to lines 133, 145, and 181.

Comment on lines 161 to 162
focusedFileContainer = delegateTarget.closest('[class*="DiffFileHeader-module__diff-file-header"]')!
.parentElement!;
Copy link
Member
@SunsetTechuila SunsetTechuila Jan 15, 2026

Choose a reason for hiding this comment

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

You are selecting only a header

Suggested change
focusedFileContainer = delegateTarget.closest('[class*="DiffFileHeader-module__diff-file-header"]')!
.parentElement!;
focusedFileContainer = delegateTarget.closest('div[id^="diff-"]')!;

maschwenk and others added 3 commits January 14, 2026 22:58
The React-based view is still in preview, so we shouldn't mark legacy
code for removal yet.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Use DiffFileHeader-module__file-name selector
- Use $optional for span to handle both renamed and non-renamed files
- Strip invisible LRM characters from filenames

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Select the whole diff container (div[id^="diff-"]) instead of just the
header, so the entire file diff is removed after discard.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@maschwenk
Copy link
Contributor Author

@SunsetTechuila I was also able to follow the good instructions in https://github.com/refined-github/refined-github/wiki/Contributing to help me actually test this by the end. And I can confirm it's working! Thanks so much!

@maschwenk
Copy link
Contributor Author

@SunsetTechuila @fregante do I need to do anything to make it mergeable? 😆 I want this so badly it's my favorite feature of RG

@SunsetTechuila
Copy link
Member

do I need to do anything

Only wait a bit

@fregante fregante changed the title restore-file - Add support for new React view restore-file - Add support for new PR view Jan 22, 2026
@fregante fregante merged commit 39c1c4f into refined-github:main Jan 22, 2026
11 checks passed
@fregante
Copy link
Member

Thanks @maschwenk!

@SunsetTechuila SunsetTechuila linked an issue Jan 25, 2026 that may be closed by this pull request
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

restore-file broken in new GitHub PR File List view

3 participants

0