Skip to content

Commit fe17f6a

Browse files
[ui-storagebrowser] refreshes file stats when file is edited (#3920)
* [ui-storagebrowser] refreshes file stats when file is edited
1 parent 4b551c9 commit fe17f6a

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

desktop/core/src/desktop/js/apps/storageBrowser/StorageBrowserTab/StorageBrowserTab.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ const StorageBrowserTab = ({ homeDir, testId }: StorageBrowserTabProps): JSX.Ele
4646

4747
const { t } = i18nReact.useTranslation();
4848

49-
const { data: fileStats, loading } = useLoadData<FileStats>(FILE_STATS_API_URL, {
49+
const {
50+
data: fileStats,
51+
loading,
52+
reloadData
53+
} = useLoadData<FileStats>(FILE_STATS_API_URL, {
5054
params: {
5155
path: filePath
5256
},
@@ -89,7 +93,7 @@ const StorageBrowserTab = ({ homeDir, testId }: StorageBrowserTabProps): JSX.Ele
8993
<StorageDirectoryPage fileStats={fileStats} onFilePathChange={setFilePath} />
9094
)}
9195
{fileStats?.type === BrowserViewType.file && (
92-
<StorageFilePage fileName={fileName} fileStats={fileStats} />
96+
<StorageFilePage fileName={fileName} fileStats={fileStats} onReload={reloadData} />
9397
)}
9498
</div>
9599
</Spin>

desktop/core/src/desktop/js/apps/storageBrowser/StorageDirectoryPage/StorageDirectoryPage.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,11 @@ const StorageDirectoryPage = ({
186186
const onRowClicked = (record: StorageDirectoryTableData) => {
187187
return {
188188
onClick: () => {
189-
onFilePathChange(record.path);
190-
if (record.type === 'dir') {
191-
setPageNumber(1);
189+
if (selectedFiles.length === 0) {
190+
onFilePathChange(record.path);
191+
if (record.type === 'dir') {
192+
setPageNumber(1);
193+
}
192194
}
193195
}
194196
};

desktop/core/src/desktop/js/apps/storageBrowser/StorageFilePage/StorageFilePage.test.tsx

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const mockFileStats: FileStats = {
6464
type: BrowserViewType.file
6565
};
6666
const mockFileName = 'file.txt';
67+
const mockReload = jest.fn();
6768

6869
describe('StorageFilePage', () => {
6970
let oldShowDownloadButton: boolean;
@@ -80,7 +81,9 @@ describe('StorageFilePage', () => {
8081
});
8182

8283
it('should render file metadata and content', () => {
83-
render(<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} />);
84+
render(
85+
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
86+
);
8487

8588
expect(screen.getByText('Size')).toBeInTheDocument();
8689
expect(screen.getByText('120.56 KB')).toBeInTheDocument();
@@ -100,7 +103,9 @@ describe('StorageFilePage', () => {
100103

101104
// TODO: fix this test when mocking of useLoadData onSuccess callback is mproperly mocked
102105
it.skip('should show edit button and hides save/cancel buttons initially', () => {
103-
render(<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} />);
106+
render(
107+
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
108+
);
104109

105110
expect(screen.getByRole('button', { name: 'Edit' })).toBeVisible();
106111
expect(screen.queryByRole('button', { name: 'Save' })).toBeNull();
@@ -113,14 +118,18 @@ describe('StorageFilePage', () => {
113118
contents: 'Initial file content',
114119
compression: 'zip'
115120
}));
116-
render(<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} />);
121+
render(
122+
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
123+
);
117124

118125
expect(screen.queryByRole('button', { name: 'Edit' })).toBeNull();
119126
});
120127

121128
it('should show save and cancel buttons when editing', async () => {
122129
const user = userEvent.setup();
123-
render(<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} />);
130+
render(
131+
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
132+
);
124133

125134
expect(screen.getByRole('button', { name: 'Edit' })).toBeVisible();
126135
expect(screen.queryByRole('button', { name: 'Save' })).toBeNull();
@@ -138,7 +147,9 @@ describe('StorageFilePage', () => {
138147

139148
it('should update textarea value and calls handleSave', async () => {
140149
const user = userEvent.setup();
141-
render(<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} />);
150+
render(
151+
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
152+
);
142153

143154
await user.click(screen.getByRole('button', { name: 'Edit' }));
144155

@@ -160,7 +171,9 @@ describe('StorageFilePage', () => {
160171

161172
it('should cancel editing and reverts textarea value', async () => {
162173
const user = userEvent.setup();
163-
render(<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} />);
174+
render(
175+
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
176+
);
164177

165178
await user.click(screen.getByRole('button', { name: 'Edit' }));
166179

@@ -180,7 +193,9 @@ describe('StorageFilePage', () => {
180193

181194
it('should download a file when download button is clicked', async () => {
182195
const user = userEvent.setup();
183-
render(<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} />);
196+
render(
197+
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
198+
);
184199

185200
await user.click(screen.getByRole('button', { name: 'Download' }));
186201

@@ -194,7 +209,9 @@ describe('StorageFilePage', () => {
194209

195210
it('should download a file when download button is clicked', async () => {
196211
const user = userEvent.setup();
197-
render(<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} />);
212+
render(
213+
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
214+
);
198215

199216
await user.click(screen.getByRole('button', { name: 'Download' }));
200217

@@ -209,15 +226,19 @@ describe('StorageFilePage', () => {
209226
it('should not render the download button when show_download_button is false', () => {
210227
(window as hueWindow).SHOW_DOWNLOAD_BUTTON = false;
211228

212-
render(<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} />);
229+
render(
230+
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
231+
);
213232

214233
expect(screen.queryByRole('button', { name: 'Download' })).toBeNull();
215234
expect(screen.queryByRole('link', { name: 'Download' })).toBeNull();
216235
});
217236

218237
// TODO: fix this test when mocking of useLoadData onSuccess callback is mproperly mocked
219238
it.skip('should render a textarea for text files', () => {
220-
render(<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} />);
239+
render(
240+
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
241+
);
221242

222243
const textarea = screen.getByRole('textbox');
223244
expect(textarea).toBeInTheDocument();
@@ -229,6 +250,7 @@ describe('StorageFilePage', () => {
229250
<StorageFilePage
230251
fileName="imagefile.png"
231252
fileStats={{ ...mockFileStats, path: '/path/to/imagefile.png' }}
253+
onReload={mockReload}
232254
/>
233255
);
234256

@@ -242,6 +264,7 @@ describe('StorageFilePage', () => {
242264
<StorageFilePage
243265
fileName="documentfile.pdf"
244266
fileStats={{ ...mockFileStats, path: '/path/to/documentfile.pdf' }}
267+
onReload={mockReload}
245268
/>
246269
);
247270

@@ -253,6 +276,7 @@ describe('StorageFilePage', () => {
253276
<StorageFilePage
254277
fileName="audiofile.mp3"
255278
fileStats={{ ...mockFileStats, path: '/path/to/audiofile.mp3' }}
279+
onReload={mockReload}
256280
/>
257281
);
258282

@@ -266,6 +290,7 @@ describe('StorageFilePage', () => {
266290
<StorageFilePage
267291
fileName="videofile.mp4"
268292
fileStats={{ ...mockFileStats, path: '/path/to/videofile.mp4' }}
293+
onReload={mockReload}
269294
/>
270295
);
271296

@@ -282,6 +307,7 @@ describe('StorageFilePage', () => {
282307
path: '/path/to/unsupportedfile.xyz'
283308
}}
284309
fileName="unsupportedfile.xyz"
310+
onReload={mockReload}
285311
/>
286312
);
287313

desktop/core/src/desktop/js/apps/storageBrowser/StorageFilePage/StorageFilePage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ import { Spin } from 'antd';
4141
import useLoadData from '../../../utils/hooks/useLoadData';
4242

4343
interface StorageFilePageProps {
44+
onReload: () => void;
4445
fileName: string;
4546
fileStats: FileStats;
4647
}
4748

48-
const StorageFilePage = ({ fileName, fileStats }: StorageFilePageProps): JSX.Element => {
49+
const StorageFilePage = ({ fileName, fileStats, onReload }: StorageFilePageProps): JSX.Element => {
4950
const { t } = i18nReact.useTranslation();
5051
const [isEditing, setIsEditing] = useState<boolean>(false);
5152
const [fileContent, setFileContent] = useState<FilePreview['contents']>();
@@ -90,6 +91,7 @@ const StorageFilePage = ({ fileName, fileStats }: StorageFilePageProps): JSX.Ele
9091
setIsEditing(true);
9192
},
9293
onSuccess: () => {
94+
onReload();
9395
huePubSub.publish('hue.global.info', { message: t('Changes saved!') });
9496
}
9597
}

0 commit comments

Comments
 (0)