Skip to content

Commit

Permalink
Added 2 swr hooks: useProject and useUsage & refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Sep 24, 2022
1 parent 943b652 commit 44ea16a
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 34 deletions.
File renamed without changes.
4 changes: 3 additions & 1 deletion components/app/custom-domain/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import {
import { useEditDomainModal } from "./edit-domain-modal";
import { DomainVerificationStatusProps } from "@/lib/types";
import DomainConfiguration from "./domain-configuration";
import useProject from "@/lib/swr/use-project";

export default function CustomDomain({ domain }: { domain: string }) {
export default function CustomDomain() {
const router = useRouter();
const { slug } = router.query as { slug: string };
const { project: { domain } = {} } = useProject();

const { data, isValidating } = useSWR<{
status: DomainVerificationStatusProps;
Expand Down
2 changes: 1 addition & 1 deletion components/app/link-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { LinkProps } from "@/lib/types";
import Tooltip, { TooltipContent } from "@/components/shared/tooltip";
import useProject from "@/lib/swr/use-project";
import useUsage from "@/lib/swr/use-usage";
import { useAddEditLinkModal } from "./add-link-modal";
import { useAddEditLinkModal } from "./add-edit-link-modal";
import { useDeleteLinkModal } from "./delete-link-modal";

export default function LinkCard({ props }: { props: LinkProps }) {
Expand Down
10 changes: 5 additions & 5 deletions components/app/plan-usage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import {
} from "@/components/shared/icons";
import { motion } from "framer-motion";
import { getStripe } from "@/lib/stripe/client";
import useProject from "@/lib/swr/use-project";
import useUsage from "@/lib/swr/use-usage";

export default function PlanUsage({ project }: { project: ProjectProps }) {
export default function PlanUsage() {
const router = useRouter();
const { slug } = router.query as { slug: string };

const { data: usage } = useSWR<number>(
project && `/api/projects/${slug}/domains/${project.domain}/usage`,
fetcher
);
const { project } = useProject();
const { usage } = useUsage(project);

const { data: linkCount } = useSWR<number>(
project && `/api/projects/${slug}/domains/${project.domain}/links/count`,
Expand Down
2 changes: 1 addition & 1 deletion lib/swr/use-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function useUsage(project: ProjectProps) {
slug: string;
};

const { data: usage, error } = useSWR(
const { data: usage, error } = useSWR<number>(
router.isReady &&
project &&
`/api/projects/${slug}/domains/${project.domain}/usage`,
Expand Down
10 changes: 3 additions & 7 deletions pages/app/[slug]/[key].tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Stats from "@/components/stats";
import AppLayout from "@/components/layout/app";
import { useRouter } from "next/router";
import useSWR from "swr";
import { fetcher } from "@/lib/utils";
import { ProjectProps } from "@/lib/types";
import useProject from "@/lib/swr/use-project";
import ErrorPage from "next/error";

export default function StatsPage() {
Expand All @@ -12,10 +10,8 @@ export default function StatsPage() {
slug: string;
};

const { data: project, error } = useSWR<ProjectProps>(
router.isReady && `/api/projects/${slug}`,
fetcher
);
const { project, error } = useProject();

// handle error page
if (error && error.status === 404) {
return <ErrorPage statusCode={404} />;
Expand Down
2 changes: 1 addition & 1 deletion pages/app/[slug]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AppLayout from "components/layout/app";
import useProject from "@/lib/swr/use-project";
import MaxWidthWrapper from "@/components/shared/max-width-wrapper";
import { useAddEditLinkModal } from "@/components/app/add-link-modal";
import { useAddEditLinkModal } from "@/components/app/add-edit-link-modal";
import LinksContainer from "@/components/app/links-container";
import ErrorPage from "next/error";

Expand Down
13 changes: 4 additions & 9 deletions pages/app/[slug]/settings.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import AppLayout from "components/layout/app";
import { useRouter } from "next/router";
import useSWR from "swr";
import { fetcher } from "@/lib/utils";
import { ProjectProps } from "@/lib/types";
import useProject from "@/lib/swr/use-project";
import MaxWidthWrapper from "@/components/shared/max-width-wrapper";
import CustomDomain from "@/components/app/custom-domain";
import PlanUsage from "@/components/app/plan-usage";
Expand All @@ -14,10 +12,7 @@ export default function ProjectLinks() {
slug: string;
};

const { data: project, error } = useSWR<ProjectProps>(
router.isReady && `/api/projects/${slug}`,
fetcher
);
const { project, error } = useProject();

// handle error page
if (error && error.status === 404) {
Expand All @@ -35,8 +30,8 @@ export default function ProjectLinks() {
</div>
<MaxWidthWrapper>
<div className="py-10 grid gap-5">
<PlanUsage project={project} />
<CustomDomain domain={project?.domain || ""} />
<PlanUsage />
<CustomDomain />
</div>
</MaxWidthWrapper>
</AppLayout>
Expand Down
12 changes: 3 additions & 9 deletions pages/app/links/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import AppLayout from "components/layout/app";
import MaxWidthWrapper from "@/components/shared/max-width-wrapper";
import { useAddEditLinkModal } from "@/components/app/add-link-modal";
import { useAddEditLinkModal } from "@/components/app/add-edit-link-modal";
import LinksContainer from "@/components/app/links-container";

export default function Links() {
const { AddEditLinkModal, AddEditLinkButton } = useAddEditLinkModal({
domainVerified: true,
exceededUsage: false,
});
const { AddEditLinkModal, AddEditLinkButton } = useAddEditLinkModal({});

return (
<AppLayout>
Expand All @@ -20,10 +17,7 @@ export default function Links() {
</div>
</MaxWidthWrapper>
</div>
<LinksContainer
exceededUsage={false}
AddEditLinkButton={AddEditLinkButton}
/>
<LinksContainer AddEditLinkButton={AddEditLinkButton} />
</AppLayout>
);
}

0 comments on commit 44ea16a

Please sign in to comment.