Skip to content
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

add switching theme command #132

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/components/TheCommandPalette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import useLocation from "wouter-preact/use-location"
import { Signal } from "@preact/signals"
import { v4 as uuidv4 } from 'uuid'

import { showBoardDialog, showCommandPalette } from "../main"
import { appTheme, showBoardDialog, showCommandPalette } from "../main"
import { State } from "../types/state"
import { Board } from "../types/board"
import { RepositoryLocalStorage } from "../repositories/repository"
import { ApplicationService } from "../applications/applicationService"
import { applyTheme, setTheme } from "../utils"
import { Theme } from "../types/theme"

const repository = new RepositoryLocalStorage()
const service = new ApplicationService(repository)
Expand Down Expand Up @@ -98,24 +100,39 @@ export default function TheCommandPalette({ appState }: { appState: Signal<State
})
}

const buildCommands = (boards: Board[]): Command[] => {
const commandThemeSwitch = (theme: Theme): Command => {
const themeNext = (theme === 'light') ? 'dark' : 'light'
return {
id: uuidv4(),
label: `Switch to ${themeNext} theme`,
action: () => {
appTheme.value = themeNext
applyTheme(appTheme.value)
setTheme(appTheme.value)
showCommandPalette.value = false
}
}
}

const buildCommands = (boards: Board[], theme: Theme): Command[] => {
return [
...commandsDefault,
...buildCommandsForBoards(boards),
...boards.flatMap(b => buildCommandsForCards(b)),
commandBoardCreate,
...buildCommandsBoardDelete(boards)
...buildCommandsBoardDelete(boards),
commandThemeSwitch(theme)
]
}

const [commandsFiltered, setCommandsFiltered] = useState<Command[]>(
buildCommands(appState.value.boards)
buildCommands(appState.value.boards, appTheme.value)
)

useEffect(() => {
if (ref.current && showCommandPalette.value) {
ref.current?.focus()
setCommandsFiltered(buildCommands(appState.value.boards))
setCommandsFiltered(buildCommands(appState.value.boards, appTheme.value))
}
}, [showCommandPalette.value, setCommandsFiltered])

Expand All @@ -127,10 +144,10 @@ export default function TheCommandPalette({ appState }: { appState: Signal<State
setIndex(0)
const query = e.currentTarget.value
if (query === '') {
setCommandsFiltered(buildCommands(appState.value.boards))
setCommandsFiltered(buildCommands(appState.value.boards, appTheme.value))
} else {
setCommandsFiltered(
buildCommands(appState.value.boards).filter(c => c.label.toLowerCase().includes(query.toLowerCase()))
buildCommands(appState.value.boards, appTheme.value).filter(c => c.label.toLowerCase().includes(query.toLowerCase()))
)
}
}
Expand Down
Loading