Allow getting team websites.

This commit is contained in:
Mike Cao 2025-09-12 08:21:29 -07:00
parent e610de383a
commit 433ce98719
10 changed files with 79 additions and 56 deletions

View file

@ -0,0 +1,17 @@
'use client';
import { Column } from '@umami/react-zen';
import { PageHeader } from '@/components/common/PageHeader';
import { useMessages } from '@/components/hooks';
import { PageBody } from '@/components/common/PageBody';
export function DashboardPage() {
const { formatMessage, labels } = useMessages();
return (
<PageBody>
<Column margin="2">
<PageHeader title={formatMessage(labels.dashboard)}></PageHeader>
</Column>
</PageBody>
);
}

View file

@ -0,0 +1,10 @@
import { Metadata } from 'next';
import { DashboardPage } from './DashboardPage';
export default async function () {
return <DashboardPage />;
}
export const metadata: Metadata = {
title: 'Dashboard',
};

View file

@ -1,20 +1,23 @@
import Link from 'next/link'; import Link from 'next/link';
import { WebsitesTable } from './WebsitesTable'; import { WebsitesTable } from './WebsitesTable';
import { DataGrid } from '@/components/common/DataGrid'; import { DataGrid } from '@/components/common/DataGrid';
import { useNavigation, useUserWebsitesQuery } from '@/components/hooks'; import { useLoginQuery, useNavigation, useUserWebsitesQuery } from '@/components/hooks';
export function WebsitesDataTable({ export function WebsitesDataTable({
userId,
teamId, teamId,
allowEdit = true, allowEdit = true,
allowView = true, allowView = true,
showActions = true, showActions = true,
}: { }: {
userId?: string;
teamId?: string; teamId?: string;
allowEdit?: boolean; allowEdit?: boolean;
allowView?: boolean; allowView?: boolean;
showActions?: boolean; showActions?: boolean;
}) { }) {
const queryResult = useUserWebsitesQuery({ teamId }); const { user } = useLoginQuery();
const queryResult = useUserWebsitesQuery({ userId: userId || user?.id, teamId });
const { renderUrl } = useNavigation(); const { renderUrl } = useNavigation();
const renderLink = (row: any) => ( const renderLink = (row: any) => (

View file

@ -3,6 +3,7 @@ import { Row, Text, Icon, DataTable, DataColumn, MenuItem } from '@umami/react-z
import { useMessages, useNavigation } from '@/components/hooks'; import { useMessages, useNavigation } from '@/components/hooks';
import { MenuButton } from '@/components/input/MenuButton'; import { MenuButton } from '@/components/input/MenuButton';
import { Eye, SquarePen } from '@/components/icons'; import { Eye, SquarePen } from '@/components/icons';
import { Empty } from '@/components/common/Empty';
export function WebsitesTable({ export function WebsitesTable({
data = [], data = [],
@ -10,20 +11,18 @@ export function WebsitesTable({
allowEdit, allowEdit,
allowView, allowView,
renderLink, renderLink,
children,
}: { }: {
data: Record<string, any>[]; data: Record<string, any>[];
showActions?: boolean; showActions?: boolean;
allowEdit?: boolean; allowEdit?: boolean;
allowView?: boolean; allowView?: boolean;
renderLink?: (row: any) => ReactNode; renderLink?: (row: any) => ReactNode;
children?: ReactNode;
}) { }) {
const { formatMessage, labels } = useMessages(); const { formatMessage, labels } = useMessages();
const { renderUrl } = useNavigation(); const { renderUrl } = useNavigation();
if (!data?.length) { if (data.length === 0) {
return children; return <Empty />;
} }
return ( return (

View file

@ -21,7 +21,7 @@ import { WebsiteSelect } from '@/components/input/WebsiteSelect';
export function WebsiteNav({ websiteId }: { websiteId: string }) { export function WebsiteNav({ websiteId }: { websiteId: string }) {
const { formatMessage, labels } = useMessages(); const { formatMessage, labels } = useMessages();
const { pathname, renderUrl, teamId } = useNavigation(); const { pathname, renderUrl, teamId, router } = useNavigation();
const renderPath = (path: string) => const renderPath = (path: string) =>
renderUrl(`/websites/${websiteId}${path}`, { renderUrl(`/websites/${websiteId}${path}`, {
@ -143,13 +143,17 @@ export function WebsiteNav({ websiteId }: { websiteId: string }) {
}, },
]; ];
const handleChange = (value: string) => {
router.push(renderUrl(`/websites/${value}`));
};
const selectedKey = items const selectedKey = items
.flatMap(e => e.items) .flatMap(e => e.items)
.find(({ path }) => path && pathname.endsWith(path.split('?')[0]))?.id; .find(({ path }) => path && pathname.endsWith(path.split('?')[0]))?.id;
return ( return (
<SideMenu items={items} selectedKey={selectedKey} allowMinimize={false} muteItems={false}> <SideMenu items={items} selectedKey={selectedKey} allowMinimize={false} muteItems={false}>
<WebsiteSelect websiteId={websiteId} teamId={teamId} /> <WebsiteSelect websiteId={websiteId} teamId={teamId} onChange={handleChange} />
</SideMenu> </SideMenu>
); );
} }

View file

@ -1,6 +1,6 @@
import { z } from 'zod'; import { z } from 'zod';
import { pagingParams } from '@/lib/schema'; import { pagingParams } from '@/lib/schema';
import { getUserWebsites } from '@/queries'; import { getAllUserWebsitesIncludingTeamOwner, getUserWebsites } from '@/queries';
import { json } from '@/lib/response'; import { json } from '@/lib/response';
import { parseRequest, getQueryFilters } from '@/lib/request'; import { parseRequest, getQueryFilters } from '@/lib/request';
@ -17,7 +17,9 @@ export async function GET(request: Request) {
const filters = await getQueryFilters(query); const filters = await getQueryFilters(query);
const websites = await getUserWebsites(auth.user.id, filters); if (query.includeTeams) {
return json(await getAllUserWebsitesIncludingTeamOwner(auth.user.id, filters));
}
return json(websites); return json(await getUserWebsites(auth.user.id, filters));
} }

View file

@ -40,5 +40,5 @@ export async function GET(
endDate, endDate,
}); });
return json({ ...data[0], comparison }); return json({ ...data, comparison });
} }

View file

@ -3,28 +3,28 @@ import { Select, SelectProps, ListItem, Text } from '@umami/react-zen';
import { import {
useUserWebsitesQuery, useUserWebsitesQuery,
useWebsiteQuery, useWebsiteQuery,
useNavigation,
useMessages, useMessages,
useLoginQuery,
} from '@/components/hooks'; } from '@/components/hooks';
import { Empty } from '@/components/common/Empty'; import { Empty } from '@/components/common/Empty';
export function WebsiteSelect({ export function WebsiteSelect({
websiteId, websiteId,
teamId, teamId,
onChange,
...props ...props
}: { }: {
websiteId?: string; websiteId?: string;
teamId?: string; teamId?: string;
} & SelectProps) { } & SelectProps) {
const { formatMessage, messages } = useMessages(); const { formatMessage, messages } = useMessages();
const { router, renderUrl } = useNavigation();
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
const { data: website } = useWebsiteQuery(websiteId); const { data: website } = useWebsiteQuery(websiteId);
const { data, isLoading } = useUserWebsitesQuery({ teamId }, { search, pageSize: 5 }); const { user } = useLoginQuery();
const { data, isLoading } = useUserWebsitesQuery(
const handleSelect = (value: any) => { { userId: user?.id, teamId },
router.push(renderUrl(`/websites/${value}`)); { search, pageSize: 5 },
}; );
const handleSearch = (value: string) => { const handleSearch = (value: string) => {
setSearch(value); setSearch(value);
@ -45,7 +45,7 @@ export function WebsiteSelect({
allowSearch={true} allowSearch={true}
searchValue={search} searchValue={search}
onSearch={handleSearch} onSearch={handleSearch}
onChange={handleSelect} onChange={onChange}
onOpenChange={handleOpenChange} onOpenChange={handleOpenChange}
listProps={{ listProps={{
renderEmptyState: () => <Empty message={formatMessage(messages.noResultsFound)} />, renderEmptyState: () => <Empty message={formatMessage(messages.noResultsFound)} />,

View file

@ -46,46 +46,34 @@ export async function getWebsites(
return pagedQuery('website', { ...criteria, where }, filters); return pagedQuery('website', { ...criteria, where }, filters);
} }
export async function getAllWebsites(userId: string) { export async function getAllUserWebsitesIncludingTeamOwner(
return prisma.client.website.findMany({ userId: string,
where: { filters?: QueryFilters,
OR: [ ): Promise<PageResult<Website[]>> {
{ userId }, return getWebsites(
{ {
team: { where: {
deletedAt: null, OR: [
teamUser: { { userId },
some: { {
userId, team: {
deletedAt: null,
members: {
some: {
role: ROLES.teamOwner,
userId,
},
}, },
}, },
}, },
}, ],
], },
deletedAt: null,
}, },
}); {
} orderBy: 'name',
...filters,
export async function getAllUserWebsitesIncludingTeamOwner(userId: string) {
return prisma.client.website.findMany({
where: {
OR: [
{ userId },
{
team: {
deletedAt: null,
teamUser: {
some: {
role: ROLES.teamOwner,
userId,
},
},
},
},
],
}, },
}); );
} }
export async function getUserWebsites( export async function getUserWebsites(

View file

@ -58,7 +58,7 @@ async function relationalQuery(
) as t ) as t
`, `,
queryParams, queryParams,
); ).then(result => result?.[0]);
} }
async function clickhouseQuery( async function clickhouseQuery(