Files
pn-new-crm/src/providers/port-provider.tsx

88 lines
2.8 KiB
TypeScript
Raw Normal View History

'use client';
feat(navigation): persist last-port for next-login + root → /dashboard Login routing previously always landed at the user's first port-role. With a multi-port operator (super-admins, multi-tenant ops) the active port reverted on every login, breaking the "I was working in X yesterday" continuity. - PortProvider PATCHes `/api/v1/me` with `preferences.defaultPortId = currentPort.id` whenever the active port changes (URL or explicit switch). Ref-keyed dedupe; fire-and-forget so navigation isn't blocked by a transient PATCH failure. - UserMenu's port-switcher also writes the preference on click so the preference is captured even for users who never re-render through PortProvider. - /dashboard resolver checks `preferences.defaultPortId` first, falling back to first-port-by-name (super-admin) or first-role (everyone else). The preference is verified against current access before being honoured — a stale id from a revoked role or archived port can't strand the user on a 403. - Add /src/app/page.tsx that redirects `/` → `/dashboard` so the middleware's `redirect=/` post-login parameter doesn't dump users on an empty 404. The existing /dashboard handler then routes them on to their resolved port. - UserMenu sign-out: replace `router.push('/api/auth/sign-out')` (which issued a GET against better-auth's POST-only endpoint, causing Safari and Comet/Arc to land the JSON response as a `sign_out` download) with `signOut()` from the auth client + an explicit redirect. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 19:06:48 +02:00
import { createContext, useContext, useEffect, useRef, type ReactNode } from 'react';
import { useParams } from 'next/navigation';
import { useUIStore } from '@/stores/ui-store';
feat(navigation): persist last-port for next-login + root → /dashboard Login routing previously always landed at the user's first port-role. With a multi-port operator (super-admins, multi-tenant ops) the active port reverted on every login, breaking the "I was working in X yesterday" continuity. - PortProvider PATCHes `/api/v1/me` with `preferences.defaultPortId = currentPort.id` whenever the active port changes (URL or explicit switch). Ref-keyed dedupe; fire-and-forget so navigation isn't blocked by a transient PATCH failure. - UserMenu's port-switcher also writes the preference on click so the preference is captured even for users who never re-render through PortProvider. - /dashboard resolver checks `preferences.defaultPortId` first, falling back to first-port-by-name (super-admin) or first-role (everyone else). The preference is verified against current access before being honoured — a stale id from a revoked role or archived port can't strand the user on a 403. - Add /src/app/page.tsx that redirects `/` → `/dashboard` so the middleware's `redirect=/` post-login parameter doesn't dump users on an empty 404. The existing /dashboard handler then routes them on to their resolved port. - UserMenu sign-out: replace `router.push('/api/auth/sign-out')` (which issued a GET against better-auth's POST-only endpoint, causing Safari and Comet/Arc to land the JSON response as a `sign_out` download) with `signOut()` from the auth client + an explicit redirect. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 19:06:48 +02:00
import { apiFetch } from '@/lib/api/client';
import type { Port } from '@/lib/db/schema/ports';
interface PortContextValue {
ports: Port[];
currentPort: Port | null;
currentPortId: string | null;
currentPortSlug: string | null;
}
const PortContext = createContext<PortContextValue>({
ports: [],
currentPort: null,
currentPortId: null,
currentPortSlug: null,
});
interface PortProviderProps {
children: ReactNode;
ports: Port[];
defaultPortId: string | null;
}
export function PortProvider({ children, ports, defaultPortId }: PortProviderProps) {
const params = useParams();
const portSlugFromUrl = params?.portSlug as string | undefined;
const setPort = useUIStore((s) => s.setPort);
const currentPortId = useUIStore((s) => s.currentPortId);
const currentPortSlug = useUIStore((s) => s.currentPortSlug);
// Resolve current port - URL slug takes priority over stored port
const currentPort =
ports.find((p) => p.slug === portSlugFromUrl) ??
ports.find((p) => p.id === currentPortId) ??
(defaultPortId ? (ports.find((p) => p.id === defaultPortId) ?? null) : null);
// Sync Zustand store whenever the active port changes
useEffect(() => {
if (currentPort && (currentPort.id !== currentPortId || currentPort.slug !== currentPortSlug)) {
setPort(currentPort.id, currentPort.slug);
}
}, [currentPort, currentPortId, currentPortSlug, setPort]);
feat(navigation): persist last-port for next-login + root → /dashboard Login routing previously always landed at the user's first port-role. With a multi-port operator (super-admins, multi-tenant ops) the active port reverted on every login, breaking the "I was working in X yesterday" continuity. - PortProvider PATCHes `/api/v1/me` with `preferences.defaultPortId = currentPort.id` whenever the active port changes (URL or explicit switch). Ref-keyed dedupe; fire-and-forget so navigation isn't blocked by a transient PATCH failure. - UserMenu's port-switcher also writes the preference on click so the preference is captured even for users who never re-render through PortProvider. - /dashboard resolver checks `preferences.defaultPortId` first, falling back to first-port-by-name (super-admin) or first-role (everyone else). The preference is verified against current access before being honoured — a stale id from a revoked role or archived port can't strand the user on a 403. - Add /src/app/page.tsx that redirects `/` → `/dashboard` so the middleware's `redirect=/` post-login parameter doesn't dump users on an empty 404. The existing /dashboard handler then routes them on to their resolved port. - UserMenu sign-out: replace `router.push('/api/auth/sign-out')` (which issued a GET against better-auth's POST-only endpoint, causing Safari and Comet/Arc to land the JSON response as a `sign_out` download) with `signOut()` from the auth client + an explicit redirect. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 19:06:48 +02:00
// Remember the last port the user landed on (URL-derived or
// explicit-switch) so the next login routes here automatically. Tracked
// in a ref-keyed dedupe so we only PATCH when the active port actually
// changes - re-renders inside the same port don't write. Fire-and-forget;
feat(navigation): persist last-port for next-login + root → /dashboard Login routing previously always landed at the user's first port-role. With a multi-port operator (super-admins, multi-tenant ops) the active port reverted on every login, breaking the "I was working in X yesterday" continuity. - PortProvider PATCHes `/api/v1/me` with `preferences.defaultPortId = currentPort.id` whenever the active port changes (URL or explicit switch). Ref-keyed dedupe; fire-and-forget so navigation isn't blocked by a transient PATCH failure. - UserMenu's port-switcher also writes the preference on click so the preference is captured even for users who never re-render through PortProvider. - /dashboard resolver checks `preferences.defaultPortId` first, falling back to first-port-by-name (super-admin) or first-role (everyone else). The preference is verified against current access before being honoured — a stale id from a revoked role or archived port can't strand the user on a 403. - Add /src/app/page.tsx that redirects `/` → `/dashboard` so the middleware's `redirect=/` post-login parameter doesn't dump users on an empty 404. The existing /dashboard handler then routes them on to their resolved port. - UserMenu sign-out: replace `router.push('/api/auth/sign-out')` (which issued a GET against better-auth's POST-only endpoint, causing Safari and Comet/Arc to land the JSON response as a `sign_out` download) with `signOut()` from the auth client + an explicit redirect. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 19:06:48 +02:00
// a transient network failure shouldn't block navigation, and the
// post-login resolver verifies access so a stale value can't strand the
// user on a 403.
const lastPersistedPortIdRef = useRef<string | null>(null);
useEffect(() => {
if (!currentPort) return;
if (lastPersistedPortIdRef.current === currentPort.id) return;
lastPersistedPortIdRef.current = currentPort.id;
void apiFetch('/api/v1/me', {
method: 'PATCH',
body: { preferences: { defaultPortId: currentPort.id } },
}).catch(() => {
/* silent - best-effort */
feat(navigation): persist last-port for next-login + root → /dashboard Login routing previously always landed at the user's first port-role. With a multi-port operator (super-admins, multi-tenant ops) the active port reverted on every login, breaking the "I was working in X yesterday" continuity. - PortProvider PATCHes `/api/v1/me` with `preferences.defaultPortId = currentPort.id` whenever the active port changes (URL or explicit switch). Ref-keyed dedupe; fire-and-forget so navigation isn't blocked by a transient PATCH failure. - UserMenu's port-switcher also writes the preference on click so the preference is captured even for users who never re-render through PortProvider. - /dashboard resolver checks `preferences.defaultPortId` first, falling back to first-port-by-name (super-admin) or first-role (everyone else). The preference is verified against current access before being honoured — a stale id from a revoked role or archived port can't strand the user on a 403. - Add /src/app/page.tsx that redirects `/` → `/dashboard` so the middleware's `redirect=/` post-login parameter doesn't dump users on an empty 404. The existing /dashboard handler then routes them on to their resolved port. - UserMenu sign-out: replace `router.push('/api/auth/sign-out')` (which issued a GET against better-auth's POST-only endpoint, causing Safari and Comet/Arc to land the JSON response as a `sign_out` download) with `signOut()` from the auth client + an explicit redirect. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 19:06:48 +02:00
});
}, [currentPort]);
return (
<PortContext.Provider
value={{
ports,
currentPort: currentPort ?? null,
currentPortId: currentPort?.id ?? null,
currentPortSlug: currentPort?.slug ?? null,
}}
>
{children}
</PortContext.Provider>
);
}
export function usePortContext(): PortContextValue {
return useContext(PortContext);
}