Skip to content

Commit

Permalink
Fix InboxList is on first render (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardguerre committed Mar 17, 2024
1 parent 924cf6f commit 2a18440
Showing 1 changed file with 52 additions and 39 deletions.
91 changes: 52 additions & 39 deletions apps/web/src/components/InboxList.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { graphql, useFragment, useSmartSubscription } from "@flowdev/relay";
import { ItemCard } from "./ItemCard";
import { ReactSortable } from "react-sortablejs";
import { useState } from "react";
import { useEffect, useState } from "react";
import { Button } from "@flowdev/ui/Button";
import { NewItemCard } from "./NewItemCard";
import { InboxListSubscription } from "../relay/__generated__/InboxListSubscription.graphql";
Expand All @@ -10,7 +10,41 @@ import { InboxListItemToBeInList_item$key } from "../relay/__generated__/InboxLi
type InboxListProps = {};

export const InboxList = (props: InboxListProps) => {
const [showNewTaskCard, setShowNewTaskCard] = useState(false);
const [showNewItemCard, setShowNewItemCard] = useState(false);
const [itemsConnectionId, setItemsConnectionId] = useState<string | null>(null);

return (
<div className="bg-background-100 flex h-full flex-col gap-4 pt-3">
<div className="flex flex-col gap-2 px-4">
<div className="text-xl font-semibold">Inbox</div>
<div className="text-foreground-700">
Quickly add items to your inbox so you can triage them later.
</div>
<div className="flex flex-col gap-4">
<Button
secondary
sm
className="bg-background-300/50 text-foreground-900 hover:bg-background-300/70 active:bg-background-300/100 w-full"
disabled={!itemsConnectionId}
onClick={() => setShowNewItemCard(true)}
>
Add item
</Button>
{showNewItemCard && itemsConnectionId && (
<NewItemCard
itemsConnectionId={itemsConnectionId}
onSave={() => setShowNewItemCard(false)}
onCancel={() => setShowNewItemCard(false)}
/>
)}
</div>
</div>
<InboxListItems onItemsConnectionIdChange={setItemsConnectionId} />
</div>
);
};

const InboxListItems = (props: { onItemsConnectionIdChange: (id: string) => void }) => {
const [data] = useSmartSubscription<InboxListSubscription>(graphql`
subscription InboxListSubscription {
items(
Expand Down Expand Up @@ -57,45 +91,24 @@ export const InboxList = (props: InboxListProps) => {
}) ?? [],
);

if (!data) return null; // TODO: loading state
useEffect(() => {
if (data?.items.__id) {
props.onItemsConnectionIdChange(data.items.__id);
}
}, [data?.items.__id]);

return (
<div className="bg-background-100 flex h-full flex-col gap-4 pt-3">
<div className="flex flex-col gap-2 px-4">
<div className="text-xl font-semibold">Inbox</div>
<div className="text-foreground-700">
Quickly add items to your inbox so you can triage them later.
</div>
<div className="flex flex-col gap-4">
<Button
secondary
sm
className="bg-background-300/50 text-foreground-900 hover:bg-background-300/70 active:bg-background-300/100 w-full"
onClick={() => setShowNewTaskCard(true)}
>
Add item
</Button>
{showNewTaskCard && (
<NewItemCard
itemsConnectionId={data.items.__id}
onSave={() => setShowNewTaskCard(false)}
onCancel={() => setShowNewTaskCard(false)}
/>
)}
<ReactSortable
list={items}
setList={() => {}}
group="shared"
className="no-scrollbar flex h-full flex-col overflow-y-scroll px-4"
>
{items.map((item) => (
<div id={item.id} key={item.id} className="pb-4">
<ItemCard key={item.id} item={item} inInbox />
</div>
</div>
<ReactSortable
list={items}
setList={() => {}}
group="shared"
className="no-scrollbar flex h-full flex-col overflow-y-scroll px-4"
>
{items.map((item) => (
<div id={item.id} key={item.id} className="pb-4">
<ItemCard key={item.id} item={item} inInbox />
</div>
))}
</ReactSortable>
</div>
))}
</ReactSortable>
);
};

0 comments on commit 2a18440

Please sign in to comment.