-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage-browser): add routed example app
- Loading branch information
1 parent
7df9f79
commit 65bb318
Showing
14 changed files
with
443 additions
and
48 deletions.
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
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({ | ||
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.