Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

feat(web): put initiatives list tab, page and sorting in the URL (#30)

Croffd6595b3f70b8
diff --git a/apps/web/src/app/project/[projectKey]/initiatives/[tab]/page.tsx b/apps/web/src/app/project/[projectKey]/initiatives/[tab]/page.tsx
new file mode 100644
index 00000000..12480316
--- /dev/null
+++ b/apps/web/src/app/project/[projectKey]/initiatives/[tab]/page.tsx
@@ -0,0 +1,21 @@
+import { redirect } from 'next/navigation';
+import RequireFeature from '@/components/common/permissions/RequireFeature';
+import InitiativesPage from '@/features/initiatives/InitiativesPage';
+import { initiativesPath, isInitiativesTab } from '@/utils/paths';
+
+// One status tab of the initiatives list. A segment that names no tab goes back to
+// the list ("All" lives at /initiatives, not /initiatives/all).
+export default async function Page({
+ params,
+}: {
+ params: Promise<{ projectKey: string; tab: string }>;
+}) {
+ const { projectKey, tab } = await params;
+ if (tab === 'all' || !isInitiativesTab(tab)) redirect(initiativesPath(projectKey));
+
+ return (
+ <RequireFeature feature="initiatives">
+ <InitiativesPage tab={tab} />
+ </RequireFeature>
+ );
+}
diff --git a/apps/web/src/app/project/[projectKey]/initiatives/[initiativeId]/issues/page.tsx b/apps/web/src/app/project/[projectKey]/initiatives/details/[initiativeId]/issues/page.tsx
similarity index 100%
rename from apps/web/src/app/project/[projectKey]/initiatives/[initiativeId]/issues/page.tsx
rename to apps/web/src/app/project/[projectKey]/initiatives/details/[initiativeId]/issues/page.tsx
diff --git a/apps/web/src/app/project/[projectKey]/initiatives/[initiativeId]/page.tsx b/apps/web/src/app/project/[projectKey]/initiatives/details/[initiativeId]/page.tsx
similarity index 100%
rename from apps/web/src/app/project/[projectKey]/initiatives/[initiativeId]/page.tsx
rename to apps/web/src/app/project/[projectKey]/initiatives/details/[initiativeId]/page.tsx
diff --git a/apps/web/src/app/project/[projectKey]/initiatives/page.tsx b/apps/web/src/app/project/[projectKey]/initiatives/page.tsx
index 87c2feb9..183dd042 100644
--- a/apps/web/src/app/project/[projectKey]/initiatives/page.tsx
+++ b/apps/web/src/app/project/[projectKey]/initiatives/page.tsx
@@ -4,7 +4,7 @@ import InitiativesPage from '@/features/initiatives/InitiativesPage';
export default function Page() {
return (
<RequireFeature feature="initiatives">
- <InitiativesPage />
+ <InitiativesPage tab="all" />
</RequireFeature>
);
}
diff --git a/apps/web/src/features/initiatives/InitiativesPage.tsx b/apps/web/src/features/initiatives/InitiativesPage.tsx
index c1600c8f..5c91fcc4 100644
--- a/apps/web/src/features/initiatives/InitiativesPage.tsx
+++ b/apps/web/src/features/initiatives/InitiativesPage.tsx
@@ -1,11 +1,18 @@
'use client';
import { useState } from 'react';
+import { useRouter, useSearchParams } from 'next/navigation';
import { Plus } from 'lucide-react';
import { useShell } from '@/context/shellContext';
import { usePermissions } from '@/hooks/usePermissions';
import { useInitiativeCountsQuery, useInitiativesQuery } from '@/services/initiatives.service';
-import type { InitiativeCounts, InitiativeSort, InitiativeStatus } from '@/lib/api';
+import {
+ INITIATIVE_SORTS,
+ type InitiativeCounts,
+ type InitiativeSort,
+ type InitiativeStatus,
+} from '@/lib/api';
+import { initiativesTabPath, type InitiativesTab } from '@/utils/paths';
import { Button } from '@/components/ui/button';
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
import InitiativesList from './components/list/InitiativesList';
@@ -15,11 +22,9 @@ import InitiativeTabCount from './components/list/InitiativeTabCount';
const PAGE_SIZE = 25;
-type Tab = 'all' | 'proposed' | 'planned' | 'active' | 'completed';
-
// One tab per lifecycle status, except the terminal statuses share a "Completed"
// tab. `statuses: undefined` means the tab takes every status.
-const TABS: { value: Tab; label: string; statuses: InitiativeStatus[] | undefined }[] = [
+const TABS: { value: InitiativesTab; label: string; statuses: InitiativeStatus[] | undefined }[] = [
{ value: 'all', label: 'All initiatives', statuses: undefined },
{ value: 'proposed', label: 'Proposed', statuses: ['proposed'] },
{ value: 'planned', label: 'Planned', statuses: ['planned'] },
@@ -28,7 +33,7 @@ const TABS: { value: Tab; label: string; statuses: InitiativeStatus[] | undefine
];
// The "Completed" tab groups the two terminal statuses, so its count sums them.
-function tabCount(counts: InitiativeCounts | undefined, tab: Tab): number | undefined {
+function tabCount(counts: InitiativeCounts | undefined, tab: InitiativesTab): number | undefined {
if (!counts) return undefined;
switch (tab) {
case 'all':
@@ -44,24 +49,33 @@ function tabCount(counts: InitiativeCounts | undefined, tab: Tab): number | unde
}
}
-// A project's initiatives, one status tab at a time. Each tab loads its own page
-// from the server, sorted and paged there; the tab counts come from a separate
-// aggregate so they stay correct regardless of the current page.
-export default function InitiativesPage() {
+// A project's initiatives, one status tab at a time. The open tab is a route of its
+// own and the page and sorting are query parameters, so the list reopens as it was
+// after a reload and can be shared as a link. Each tab loads its own page from the
+// server, sorted and paged there; the tab counts come from a separate aggregate so
+// they stay correct regardless of the current page.
+export default function InitiativesPage({ tab }: { tab: InitiativesTab }) {
const { project } = useShell();
const { can } = usePermissions();
- const [tab, setTab] = useState<Tab>('all');
- const [page, setPage] = useState(1);
- const [sort, setSort] = useState<{ key: InitiativeSort; dir: 'asc' | 'desc' } | null>(null);
+ const router = useRouter();
+ const searchParams = useSearchParams();
const [creating, setCreating] = useState(false);
const projectKey = project?.project.key ?? null;
const activeTab = TABS.find((t) => t.value === tab)!;
+ const pageParam = Number(searchParams.get('page'));
+ const page = Number.isInteger(pageParam) && pageParam > 0 ? pageParam : 1;
+ // A sort the server does not support is no sort at all, and a direction on its own
+ // means nothing.
+ const sort = INITIATIVE_SORTS.find((key) => key === searchParams.get('sort'));
+ const sortDir = searchParams.get('dir') === 'desc' ? 'desc' : 'asc';
+ const dir = sort ? sortDir : undefined;
+
const query = useInitiativesQuery(projectKey, {
statuses: activeTab.statuses,
- sort: sort?.key,
- dir: sort?.dir,
+ sort,
+ dir,
page,
pageSize: PAGE_SIZE,
});
@@ -72,17 +86,33 @@ export default function InitiativesPage() {
const items = query.data?.items ?? [];
const total = query.data?.total ?? 0;
- const changeTab = (next: Tab) => {
- setTab(next);
- setPage(1);
+ const tabPath = initiativesTabPath(project.project.key, tab);
+
+ const pushQuery = (params: URLSearchParams) => {
+ const search = params.toString();
+ router.push(search ? `${tabPath}?${search}` : tabPath);
+ };
+
+ // A tab is its own route and carries no query, so switching one drops the page and
+ // the sorting of the tab left behind.
+ const changeTab = (next: InitiativesTab) => {
+ router.push(initiativesTabPath(project.project.key, next));
+ };
+
+ const changePage = (next: number) => {
+ const params = new URLSearchParams(searchParams);
+ if (next > 1) params.set('page', String(next));
+ else params.delete('page');
+ pushQuery(params);
};
// Re-selecting the sorted column flips its direction; a new column sorts ascending.
const changeSort = (key: InitiativeSort) => {
- setSort((prev) =>
- prev?.key === key ? { key, dir: prev.dir === 'asc' ? 'desc' : 'asc' } : { key, dir: 'asc' },
- );
- setPage(1);
+ const params = new URLSearchParams(searchParams);
+ params.set('sort', key);
+ params.set('dir', sort === key && sortDir === 'asc' ? 'desc' : 'asc');
+ params.delete('page');
+ pushQuery(params);
};
return (
@@ -98,7 +128,7 @@ export default function InitiativesPage() {
</div>
<div className="px-4 pb-2">
- <Tabs value={tab} onValueChange={(v) => changeTab(v as Tab)}>
+ <Tabs value={tab} onValueChange={(v) => changeTab(v as InitiativesTab)}>
<TabsList variant="line">
{TABS.map((t) => (
<TabsTrigger key={t.value} value={t.value}>
@@ -117,12 +147,12 @@ export default function InitiativesPage() {
canCreate={can('initiatives', 'create')}
onCreate={() => setCreating(true)}
statusLabel={activeTab.statuses ? activeTab.label : undefined}
- sort={sort?.key}
- dir={sort?.dir}
+ sort={sort}
+ dir={dir}
onSort={changeSort}
/>
- <InitiativesPagination page={page} pageSize={PAGE_SIZE} total={total} onPage={setPage} />
+ <InitiativesPagination page={page} pageSize={PAGE_SIZE} total={total} onPage={changePage} />
{creating && projectKey && (
<CreateInitiativeDialog projectKey={projectKey} onClose={() => setCreating(false)} />
diff --git a/apps/web/src/hooks/useShellRoute.ts b/apps/web/src/hooks/useShellRoute.ts
index 58254453..86457237 100644
--- a/apps/web/src/hooks/useShellRoute.ts
+++ b/apps/web/src/hooks/useShellRoute.ts
@@ -44,7 +44,9 @@ export function useShellRoute(): ShellRoute {
section: sub === 'settings' ? (segs[3] ?? null) : null,
aiTeamCrumb: sub === 'ai-team' ? aiTeamLabel(segs[3] ?? null) : null,
routeIssueSeq: sub === 'issue' && segs[3] ? Number(segs[3]) : null,
- routeInitiativeId: sub === 'initiatives' && segs[3] ? Number(segs[3]) : null,
+ // /initiatives/details/:id — the segment right after 'initiatives' is a list tab.
+ routeInitiativeId:
+ sub === 'initiatives' && segs[3] === 'details' && segs[4] ? Number(segs[4]) : null,
onBoard: sub == null || sub === 'view',
};
}
diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts
index 6c065654..bff16a3c 100644
--- a/apps/web/src/lib/api.ts
+++ b/apps/web/src/lib/api.ts
@@ -1415,7 +1415,9 @@ export interface Initiative {
// Columns the initiative list can be sorted by, server-side. progress and health
// are derived and not sortable.
-export type InitiativeSort = 'title' | 'priority' | 'targetDate' | 'owner';
+export const INITIATIVE_SORTS = ['title', 'priority', 'targetDate', 'owner'] as const;
+
+export type InitiativeSort = (typeof INITIATIVE_SORTS)[number];
export interface InitiativeListParams {
statuses?: string[];
diff --git a/apps/web/src/utils/paths.ts b/apps/web/src/utils/paths.ts
index c9f41f48..5ec2f052 100644
--- a/apps/web/src/utils/paths.ts
+++ b/apps/web/src/utils/paths.ts
@@ -64,8 +64,21 @@ export const issuePath = (key: string, sequenceNumber: number) =>
export const initiativesPath = (key: string) => `${projectPath(key)}/initiatives`;
-// The initiative detail tabs are routes of their own, so a reload reopens the tab
-// the user was on. Overview is the tab at the initiative's own path.
+// The status tabs of the initiatives list are routes of their own, so a reload or a
+// shared link reopens the tab the user was on. "All" is the tab at the list path;
+// the page and the sorting stay in the query string.
+const INITIATIVES_TABS = ['all', 'proposed', 'planned', 'active', 'completed'] as const;
+
+export type InitiativesTab = (typeof INITIATIVES_TABS)[number];
+
+export const isInitiativesTab = (value: string): value is InitiativesTab =>
+ (INITIATIVES_TABS as readonly string[]).includes(value);
+
+export const initiativesTabPath = (key: string, tab: InitiativesTab) =>
+ tab === 'all' ? initiativesPath(key) : `${initiativesPath(key)}/${tab}`;
+
+// The initiative detail tabs are routes of their own too. They sit under /details/
+// so the tab segment of the list above stays unambiguous.
export type InitiativeTab = 'overview' | 'issues';
export const initiativePath = (
@@ -73,7 +86,7 @@ export const initiativePath = (
initiativeId: number,
tab: InitiativeTab = 'overview',
) => {
- const base = `${initiativesPath(key)}/${initiativeId}`;
+ const base = `${initiativesPath(key)}/details/${initiativeId}`;
return tab === 'overview' ? base : `${base}/${tab}`;
};