Skip to content

📧✨ Beautiful email. For everyone.

feat: move unsubscribe into the reader toolbar with a loading state (#11)

Paul Koecka920aa39fb98
diff --git a/apps/desktop/src/renderer/src/App.tsx b/apps/desktop/src/renderer/src/App.tsx
index 8fae4a6..20775ec 100644
--- a/apps/desktop/src/renderer/src/App.tsx
+++ b/apps/desktop/src/renderer/src/App.tsx
@@ -25,6 +25,7 @@ import {
AlertCircle,
Star,
MailOpen,
+ MailMinus,
FolderInput,
ShieldX,
Reply,
@@ -73,7 +74,7 @@ import {
suspendPersistence,
resumePersistence,
} from './cache'
-import { Reader } from './Reader'
+import { Reader, type UnsubscribeState } from './Reader'
import { MoveDialog } from './MoveDialog'
import { CommandPalette, type Command } from './CommandPalette'
import { Sidebar, views } from './Sidebar'
@@ -163,6 +164,7 @@ export function App() {
const selectionAnchor = useRef<string | undefined>(undefined)
const [draftSelected, setDraftSelected] = useState(new Set<string>()),
draftAnchor = useRef<string | undefined>(undefined)
+ const [unsubscribes, setUnsubscribes] = useState(new Map<string, 'pending' | 'done'>())
// Keep successful deletions hidden for this session; delayed server results can still be stale.
const [discarding, setDiscarding] = useState(new Set<string>()),
pendingDiscard = useRef<{ toastId: number; items: DraftItem[] } | undefined>(undefined)
@@ -861,6 +863,38 @@ export function App() {
requestAnimationFrame(() => searchInput.current?.select())
}
const latest = thread.data?.at(-1)
+ // The newest mailing-list message carries the sender's current unsubscribe address.
+ const listMessage = thread.data && [...thread.data].reverse().find((m) => m.unsubscribe)
+ const listKey = listMessage && scopeKey(listMessage.accountId, listMessage.id)
+ const unsubscribeState: UnsubscribeState | undefined = listKey
+ ? unsubscribes.get(listKey) || 'available'
+ : undefined
+ const unsubscribe = async () => {
+ if (!listMessage || !listKey || unsubscribes.has(listKey)) return
+ const mark = (state?: 'pending' | 'done') =>
+ setUnsubscribes((current) => {
+ const next = new Map(current)
+ if (state) next.set(listKey, state)
+ else next.delete(listKey)
+ return next
+ })
+ const sender = listMessage.from[0]?.name || listMessage.from[0]?.email || 'this mailing list'
+ mark('pending')
+ try {
+ const result = await api.unsubscribe(listMessage.accountId, listMessage.id)
+ if (result.kind === 'done') {
+ mark('done')
+ notify('Unsubscribed from ' + sender)
+ return
+ }
+ mark()
+ if (result.kind === 'mailto' && result.url) onMailto(result.url)
+ else notify('Finish unsubscribing in your browser', 'info')
+ } catch (error) {
+ mark()
+ fail(error)
+ }
+ }
const toggleStar = () =>
void act(
(
@@ -982,6 +1016,10 @@ export function App() {
},
{ ...on, enabled: !blocked && !!route.thread },
)
+ useHotkey('Mod+U', () => void unsubscribe(), {
+ ...on,
+ enabled: !blocked && unsubscribeState === 'available',
+ })
useHotkeySequence(['G', 'I'], () => go({ view: 'inbox' }), on)
useHotkeySequence(['G', 'S'], () => go({ view: 'starred' }), on)
useHotkeySequence(['G', 'T'], () => go({ view: 'sent' }), on)
@@ -1105,6 +1143,21 @@ export function App() {
},
]
: []),
+ ...(unsubscribeState && unsubscribeState !== 'done'
+ ? [
+ {
+ id: 'unsubscribe',
+ label: 'Unsubscribe from mailing list',
+ icon: MailMinus,
+ key: 'Ctrl U',
+ keywords: 'newsletter opt out stop',
+ run: () => void unsubscribe(),
+ ...(unsubscribeState === 'pending'
+ ? { disabled: true, description: 'Unsubscribing…' }
+ : {}),
+ },
+ ]
+ : []),
].map((c) => ({
...c,
group: route.thread ? 'Conversation' : selected.size > 1 ? 'Selection' : 'Conversation',
@@ -1388,6 +1441,8 @@ export function App() {
onReply={(message, kind) => void compose(message, kind)}
notify={notify}
onMailto={onMailto}
+ unsubscribe={unsubscribeState}
+ onUnsubscribe={() => void unsubscribe()}
/>
) : (
<EmptyState
diff --git a/apps/desktop/src/renderer/src/AppDialogs.tsx b/apps/desktop/src/renderer/src/AppDialogs.tsx
index 8055fc4..5c65c43 100644
--- a/apps/desktop/src/renderer/src/AppDialogs.tsx
+++ b/apps/desktop/src/renderer/src/AppDialogs.tsx
@@ -32,6 +32,7 @@ const shortcutSections: [string, [string, string][]][] = [
['Delete selected drafts', '#'],
['Mark as spam', '!'],
['Move to folder', 'V'],
+ ['Unsubscribe from mailing list', 'Ctrl U'],
['Star or unstar', 'S'],
['Mark unread / read', 'U ⇧I'],
['Undo last action', 'Z'],
diff --git a/apps/desktop/src/renderer/src/Reader.tsx b/apps/desktop/src/renderer/src/Reader.tsx
index 4c3935c..1e7f91b 100644
--- a/apps/desktop/src/renderer/src/Reader.tsx
+++ b/apps/desktop/src/renderer/src/Reader.tsx
@@ -12,7 +12,6 @@ import {
ReplyAll,
Forward,
Download,
- ExternalLink,
ImageOff,
ChevronRight,
ShieldX,
@@ -26,8 +25,11 @@ import {
Inbox,
Paperclip,
ShieldCheck,
+ Check,
+ MailMinus,
} from '@inlark/ui/icons'
-import { Button, IconButton, Dropdown, MenuItem } from '@inlark/ui'
+import { Tooltip } from '@base-ui/react/tooltip'
+import { Button, IconButton, Dropdown, MenuItem, Spinner } from '@inlark/ui'
import {
friendlyError,
type Account,
@@ -509,9 +511,57 @@ interface ReaderProps {
onReply: (message: Message, kind: 'reply' | 'replyAll' | 'forward') => void
notify: (message: string) => void
onMailto: (url: string) => void
+ /** Set when the conversation comes from a mailing list that can be left. */
+ unsubscribe?: UnsubscribeState
+ onUnsubscribe: () => void
/** Messages are list summaries while the full conversation loads. */
loading?: boolean
}
+export type UnsubscribeState = 'available' | 'pending' | 'done'
+const unsubscribeLabels = {
+ available: 'Unsubscribe',
+ pending: 'Unsubscribing…',
+ done: 'Unsubscribed',
+} satisfies Record<UnsubscribeState, string>
+function UnsubscribeButton({ state, onClick }: { state: UnsubscribeState; onClick: () => void }) {
+ return (
+ <Tooltip.Root>
+ <Tooltip.Trigger
+ render={
+ <Button
+ variant="ghost"
+ className={'unsubscribe-button unsubscribe-' + state}
+ aria-label={
+ state === 'available'
+ ? 'Unsubscribe from this mailing list'
+ : unsubscribeLabels[state]
+ }
+ aria-busy={state === 'pending'}
+ disabled={state !== 'available'}
+ onClick={onClick}
+ />
+ }
+ >
+ {state === 'pending' ? (
+ <Spinner size={15} />
+ ) : state === 'done' ? (
+ <Check size={15} />
+ ) : (
+ <MailMinus size={15} />
+ )}
+ <span>{unsubscribeLabels[state]}</span>
+ </Tooltip.Trigger>
+ <Tooltip.Portal>
+ <Tooltip.Positioner sideOffset={7}>
+ <Tooltip.Popup className="tooltip">
+ Unsubscribe from this mailing list
+ <kbd>Ctrl U</kbd>
+ </Tooltip.Popup>
+ </Tooltip.Positioner>
+ </Tooltip.Portal>
+ </Tooltip.Root>
+ )
+}
const attachmentIcon = (type: string, name: string) =>
type.startsWith('image/')
? FileImage
@@ -541,6 +591,8 @@ export function Reader({
onReply,
notify,
onMailto,
+ unsubscribe,
+ onUnsubscribe,
loading = false,
}: ReaderProps) {
const last = messages[messages.length - 1]
@@ -663,6 +715,12 @@ export function Reader({
</MenuItem>
{moveLimit && <p className="menu-note">{moveLimit}</p>}
</Dropdown>
+ {unsubscribe && (
+ <>
+ <span className="toolbar-divider" />
+ <UnsubscribeButton state={unsubscribe} onClick={onUnsubscribe} />
+ </>
+ )}
<div className="reader-navigation">
<span role="status">{busy ? 'Updating…' : position}</span>
<IconButton
@@ -787,27 +845,6 @@ export function Reader({
)}
{open && (
<div className="message-content">
- {message.unsubscribe && (
- <button
- className="unsubscribe-link"
- onClick={async () => {
- try {
- const result = await api.unsubscribe(account.id, message.id)
- if (result.kind === 'mailto' && result.url) onMailto(result.url)
- else
- notify(
- result.kind === 'done'
- ? 'Unsubscribe request sent.'
- : 'Unsubscribe page opened.',
- )
- } catch (error) {
- notify(friendlyError(error))
- }
- }}
- >
- Unsubscribe from this mailing list <ExternalLink size={11} />
- </button>
- )}
<EmailBody
message={message}
remoteImages={settings.remoteImages}
diff --git a/apps/desktop/src/renderer/src/demo.ts b/apps/desktop/src/renderer/src/demo.ts
index c363a9e..9ab8287 100644
--- a/apps/desktop/src/renderer/src/demo.ts
+++ b/apps/desktop/src/renderer/src/demo.ts
@@ -296,7 +296,7 @@ function message(accountIndex: number, index: number, bodies = true): Message {
]
: [],
...(type === 'newsletter'
- ? { unsubscribe: { url: 'https://example.com/unsubscribe', oneClick: false } }
+ ? { unsubscribe: { url: 'https://example.com/unsubscribe', oneClick: true } }
: {}),
}
}
@@ -982,7 +982,10 @@ export const demoAPI: DesktopMailAPI = {
return null
}
},
- unsubscribe: async () => ({ kind: 'done' }),
+ unsubscribe: async () => {
+ await pause(1200)
+ return { kind: 'done' }
+ },
openExternal: async (url) => {
if (/^https?:/.test(url)) window.open(url, '_blank', 'noopener,noreferrer')
},
diff --git a/apps/desktop/src/renderer/src/styles.css b/apps/desktop/src/renderer/src/styles.css
index e1d6c8f..9a42903 100644
--- a/apps/desktop/src/renderer/src/styles.css
+++ b/apps/desktop/src/renderer/src/styles.css
@@ -1023,15 +1023,24 @@ svg.starred {
.collapsed-message .message-heading {
opacity: 0.7;
}
-.unsubscribe-link {
- display: flex;
+/* Leaving a mailing list is a conversation action, so it sits with the others in the toolbar. */
+.unsubscribe-button {
+ min-height: 29px;
+ height: 29px;
+ padding: 0 10px 0 8px;
gap: 6px;
- align-items: center;
- background: none;
- border: 0;
- font-size: 11px;
+}
+.unsubscribe-button svg {
+ flex-shrink: 0;
+}
+.unsubscribe-button:disabled {
+ opacity: 1;
+}
+.unsubscribe-pending {
color: var(--muted);
- padding: 0 0 16px;
+}
+.unsubscribe-done {
+ color: var(--green);
}
.attachment-list {
display: flex;
@@ -2710,6 +2719,13 @@ fieldset.compose-format:disabled {
.selection-bar .toolbar-divider {
margin: 0 2px;
}
+ .unsubscribe-button {
+ width: 29px;
+ padding: 5px;
+ }
+ .unsubscribe-button span {
+ display: none;
+ }
}
@media (max-width: 700px) {
.app-shell:not(.sidebar-collapsed) {
diff --git a/packages/ui/src/icons.tsx b/packages/ui/src/icons.tsx
index 30eaac7..30f42fa 100644
--- a/packages/ui/src/icons.tsx
+++ b/packages/ui/src/icons.tsx
@@ -42,6 +42,7 @@ import {
LoaderCircle as LoaderCircleGlyph,
LockKeyhole as LockKeyholeGlyph,
Mail as MailGlyph,
+ MailMinus01Icon as MailMinusGlyph,
MailOpen as MailOpenGlyph,
Minus as MinusGlyph,
Monitor as MonitorGlyph,
@@ -125,6 +126,7 @@ export const List = icon(ListGlyph)
export const LoaderCircle = icon(LoaderCircleGlyph)
export const LockKeyhole = icon(LockKeyholeGlyph)
export const Mail = icon(MailGlyph)
+export const MailMinus = icon(MailMinusGlyph)
export const MailOpen = icon(MailOpenGlyph)
export const Minus = icon(MinusGlyph)
export const Monitor = icon(MonitorGlyph)