-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { ReadonlyRequestCookies } from '@vercel/flags'; | ||
import { flag, dedupe } from '@vercel/flags/next'; | ||
|
||
interface Entities { | ||
user?: { id: string }; | ||
} | ||
|
||
const identify = dedupe( | ||
({ cookies }: { cookies: ReadonlyRequestCookies }): Entities => { | ||
const userId = cookies.get('dashboard-user-id')?.value; | ||
return { user: userId ? { id: userId } : undefined }; | ||
}, | ||
); | ||
|
||
export const dashboardFlag = flag<boolean, Entities>({ | ||
key: 'dashboard-flag', | ||
identify, | ||
description: 'Flag used on the Dashboard Pages example', | ||
decide({ entities }) { | ||
if (!entities?.user) return false; | ||
// Allowed users could be loaded from Edge Config or elsewhere | ||
const allowedUsers = ['user1']; | ||
|
||
return allowedUsers.includes(entities.user.id); | ||
}, | ||
}); |
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 { FlagContextProvider } from '@vercel/flags/react'; | ||
import { dashboardFlag } from './flags'; | ||
|
||
export default async function Layout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const dashboard = await dashboardFlag(); | ||
|
||
return ( | ||
<FlagContextProvider values={{ [dashboardFlag.key]: dashboard }}> | ||
{children} | ||
</FlagContextProvider> | ||
); | ||
} |
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,47 @@ | ||
'use client'; | ||
|
||
import { Button } from '@/components/ui/button'; | ||
import { useFlagContext } from '@vercel/flags/react'; | ||
import { DemoFlag } from '@/components/demo-flag'; | ||
import { useRouter } from 'next/navigation'; | ||
import type { FlagValuesType } from '@vercel/flags'; | ||
|
||
export default function Page() { | ||
const dashboard = useFlagContext<boolean>('dashboard-flag'); | ||
const router = useRouter(); | ||
return ( | ||
<> | ||
<DemoFlag name="dashboard-flag" value={dashboard} /> | ||
<div className="flex gap-2"> | ||
<Button | ||
onClick={() => { | ||
document.cookie = 'dashboard-user-id=user1; path=/'; | ||
router.refresh(); | ||
}} | ||
variant="outline" | ||
> | ||
Act as a flagged in user | ||
</Button> | ||
<Button | ||
onClick={() => { | ||
document.cookie = 'dashboard-user-id=user2; path=/'; | ||
router.refresh(); | ||
}} | ||
variant="outline" | ||
> | ||
Act as a regular user | ||
</Button> | ||
<Button | ||
onClick={() => { | ||
document.cookie = | ||
'dashboard-user-id=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT'; | ||
router.refresh(); | ||
}} | ||
variant="outline" | ||
> | ||
Clear cookie | ||
</Button> | ||
</div> | ||
</> | ||
); | ||
} |
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