Skip to content

Commit

Permalink
Hide list & cog buttons on empty notes
Browse files Browse the repository at this point in the history
  • Loading branch information
evwilliams committed Jan 20, 2024
1 parent 9363ef4 commit f737712
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
28 changes: 22 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function App() {
focusThought,
clearThought,
rememberThought,
countThoughts
} = useThoughts()

const [activeTab, setActiveTab] = useState<TabKey>('write')
Expand All @@ -26,8 +27,16 @@ function App() {
setActiveTab(tabKey === activeTab ? 'write' : tabKey)
}

const hasMultipleNotes = () => {
if (!sortedThoughts || !activeThought)
return false
return sortedThoughts.length > 1 || activeThought.text.length > 0
}

const emptyActiveThought = () => !activeThought || activeThought.text.length > 0

const plusPressed = async () => {
if (!activeThought || activeThought.text.length > 0)
if (emptyActiveThought())
await createThought()
setActiveTab('write')
}
Expand All @@ -37,6 +46,13 @@ function App() {
setActiveTab('write')
}

const deletePressed = async (t: Thought) => {
await clearThought(t)
const numThoughts = await countThoughts()
if (numThoughts < 1)
setActiveTab('write')
}

const rememberPressed = async (thoughtId: number, when: RememberWhen) => {
await rememberThought(thoughtId, when)
}
Expand All @@ -49,7 +65,7 @@ function App() {
className="w-full"
thoughts={sortedThoughts}
selectionHandler={selectThought}
onDeleteClicked={clearThought}
onDeleteClicked={deletePressed}
/>
),
write: (
Expand All @@ -71,18 +87,18 @@ function App() {
</div>

<div className="Buttons flex flex-row items-center justify-center gap-16 py-4 lg:pb-12">
<ListIcon
{hasMultipleNotes() && <ListIcon
className="h-6 w-6 text-neutral-400"
onClick={() => tabPressed('list')}
/>
/>}
<PlusIcon
className="h-12 w-12 text-neutral-800"
onClick={plusPressed}
/>
<CogIcon
{hasMultipleNotes() && <CogIcon
className="h-6 w-6 text-neutral-400"
onClick={() => tabPressed('settings')}
/>
/>}
</div>
</div>
)
Expand Down
9 changes: 7 additions & 2 deletions src/data/ThoughtStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ export const useThoughts = () => {
})
}

const clearThought = (t: Thought) => {
const clearThought = async (t: Thought) => {
if (activeThought && activeThought.id === t.id) {
setActiveThought(undefined)
}
t.id && db.thoughts.delete(t.id)
return t.id && db.thoughts.delete(t.id)
}

const countThoughts = () => {
return db.thoughts.count()
}

return {
Expand All @@ -89,5 +93,6 @@ export const useThoughts = () => {
focusThought,
clearThought,
rememberThought,
countThoughts,
}
}

0 comments on commit f737712

Please sign in to comment.