Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): switch organization before navigating and stay put when it fails

Richie McIlroy0c466d67a163
diff --git a/apps/web/app/s/[videoId]/_components/DashboardBackLink.tsx b/apps/web/app/s/[videoId]/_components/DashboardBackLink.tsx
index d8b1912f8..372731950 100644
--- a/apps/web/app/s/[videoId]/_components/DashboardBackLink.tsx
+++ b/apps/web/app/s/[videoId]/_components/DashboardBackLink.tsx
@@ -5,7 +5,8 @@ import clsx from "clsx";
import { ChevronLeft, Folder, LayoutGrid, Users } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
-import { type MouseEvent, useState } from "react";
+import { useState } from "react";
+import { toast } from "sonner";
import { updateActiveOrganization } from "@/app/(org)/dashboard/_components/Navbar/server";
import type { ShareDashboardDestination } from "@/lib/share-dashboard-destination";
@@ -31,66 +32,67 @@ export function DashboardBackLink({
const accessibleLabel = `Back to ${destination.label}`;
const { switchOrganizationId } = destination;
- const handleClick = async (event: MouseEvent<HTMLAnchorElement>) => {
- if (
- !switchOrganizationId ||
- event.button !== 0 ||
- event.metaKey ||
- event.ctrlKey ||
- event.shiftKey ||
- event.altKey
- ) {
- return;
- }
- event.preventDefault();
- if (isSwitching) return;
- setIsSwitching(true);
- try {
- await updateActiveOrganization(
- switchOrganizationId as Organisation.OrganisationId,
- );
- } catch (error) {
- console.error("Failed to switch organization before going back", error);
- }
- router.push(destination.href);
- };
+ const classes = clsx(
+ compact
+ ? "flex size-8 shrink-0 items-center justify-center rounded-full border border-gray-5 bg-gray-2 text-gray-11 hover:bg-gray-4"
+ : "group inline-flex h-8 min-w-0 max-w-full items-center gap-1.5 rounded-full py-1 pl-1.5 pr-3 text-[13px] text-gray-11 hover:bg-gray-3",
+ "transition-colors hover:text-gray-12 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-9 disabled:cursor-wait disabled:opacity-70",
+ className,
+ );
+
+ const content = compact ? (
+ <ChevronLeft className="size-4" aria-hidden />
+ ) : (
+ <>
+ <ChevronLeft
+ className="size-4 shrink-0 transition-transform duration-200 group-hover:-translate-x-0.5"
+ aria-hidden
+ />
+ <Icon className="size-3.5 shrink-0 text-gray-10" aria-hidden />
+ <span className="truncate">{destination.label}</span>
+ </>
+ );
+
+ // A plain link would let a new tab skip the switch and open the dashboard
+ // in an organization that doesn't hold this Cap.
+ if (switchOrganizationId) {
+ const switchAndGoBack = async () => {
+ setIsSwitching(true);
+ try {
+ await updateActiveOrganization(
+ switchOrganizationId as Organisation.OrganisationId,
+ );
+ } catch (error) {
+ console.error("Failed to switch organization", error);
+ toast.error("Couldn't open your dashboard. Please try again.");
+ setIsSwitching(false);
+ return;
+ }
+ router.push(destination.href);
+ };
- if (compact) {
return (
- <Link
- href={destination.href}
- onClick={handleClick}
+ <button
+ type="button"
+ onClick={switchAndGoBack}
+ disabled={isSwitching}
aria-label={accessibleLabel}
- aria-busy={isSwitching}
- title={accessibleLabel}
- className={clsx(
- "flex size-8 shrink-0 items-center justify-center rounded-full border border-gray-5 bg-gray-2 text-gray-11 transition-colors hover:bg-gray-4 hover:text-gray-12 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-9",
- className,
- )}
+ title={compact ? accessibleLabel : undefined}
+ className={classes}
>
- <ChevronLeft className="size-4" aria-hidden />
- </Link>
+ {content}
+ </button>
);
}
return (
<Link
href={destination.href}
- onClick={handleClick}
aria-label={accessibleLabel}
- aria-busy={isSwitching}
- className={clsx(
- "group inline-flex h-8 min-w-0 max-w-full items-center gap-1.5 rounded-full py-1 pl-1.5 pr-3 text-[13px] text-gray-11 transition-colors hover:bg-gray-3 hover:text-gray-12 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-9",
- isSwitching && "opacity-70",
- className,
- )}
+ title={compact ? accessibleLabel : undefined}
+ className={classes}
>
- <ChevronLeft
- className="size-4 shrink-0 transition-transform duration-200 group-hover:-translate-x-0.5"
- aria-hidden
- />
- <Icon className="size-3.5 shrink-0 text-gray-10" aria-hidden />
- <span className="truncate">{destination.label}</span>
+ {content}
</Link>
);
}