1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
"use client"
import { Command } from "cmdk"
import { useEffect, useRef, useState } from "react"
import { useRouter } from "next/navigation"
import { useUserInterfaceStore } from "@/lib/stores/user-interface-store"
import { useSubscriptions } from "@/lib/queries/use-subscriptions"
export function CommandPalette() {
const isOpen = useUserInterfaceStore((state) => state.isCommandPaletteOpen)
const setOpen = useUserInterfaceStore((state) => state.setCommandPaletteOpen)
const toggleSidebar = useUserInterfaceStore((state) => state.toggleSidebar)
const setEntryListViewMode = useUserInterfaceStore(
(state) => state.setEntryListViewMode
)
const setAddFeedDialogOpen = useUserInterfaceStore(
(state) => state.setAddFeedDialogOpen
)
const router = useRouter()
const { data: subscriptionsData } = useSubscriptions()
const listReference = useRef<HTMLDivElement>(null)
const [inputValue, setInputValue] = useState("")
useEffect(() => {
function handleKeyDown(event: KeyboardEvent) {
if (event.key === "k" && (event.metaKey || event.ctrlKey)) {
event.preventDefault()
setOpen(!isOpen)
}
}
document.addEventListener("keydown", handleKeyDown)
return () => document.removeEventListener("keydown", handleKeyDown)
}, [isOpen, setOpen])
useEffect(() => {
if (!isOpen) return
function handleKeyDown(event: KeyboardEvent) {
if (event.key === "Escape") {
setOpen(false)
return
}
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
setTimeout(() => {
const list = listReference.current
if (!list) return
const selected = list.querySelector('[aria-selected="true"]') as HTMLElement
if (!selected) return
const listRect = list.getBoundingClientRect()
const selectedRect = selected.getBoundingClientRect()
if (selectedRect.bottom > listRect.bottom) {
list.scrollTop += selectedRect.bottom - listRect.bottom
} else if (selectedRect.top < listRect.top) {
list.scrollTop -= listRect.top - selectedRect.top
}
}, 0)
}
}
document.addEventListener("keydown", handleKeyDown)
return () => document.removeEventListener("keydown", handleKeyDown)
}, [isOpen, setOpen])
if (!isOpen) return null
function handleInputKeyDown(event: React.KeyboardEvent) {
if (event.key === "Backspace" && inputValue === "") {
event.preventDefault()
setOpen(false)
}
}
function navigateAndClose(path: string) {
router.push(path)
setOpen(false)
}
function actionAndClose(action: () => void) {
action()
setOpen(false)
}
return (
<div className="fixed inset-0 z-50 flex items-start justify-center pt-[20vh]">
<div
className="fixed inset-0 bg-background-primary/80"
onClick={() => setOpen(false)}
/>
<Command className="relative w-full max-w-lg border border-border bg-background-secondary">
<Command.Input
placeholder="type a command..."
className="w-full border-b border-border bg-transparent px-4 py-3 text-text-primary outline-none placeholder:text-text-dim"
autoFocus
value={inputValue}
onValueChange={setInputValue}
onKeyDown={handleInputKeyDown}
/>
<Command.List ref={listReference} className="max-h-80 overflow-auto p-2">
<Command.Empty className="p-4 text-center text-text-dim">
no results found
</Command.Empty>
<Command.Group
heading="navigation"
className="mb-2 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1 [&_[cmdk-group-heading]]:text-text-dim"
>
<Command.Item
onSelect={() => navigateAndClose("/reader")}
className="cursor-pointer px-2 py-1 text-text-secondary aria-selected:bg-background-tertiary aria-selected:text-text-primary"
>
go to all entries
</Command.Item>
<Command.Item
onSelect={() => navigateAndClose("/reader/saved")}
className="cursor-pointer px-2 py-1 text-text-secondary aria-selected:bg-background-tertiary aria-selected:text-text-primary"
>
go to saved
</Command.Item>
<Command.Item
onSelect={() => navigateAndClose("/reader/settings")}
className="cursor-pointer px-2 py-1 text-text-secondary aria-selected:bg-background-tertiary aria-selected:text-text-primary"
>
go to settings
</Command.Item>
</Command.Group>
{subscriptionsData &&
subscriptionsData.subscriptions.length > 0 && (
<Command.Group
heading="feeds"
className="mb-2 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1 [&_[cmdk-group-heading]]:text-text-dim"
>
{subscriptionsData.subscriptions.map((subscription) => (
<Command.Item
key={subscription.subscriptionIdentifier}
value={`feed-${subscription.subscriptionIdentifier}-${subscription.customTitle ?? subscription.feedTitle}`}
onSelect={() =>
navigateAndClose(
`/reader?feed=${subscription.feedIdentifier}`
)
}
className="cursor-pointer px-2 py-1 text-text-secondary aria-selected:bg-background-tertiary aria-selected:text-text-primary"
>
{subscription.customTitle ?? subscription.feedTitle}
</Command.Item>
))}
</Command.Group>
)}
<Command.Group
heading="actions"
className="mb-2 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1 [&_[cmdk-group-heading]]:text-text-dim"
>
<Command.Item
onSelect={() =>
actionAndClose(() => setAddFeedDialogOpen(true))
}
className="cursor-pointer px-2 py-1 text-text-secondary aria-selected:bg-background-tertiary aria-selected:text-text-primary"
>
add feed
</Command.Item>
<Command.Item
onSelect={() => actionAndClose(toggleSidebar)}
className="cursor-pointer px-2 py-1 text-text-secondary aria-selected:bg-background-tertiary aria-selected:text-text-primary"
>
toggle sidebar
</Command.Item>
<Command.Item
onSelect={() =>
actionAndClose(() => setEntryListViewMode("compact"))
}
className="cursor-pointer px-2 py-1 text-text-secondary aria-selected:bg-background-tertiary aria-selected:text-text-primary"
>
compact view
</Command.Item>
<Command.Item
onSelect={() =>
actionAndClose(() => setEntryListViewMode("comfortable"))
}
className="cursor-pointer px-2 py-1 text-text-secondary aria-selected:bg-background-tertiary aria-selected:text-text-primary"
>
comfortable view
</Command.Item>
<Command.Item
onSelect={() =>
actionAndClose(() => setEntryListViewMode("expanded"))
}
className="cursor-pointer px-2 py-1 text-text-secondary aria-selected:bg-background-tertiary aria-selected:text-text-primary"
>
expanded view
</Command.Item>
<Command.Item
onSelect={() =>
actionAndClose(() => {
const store = useUserInterfaceStore.getState()
store.resetSidebarLayout?.()
store.resetDetailLayout?.()
})
}
className="cursor-pointer px-2 py-1 text-text-secondary aria-selected:bg-background-tertiary aria-selected:text-text-primary"
>
reset panel sizes
</Command.Item>
</Command.Group>
</Command.List>
</Command>
</div>
)
}
|