Repository: arthurdedeus/r2
Pull request #27: feat: Add new habit
Author: login: arthurdedeus
Created: 2025-04-07T21:47:29Z | Merged: 2025-04-07T22:05:53Z

closes #5

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

- **New Features**
  - Launched an enhanced habit management experience with a streamlined interface for adding and updating habits.
  - Offers seamless, real-time data updates for a more responsive user experience.

- **Refactor**
  - Modernized data fetching and caching to ensure consistent habit information.
  - Improved the application's context and authentication setup for a more reliable performance.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Linked issue #5: Add new Habits

Diff:
diff --git a/package-lock.json b/package-lock.json
index a85b92b..6657e15 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -11,6 +11,7 @@
         "@clerk/nextjs": "^6.12.5",
         "@supabase/ssr": "^0.5.2",
         "@supabase/supabase-js": "^2.49.4",
+        "@tanstack/react-query": "^5.72.0",
         "class-variance-authority": "^0.7.1",
         "clsx": "^2.1.1",
         "lucide-react": "^0.475.0",
@@ -1131,6 +1132,32 @@
         "tslib": "^2.8.0"
       }
     },
+    "node_modules/@tanstack/query-core": {
+      "version": "5.72.0",
+      "resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.72.0.tgz",
+      "integrity": "sha512-aa3p6Mou++JLLxxxVX9AB9uGeRIGc0JWkw96GASXuMG8K3D+JpYbSFcqXbkGFJ1eX2jKHPurmCBoO43RjjXJCA==",
+      "license": "MIT",
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/tannerlinsley"
+      }
+    },
+    "node_modules/@tanstack/react-query": {
+      "version": "5.72.0",
+      "resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.72.0.tgz",
+      "integrity": "sha512-4Dejq/IiXrPlr/0xxj4H2GbC6KckwfTCoHWbd02+UoIV0laC9yke0d0KegmFdXJA712I6UCuy8WpPM76uuPJ+w==",
+      "license": "MIT",
+      "dependencies": {
+        "@tanstack/query-core": "5.72.0"
+      },
+      "funding": {
+        "type": "github",
+        "url": "https://github.com/sponsors/tannerlinsley"
+      },
+      "peerDependencies": {
+        "react": "^18 || ^19"
+      }
+    },
     "node_modules/@types/cookie": {
       "version": "0.6.0",
       "resolved": "https://registry.npmjs.org/@types/cookie/-/cookie-0.6.0.tgz",
diff --git a/package.json b/package.json
index d46ccf9..60c9b7f 100644
--- a/package.json
+++ b/package.json
@@ -12,6 +12,7 @@
     "@clerk/nextjs": "^6.12.5",
     "@supabase/ssr": "^0.5.2",
     "@supabase/supabase-js": "^2.49.4",
+    "@tanstack/react-query": "^5.72.0",
     "class-variance-authority": "^0.7.1",
     "clsx": "^2.1.1",
     "lucide-react": "^0.475.0",
diff --git a/src/app/api/habits/route.ts b/src/app/api/habits/route.ts
new file mode 100644
index 0000000..d168124
--- /dev/null
+++ b/src/app/api/habits/route.ts
@@ -0,0 +1,37 @@
+import { Mark } from "@/app/habits/types";
+import { createHabit, getOrCreateHabitGroup } from "@/lib/crud";
+import { Tables } from "@/lib/supabase/database.types";
+import { NextRequest, NextResponse } from "next/server";
+
+interface CreateHabitBody {
+  name: string;
+  marks: Mark[];
+  month: number;
+  year: number;
+}
+
+export async function POST(
+  request: NextRequest,
+): Promise<NextResponse<Tables<"habits"> | { error: string }>> {
+  const body: CreateHabitBody = await request.json();
+  const habitGroup = await getOrCreateHabitGroup({
+    month: body.month,
+    year: body.year,
+  });
+
+  try {
+    const habit = await createHabit({
+      habitGroupId: habitGroup.id,
+      marks: body.marks,
+      name: body.name,
+    });
+
+    return NextResponse.json(habit, { status: 200 });
+  } catch (error) {
+    console.error(error);
+    return NextResponse.json(
+      { error: "Internal Server Error" },
+      { status: 500 },
+    );
+  }
+}
diff --git a/src/app/habits/components/habits-table.tsx b/src/app/habits/components/habits-table.tsx
index a0f318e..6c4efb3 100644
--- a/src/app/habits/components/habits-table.tsx
+++ b/src/app/habits/components/habits-table.tsx
@@ -1,7 +1,8 @@
 import { useEffect, useState } from "react";
-import { Habit, Mark } from "../types";
+import { Habit, Mark, HabitGroup } from "@/app/habits/types";
 import { cn } from "@/lib/utils";
 import React from "react";
+import { useQuery, useQueryClient } from "@tanstack/react-query";
 
 interface HabitsTableProps {
   currentWeek: number;
@@ -16,9 +17,22 @@ export const HabitsTable = ({
   month,
   year,
 }: HabitsTableProps) => {
-  const [habitList, setHabitList] = useState<Habit[]>([]);
   const [isDesktop, setIsDesktop] = useState<boolean | undefined>(undefined);
-  const [loading, setLoading] = useState(true);
+  const [newHabit, setNewHabit] = useState<Habit | null>(null);
+  const newHabitInputRef = React.useRef<HTMLInputElement>(null);
+  const queryClient = useQueryClient();
+  const { data, isLoading } = useQuery<HabitGroup>({
+    queryKey: ["habit-groups", month, year],
+    queryFn: async () => {
+      const params = new URLSearchParams({
+        userId: String(1),
+        month: String(month),
+        year: String(year),
+      }).toString();
+
+      return await (await fetch("/api/habit-groups?" + params)).json();
+    },
+  });
 
   const mobileGridStyle = {
     gridTemplateColumns: "100px repeat(7, minmax(30px, 1fr))",
@@ -42,25 +56,17 @@ export const HabitsTable = ({
     const newMarks = [...habit.marks];
     newMarks[day] = newMark;
 
-    setHabitList((prev) => {
-      const newHabitList = [...prev];
-      const index = newHabitList.findIndex((h) => h.name === habit.name);
-      if (index === -1) return prev;
-
-      newHabitList[index] = {
-        ...newHabitList[index],
-        marks: newMarks,
-      };
-      return newHabitList;
-    });
-
     fetch(`/api/habits/${habit.id}`, {
       method: "PATCH",
       headers: {
         "Content-Type": "application/json",
       },
       body: JSON.stringify({ marks: newMarks }),
-    });
+    }).then(() =>
+      queryClient.invalidateQueries({
+        queryKey: ["habit-groups", month, year],
+      }),
+    );
   };
 
   const isWeekend = (day: number | null) => {
@@ -78,13 +84,12 @@ export const HabitsTable = ({
     const startDay = currentWeek * 7;
     const days = [];
 
-    // Always push 7 items for mobile view
     for (let i = 0; i < 7; i++) {
       const dayIndex = startDay + i;
       if (dayIndex < daysInMonth) {
         days.push(dayIndex);
       } else {
-        days.push(null); // Push null for empty cells
+        days.push(null);
       }
     }
 
@@ -93,82 +98,136 @@ export const HabitsTable = ({
 
   const days = getCurrentWeekDays();
 
-  useEffect(() => {
-    setLoading(true);
-    async function getHabitGroups() {
-      try {
-        const params = new URLSearchParams({
-          userId: String(1),
-          month: String(month),
-          year: String(year),
-        }).toString();
-
-        const res = await fetch("/api/habit-groups?" + params);
-        const json = await res.json();
-        setHabitList(json.habits);
-      } catch (error) {
-        console.error("Error fetching data:", error);
-      } finally {
-        setLoading(false);
-      }
-    }
+  const handleAddHabit = () => {
+    setNewHabit({
+      name: "",
+      marks: Array(daysInMonth).fill(""),
+      id: 0,
+    });
+  };
 
-    getHabitGroups();
-  }, [month, year]);
+  const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
+    setNewHabit((prev) => {
+      if (!prev) return prev;
+      console.log({ prev });
+      return { ...prev, name: event.target.value };
+    });
+  };
+
+  const createNewHabit = async () => {
+    if (!newHabit) return;
+    fetch("/api/habits", {
+      method: "POST",
+      headers: {
+        "Content-Type": "application/json",
+      },
+      body: JSON.stringify({
+        marks: newHabit.marks,
+        month: month,
+        name: newHabit.name,
+        year: year,
+      }),
+    }).then(() =>
+      queryClient
+        .invalidateQueries({
+          queryKey: ["habit-groups", month, year],
+        })
+        .then(() => {
+          setNewHabit(null);
+        }),
+    );
+  };
+
+  useEffect(() => {
+    newHabitInputRef?.current?.focus();
+  }, [newHabit]);
 
   useEffect(() => {
-    if (window !== undefined) {
+    if (typeof window !== "undefined") {
       setIsDesktop(window.innerWidth >= 768);
     }
   }, []);
 
   return (
     <div className="overflow-x-auto rounded">
-      {!loading && (
-        <div
-          className="grid gap-px bg-neutral-200 min-w-full"
-          style={isDesktop ? desktopGridStyle : mobileGridStyle}
-        >
-          <div className="bg-white p-2 font-medium">
-            <span className="hidden md:inline">Habit</span>
-          </div>
-          {days.map((dayIndex, i) => (
-            <div
-              key={i}
-              className={cn(
-                dayIndex !== null ? "bg-white" : "bg-neutral-200",
-                "p-2 text-center text-sm whitespace-nowrap",
-                isWeekend(dayIndex) ? "font-bold" : "",
-              )}
-            >
-              {dayIndex !== null && dayIndex + 1}
+      <div
+        className="grid gap-px bg-neutral-200 min-w-full"
+        style={isDesktop ? desktopGridStyle : mobileGridStyle}
+      >
+        {!isLoading && (
+          <>
+            <div className="flex flex-row justify-between bg-white p-2 font-medium">
+              <span className="hidden md:inline">Habit</span>
+              <button className="hidden md:inline" onClick={handleAddHabit}>
+                +
+              </button>
             </div>
-          ))}
-
-          {habitList?.map((habit) => (
-            <React.Fragment key={habit.name}>
-              <div className="bg-white p-2 text-sm truncate">{habit.name}</div>
-              {days.map((dayIndex, i) => (
-                <div
-                  key={`${habit.name}-${i}`}
-                  className={cn(
-                    dayIndex !== null
-                      ? "bg-white hover:bg-neutral-50"
-                      : "bg-neutral-200",
-                    "p-2 text-center text-sm",
-                  )}
-                  onClick={() =>
-                    dayIndex !== null && toggleMark(habit, dayIndex)
-                  }
-                  role={dayIndex !== null ? "button" : undefined}
-                >
-                  {dayIndex !== null && habit.marks[dayIndex]}
+            {days.map((dayIndex, i) => (
+              <div
+                key={i}
+                className={cn(
+                  dayIndex !== null ? "bg-white" : "bg-neutral-200",
+                  "p-2 text-center text-sm whitespace-nowrap",
+                  isWeekend(dayIndex) ? "font-bold" : "",
+                )}
+              >
+                {dayIndex !== null && dayIndex + 1}
+              </div>
+            ))}
+            {data?.habits?.map((habit) => (
+              <React.Fragment key={habit.id}>
+                <div className="bg-white p-2 text-sm truncate">
+                  {habit.name}
                 </div>
-              ))}
-            </React.Fragment>
-          ))}
-        </div>
-      )}
+                {days.map((dayIndex, i) => (
+                  <div
+                    key={`${habit.name}-${i}`}
+                    className={cn(
+                      dayIndex !== null
+                        ? "bg-white hover:bg-neutral-50"
+                        : "bg-neutral-200",
+                      "p-2 text-center text-sm",
+                    )}
+                    onClick={() =>
+                      dayIndex !== null && toggleMark(habit, dayIndex)
+                    }
+                    role={dayIndex !== null ? "button" : undefined}
+                  >
+                    {dayIndex !== null && habit.marks[dayIndex]}
+                  </div>
+                ))}
+              </React.Fragment>
+            ))}
+            {newHabit && (
+              <>
+                <input
+                  className="bg-white p-2 text-sm truncate"
+                  onChange={handleInputChange}
+                  onBlur={createNewHabit}
+                  onKeyDown={(event) => {
+                    if (event.key === "Enter") {
+                      event.currentTarget.blur();
+                    }
+                  }}
+                  ref={newHabitInputRef}
+                />
+                {days.map((dayIndex, i) => (
+                  <div
+                    key={`${newHabit.name}-${i}`}
+                    className={cn(
+                      dayIndex !== null
+                        ? "bg-white hover:bg-neutral-50"
+                        : "bg-neutral-200",
+                      "p-2 text-center text-sm",
+                    )}
+                    role={dayIndex !== null ? "button" : undefined}
+                  />
+                ))}
+              </>
+            )}
+          </>
+        )}
+      </div>
     </div>
   );
 };
diff --git a/src/app/habits/types.ts b/src/app/habits/types.ts
index 29813d0..2e4f630 100644
--- a/src/app/habits/types.ts
+++ b/src/app/habits/types.ts
@@ -5,3 +5,10 @@ export type Habit = {
   name: string;
   marks: Mark[];
 };
+
+export type HabitGroup = {
+  id: number;
+  user_id: string;
+  created_at: Date;
+  habits: Habit[];
+};
diff --git a/src/app/layout.tsx b/src/app/layout.tsx
index b115a41..649a421 100644
--- a/src/app/layout.tsx
+++ b/src/app/layout.tsx
@@ -1,7 +1,6 @@
 import type { Metadata } from "next";
 import { Geist, Geist_Mono } from "next/font/google";
 import {
-  ClerkProvider,
   SignInButton,
   SignUpButton,
   SignedIn,
@@ -9,6 +8,7 @@ import {
   UserButton,
 } from "@clerk/nextjs";
 import "./globals.css";
+import Providers from "./providers";
 
 const geistSans = Geist({
   variable: "--font-geist-sans",
@@ -31,7 +31,7 @@ export default function RootLayout({
   children: React.ReactNode;
 }>) {
   return (
-    <ClerkProvider>
+    <Providers>
       <html lang="en">
         <body
           className={`${geistSans.variable} ${geistMono.variable} antialiased`}
@@ -48,6 +48,6 @@ export default function RootLayout({
           {children}
         </body>
       </html>
-    </ClerkProvider>
+    </Providers>
   );
 }
diff --git a/src/app/providers.tsx b/src/app/providers.tsx
new file mode 100644
index 0000000..bcee7d3
--- /dev/null
+++ b/src/app/providers.tsx
@@ -0,0 +1,17 @@
+"use client";
+import { ClerkProvider } from "@clerk/nextjs";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+
+export default function Providers({
+  children,
+}: Readonly<{
+  children: React.ReactNode;
+}>) {
+  const queryClient = new QueryClient();
+
+  return (
+    <ClerkProvider>
+      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
+    </ClerkProvider>
+  );
+}
diff --git a/src/lib/crud.ts b/src/lib/crud.ts
index 285ee1e..3a4ab86 100644
--- a/src/lib/crud.ts
+++ b/src/lib/crud.ts
@@ -12,6 +12,12 @@ interface UpdateHabitProps {
   marks?: Mark[];
 }
 
+interface CreateHabitProps {
+  name: string;
+  marks: Mark[];
+  habitGroupId: number;
+}
+
 export async function getHabitGroupByMonth({
   year,
   month,
@@ -32,6 +38,33 @@ export async function getHabitGroupByMonth({
   return habitGroup;
 }
 
+export async function getOrCreateHabitGroup({
+  year,
+  month,
+}: GetHabitGroupByMonthProps) {
+  const supabase = await createClient();
+
+  let habitGroup;
+  try {
+    habitGroup = await getHabitGroupByMonth({ year, month });
+    return habitGroup;
+  } catch (error) {
+    console.error(error);
+  }
+
+  const { data: newHabitGroup, error } = await supabase
+    .from("habitgroups")
+    .insert({ month, year })
+    .select()
+    .single();
+
+  if (error) {
+    throw new Error(error.message);
+  }
+
+  return newHabitGroup;
+}
+
 export async function updateHabit({ id, marks, name }: UpdateHabitProps) {
   const supabase = await createClient();
   const { data: habit, error } = await supabase
@@ -47,3 +80,22 @@ export async function updateHabit({ id, marks, name }: UpdateHabitProps) {
 
   return habit;
 }
+
+export async function createHabit({
+  habitGroupId,
+  marks,
+  name,
+}: CreateHabitProps) {
+  const supabase = await createClient();
+  const { data: habit, error } = await supabase
+    .from("habits")
+    .insert({ marks, name, habit_group_id: habitGroupId })
+    .select()
+    .single();
+
+  if (error) {
+    throw new Error(error.message);
+  }
+
+  return habit;
+}


Reviews:
user: login: coderabbitai[bot]
body: **Actionable comments posted: 1**

<details>
<summary>🔭 Outside diff range comments (1)</summary><blockquote>

<details>
<summary>src/app/api/habits/route.ts (1)</summary><blockquote>

`1-38`: _⚠️ Potential issue_

**Add guard for a missing or null habit group.**

If `getOrCreateHabitGroup` fails or returns `null`, accessing `habitGroup.id` will cause a runtime error. Consider adding a guard condition to prevent undefined property errors and return an appropriate response to the client.  

```diff
  const habitGroup = await getOrCreateHabitGroup({
    month: body.month,
    year: body.year,
  });

+ if (!habitGroup) {
+   console.error("Habit group was not found or created.");
+   return NextResponse.json({ error: "No habit group found" }, { status: 404 });
+ }

  try {
    const habit = await createHabit({
      habitGroupId: habitGroup.id,
      ...
    });
```

</blockquote></details>

</blockquote></details>

<details>
<summary>🧹 Nitpick comments (5)</summary><blockquote>

<details>
<summary>src/app/providers.tsx (1)</summary><blockquote>

`1-17`: **Consider memoizing the QueryClient instance**

The implementation of the Providers component is clean and follows best practices for combining multiple providers. However, creating a new QueryClient on every render could lead to unnecessary cache resets and re-renders.

Consider memoizing the QueryClient instance or moving it outside the component:

```diff
-  const queryClient = new QueryClient();
+  const queryClient = React.useMemo(() => new QueryClient(), []);
```

Or alternatively:

```diff
+"use client";
+import { ClerkProvider } from "@clerk/nextjs";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+
+// Create a client
+const queryClient = new QueryClient();
+
+export default function Providers({
+  children,
+}: Readonly<{
+  children: React.ReactNode;
+}>) {
+  return (
+    <ClerkProvider>
+      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
+    </ClerkProvider>
+  );
+}
```

</blockquote></details>
<details>
<summary>src/app/habits/components/habits-table.tsx (4)</summary><blockquote>

`24-35`: **Missing explicit error handling in the query function.**

Your use of `useQuery` to fetch habit groups is correct, but consider adding an `onError` callback or try/catch within the query function to handle fetch failures or unexpected HTTP statuses.  

```diff
useQuery<HabitGroup>({
  queryKey: ["habit-groups", month, year],
  queryFn: async () => {
    const params = ...;
    const response = await fetch("/api/habit-groups?" + params);
+   if (!response.ok) {
+     throw new Error(`Could not fetch habit groups: ${response.statusText}`);
+   }
    return await response.json();
  },
});
```

---

`65-69`: **Consider chaining a catch block for error handling.**

Currently, any fetch failure inside `toggleMark` results in no further action. Add a `.catch()` or handle potential failures with an `async/await` try/catch. This ensures the UI accurately reflects whether the operation succeeded.  

```diff
fetch(`/api/habits/${habit.id}`, {
  method: "PATCH",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ marks: newMarks }),
})
.then(() =>
  queryClient.invalidateQueries({ queryKey: ["habit-groups", month, year] })
)
+ .catch((error) => {
+   console.error("Failed to update marks:", error);
+ });
```

---

`109-115`: **Remove or refine console logging in production.**

`console.log({ prev });` might be left over from debugging. Either remove it or replace it with more structured logging if needed.  

```diff
- console.log({ prev });
```

---

`117-139`: **Add error handling for the create habit request.**

Like the mark-updating fetch, adding `.catch()` or using try/catch with `await` would help ensure UI state is correct if the request fails. This also helps debug any network or server-side issues.  

```diff
fetch("/api/habits", {
  method: "POST",
  ...
})
.then(() =>
  queryClient.invalidateQueries({ queryKey: ["habit-groups", month, year] })
)
+ .catch((error) => {
+   console.error("Failed to create a new habit:", error);
+ })
.then(() => {
  setNewHabit(null);
});
```

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

**Configuration used: CodeRabbit UI**
**Review profile: CHILL**
**Plan: Pro**

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between bdde54e67d91de73e8e1bb9f6f7130d48f417d1d and 381efcdc63736cd7404a7cd7562de8d0bb6a810d.

</details>

<details>
<summary>⛔ Files ignored due to path filters (1)</summary>

* `package-lock.json` is excluded by `!**/package-lock.json`

</details>

<details>
<summary>📒 Files selected for processing (7)</summary>

* `package.json` (1 hunks)
* `src/app/api/habits/route.ts` (1 hunks)
* `src/app/habits/components/habits-table.tsx` (5 hunks)
* `src/app/habits/types.ts` (1 hunks)
* `src/app/layout.tsx` (3 hunks)
* `src/app/providers.tsx` (1 hunks)
* `src/lib/crud.ts` (3 hunks)

</details>

<details>
<summary>🧰 Additional context used</summary>

<details>
<summary>🧬 Code Definitions (4)</summary>

<details>
<summary>src/lib/crud.ts (2)</summary><blockquote>

<details>
<summary>src/app/habits/types.ts (1)</summary>

* `Mark` (1-1)

</details>
<details>
<summary>src/lib/supabase/server.ts (1)</summary>

* `createClient` (5-17)

</details>

</blockquote></details>
<details>
<summary>src/app/layout.tsx (1)</summary><blockquote>

<details>
<summary>src/app/providers.tsx (1)</summary>

* `Providers` (5-17)

</details>

</blockquote></details>
<details>
<summary>src/app/api/habits/route.ts (3)</summary><blockquote>

<details>
<summary>src/app/habits/types.ts (1)</summary>

* `Mark` (1-1)

</details>
<details>
<summary>src/lib/supabase/database.types.ts (1)</summary>

* `Tables` (140-163)

</details>
<details>
<summary>src/lib/crud.ts (2)</summary>

* `getOrCreateHabitGroup` (41-66)
* `createHabit` (84-101)

</details>

</blockquote></details>
<details>
<summary>src/app/habits/components/habits-table.tsx (1)</summary><blockquote>

<details>
<summary>src/app/habits/types.ts (2)</summary>

* `Habit` (3-7)
* `HabitGroup` (9-14)

</details>

</blockquote></details>

</details>

</details>

<details>
<summary>🔇 Additional comments (12)</summary><blockquote>

<details>
<summary>package.json (1)</summary>

`15-15`: **Good addition of react-query for data management**

Adding `@tanstack/react-query` is a solid choice for managing server state and data fetching. This library will help with caching, background updates, and optimistic updates when working with habits data.

</details>
<details>
<summary>src/app/habits/types.ts (1)</summary>

`9-14`: **Well-structured HabitGroup type definition**

The new `HabitGroup` type is well-defined with appropriate properties and clear relationships to the existing `Habit` type. This structure will provide good type safety when working with habit groups throughout the application.

</details>
<details>
<summary>src/app/layout.tsx (1)</summary>

`11-11`: **Good refactoring of provider management**

The refactoring to use a centralized `Providers` component is a clean approach. This separates the concerns of provider management from the layout component and makes it easier to add or modify providers in the future.



Also applies to: 34-34, 51-51

</details>
<details>
<summary>src/lib/crud.ts (2)</summary>

`15-19`: **Interface creation looks good.**

The new `CreateHabitProps` interface is consistent with the existing code's requirements for habit creation, ensuring typed arguments for name, marks, and habitGroupId.

---

`83-101`: **Creation logic is clear and follows Supabase best practices.**

The `createHabit` function performs an insert and throws meaningful errors if the operation fails. This straightforward approach is good for debugging and maintainability.

</details>
<details>
<summary>src/app/habits/components/habits-table.tsx (7)</summary>

`2-5`: **New imports look correct.**

Importing `Habit`, `Mark`, `HabitGroup` from the local types and `useQuery`, `useQueryClient` from `@tanstack/react-query` is consistent with the proposed functionality.

---

`21-23`: **State and ref setup is appropriate.**

Storing `newHabit` in state and creating a ref for the habit input field aligns well with React best practices for controlled form elements and focusing.

---

`92-92`: **No issues with pushing null for out-of-range days.**

Populating "null" in the array helps represent days that exceed the month’s boundaries for the current display logic. This is a sensible approach for handling partial weeks.

---

`101-107`: **Initializing a placeholder habit is a clear approach.**

Using an ID of 0 as a placeholder is fine, since the actual ID is assigned by the backend. The default empty marks array ensures the UI can render a new habit immediately.

---

`141-143`: **Smart focus logic.**

Automatically focusing the newly created habit input field is a good UX decision, avoiding extra clicks.

---

`146-146`: **Window size check is correct.**

Setting `isDesktop` to compare `innerWidth` against `768` is straightforward. Ensure you handle any needed resize listeners if the window is resized.

---

`153-230`: **Comprehensive rendering logic with dynamic columns.**

The grid-based approach for days and habits is well-structured. The conditional logic for weekend styling and new/null day placeholders is straightforward. The code is readable and clearly organized.

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-07T21:56:53Z
user: login: arthurdedeus
state: COMMENTED
author_association: OWNER
submitted_at: 2025-04-07T22:05:13Z
user: login: coderabbitai[bot]
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-07T22:05:42Z

Review comments:
diff_hunk: @@ -32,6 +38,33 @@ export async function getHabitGroupByMonth({
   return habitGroup;
 }
 
+export async function getOrCreateHabitGroup({
+  year,
+  month,
+}: GetHabitGroupByMonthProps) {
+  const supabase = await createClient();
+
+  let habitGroup;
+  try {
+    habitGroup = await getHabitGroupByMonth({ year, month });
+    return habitGroup;
+  } catch (error) {
+    console.error(error);
+  }
+
+  const { data: newHabitGroup, error } = await supabase
+    .from("habitgroups")
+    .insert({ month, year })
+    .select()
+    .single();
+
+  if (error) {
+    throw new Error(error.message);
+  }
+
+  return newHabitGroup;
+}
path: src/lib/crud.ts
user: login: coderabbitai[bot]
body: _🛠️ Refactor suggestion_

**Handle non-404 errors differently to avoid accidental duplication.**

This function swallows all errors and proceeds to create a new habit group, which may unintentionally create a duplicate if the error was unrelated to a missing record (e.g., network issues). Consider distinguishing 404 from other errors to handle them more gracefully.  

```diff
  try {
    habitGroup = await getHabitGroupByMonth({ year, month });
    return habitGroup;
  } catch (error) {
    + // If the error is specifically a 404 (record not found), proceed. Otherwise, rethrow.
    + // e.g., if (error.status !== 404) throw error;
    console.error(error);
  }
```


> Committable suggestion skipped: line range outside the PR's diff.

<!-- This is an auto-generated reply by CodeRabbit -->
created_at: 2025-04-07T21:56:52Z
author_association: NONE
diff_hunk: @@ -32,6 +38,33 @@ export async function getHabitGroupByMonth({
   return habitGroup;
 }
 
+export async function getOrCreateHabitGroup({
+  year,
+  month,
+}: GetHabitGroupByMonthProps) {
+  const supabase = await createClient();
+
+  let habitGroup;
+  try {
+    habitGroup = await getHabitGroupByMonth({ year, month });
+    return habitGroup;
+  } catch (error) {
+    console.error(error);
+  }
+
+  const { data: newHabitGroup, error } = await supabase
+    .from("habitgroups")
+    .insert({ month, year })
+    .select()
+    .single();
+
+  if (error) {
+    throw new Error(error.message);
+  }
+
+  return newHabitGroup;
+}
path: src/lib/crud.ts
user: login: arthurdedeus
body: this is ok for now
created_at: 2025-04-07T22:05:13Z
author_association: OWNER
diff_hunk: @@ -32,6 +38,33 @@ export async function getHabitGroupByMonth({
   return habitGroup;
 }
 
+export async function getOrCreateHabitGroup({
+  year,
+  month,
+}: GetHabitGroupByMonthProps) {
+  const supabase = await createClient();
+
+  let habitGroup;
+  try {
+    habitGroup = await getHabitGroupByMonth({ year, month });
+    return habitGroup;
+  } catch (error) {
+    console.error(error);
+  }
+
+  const { data: newHabitGroup, error } = await supabase
+    .from("habitgroups")
+    .insert({ month, year })
+    .select()
+    .single();
+
+  if (error) {
+    throw new Error(error.message);
+  }
+
+  return newHabitGroup;
+}
path: src/lib/crud.ts
user: login: coderabbitai[bot]
body: @arthurdedeus Got it! I'll respect your decision to keep the current implementation as is for now.
---

<details>
<summary>✏️ Learnings added</summary>

```
Learnt from: arthurdedeus
PR: arthurdedeus/r2#27
File: src/lib/crud.ts:41-66
Timestamp: 2025-04-07T22:05:40.680Z
Learning: The user prefers to keep the current error handling approach in the `getOrCreateHabitGroup` function for now, where all errors are caught and logged before attempting to create a new habit group.
```

> Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure [review instructions](https://docs.coderabbit.ai/guides/review-instructions). You can manage existing learnings in the [app](https://app.coderabbit.ai/learnings).

</details>

<!-- This is an auto-generated reply by CodeRabbit -->
created_at: 2025-04-07T22:05:42Z
author_association: NONE

PR comments:
user: login: vercel[bot]
created_at: 2025-04-07T21:47:33Z
body: [vc]: #GCYwoQKGIW2qDRe4ee2IT6XNsanBWsXl7qCu64dcokg=:eyJpc01vbm9yZXBvIjp0cnVlLCJ0eXBlIjoiZ2l0aHViIiwicHJvamVjdHMiOlt7Im5hbWUiOiJyMiIsImxpdmVGZWVkYmFjayI6eyJyZXNvbHZlZCI6MCwidW5yZXNvbHZlZCI6MCwidG90YWwiOjAsImxpbmsiOiJyMi1naXQtZmVhdC1hZGQtbmV3LWhhYml0LWFydGh1cmRlZGV1cy1wcm9qZWN0cy52ZXJjZWwuYXBwIn0sImluc3BlY3RvclVybCI6Imh0dHBzOi8vdmVyY2VsLmNvbS9hcnRodXJkZWRldXMtcHJvamVjdHMvcjIvN2ZKSlNqOUpaNFptdzh5Q2RKMUx2S3dFd0I1VCIsInByZXZpZXdVcmwiOiJyMi1naXQtZmVhdC1hZGQtbmV3LWhhYml0LWFydGh1cmRlZGV1cy1wcm9qZWN0cy52ZXJjZWwuYXBwIiwibmV4dENvbW1pdFN0YXR1cyI6IkRFUExPWUVEIiwicm9vdERpcmVjdG9yeSI6bnVsbH1dfQ==
**The latest updates on your projects**. Learn more about [Vercel for Git ↗︎](https://vercel.link/github-learn-more)

| Name | Status | Preview | Comments | Updated (UTC) |
| :--- | :----- | :------ | :------- | :------ |
| **r2** | ✅ Ready ([Inspect](https://vercel.com/arthurdedeus-projects/r2/7fJJSj9JZ4Zmw8yCdJ1LvKwEwB5T)) | [Visit Preview](https://r2-git-feat-add-new-habit-arthurdedeus-projects.vercel.app) | 💬 [**Add feedback**](https://vercel.live/open-feedback/r2-git-feat-add-new-habit-arthurdedeus-projects.vercel.app?via=pr-comment-feedback-link) | Apr 7, 2025 9:51pm |


author_association: CONTRIBUTOR
user: login: coderabbitai[bot]
created_at: 2025-04-07T21:47:35Z
body: <!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- walkthrough_start -->

## Walkthrough
This pull request introduces several enhancements focused on habit creation and management. A new dependency, `@tanstack/react-query`, is added to support improved data fetching. On the backend, a new API route and supporting CRUD functions handle habit creation and habit group retrieval/creation, while the frontend habits table component is refactored to leverage react-query for state management and query invalidation. Additionally, provider components are updated by replacing the existing `<ClerkProvider>` with a new `<Providers>` component that wraps authentication and query clients.

## Changes

| File(s)                                                     | Change Summary                                                                                                                                                                                                                                             |
|-------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `package.json`                                              | Added dependency `"@tanstack/react-query": "^5.72.0"` in the dependencies section.                                                                                                                                                                      |
| `src/app/api/habits/route.ts`<br>`src/lib/crud.ts`           | Introduced a new API POST route for creating habits, along with new functions `getOrCreateHabitGroup` and `createHabit` and an interface `CreateHabitProps` to manage habit creation and habit group retrieval/creation.                          |
| `src/app/habits/components/...habits-table.tsx`<br>`src/app/habits/types.ts` | Refactored the habits table to use react-query for fetching habit groups, removed local state management, and added functionality for habit creation. Also, a new type `HabitGroup` is introduced for habit group structure.                    |
| `src/app/layout.tsx`<br>`src/app/providers.tsx`              | Replaced `<ClerkProvider>` with the new `<Providers>` component. A new Providers component was added to wrap the app with both `ClerkProvider` and `QueryClientProvider`, updating the authentication and data fetching context.                   |

## Sequence Diagram(s)
```mermaid
sequenceDiagram
  participant User
  participant HabitsTable
  participant API_Route
  participant CRUD
  participant DB

  User->>HabitsTable: Enter new habit in input
  HabitsTable->>API_Route: Send POST request with habit details
  API_Route->>CRUD: Call getOrCreateHabitGroup (with month/year)
  CRUD->>DB: Query for existing habit group
  DB-->>CRUD: Return habit group data or error
  CRUD->>DB: Create habit group if needed
  DB-->>CRUD: Return new habit group data
  API_Route->>CRUD: Call createHabit (with groupId, marks, name)
  CRUD->>DB: Insert new habit record
  DB-->>CRUD: Confirm habit creation
  API_Route-->>HabitsTable: Return created habit (status 200)
  HabitsTable->>HabitsTable: Invalidate query to refresh UI
```

## Assessment against linked issues

| Objective                 | Addressed | Explanation |
|---------------------------|-----------|-------------|
| Add new Habits (#5)       | ✅        |             |

## Poem
> Oh, what a joy to see this code,  
> New habits formed on a bouncing road.  
> APIs and queries hop in line,  
> With providers that beautifully align.  
> I’m a rabbit, thrilled by every tweak—  
> Code so fresh, it makes my whiskers peak!  🐇✨

<!-- walkthrough_end -->
<!-- internal state start -->


<!-- DwQgtGAEAqAWCWBnSTIEMB26CuAXA9mAOYCmGJATmriQCaQDG+Ats2bgFyQAOFk+AIwBWJBrngA3EsgEBPRvlqU0AgfFwA6NPEgQAfACgjoCEYDEZyAAUASpADK2VmgqyjcEj2wAbb5AokAI7Y0riQ4rjedJAARABmJNRcAIK09OQA7pCwKuoxkBloyIjYAszqNPRy4bCe2IiU6BS4sNgUSkr16Bj0qPBKGOJx8NFFkBhOAo0ATADsGjC1PBTwzC7y3G3c+A38cTWom77+QSGIYagEKIMUitgMnmjjJFkJ1G2eAd7U0VdoafAMER0NlcmEMuoEFgWp5ePgRGIFqlaOp4PgMGhfLIADQoC7IDD4So1agHZBHPw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mqAwMDivgDgfiIodPOCdOfjTKgQLlkCQP9ngtPpducuQQ3gWTFC9m4WiRTpGUDgCWIThywQR1SagXnJ+STSXwaBNwCd63JDmShXKRGWQGEiZyNjUjUSSfKVnhKlU0oh7luF5O6AlSGiAf7UUTbt8lWykhWaMkWXFB5EFAcm8gCE8fmm8exl8erVe3wSyLEbBFDspiWwUtNLHVFETxzJCbzjBAkZfLGAtNHonywmWYxTavmn3gEipIr36ePl20fGeLwy2f9y0nKE6jwuqBaHWerwxYzIFjYCZH2eqahYHnqbPveW30voRYBo6hvuajvsjPQTBqfohpfsTNKWEceHxpMl5YzkgABc/iBazu/qVWMJobocGU7MTICY/vAYjOhaxRxQCaCbfleZ/mjgQYwNQLUhQe/kRSKd4RKbKZaGBZCKleofgFocwTla5lfqeeeiVYgbuZde0mMXzQQZsejnsaEXMa9m0eqK4FFfEYleteIOlbtdlYYZ1UhJ+hPDHCA2MhnFrDRP4qbEmNXHXExK3GoV3AHAhmHCPEMBTabHUFLMQDlMrRGDRw1OkgxWLAMBTYAGYAAOAARiOwYFoAYAADY23Zg22B2+3ZgAAWayCdtAWYcdjkAd6YJQDt2gayVQAdtAbt6yegFtitpcKt/oGtutl4OgOU8sMtoAA -->

<!-- internal state end -->
<!-- finishing_touch_checkbox_start -->

<details>
<summary>✨ Finishing Touches</summary>

- [ ] <!-- {"checkboxId": "7962f53c-55bc-4827-bfbf-6a18da830691"} --> 📝 Generate Docstrings

</details>

<!-- finishing_touch_checkbox_end -->
<!-- tips_start -->

---

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

<details>
<summary>❤️ Share</summary>

- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai)
- [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai)
- [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai)
- [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

</details>

<details>
<summary>🪧 Tips</summary>

### Chat

There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=arthurdedeus/r2&utm_content=27):

- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
  - `I pushed a fix in commit <commit_id>, please review it.`
  - `Generate unit testing code for this file.`
  - `Open a follow-up GitHub issue for this discussion.`
- Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples:
  - `@coderabbitai generate unit testing code for this file.`
  -	`@coderabbitai modularize this function.`
- PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
  - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.`
  - `@coderabbitai read src/utils.ts and generate unit testing code.`
  - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.`
  - `@coderabbitai help me debug CodeRabbit configuration file.`

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

### CodeRabbit Commands (Invoked using PR comments)

- `@coderabbitai pause` to pause the reviews on a PR.
- `@coderabbitai resume` to resume the paused reviews.
- `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
- `@coderabbitai full review` to do a full review from scratch and review all the files again.
- `@coderabbitai summary` to regenerate the summary of the PR.
- `@coderabbitai generate docstrings` to [generate docstrings](https://docs.coderabbit.ai/finishing-touches/docstrings) for this PR.
- `@coderabbitai resolve` resolve all the CodeRabbit review comments.
- `@coderabbitai plan` to trigger planning for file edits and PR creation.
- `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository.
- `@coderabbitai help` to get help.

### Other keywords and placeholders

- Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed.
- Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description.
- Add `@coderabbitai` anywhere in the PR title to generate the title automatically.

### CodeRabbit Configuration File (`.coderabbit.yaml`)

- You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository.
- Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json`

### Documentation and Community

- Visit our [Documentation](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit.
- Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback.
- Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.

</details>

<!-- tips_end -->
author_association: NONE