Skip to content

Commit 889cafb

Browse files
authored
chore: fix caps count for empty state + display count in sidebar. (#1151)
* fix caps count * display caps count in sidebar * Update Items.tsx
1 parent 00a7d88 commit 889cafb

6 files changed

Lines changed: 44 additions & 5 deletions

File tree

apps/web/app/(org)/dashboard/Contexts.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type SharedContext = {
2222
sharedSpaces: Spaces[] | null;
2323
activeSpace: Spaces | null;
2424
user: typeof users.$inferSelect;
25+
userCapsCount: number | null;
2526
isSubscribed: boolean;
2627
toggleSidebarCollapsed: () => void;
2728
anyNewNotifications: boolean;
@@ -54,6 +55,7 @@ export function DashboardContexts({
5455
organizationData,
5556
activeOrganization,
5657
spacesData,
58+
userCapsCount,
5759
user,
5860
isSubscribed,
5961
organizationSettings,
@@ -67,6 +69,7 @@ export function DashboardContexts({
6769
organizationData: SharedContext["organizationData"];
6870
activeOrganization: SharedContext["activeOrganization"];
6971
spacesData: SharedContext["spacesData"];
72+
userCapsCount: SharedContext["userCapsCount"];
7073
user: SharedContext["user"];
7174
isSubscribed: SharedContext["isSubscribed"];
7275
organizationSettings: SharedContext["organizationSettings"];
@@ -160,6 +163,7 @@ export function DashboardContexts({
160163
organizationData,
161164
activeOrganization,
162165
spacesData,
166+
userCapsCount,
163167
anyNewNotifications,
164168
userPreferences,
165169
organizationSettings,

apps/web/app/(org)/dashboard/_components/Navbar/Items.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ const AdminNavItems = ({ toggleMobileNav }: Props) => {
5050
const pathname = usePathname();
5151
const [open, setOpen] = useState(false);
5252
const [hoveredItem, setHoveredItem] = useState<string | null>(null);
53-
const { user, sidebarCollapsed } = useDashboardContext();
53+
const { user, sidebarCollapsed, userCapsCount } = useDashboardContext();
5454

5555
const manageNavigation = [
5656
{
5757
name: "My Caps",
5858
href: `/dashboard/caps`,
59+
extraText: userCapsCount,
5960
icon: <CapIcon />,
6061
subNav: [],
6162
},
@@ -346,6 +347,7 @@ const AdminNavItems = ({ toggleMobileNav }: Props) => {
346347
sidebarCollapsed={sidebarCollapsed}
347348
toggleMobileNav={toggleMobileNav}
348349
isPathActive={isPathActive}
350+
extraText={item.extraText}
349351
/>
350352
</div>
351353
))}
@@ -435,6 +437,7 @@ const NavItem = ({
435437
sidebarCollapsed,
436438
toggleMobileNav,
437439
isPathActive,
440+
extraText,
438441
}: {
439442
name: string;
440443
href: string;
@@ -446,6 +449,7 @@ const NavItem = ({
446449
sidebarCollapsed: boolean;
447450
toggleMobileNav?: () => void;
448451
isPathActive: (path: string) => boolean;
452+
extraText: number | null | undefined;
449453
}) => {
450454
const iconRef = useRef<CogIconHandle>(null);
451455
return (
@@ -487,6 +491,11 @@ const NavItem = ({
487491
>
488492
{name}
489493
</p>
494+
{extraText && !sidebarCollapsed && (
495+
<p className="ml-auto text-xs font-medium text-gray-11">
496+
{extraText}
497+
</p>
498+
)}
490499
</Link>
491500
</Tooltip>
492501
);

apps/web/app/(org)/dashboard/_components/Navbar/Top.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ import type { DownloadIconHandle } from "../AnimatedIcons/Download";
4747
import type { ReferIconHandle } from "../AnimatedIcons/Refer";
4848

4949
const Top = () => {
50-
const { activeSpace, anyNewNotifications, activeOrganization } =
51-
useDashboardContext();
50+
const { activeSpace, anyNewNotifications } = useDashboardContext();
5251
const [toggleNotifications, setToggleNotifications] = useState(false);
5352
const bellRef = useRef<HTMLDivElement>(null);
5453
const { theme, setThemeHandler } = useTheme();

apps/web/app/(org)/dashboard/caps/page.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,13 @@ export default async function CapsPage(props: PageProps<"/dashboard/caps">) {
113113
const totalCountResult = await db()
114114
.select({ count: count() })
115115
.from(videos)
116-
.where(eq(videos.ownerId, userId));
116+
.leftJoin(organizations, eq(videos.orgId, organizations.id))
117+
.where(
118+
and(
119+
eq(videos.ownerId, userId),
120+
eq(organizations.id, user.activeOrganizationId),
121+
),
122+
);
117123

118124
const totalCount = totalCountResult[0]?.count || 0;
119125

apps/web/app/(org)/dashboard/dashboard-data.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
spaceMembers,
1010
spaces,
1111
users,
12+
videos,
1213
} from "@cap/database/schema";
1314
import { and, count, eq, inArray, isNull, or, sql } from "drizzle-orm";
1415

@@ -85,7 +86,7 @@ export async function getDashboardData(user: typeof userSelectProps) {
8586
let anyNewNotifications = false;
8687
let spacesData: Spaces[] = [];
8788
let organizationSettings: OrganizationSettings | null = null;
88-
89+
let userCapsCount = 0;
8990
// Find active organization ID
9091

9192
let activeOrganizationId = organizationIds.find(
@@ -181,6 +182,20 @@ export async function getDashboardData(user: typeof userSelectProps) {
181182
);
182183
const orgVideoCount = orgVideoCountResult[0]?.value || 0;
183184

185+
const userCapsCountResult = await db()
186+
.select({
187+
value: sql<number>`COUNT(DISTINCT ${videos.id})`,
188+
})
189+
.from(videos)
190+
.where(
191+
and(
192+
eq(videos.orgId, activeOrgInfo.organization.id),
193+
eq(videos.ownerId, user.id),
194+
),
195+
);
196+
197+
userCapsCount = userCapsCountResult[0]?.value || 0;
198+
184199
const allSpacesEntry = {
185200
id: activeOrgInfo.organization.id,
186201
primary: true,
@@ -279,12 +294,14 @@ export async function getDashboardData(user: typeof userSelectProps) {
279294
spacesData,
280295
anyNewNotifications,
281296
userPreferences,
297+
userCapsCount,
282298
};
283299
} catch (error) {
284300
console.error("Failed to fetch dashboard data", error);
285301
return {
286302
organizationSelect: [],
287303
spacesData: [],
304+
userCapsCount: null,
288305
anyNewNotifications: false,
289306
userPreferences: null,
290307
organizationSettings: null,

apps/web/app/(org)/dashboard/layout.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,23 @@ export default async function DashboardLayout({
3333
}
3434

3535
let organizationSelect: Organization[] = [];
36+
let userCapsCount: number | null = null;
3637
let organizationSettings: OrganizationSettings | null = null;
3738
let spacesData: Spaces[] = [];
3839
let anyNewNotifications = false;
3940
let userPreferences: UserPreferences;
4041
try {
4142
const dashboardData = await getDashboardData(user);
4243
organizationSelect = dashboardData.organizationSelect;
44+
userCapsCount = dashboardData.userCapsCount;
4345
organizationSettings = dashboardData.organizationSettings;
4446
userPreferences = dashboardData.userPreferences?.preferences || null;
4547
spacesData = dashboardData.spacesData;
4648
anyNewNotifications = dashboardData.anyNewNotifications;
4749
} catch (error) {
4850
console.error("Failed to load dashboard data", error);
4951
organizationSelect = [];
52+
userCapsCount = null;
5053
organizationSettings = null;
5154
spacesData = [];
5255
anyNewNotifications = false;
@@ -75,6 +78,7 @@ export default async function DashboardLayout({
7578
<UploadingProvider>
7679
<DashboardContexts
7780
organizationSettings={organizationSettings}
81+
userCapsCount={userCapsCount}
7882
organizationData={organizationSelect}
7983
activeOrganization={activeOrganization || null}
8084
spacesData={spacesData}

0 commit comments

Comments
 (0)