-
Notifications
You must be signed in to change notification settings - Fork 221
feat(web): Add banner to notify user when permissions are syncing for the first time #852
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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
60 changes: 60 additions & 0 deletions
60
packages/web/src/app/[domain]/components/permissionSyncBanner.tsx
This file contains hidden or 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,60 @@ | ||
| 'use client'; | ||
|
|
||
| import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; | ||
| import { Loader2, Info } from "lucide-react"; | ||
| import { useQuery } from "@tanstack/react-query"; | ||
| import { unwrapServiceError } from "@/lib/utils"; | ||
| import { getPermissionSyncStatus } from "@/app/api/(client)/client"; | ||
| import { useRouter } from "next/navigation"; | ||
| import { useEffect } from "react"; | ||
| import { usePrevious } from "@uidotdev/usehooks"; | ||
|
|
||
| const POLL_INTERVAL_MS = 5000; | ||
|
|
||
| export function PermissionSyncBanner() { | ||
| const router = useRouter(); | ||
|
|
||
| const { data: hasPendingFirstSync, isError, isPending } = useQuery({ | ||
| queryKey: ["permissionSyncStatus"], | ||
| queryFn: () => unwrapServiceError(getPermissionSyncStatus()), | ||
| select: (data) => { | ||
| return data.hasPendingFirstSync; | ||
| }, | ||
| refetchInterval: (query) => { | ||
| const hasPendingFirstSync = query.state.data?.hasPendingFirstSync; | ||
| // Keep polling while sync is in progress, stop when done | ||
| return hasPendingFirstSync ? POLL_INTERVAL_MS : false; | ||
| }, | ||
| }); | ||
|
|
||
| const previousHasPendingFirstSync = usePrevious(hasPendingFirstSync); | ||
|
|
||
| // Refresh the page when sync completes | ||
| useEffect(() => { | ||
| if (previousHasPendingFirstSync === true && hasPendingFirstSync === false) { | ||
| router.refresh(); | ||
| } | ||
| }, [hasPendingFirstSync, previousHasPendingFirstSync, router]); | ||
|
|
||
| // Don't show anything if we can't get status or no pending first sync | ||
| if (isError || isPending) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!hasPendingFirstSync) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <Alert className="rounded-none border-x-0 border-t-0 bg-accent"> | ||
| <Info className="h-4 w-4 mt-0.5" /> | ||
| <AlertTitle className="flex items-center gap-2"> | ||
| Syncing repository access with Sourcebot. | ||
| <Loader2 className="h-4 w-4 animate-spin" /> | ||
| </AlertTitle> | ||
| <AlertDescription> | ||
| Sourcebot is syncing what repositories you have access to from a code host. This may take a minute. | ||
| </AlertDescription> | ||
| </Alert> | ||
| ); | ||
| } | ||
This file contains hidden or 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 hidden or 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
63 changes: 63 additions & 0 deletions
63
packages/web/src/app/api/(server)/ee/permissionSyncStatus/route.ts
This file contains hidden or 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,63 @@ | ||
| 'use server'; | ||
|
|
||
| import { apiHandler } from "@/lib/apiHandler"; | ||
| import { serviceErrorResponse } from "@/lib/serviceError"; | ||
| import { isServiceError } from "@/lib/utils"; | ||
| import { withAuthV2 } from "@/withAuthV2"; | ||
| import { getEntitlements } from "@sourcebot/shared"; | ||
| import { AccountPermissionSyncJobStatus } from "@sourcebot/db"; | ||
| import { StatusCodes } from "http-status-codes"; | ||
| import { ErrorCode } from "@/lib/errorCodes"; | ||
|
|
||
| export interface PermissionSyncStatusResponse { | ||
| hasPendingFirstSync: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether a user has a account that has it's permissions | ||
| * synced for the first time. | ||
brendan-kellam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| export const GET = apiHandler(async () => { | ||
| const entitlements = getEntitlements(); | ||
| if (!entitlements.includes('permission-syncing')) { | ||
| return serviceErrorResponse({ | ||
| statusCode: StatusCodes.FORBIDDEN, | ||
| errorCode: ErrorCode.INSUFFICIENT_PERMISSIONS, | ||
| message: "Permission syncing is not enabled for your license", | ||
| }); | ||
brendan-kellam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const result = await withAuthV2(async ({ prisma, user }) => { | ||
| const accounts = await prisma.account.findMany({ | ||
| where: { | ||
| userId: user.id, | ||
| provider: { in: ['github', 'gitlab'] } | ||
| }, | ||
| include: { | ||
| permissionSyncJobs: { | ||
| orderBy: { createdAt: 'desc' }, | ||
| take: 1, | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| const activeStatuses: AccountPermissionSyncJobStatus[] = [ | ||
| AccountPermissionSyncJobStatus.PENDING, | ||
| AccountPermissionSyncJobStatus.IN_PROGRESS | ||
| ]; | ||
|
|
||
| const hasPendingFirstSync = accounts.some(account => | ||
| account.permissionSyncedAt === null && | ||
| account.permissionSyncJobs.length > 0 && | ||
| activeStatuses.includes(account.permissionSyncJobs[0].status) | ||
| ); | ||
|
|
||
| return { hasPendingFirstSync } satisfies PermissionSyncStatusResponse; | ||
| }); | ||
|
|
||
| if (isServiceError(result)) { | ||
| return serviceErrorResponse(result); | ||
| } | ||
|
|
||
| return Response.json(result, { status: StatusCodes.OK }); | ||
| }); | ||
This file contains hidden or 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,72 @@ | ||
| import * as React from "react" | ||
| import { cva, type VariantProps } from "class-variance-authority" | ||
|
|
||
| import { cn } from "@/lib/utils" | ||
|
|
||
| const alertVariants = cva("grid gap-0.5 rounded-lg border px-2.5 py-2 text-left text-sm has-data-[slot=alert-action]:relative has-data-[slot=alert-action]:pr-18 has-[>svg]:grid-cols-[auto_1fr] has-[>svg]:gap-x-2 *:[svg]:row-span-2 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4 w-full relative group/alert", { | ||
| variants: { | ||
| variant: { | ||
| default: "bg-card text-card-foreground", | ||
| destructive: "text-destructive bg-card *:data-[slot=alert-description]:text-destructive/90 *:[svg]:text-current", | ||
| }, | ||
| }, | ||
| defaultVariants: { | ||
| variant: "default", | ||
| }, | ||
| }) | ||
|
|
||
| function Alert({ | ||
| className, | ||
| variant, | ||
| ...props | ||
| }: React.ComponentProps<"div"> & VariantProps<typeof alertVariants>) { | ||
| return ( | ||
| <div | ||
| data-slot="alert" | ||
| role="alert" | ||
| className={cn(alertVariants({ variant }), className)} | ||
| {...props} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| function AlertTitle({ className, ...props }: React.ComponentProps<"div">) { | ||
| return ( | ||
| <div | ||
| data-slot="alert-title" | ||
| className={cn( | ||
| "font-medium group-has-[>svg]/alert:col-start-2 [&_a]:hover:text-foreground [&_a]:underline [&_a]:underline-offset-3", | ||
| className | ||
| )} | ||
| {...props} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| function AlertDescription({ | ||
| className, | ||
| ...props | ||
| }: React.ComponentProps<"div">) { | ||
| return ( | ||
| <div | ||
| data-slot="alert-description" | ||
| className={cn( | ||
| "text-muted-foreground text-sm text-balance md:text-pretty group-has-[>svg]/alert:col-start-2 [&_p:not(:last-child)]:mb-4 [&_a]:hover:text-foreground [&_a]:underline [&_a]:underline-offset-3", | ||
| className | ||
| )} | ||
| {...props} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| function AlertAction({ className, ...props }: React.ComponentProps<"div">) { | ||
| return ( | ||
| <div | ||
| data-slot="alert-action" | ||
| className={cn("absolute top-2 right-2", className)} | ||
| {...props} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| export { Alert, AlertTitle, AlertDescription, AlertAction } |
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.
Uh oh!
There was an error while loading. Please reload this page.