Skip to content

Commit

Permalink
fix(sto-bro): fix conditional value provided to FoldersMessageProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
calebpollman committed Nov 16, 2024
1 parent dfa6b61 commit 9cf100b
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 36 deletions.
11 changes: 7 additions & 4 deletions packages/react-core/src/utils/createContextUtilities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ export default function createContextUtilities<
throw new Error(INVALID_OPTIONS_MESSAGE);
}

const contextDisplayName = `${contextName}Context`;
const providerDisplayName = `${contextName}Provider`;

const Context = React.createContext<ContextType | undefined>(defaultValue);
Context.displayName = contextDisplayName;

function Provider(props: React.PropsWithChildren<ContextType>) {
const { children, ...context } = props;
Expand All @@ -113,8 +117,7 @@ export default function createContextUtilities<
return <Context.Provider value={value}>{children}</Context.Provider>;
}

Provider.displayName = `${contextName}Provider`;

Provider.displayName = providerDisplayName;
return {
[`use${contextName}`]: function (params?: HookParams) {
const context = React.useContext(Context);
Expand All @@ -125,7 +128,7 @@ export default function createContextUtilities<

return context;
},
[`${contextName}Provider`]: Provider,
[`${contextName}Context`]: Context,
[providerDisplayName]: Provider,
[contextDisplayName]: Context,
} as CreateContextUtilitiesReturn<ContextType, ContextName>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function DropdownMenu({
icon={icon}
label={label}
onClick={() => {
setIsOpen(false);
onItemSelect?.(id);
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ exports[`CopyView display text values should match snapshot values 1`] = `
"actionExitLabel": "Exit",
"actionStartLabel": "Copy",
"getActionCompleteMessage": [Function],
"getFolderSelectedMessage": [Function],
"getListFoldersResultsMessage": [Function],
"loadingIndicatorLabel": "Loading",
"overwriteWarningMessage": "Copied files will overwrite existing files at selected destination.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ export const DEFAULT_COPY_VIEW_DISPLAY_TEXT: DefaultCopyViewDisplayText = {
type: 'error',
};
},
getFolderSelectedMessage: (key: string) => {
return `Current folder selected: ${key}. There are no additional folders under this path.`;
},
searchSubmitLabel: 'Submit',
searchClearLabel: 'Clear search',
};
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export interface DefaultCopyViewDisplayText
getListFoldersResultsMessage: (
data: ListFoldersMessageData
) => { content?: string; type?: MessageType } | undefined;
getFolderSelectedMessage: (path: string) => string;
loadingIndicatorLabel: 'Loading';
overwriteWarningMessage: string;
searchPlaceholder: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export function CopyViewProvider({
actionExitLabel,
actionStartLabel,
getActionCompleteMessage,
getListFoldersResultsMessage,
overwriteWarningMessage,
searchPlaceholder,
searchSubmitLabel,
Expand Down Expand Up @@ -55,7 +54,6 @@ export function CopyViewProvider({
hasError: hasFoldersError,
message: foldersErrorMessage,
query,
hasInitialized: hasFoldersInitialized,
onQuery,
onSearchClear,
onSearch,
Expand Down Expand Up @@ -89,15 +87,6 @@ export function CopyViewProvider({
}
: getActionCompleteMessage({ counts: statusCounts });

const foldersMessage = !hasFoldersInitialized
? undefined
: getListFoldersResultsMessage({
hasError: hasFoldersError,
message: foldersErrorMessage,
folders: pageItems,
query,
});

return (
<ControlsContextProvider
data={{
Expand Down Expand Up @@ -141,7 +130,12 @@ export function CopyViewProvider({
onPaginate={onPaginate}
>
<FoldersTableProvider folders={pageItems} onSelect={onSelect}>
<FoldersMessageProvider {...foldersMessage}>
<FoldersMessageProvider
folders={folders.pageItems}
hasError={hasFoldersError}
message={foldersErrorMessage}
query={query}
>
{children}
</FoldersMessageProvider>
</FoldersTableProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,39 @@ import React from 'react';
import { createContextUtilities } from '@aws-amplify/ui-react-core';

import { ControlsContextProvider } from '../../../controls/context';
import { MessageProps } from '../../../composables/Message';

import { MessageControl } from '../../../controls/MessageControl';
import { ViewElement } from '../../../context/elements';
import { STORAGE_BROWSER_BLOCK } from '../../../constants';
import { FolderData } from '../../../actions';
import { useDisplayText } from '../../../displayText';

export interface FoldersMessageProps extends MessageProps {}
export interface FoldersMessageProps {
hasError?: boolean;
message?: string;
folders?: FolderData[];
query?: string;
}

const defaultValue: FoldersMessageProps = {};
export const { useFoldersMessage, FoldersMessageProvider } =
createContextUtilities({ contextName: 'FoldersMessage', defaultValue });

export const FoldersMessageControl = (): React.JSX.Element => {
const message = useFoldersMessage();
const {
CopyView: { getListFoldersResultsMessage },
} = useDisplayText();
const { hasError, folders, message, query } = useFoldersMessage();

const messageContent = getListFoldersResultsMessage({
hasError,
folders,
message,
query,
});

return (
<ControlsContextProvider data={{ message }}>
<ControlsContextProvider data={{ message: messageContent }}>
<ViewElement className={`${STORAGE_BROWSER_BLOCK}__message`}>
<MessageControl />
</ViewElement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ const defaultViewState: CopyViewState = {
destinationList: [],
folders: {
hasError: false,
hasInitialized: false,
hasNextPage: false,
highestPageVisited: 1,
page: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
exports[`useFolders should return the correct initial state 1`] = `
{
"hasError": false,
"hasInitialized": false,
"hasNextPage": true,
"highestPageVisited": 1,
"isLoading": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export interface UseCopyViewOptions {

export interface FoldersState {
hasError: boolean;
hasInitialized: boolean;
hasNextPage: boolean;
highestPageVisited: number;
isLoading: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { useDataState, useHasValueUpdated } from '@aws-amplify/ui-react-core';
import { useDataState } from '@aws-amplify/ui-react-core';

import { usePaginate } from '../../hooks/usePaginate';
import { listLocationItemsHandler, FolderData } from '../../../actions';
Expand Down Expand Up @@ -52,12 +52,6 @@ export const useFolders = ({

const { items, nextToken } = data;

const hasInitializedRef = React.useRef(false);
const hasItemsChanged = useHasValueUpdated(items, true);
if (hasItemsChanged) {
hasInitializedRef.current = true;
}

const onInitialize = React.useCallback(() => {
handleList({
config: getInput(),
Expand Down Expand Up @@ -122,7 +116,6 @@ export const useFolders = ({

return {
hasError,
hasInitialized: hasInitializedRef.current,
hasNextPage: hasNextToken,
highestPageVisited,
isLoading,
Expand Down

0 comments on commit 9cf100b

Please sign in to comment.