Skip to content

Commit

Permalink
improve useLinksCount
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Oct 7, 2024
1 parent d1bcbd9 commit b20303f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 26,12 @@ export default function WorkspaceTagsClient() {
const { AddEditTagModal, AddTagButton } = useAddEditTagModal();

const { tags, loading } = useTags();
const { data: tagsCount } = useLinksCount({
const { data: tagsCount } = useLinksCount<
{
tagId: string;
_count: number;
}[]
>({
groupBy: "tagId",
showArchived: true,
});
Expand Down
4 changes: 2 additions & 2 deletions apps/web/lib/swr/use-links-count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 8,7 @@ import useWorkspace from "./use-workspace";

const partialQuerySchema = getLinksCountQuerySchema.partial();

export default function useLinksCount(
export default function useLinksCount<T = any>(
opts: z.infer<typeof partialQuerySchema> & { ignoreParams?: boolean } = {},
) {
const { id: workspaceId } = useWorkspace();
Expand Down Expand Up @@ -49,7 49,7 @@ export default function useLinksCount(
);

return {
data,
data: data as T,
loading: !error && data === undefined,
error,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 31,7 @@ function OnboardingButtonInner({
const { slug } = useParams() as { slug: string };

const { data: domainsCount, loading: domainsLoading } = useDomainsCount();
const { data: linksCount, loading: linksLoading } = useLinksCount({
const { data: linksCount, loading: linksLoading } = useLinksCount<number>({
ignoreParams: true,
});
const { users, loading: usersLoading } = useUsers();
Expand Down
4 changes: 2 additions & 2 deletions apps/web/ui/links/archived-links-hint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 15,8 @@ export default function ArchivedLinksHint() {
}

function ArchivedLinksHintHelper() {
const { data: count } = useLinksCount();
const { data: totalCount } = useLinksCount({ showArchived: true });
const { data: count } = useLinksCount<number>();
const { data: totalCount } = useLinksCount<number>({ showArchived: true });
const archivedCount = totalCount - count;

const { setShowArchived } = useContext(LinksDisplayContext);
Expand Down
2 changes: 1 addition & 1 deletion apps/web/ui/links/links-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 39,7 @@ export default function LinksContainer({
const { viewMode, sort, showArchived } = useContext(LinksDisplayContext);

const { links, isValidating } = useLinks({ sort, showArchived });
const { data: count } = useLinksCount({ showArchived });
const { data: count } = useLinksCount<number>({ showArchived });

return (
<MaxWidthWrapper className="grid gap-y-2">
Expand Down
63 changes: 28 additions & 35 deletions apps/web/ui/links/use-link-filters.tsx
Original file line number Diff line number Diff line change
@@ -1,10 1,8 @@
import useDomains from "@/lib/swr/use-domains";
import useLinksCount from "@/lib/swr/use-links-count";
import useTags from "@/lib/swr/use-tags";
import useUsers from "@/lib/swr/use-users";
import useWorkspace from "@/lib/swr/use-workspace";
import { Avatar, BlurImage, Globe, Tag, User, useRouterStuff } from "@dub/ui";
import { DUB_WORKSPACE_ID, GOOGLE_FAVICON_URL, nFormatter } from "@dub/utils";
import { GOOGLE_FAVICON_URL, nFormatter } from "@dub/utils";
import { useContext, useMemo } from "react";
import { LinksDisplayContext } from "./links-display-provider";
import TagBadge from "./tag-badge";
Expand Down Expand Up @@ -143,50 141,40 @@ export function useLinkFilters() {
}

function useDomainFilterOptions() {
const { id: workspaceId } = useWorkspace();
const { showArchived } = useContext(LinksDisplayContext);

const { data: domainsCount } = useLinksCount({
const { data: domainsCount } = useLinksCount<
{
domain: string;
_count: number;
}[]
>({
groupBy: "domain",
showArchived,
});
const { activeWorkspaceDomains, activeDefaultDomains } = useDomains();

return useMemo(() => {
if (domainsCount?.length === 0) return [];

const workspaceDomains = activeWorkspaceDomains?.map((domain) => ({
...domain,
count:
domainsCount?.find(({ domain: d }) => d === domain.slug)?._count || 0,
}));

const defaultDomains =
workspaceId === `ws_${DUB_WORKSPACE_ID}`
? []
: activeDefaultDomains
?.map((domain) => ({
...domain,
count:
domainsCount?.find(({ domain: d }) => d === domain.slug)
?._count || 0,
}))
.filter((d) => d.count > 0);

const finalOptions = [
...(workspaceDomains || []),
...(defaultDomains || []),
].sort((a, b) => b.count - a.count);

return finalOptions;
}, [activeWorkspaceDomains, activeDefaultDomains, domainsCount, workspaceId]);
if (!domainsCount || domainsCount.length === 0) return [];

return domainsCount
.map(({ domain, _count }) => ({
slug: domain,
count: _count,
}))
.sort((a, b) => b.count - a.count);
}, [domainsCount]);
}

function useTagFilterOptions() {
const { tags } = useTags();
const { showArchived } = useContext(LinksDisplayContext);

const { data: tagsCount } = useLinksCount({ groupBy: "tagId", showArchived });
const { data: tagsCount } = useLinksCount<
{
tagId: string;
_count: number;
}[]
>({ groupBy: "tagId", showArchived });

return useMemo(
() =>
Expand All @@ -204,7 192,12 @@ function useUserFilterOptions() {
const { users } = useUsers();
const { showArchived } = useContext(LinksDisplayContext);

const { data: usersCount } = useLinksCount({
const { data: usersCount } = useLinksCount<
{
userId: string;
_count: number;
}[]
>({
groupBy: "userId",
showArchived,
});
Expand Down
2 changes: 1 addition & 1 deletion apps/web/ui/modals/complete-setup-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 25,7 @@ function CompleteSetupModal({
const { slug } = useParams() as { slug: string };

const { data: domainsCount } = useDomainsCount();
const { data: linksCount } = useLinksCount({ ignoreParams: true });
const { data: linksCount } = useLinksCount<number>({ ignoreParams: true });
const { users } = useUsers();
const { users: invites } = useUsers({ invites: true });
const { setShowLinkBuilder } = useContext(ModalContext);
Expand Down

0 comments on commit b20303f

Please sign in to comment.