fix: discard file content cause lots of logs

fixes #653
This commit is contained in:
lin onetwo 2025-11-19 00:56:06 +08:00
parent 3157d0f17b
commit 3161113a67
4 changed files with 86 additions and 20 deletions

View file

@ -91,4 +91,47 @@ Feature: Git Log Window
And I wait for 2 seconds for "file system events to stabilize after git revert"
# The modified content should be reverted, and make sure file won't be deleted
Then I should not see a "missing tiddler indicator" element in browser view with selector "[data-tiddler-title='Index']:has-text('')"
Then I should not see a "modified content in Index tiddler" element in browser view with selector "[data-tiddler-title='Index']:has-text('Modified Index content')"
Then I should not see a "modified content in Index tiddler" element in browser view with selector "[data-tiddler-title='Index']:has-text('Modified Index content')"
@git
Scenario: Discard uncommitted changes for a single file
# Modify the existing Index.tid file to create uncommitted changes
And I modify file "{tmpDir}/wiki/tiddlers/Index.tid" to contain "Discard test content - should be reverted!"
Then I wait for tiddler "Index" to be updated by watch-fs
And I wait for 1 seconds for "git to detect file changes"
# Open Git Log window
When I click menu " > "
And I wait for 1 seconds for "git log window to open"
And I should see "Discard test content" in the browser view content
And I switch to "gitHistory" window
And I wait for the page to load completely
# Wait for git log data to load
And I wait for 2 seconds for "git log data to load"
Then I should see a "uncommitted changes row" element with selector "tr:has-text('')"
# Click on the uncommitted changes row
When I click on a "uncommitted changes row" element with selector "tr:has-text('')"
And I wait for 0.5 seconds for "file list to populate"
# Verify we can see the modified Index.tid file
Then I should see a "Index.tid file in uncommitted list" element with selector "li:has-text('Index.tid')"
# Click on the Index.tid file to select it
When I click on a "Index.tid file in list" element with selector "li:has-text('Index.tid')"
And I wait for 1 seconds for "file diff to load in right panel"
# Verify the file diff panel has loaded by checking for the file name header
Then I should see a "file name header in diff panel" element with selector "h6:has-text('Index.tid')"
# Click the Actions tab in the file diff panel (the one that has the file name above it)
# We need to find the Actions tab that is a sibling of the h6 containing "Index.tid"
When I click on a "actions tab in file diff panel" element with selector "h6:has-text('Index.tid') ~ div button[role='tab']:has-text(''), h6:has-text('Index.tid') ~ div button[role='tab']:has-text('Actions')"
And I wait for 1 seconds for "actions tab content to render"
# Verify the discard changes button exists (only shows for uncommitted changes)
Then I should see a "discard changes button" element with selector "button:has-text(''), button:has-text('Discard changes')"
When I click on a "discard changes button" element with selector "button:has-text(''), button:has-text('Discard changes')"
# Wait for git discard operation to complete
And I wait for 2 seconds for "git discard to complete and UI to refresh"
# Verify the file is no longer in the uncommitted list (should go back to showing no selection)
Then I should not see a "Index.tid file still selected" element with selector "li:has-text('Index.tid')[class*='selected']"
# Switch back to main window to verify the discard
When I switch to "main" window
# Wait for file system events to stabilize after git discard
And I wait for 2 seconds for "file system events to stabilize after git discard"
# The modified content should be discarded
Then I should not see a "modified content in Index tiddler" element in browser view with selector "[data-tiddler-title='Index']:has-text('Discard test content')"

View file

