Skip to content

Commit

Permalink
Merge branch 'feat-storage-browser/main' into feat-storage-browser/fi…
Browse files Browse the repository at this point in the history
…le-too-big
  • Loading branch information
AllanZhengYP authored Nov 16, 2024
2 parents cc4056f + dadcccc commit 0824745
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,26 +228,38 @@ describe('Search', () => {
]);
});

it('should not match prefix', () => {
it('should only match paths ahead of prefix', () => {
const mockData = [
{
key: 'collections/photo1.jpg',
key: 'photos/album/random-photos/photo1.jpg',
},
{
key: 'collections/photos/',
key: 'photos/album/cats/pic.jpg',
},
];

const output = searchItems<Item>({
list: mockData,
prefix: 'collections/',
prefix: 'photos/album/',
options: {
filterBy: 'key',
groupBy: '/',
query: 'collection',
query: 'photo',
},
});
expect(output).toEqual([]);

expect(output).toEqual([
{
id: expect.any(String),
key: 'photos/album/random-photos/',
type: 'FOLDER',
},
{
id: expect.any(String),
key: 'photos/album/random-photos/photo1.jpg',
type: 'FILE',
},
]);
});

it('should handle consecutive delimiters', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ export function searchItems<T>({ prefix, list, options }: Search<T>): T[] {
matchedPath += groupBy;
}

// ignore prefix for match
if (matchedPath !== prefix && !uniquePaths.has(matchedPath)) {
// ignore anything below the prefix for matching
if (matchedPath.length > prefix.length && !uniquePaths.has(matchedPath)) {
// add a new item
uniquePaths.set(matchedPath, {
...item,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { getLocationDetailViewTableData } from './getLocationDetailViewTableData
import { useLocationDetailView } from './useLocationDetailView';
import { LocationDetailViewProps } from './types';
import { MessageControl } from '../../controls/MessageControl';
import { MessageProps } from '../../composables/Message';

const DEFAULT_PAGE_SIZE = 100;
export const DEFAULT_LIST_OPTIONS = {
Expand Down Expand Up @@ -61,7 +60,9 @@ export function LocationDetailView({
fileDataItems,
hasFiles,
hasError,
hasDownloadError,
message,
downloadErrorMessage,
searchQuery,
onDropFiles,
onRefresh,
Expand All @@ -78,25 +79,12 @@ export function LocationDetailView({
hasExhaustedSearch,
} = useLocationDetailView({ onNavigate: onNavigateProp, onExit });

let messageControlContent: MessageProps | undefined;

if (hasError) {
messageControlContent = getListItemsResultMessage({
items: pageItems,
hasError,
message,
});
} else if (hasExhaustedSearch) {
messageControlContent = getListItemsResultMessage({
items: pageItems,
hasExhaustedSearch,
message,
});
} else {
messageControlContent = getListItemsResultMessage({
items: pageItems,
});
}
const messageControlContent = getListItemsResultMessage({
items: pageItems,
hasError: hasError || hasDownloadError,
hasExhaustedSearch,
message: hasError ? message : downloadErrorMessage,
});

return (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,45 @@ describe('LocationDetailView', () => {
items: expect.any(Array),
hasError: true,
message: errorMessage,
hasExhaustedSearch: false,
});
});

it('invokes getListItemsResultMessage() with expected params when there is a download error', () => {
mockUseProcessTasks.mockReturnValueOnce([
{
isProcessing: false,
isProcessingComplete: false,
statusCounts: {
...INITIAL_STATUS_COUNTS,
FAILED: 1,
},
tasks: [
{
data: { key: 'test-key', id: '123' },
message: 'NotFound',
progress: 0,
status: 'FAILED',
},
],
},
jest.fn(),
]);

mockListItemsAction({
isLoading: false,
hasError: false,
result: [{ key: 'test1', type: 'FOLDER' }],
nextToken: 'some-token',
});

render(<LocationDetailView />);

expect(mockGetListItemsResultMessage).toHaveBeenCalledWith({
items: expect.any(Array),
hasError: true,
message: 'Failed to download test-key due to error: NotFound.',
hasExhaustedSearch: false,
});
});

Expand Down Expand Up @@ -269,6 +308,7 @@ describe('LocationDetailView', () => {
expect(mockGetListItemsResultMessage).toHaveBeenCalledWith({
items: expect.any(Array),
hasExhaustedSearch: true,
hasError: false,
message: undefined,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ import { createEnhancedListHandler } from '../../actions/createEnhancedListHandl
import { useGetActionInput } from '../../providers/configuration';
import { LocationState } from '../../providers/store/location';
import { useSearch } from '../hooks/useSearch';
import { useProcessTasks } from '../../tasks';
import { downloadHandler, FileDataItem } from '../../actions/handlers';
import { Tasks, useProcessTasks } from '../../tasks';
import {
downloadHandler,
DownloadHandlerData,
FileDataItem,
} from '../../actions/handlers';

interface UseLocationDetailView {
hasError: boolean;
hasNextPage: boolean;
hasDownloadError: boolean;
highestPageVisited: number;
isLoading: boolean;
isSearchingSubfolders: boolean;
Expand All @@ -30,6 +35,7 @@ interface UseLocationDetailView {
fileDataItems: FileDataItem[] | undefined;
hasFiles: boolean;
message: string | undefined;
downloadErrorMessage: string | undefined;
shouldShowEmptyMessage: boolean;
searchQuery: string;
hasExhaustedSearch: boolean;
Expand Down Expand Up @@ -81,6 +87,18 @@ const listLocationItemsAction = createEnhancedListHandler(
listLocationItemsHandler
);

const getDownloadErrorMessageFromFailedDownloadTask = (
tasks: Tasks<DownloadHandlerData>
): string | undefined => {
if (!tasks.length) {
return undefined;
}

return `Failed to download ${
tasks[0].data.fileKey ?? tasks[0].data.key
} due to error: ${tasks[0].message}.`;
};

export function useLocationDetailView(
options?: UseLocationDetailViewOptions
): UseLocationDetailView {
Expand All @@ -100,7 +118,7 @@ export function useLocationDetailView(
const { fileDataItems } = locationItems;
const hasInvalidPrefix = isUndefined(prefix);

const [_, handleDownload] = useProcessTasks(downloadHandler);
const [downloadTaskResult, handleDownload] = useProcessTasks(downloadHandler);

const [{ data, isLoading, hasError, message }, handleList] = useDataState(
listLocationItemsAction,
Expand Down Expand Up @@ -210,9 +228,13 @@ export function useLocationDetailView(
fileDataItems,
hasFiles: fileItems.length > 0,
hasError,
hasDownloadError: downloadTaskResult.statusCounts.FAILED > 0,
hasNextPage: hasNextToken,
highestPageVisited,
message,
downloadErrorMessage: getDownloadErrorMessageFromFailedDownloadTask(
downloadTaskResult.tasks
),
shouldShowEmptyMessage,
isLoading,
isSearchingSubfolders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,11 @@ export function LocationsView({
} = useLocationsView(props);

// TODO: add hasExhaustedSearch + query param
const messageControlContent = hasError
? getListLocationsResultMessage({
locations: pageItems,
hasError,
message,
})
: getListLocationsResultMessage({
locations: pageItems,
});
const messageControlContent = getListLocationsResultMessage({
locations: pageItems,
hasError,
message,
});

const headers = getHeaders({
hasObjectLocations: pageItems.some(({ type }) => type === 'OBJECT'),
Expand Down
Loading

0 comments on commit 0824745

Please sign in to comment.