-
Notifications
You must be signed in to change notification settings - Fork 306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(storage-browser): integrate browser navigation events #5941
Merged
calebpollman
merged 6 commits into
feat-storage-browser/main
from
feat-storage-browser/browser-navigation
Oct 25, 2024
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7df9f79
feat(storage-browser): integrate browser navigation
calebpollman 65bb318
feat(storage-browser): add routed example app
calebpollman ff1ebdb
fix unit tests
calebpollman b4e4c4b
remove unused test utilities
calebpollman 99be6e8
remove testUtils pattern from jest coverage exclusion
calebpollman a5b5816
fix navigation e2e test
calebpollman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...es/next/pages/ui/components/storage/storage-browser/default-auth/routed/StorageBrowser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Amplify } from 'aws-amplify'; | ||
|
||
import { | ||
createAmplifyAuthAdapter, | ||
createStorageBrowser, | ||
elementsDefault, | ||
} from '@aws-amplify/ui-react-storage/browser'; | ||
import '@aws-amplify/ui-react-storage/styles.css'; | ||
import '@aws-amplify/ui-react-storage/storage-browser-styles.css'; | ||
|
||
import config from './aws-exports'; | ||
|
||
Amplify.configure(config); | ||
|
||
const defaultPrefixes = [ | ||
'public/', | ||
// intentionally added to test a prefix that should return 403 forbidden | ||
'forbidden/', | ||
(identityId: string) => `protected/${identityId}/`, | ||
(identityId: string) => `private/${identityId}/`, | ||
]; | ||
|
||
export const { StorageBrowser } = createStorageBrowser({ | ||
elements: elementsDefault, | ||
config: createAmplifyAuthAdapter({ options: { defaultPrefixes } }), | ||
}); |
58 changes: 58 additions & 0 deletions
58
.../storage/storage-browser/default-auth/routed/[locations]/[location-detail]/index.page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useRouter } from 'next/router'; | ||
|
||
import { signOut } from 'aws-amplify/auth'; | ||
import { Button, Flex } from '@aws-amplify/ui-react'; | ||
|
||
import { StorageBrowser } from '../../StorageBrowser'; | ||
|
||
import '@aws-amplify/ui-react-storage/storage-browser-styles.css'; | ||
import '@aws-amplify/ui-react-storage/styles.css'; | ||
|
||
export default function Page() { | ||
const router = useRouter(); | ||
|
||
if (!router.query.bucket) return null; | ||
|
||
return ( | ||
<Flex> | ||
<Button | ||
onClick={() => { | ||
signOut(); | ||
router.replace( | ||
router.pathname.replace('[locations]/[location-detail]', '') | ||
); | ||
}} | ||
> | ||
Sign Out | ||
</Button> | ||
<StorageBrowser.Provider | ||
actionType={ | ||
typeof router.query.actionType === 'string' | ||
? router.query.actionType | ||
: undefined | ||
} | ||
location={router.query as any} | ||
> | ||
<StorageBrowser.LocationDetailView | ||
onActionSelect={(actionType) => { | ||
router.replace({ query: { ...router.query, actionType } }); | ||
}} | ||
onExit={() => { | ||
router.back(); | ||
}} | ||
/> | ||
{typeof router.query.actionType === 'string' ? ( | ||
<dialog open={!!router.query.actionType}> | ||
<StorageBrowser.LocationActionView | ||
onClose={() => { | ||
router.replace({ | ||
query: { ...router.query, actionType: undefined }, | ||
}); | ||
}} | ||
/> | ||
</dialog> | ||
) : null} | ||
</StorageBrowser.Provider> | ||
</Flex> | ||
); | ||
} |
41 changes: 41 additions & 0 deletions
41
...ages/ui/components/storage/storage-browser/default-auth/routed/[locations]/index.page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react'; | ||
import { useRouter } from 'next/router'; | ||
|
||
import { signOut } from 'aws-amplify/auth'; | ||
import { Button, Flex } from '@aws-amplify/ui-react'; | ||
|
||
import { StorageBrowser } from '../StorageBrowser'; | ||
|
||
import '@aws-amplify/ui-react-storage/storage-browser-styles.css'; | ||
import '@aws-amplify/ui-react-storage/styles.css'; | ||
|
||
function Locations() { | ||
const router = useRouter(); | ||
|
||
return ( | ||
<Flex direction="column"> | ||
<Button | ||
onClick={() => { | ||
signOut(); | ||
router.replace(router.pathname.replace('[locations]', '')); | ||
}} | ||
> | ||
Sign Out | ||
</Button> | ||
<StorageBrowser.Provider> | ||
<StorageBrowser.Provider> | ||
<StorageBrowser.LocationsView | ||
onNavigate={(destination) => { | ||
router.push({ | ||
pathname: `${router.pathname}/location-detail`, | ||
query: { ...destination }, | ||
}); | ||
}} | ||
/> | ||
</StorageBrowser.Provider> | ||
</StorageBrowser.Provider> | ||
</Flex> | ||
); | ||
} | ||
|
||
export default Locations; |
2 changes: 2 additions & 0 deletions
2
examples/next/pages/ui/components/storage/storage-browser/default-auth/routed/aws-exports.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import awsExports from '@environments/storage/file-uploader/src/aws-exports'; | ||
export default awsExports; |
23 changes: 23 additions & 0 deletions
23
examples/next/pages/ui/components/storage/storage-browser/default-auth/routed/index.page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import { useRouter } from 'next/router'; | ||
|
||
import useIsSignedIn from './useIsSignedIn'; | ||
|
||
import { Authenticator } from '@aws-amplify/ui-react'; | ||
|
||
import '@aws-amplify/ui-react-storage/styles.css'; | ||
import '@aws-amplify/ui-react-storage/storage-browser-styles.css'; | ||
|
||
function Example() { | ||
const router = useRouter(); | ||
|
||
useIsSignedIn({ | ||
onSignIn: () => { | ||
router.push(`${router.pathname}/locations`); | ||
}, | ||
}); | ||
|
||
return <Authenticator />; | ||
} | ||
|
||
export default Example; |
71 changes: 71 additions & 0 deletions
71
...les/next/pages/ui/components/storage/storage-browser/default-auth/routed/useIsSignedIn.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React from 'react'; | ||
|
||
import { fetchAuthSession } from 'aws-amplify/auth'; | ||
import { Hub, HubCallback } from '@aws-amplify/core'; | ||
import { isFunction } from '@aws-amplify/ui'; | ||
|
||
export interface UseIsSignedInParams { | ||
onSignIn?: () => void; | ||
onSignOut?: () => void; | ||
} | ||
|
||
interface UseIsSignedIn { | ||
isSignedIn: boolean; | ||
} | ||
|
||
const INITIAL_STATE: UseIsSignedIn = { isSignedIn: false }; | ||
|
||
/** | ||
* listens for `Auth` sign in and sign out events | ||
* | ||
* @param {UseIsSignedInParams} params `onSignIn` and `onSignOut` event callbacks | ||
* @returns {UseIsSignedIn} Outputs `isSignedIn` | ||
*/ | ||
export default function useIsSignedIn({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied from an internal file |
||
onSignIn, | ||
onSignOut, | ||
}: UseIsSignedInParams): UseIsSignedIn { | ||
const [output, setOutput] = React.useState<UseIsSignedIn>( | ||
() => INITIAL_STATE | ||
); | ||
|
||
React.useEffect(() => { | ||
fetchAuthSession().then(() => { | ||
if (isFunction(onSignIn)) onSignIn(); | ||
}); | ||
}, [onSignIn]); | ||
|
||
const handleEvent: HubCallback = React.useCallback( | ||
({ payload }) => { | ||
switch (payload.event) { | ||
case 'signIn': | ||
case 'autoSignIn': { | ||
if (isFunction(onSignIn)) { | ||
onSignIn(); | ||
} | ||
setOutput({ isSignedIn: true }); | ||
break; | ||
} | ||
case 'signOut': { | ||
if (isFunction(onSignOut)) { | ||
onSignOut(); | ||
} | ||
setOutput({ isSignedIn: false }); | ||
break; | ||
} | ||
default: { | ||
break; | ||
} | ||
} | ||
}, | ||
[onSignIn, onSignOut] | ||
); | ||
|
||
React.useEffect(() => { | ||
const unsubscribe = Hub.listen('auth', handleEvent, 'useIsSignedIn'); | ||
|
||
return unsubscribe; | ||
}, [handleEvent]); | ||
|
||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...es/next/pages/ui/components/storage/storage-browser/managed-auth/routed/StorageBrowser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Auth } from '../../managedAuthAdapter'; | ||
import { | ||
createManagedAuthAdapter, | ||
createStorageBrowser, | ||
} from '@aws-amplify/ui-react-storage/browser'; | ||
|
||
export const routedAuth = new Auth({ persistCredentials: true }); | ||
|
||
const config = createManagedAuthAdapter({ | ||
credentialsProvider: routedAuth.credentialsProvider, | ||
region: process.env.NEXT_PUBLIC_MANAGED_AUTH_REGION, | ||
accountId: process.env.NEXT_PUBLIC_MANAGED_AUTH_ACCOUNT_ID, | ||
registerAuthListener: routedAuth.registerAuthListener, | ||
}); | ||
|
||
export const { StorageBrowser } = createStorageBrowser({ config }); |
52 changes: 52 additions & 0 deletions
52
.../storage/storage-browser/managed-auth/routed/[locations]/[location-detail]/index.page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useRouter } from 'next/router'; | ||
|
||
import { Flex } from '@aws-amplify/ui-react'; | ||
|
||
import { SignOutButton } from '../../components'; | ||
import { StorageBrowser } from '../../StorageBrowser'; | ||
|
||
import '@aws-amplify/ui-react-storage/storage-browser-styles.css'; | ||
import '@aws-amplify/ui-react-storage/styles.css'; | ||
|
||
export default function Page() { | ||
const { back, query, pathname, replace } = useRouter(); | ||
|
||
if (!query.bucket) return null; | ||
|
||
return ( | ||
<Flex> | ||
<SignOutButton | ||
onSignOut={() => { | ||
replace(pathname.replace('[locations]/[location-detail]', '')); | ||
}} | ||
/> | ||
<StorageBrowser.Provider | ||
actionType={ | ||
typeof query.actionType === 'string' ? query.actionType : undefined | ||
} | ||
location={query as any} | ||
> | ||
<StorageBrowser.LocationDetailView | ||
onActionSelect={(actionType) => { | ||
replace({ query: { ...query, actionType } }); | ||
}} | ||
onNavigate={(destination) => { | ||
replace({ query: { ...destination } }); | ||
}} | ||
onExit={() => { | ||
back(); | ||
}} | ||
/> | ||
{typeof query.actionType === 'string' ? ( | ||
<dialog open={!!query.actionType}> | ||
<StorageBrowser.LocationActionView | ||
onClose={() => { | ||
replace({ query: { ...query, actionType: undefined } }); | ||
}} | ||
/> | ||
</dialog> | ||
) : null} | ||
</StorageBrowser.Provider> | ||
</Flex> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useControl
has been removed fromcreateStorageBrowser
and will be replaced withuseView
. In the meantime updated this app to work with the new event callbacks