@ -135,9 +135,10 @@ const ImagePreview = styled('img')`
interface IFileDiffPanelProps {
commitHash: string;
filePath: string | null;
onDiscardSuccess?: () => void;
}
export function FileDiffPanel({ commitHash, filePath }: IFileDiffPanelProps): React.JSX.Element {
export function FileDiffPanel({ commitHash, filePath, onDiscardSuccess }: IFileDiffPanelProps): React.JSX.Element {
const { t } = useTranslation();
const [diff, setDiff] = useState<string>('');
const [fileContent, setFileContent] = useState<string>('');
@ -191,7 +192,8 @@ export function FileDiffPanel({ commitHash, filePath }: IFileDiffPanelProps): Re
try {
await window.service.git.discardFileChanges(workspace.wikiFolderLocation, filePath);
// TODO: Show success message and refresh file list
// Clear selection and trigger refresh
onDiscardSuccess?.();
} catch (error) {
console.error('Failed to discard changes:', error);
// TODO: Show error message

View file

@ -381,6 +381,11 @@ export default function GitHistory(): React.JSX.Element {
<FileDiffPanel
commitHash={selectedCommit?.hash || ''}
filePath={selectedFile}
onDiscardSuccess={() => {
setSelectedFile(null);
// Trigger git log refresh after discard
setShouldSelectFirst(true);
}}
/>
</DiffPanelWrapper>
</ContentWrapper>

View file

@ -1,6 +1,6 @@
import type { IWorkspace } from '@services/workspaces/interface';
import useObservable from 'beautiful-react-hooks/useObservable';
import { useEffect, useMemo, useState } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
import { filter } from 'rxjs/operators';
import type { GitLogEntry } from './types';
@ -22,6 +22,8 @@ export function useGitLogData(): IGitLogData {
const [workspaceInfo, setWorkspaceInfo] = useState<IWorkspace | null>(null);
const [refreshTrigger, setRefreshTrigger] = useState(0);
const [lastChangeType, setLastChangeType] = useState<string | null>(null);
const lastLoggedEntriesCount = useRef<number>(0);
const lastRefreshTime = useRef<number>(0);
// Get workspace info once
useEffect(() => {
@ -68,10 +70,19 @@ export function useGitLogData(): IGitLogData {
);
useObservable(gitStateChange$, (change) => {
// Store the type of change so we can auto-select first commit after a manual commit
setLastChangeType(change?.type ?? null);
// Trigger refresh when git state changes
setRefreshTrigger((previous) => previous + 1);
// Debounce git state changes to prevent excessive refreshes
// Git operations (like discard) may trigger multiple file system events
const now = Date.now();
const timeSinceLastRefresh = now - lastRefreshTime.current;
// Allow immediate refresh if enough time has passed (300ms)
if (timeSinceLastRefresh >= 300) {
lastRefreshTime.current = now;
// Store the type of change so we can auto-select first commit after a manual commit
setLastChangeType(change?.type ?? null);
// Trigger refresh when git state changes
setRefreshTrigger((previous) => previous + 1);
}
});
// Load git log data
@ -116,11 +127,12 @@ export function useGitLogData(): IGitLogData {
requestAnimationFrame(() => {
setEntries(entriesWithFiles);
setCurrentBranch(result.currentBranch);
// Log for E2E test timing - indicates UI has been updated with new commits
void window.service.native.log('info', '[test-id-git-log-refreshed]', {
commitCount: entriesWithFiles.length,
wikiFolderLocation: workspaceInfo.wikiFolderLocation,
});
});
// Log for E2E test timing - only log once per load, not in requestAnimationFrame
void window.service.native.log('info', '[test-id-git-log-refreshed]', {
commitCount: entriesWithFiles.length,
wikiFolderLocation: workspaceInfo.wikiFolderLocation,
});
} catch (error_) {
const error = error_ as Error;
@ -140,13 +152,17 @@ export function useGitLogData(): IGitLogData {
// Log when entries are updated and rendered to DOM
useEffect(() => {
if (entries.length > 0 && workspaceInfo && 'wikiFolderLocation' in workspaceInfo) {
// Use setTimeout to ensure DOM has been updated after state changes
setTimeout(() => {
void window.service.native.log('info', '[test-id-git-log-data-rendered]', {
commitCount: entries.length,
wikiFolderLocation: workspaceInfo.wikiFolderLocation,
});
}, 100);
// Only log if the entries count actually changed (to avoid logging on every re-render)
if (lastLoggedEntriesCount.current !== entries.length) {
lastLoggedEntriesCount.current = entries.length;
// Use setTimeout to ensure DOM has been updated after state changes
setTimeout(() => {
void window.service.native.log('info', '[test-id-git-log-data-rendered]', {
commitCount: entries.length,
wikiFolderLocation: workspaceInfo.wikiFolderLocation,
});
}, 100);
}
}
}, [entries, workspaceInfo]);