Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions apps/start/src/hooks/use-project-document-title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useEffect, useRef } from 'react';

const BASE_SUFFIX = ' | OpenPanel.dev';

function inject(title: string, projectName: string): string {
if (!title.endsWith(BASE_SUFFIX)) return title;
if (title.includes(` | ${projectName}${BASE_SUFFIX}`)) return title;
return title.replace(BASE_SUFFIX, ` | ${projectName}${BASE_SUFFIX}`);
}

// Browser tab titles are set by each route's `head()` (server + client).
// Rather than threading project name through every route's loader/head,
// we patch the title imperatively on the client whenever it changes —
// observed via a MutationObserver on <title>. This keeps the change
// localized to one hook mounted under the project layout.
export function useProjectDocumentTitle(projectName: string | undefined) {
const lastApplied = useRef<string | null>(null);

useEffect(() => {
if (!projectName) return;

const apply = () => {
const current = document.title;
if (current === lastApplied.current) return;
const next = inject(current, projectName);
if (next !== current) {
document.title = next;
}
lastApplied.current = document.title;
};

apply();

// Observe the whole <head> rather than the <title> element directly,
// so we still catch updates if React swaps the title node entirely.
const observer = new MutationObserver(apply);
observer.observe(document.head, {
childList: true,
characterData: true,
subtree: true,
});
return () => observer.disconnect();
}, [projectName]);
}
24 changes: 18 additions & 6 deletions apps/start/src/routes/_app.$organizationId.$projectId.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BillingPrompt from '@/components/organization/billing-prompt';
import { useProjectDocumentTitle } from '@/hooks/use-project-document-title';
import { useTRPC } from '@/integrations/trpc/react';
import { PAGE_TITLES, createProjectTitle } from '@/utils/title';
import { FREE_PRODUCT_IDS } from '@openpanel/payments';
Expand All @@ -17,22 +18,33 @@ export const Route = createFileRoute('/_app/$organizationId/$projectId')({
};
},
loader: async ({ context, params }) => {
await context.queryClient.prefetchQuery(
context.trpc.organization.get.queryOptions({
organizationId: params.organizationId,
}),
);
await Promise.all([
context.queryClient.prefetchQuery(
context.trpc.organization.get.queryOptions({
organizationId: params.organizationId,
}),
),
context.queryClient.prefetchQuery(
context.trpc.project.getProjectWithClients.queryOptions({
projectId: params.projectId,
}),
),
]);
},
});

function ProjectDashboard() {
const { organizationId } = Route.useParams();
const { organizationId, projectId } = Route.useParams();
const trpc = useTRPC();
const { data: organization } = useSuspenseQuery(
trpc.organization.get.queryOptions({
organizationId,
}),
);
const { data: project } = useSuspenseQuery(
trpc.project.getProjectWithClients.queryOptions({ projectId }),
);
useProjectDocumentTitle(project?.name);

if (
organization.subscriptionProductId &&
Expand Down