Skip to content

Commit

Permalink
fix(code/frontend): should reset file tree if revison changes (#39988)
Browse files Browse the repository at this point in the history
  • Loading branch information
WangQianliang authored Jul 4, 2019
1 parent d79ac8f commit f2cb776
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 6 deletions.
5 changes: 4 additions & 1 deletion x-pack/legacy/plugins/code/public/actions/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface RepoTreePayload {
tree: FileTree;
path: string;
withParents: boolean | undefined;
revision: string;
}

export interface TreeCommitPayload {
Expand All @@ -48,7 +49,9 @@ export interface TreeCommitPayload {
}

export const fetchRootRepoTree = createAction<FetchRepoPayloadWithRevision>('FETCH ROOT REPO TREE');
export const fetchRootRepoTreeSuccess = createAction<FileTree>('FETCH ROOT REPO TREE SUCCESS');
export const fetchRootRepoTreeSuccess = createAction<{ tree: FileTree; revision: string }>(
'FETCH ROOT REPO TREE SUCCESS'
);
export const fetchRootRepoTreeFailed = createAction<Error>('FETCH ROOT REPO TREE FAILED');

export const fetchRepoTree = createAction<FetchRepoTreePayload>('FETCH REPO TREE');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ test('render correctly', () => {
location={location}
closeTreePath={mockFunction}
openTreePath={mockFunction}
isNotFound={false}
/>
)
.toJSON();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface Props extends RouteComponentProps<MainRouteParams> {
closeTreePath: (paths: string) => void;
openTreePath: (paths: string) => void;
openedPaths: string[];
isNotFound: boolean;
}

export class CodeFileTree extends React.Component<Props> {
Expand Down Expand Up @@ -238,13 +239,15 @@ export class CodeFileTree extends React.Component<Props> {
}

private isPathOpen(path: string) {
if (this.props.isNotFound) return false;
return this.props.openedPaths.includes(path);
}
}

const mapStateToProps = (state: RootState) => ({
node: state.fileTree.tree,
openedPaths: state.fileTree.openedPaths,
isNotFound: state.file.isNotFound,
});

const mapDispatchToProps = {
Expand Down
11 changes: 9 additions & 2 deletions x-pack/legacy/plugins/code/public/reducers/file_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface FileTreeState {
fileTreeLoadingPaths: string[];
// store not found directory as an array to calculate `notFound` flag by finding whether path is in this array
notFoundDirs: string[];
revision: string;
}

const initialState: FileTreeState = {
Expand All @@ -74,6 +75,7 @@ const initialState: FileTreeState = {
openedPaths: [],
fileTreeLoadingPaths: [''],
notFoundDirs: [],
revision: '',
};

type FileTreePayload = FetchRepoTreePayload & RepoTreePayload & FileTree & string;
Expand All @@ -86,6 +88,7 @@ export const fileTree = handleActions<FileTreeState, FileTreePayload>(
}),
[String(fetchRepoTreeSuccess)]: (state, action: Action<RepoTreePayload>) =>
produce<FileTreeState>(state, draft => {
draft.revision = action.payload!.revision;
draft.notFoundDirs = draft.notFoundDirs.filter(dir => dir !== action.payload!.path);
draft.fileTreeLoadingPaths = draft.fileTreeLoadingPaths.filter(
p => p !== action.payload!.path && p !== ''
Expand All @@ -108,10 +111,14 @@ export const fileTree = handleActions<FileTreeState, FileTreePayload>(
}
}
}),
[String(fetchRootRepoTreeSuccess)]: (state, action: Action<FileTree>) =>
[String(fetchRootRepoTreeSuccess)]: (
state,
action: Action<{ revision: string; tree: FileTree }>
) =>
produce<FileTreeState>(state, draft => {
draft.fileTreeLoadingPaths = draft.fileTreeLoadingPaths.filter(p => p !== '/' && p !== '');
draft.tree = mergeNode(draft.tree, action.payload!);
draft.tree = mergeNode(draft.tree, action.payload!.tree);
draft.revision = action.payload!.revision;
}),
[String(fetchRootRepoTreeFailed)]: state =>
produce<FileTreeState>(state, draft => {
Expand Down
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/code/public/sagas/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
repoScopeSelector,
urlQueryStringSelector,
createTreeSelector,
getTreeRevision,
} from '../selectors';
import { history } from '../utils/url';
import { mainRoutePattern } from './patterns';
Expand Down Expand Up @@ -189,8 +190,9 @@ function* handleMainRouteChange(action: Action<Match>) {
}
const lastRequestPath = yield select(lastRequestPathSelector);
const currentTree: FileTree = yield select(getTree);
const currentTreeRevision: string = yield select(getTreeRevision);
// repo changed
if (currentTree.repoUri !== repoUri) {
if (currentTree.repoUri !== repoUri || revision !== currentTreeRevision) {
yield put(resetRepoTree());
yield put(fetchRepoCommits({ uri: repoUri, revision }));
yield put(fetchRootRepoTree({ uri: repoUri, revision }));
Expand Down
3 changes: 2 additions & 1 deletion x-pack/legacy/plugins/code/public/sagas/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function* handleFetchRepoTree(action: Action<FetchRepoTreePayload>) {
tree.repoUri = action.payload!.uri;
yield put(
fetchRepoTreeSuccess({
revision: action.payload!.revision,
tree,
path: action.payload!.path,
withParents: action.payload!.parents,
Expand Down Expand Up @@ -106,7 +107,7 @@ function* handleFetchRootRepoTree(action: Action<FetchRepoPayloadWithRevision>)
try {
const { uri, revision } = action.payload!;
const tree = yield call(requestRepoTree, { uri, revision, path: '', isDir: true });
yield put(fetchRootRepoTreeSuccess(tree));
yield put(fetchRootRepoTreeSuccess({ tree, revision }));
} catch (err) {
yield put(fetchRootRepoTreeFailed(err));
}
Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/code/public/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { FileTree, RepositoryUri } from '../../model';
import { RootState } from '../reducers';

export const getTree = (state: RootState) => state.fileTree.tree;
export const getTreeRevision = (state: RootState) => state.fileTree.revision;

export const lastRequestPathSelector: (state: RootState) => string = (state: RootState) =>
state.symbol.lastRequestPath || '';
Expand Down
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/code/public/style/_layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@

.codeContainer__select {
margin-right: $euiSizeS;
min-width: 12rem;
width: 12rem;
flex-grow: 0;
flex-shrink: 0;
}

.codeContainer__tabs {
Expand Down

0 comments on commit f2cb776

Please sign in to comment.