-
Notifications
You must be signed in to change notification settings - Fork 517
feat: unread badge for automations #2656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
janburzinski
wants to merge
4
commits into
main
Choose a base branch
from
emdash/automation-unread-badge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+241
−18
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
977ee5a
feat(automations): add unread run badge
janburzinski 3b3b354
fix(automations): baseline unread notifications
janburzinski b849c3d
fix(automations): refresh unread badge
janburzinski fd57d65
fix(automations): refine unread run badges
janburzinski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
apps/emdash-desktop/src/renderer/features/automations/use-automation-unread-count.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { useQuery, useQueryClient } from '@tanstack/react-query'; | ||
| import { useCallback, useEffect } from 'react'; | ||
| import { useAppSettingsKey } from '@renderer/features/settings/use-app-settings-key'; | ||
| import { events, rpc } from '@renderer/lib/ipc'; | ||
| import { isTerminalAutomationRunStatus } from '@shared/core/automations/automation-run'; | ||
| import { automationRunChangedChannel } from '@shared/core/automations/automationEvents'; | ||
|
|
||
| export const automationUnreadCountQueryKey = (lastReadAt: number) => | ||
| ['automations', 'unread-count', lastReadAt] as const; | ||
|
|
||
| export function useAutomationUnreadCount() { | ||
| const { value: interfaceSettings, updateAsync } = useAppSettingsKey('interface'); | ||
| const lastReadAt = interfaceSettings?.automationsLastReadAt ?? 0; | ||
| const queryClient = useQueryClient(); | ||
|
|
||
| useEffect(() => { | ||
| if (lastReadAt !== 0 || interfaceSettings === undefined) return; | ||
| void rpc.automations.getNotificationsBaselineTimestamp().then((baseline) => { | ||
| void updateAsync({ automationsLastReadAt: baseline }); | ||
| }); | ||
| }, [interfaceSettings, lastReadAt, updateAsync]); | ||
|
|
||
| const query = useQuery({ | ||
| queryKey: automationUnreadCountQueryKey(lastReadAt), | ||
| queryFn: () => rpc.automations.countUnreadFinishedRuns(lastReadAt), | ||
| enabled: lastReadAt > 0, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| return events.on(automationRunChangedChannel, ({ run }) => { | ||
| if (!isTerminalAutomationRunStatus(run.status)) return; | ||
| void queryClient.invalidateQueries({ queryKey: ['automations', 'unread-count'] }); | ||
| }); | ||
| }, [queryClient]); | ||
|
janburzinski marked this conversation as resolved.
|
||
|
|
||
| return query.data ?? 0; | ||
| } | ||
|
|
||
| export function useMarkAutomationsRead() { | ||
| const { updateAsync } = useAppSettingsKey('interface'); | ||
| const queryClient = useQueryClient(); | ||
|
|
||
| return useCallback(async () => { | ||
| await updateAsync({ automationsLastReadAt: Date.now() }); | ||
| void queryClient.invalidateQueries({ queryKey: ['automations', 'unread-count'] }); | ||
| }, [updateAsync, queryClient]); | ||
| } | ||
94 changes: 94 additions & 0 deletions
94
apps/emdash-desktop/src/renderer/features/sidebar/automations-sidebar-item.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { CheckCheck, Clock } from 'lucide-react'; | ||
| import { observer } from 'mobx-react-lite'; | ||
| import { useEffect, useState } from 'react'; | ||
| import { | ||
| useAutomationUnreadCount, | ||
| useMarkAutomationsRead, | ||
| } from '@renderer/features/automations/use-automation-unread-count'; | ||
| import { toast } from '@renderer/lib/hooks/use-toast'; | ||
| import { | ||
| isCurrentView, | ||
| useNavigate, | ||
| useWorkspaceSlots, | ||
| } from '@renderer/lib/layout/navigation-provider'; | ||
| import { | ||
| ContextMenu, | ||
| ContextMenuContent, | ||
| ContextMenuItem, | ||
| ContextMenuTrigger, | ||
| } from '@renderer/lib/ui/context-menu'; | ||
| import { cn } from '@renderer/utils/utils'; | ||
| import { SidebarMenuAction, SidebarMenuRow } from './sidebar-primitives'; | ||
|
|
||
| function formatUnreadCount(count: number): string { | ||
| if (count > 99) return '99+'; | ||
| return String(count); | ||
| } | ||
|
|
||
| export const AutomationsSidebarItem = observer(function AutomationsSidebarItem() { | ||
| const { navigate } = useNavigate(); | ||
| const { currentView } = useWorkspaceSlots(); | ||
| const unreadCount = useAutomationUnreadCount(); | ||
| const markAsRead = useMarkAutomationsRead(); | ||
| const isActive = isCurrentView(currentView, 'automations'); | ||
| const [menuOpen, setMenuOpen] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| if (unreadCount === 0) setMenuOpen(false); | ||
| }, [unreadCount]); | ||
|
|
||
| async function handleMarkAsRead() { | ||
| try { | ||
| await markAsRead(); | ||
| setMenuOpen(false); | ||
| } catch { | ||
| toast({ | ||
| title: 'Could not mark as read', | ||
| description: 'Your read state could not be saved. Please try again.', | ||
| variant: 'destructive', | ||
| }); | ||
| } | ||
| } | ||
|
janburzinski marked this conversation as resolved.
|
||
|
|
||
| return ( | ||
| <ContextMenu | ||
| open={menuOpen} | ||
| onOpenChange={(open) => { | ||
| if (open && unreadCount === 0) return; | ||
| setMenuOpen(open); | ||
| }} | ||
| > | ||
| <ContextMenuTrigger className="w-full"> | ||
| <SidebarMenuRow | ||
| isActive={isActive} | ||
| aria-label="Automations" | ||
| className="w-full justify-between" | ||
| onMouseDown={(e) => e.preventDefault()} | ||
| onClick={() => navigate('automations')} | ||
| > | ||
| <SidebarMenuAction aria-label="Automations" className="gap-2"> | ||
| <Clock className="h-5 w-5 shrink-0 sm:h-4 sm:w-4" /> | ||
| <span className="truncate">Automations</span> | ||
| </SidebarMenuAction> | ||
| {unreadCount > 0 ? ( | ||
| <span | ||
| aria-label={`${unreadCount} unread automation run${unreadCount === 1 ? '' : 's'}`} | ||
| className={cn( | ||
| 'ml-2 inline-flex h-5 min-w-5 shrink-0 items-center justify-center rounded-full', | ||
| 'bg-background-tertiary-2 px-1.5 text-[10px] font-medium tabular-nums text-foreground-tertiary' | ||
| )} | ||
| > | ||
| {formatUnreadCount(unreadCount)} | ||
| </span> | ||
| ) : null} | ||
| </SidebarMenuRow> | ||
| </ContextMenuTrigger> | ||
| <ContextMenuContent side="bottom" align="start"> | ||
| <ContextMenuItem onClick={() => void handleMarkAsRead()}> | ||
| <CheckCheck className="size-4" /> | ||
| Mark all as read | ||
| </ContextMenuItem> | ||
| </ContextMenuContent> | ||
| </ContextMenu> | ||
| ); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.