Skip to content

Commit

Permalink
feat: prompt menu improvements (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelwt authored Mar 14, 2024
1 parent 940aa69 commit ec99d5e
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/api/v1/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ templates.get("/", async (ctx: Context) => {
left join template_version tv on tv.template_id = t.id
where t.project_id = ${ctx.state.projectId}
group by t.id, t.name, t.slug, t.mode, t.created_at, t.group, t.project_id
order by max(tv.created_at) desc
order by t.created_at desc
`

// uncamel each template's versions' extras' keys
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/components/Blocks/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {

import { useLocalStorage } from "@mantine/hooks"
import { useVirtual } from "@tanstack/react-virtual"
import { useFixedColorScheme } from "@/utils/colors"
import { useFixedColorScheme } from "@/utils/hooks"

// outside for reference
const emptyArray = []
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/components/Blocks/Feedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@tabler/icons-react"
import { useEffect } from "react"
import analytics from "../../utils/analytics"
import { useFixedColorScheme } from "@/utils/colors"
import { useFixedColorScheme } from "@/utils/hooks"

export default function Feedback({ data = {} }: { data: Record<string, any> }) {
const scheme = useFixedColorScheme()
Expand Down
15 changes: 12 additions & 3 deletions packages/frontend/components/Blocks/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { ActionIcon, Input, Kbd, Text } from "@mantine/core"
import { useFocusWithin, useHotkeys } from "@mantine/hooks"
import { Input } from "@mantine/core"
import { useFocusWithin } from "@mantine/hooks"
import { IconSearch, IconX } from "@tabler/icons-react"
import HotkeysInfo from "./HotkeysInfo"

import { useGlobalShortcut } from "@/utils/hooks"

export default function SearchBar({ query, setQuery, ...props }) {
const { ref, focused } = useFocusWithin()

useHotkeys([["mod+K", () => ref.current.focus()]])
useGlobalShortcut([
[
"mod+K",
() => {
if (ref.current?.focus) ref.current.focus()
},
],
])

const showCross = query && query.length > 0

Expand Down
26 changes: 25 additions & 1 deletion packages/frontend/components/Prompts/TemplateMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { notifications } from "@mantine/notifications"

import { cleanSlug, formatCompactFromNow } from "@/utils/format"
import Router from "next/router"
import { useEffect, useState } from "react"
import SearchBar from "../Blocks/SearchBar"

export const defaultTemplateVersion = {
content: [
Expand Down Expand Up @@ -256,6 +258,17 @@ const TemplateList = ({
}) => {
const { templates, loading, isInserting } = useTemplates()

const [filter, setFilter] = useState("")
const [filteredTemplates, setFilteredTemplates] = useState(templates)

useEffect(() => {
if (templates) {
setFilteredTemplates(
templates.filter((t) => t.slug.includes(filter.toLowerCase())),
)
}
}, [filter, templates])

if (loading) return <Loader />

return (
Expand All @@ -278,7 +291,18 @@ const TemplateList = ({
</ActionIcon>
}
/>
{templates?.map((template, index) => (

<SearchBar
placeholder="Filter..."
query={filter}
size="xs"
w="fit-content"
mx="md"
my="xs"
setQuery={setFilter}
/>

{filteredTemplates?.map((template, index) => (
<TemplateListItem
key={index}
template={template}
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/components/SmartViewer/Message.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getColorForRole, useFixedColorScheme } from "@/utils/colors"
import { getColorForRole } from "@/utils/colors"
import {
Box,
Code,
Expand Down
5 changes: 3 additions & 2 deletions packages/frontend/pages/prompts/[[...id]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
Textarea,
Tooltip,
} from "@mantine/core"
import { useHotkeys } from "@mantine/hooks"

import {
IconBolt,
IconBracketsAngle,
Expand Down Expand Up @@ -52,6 +52,7 @@ import Empty from "@/components/Layout/Empty"
import { MODELS } from "shared"
import { usePromptVariables } from "@/utils/promptsHooks"
import { openConfirmModal } from "@mantine/modals"
import { useGlobalShortcut } from "@/utils/hooks"

const ParamItem = ({ name, value }) => (
<Group justify="space-between">
Expand Down Expand Up @@ -106,7 +107,7 @@ function Playground() {

const [rename, setRename] = useState(null)

useHotkeys([
useGlobalShortcut([
[
"mod+S",
() => {
Expand Down
20 changes: 0 additions & 20 deletions packages/frontend/utils/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,3 @@ export function getUserColor(scheme, theme, id: string) {
const finalColor = theme.colors[userColor][scheme === "dark" ? 8 : 4]
return finalColor
}

// fixes the stutter effect in dark mode
// needs to be outside the hook as window.computedColorScheme doesn't reflect the update
// but we need to update the ddefault value
// TODO FIX: THIS ACTUALLY DOESNT WORK WELL WITH MANTINE CSS VARS
let defaultColorScheme =
typeof window !== "undefined" ? window?.computedColorScheme : null

export function useFixedColorScheme() {
const [scheme, setScheme] = useState(defaultColorScheme)

const mantineScheme = useColorScheme()

useDidUpdate(() => {
defaultColorScheme = mantineScheme
setScheme(mantineScheme)
}, [mantineScheme])

return scheme
}
3 changes: 2 additions & 1 deletion packages/frontend/utils/dataHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { useContext } from "react"
import useSWR, { SWRConfiguration, useSWRConfig } from "swr"
import useSWRInfinite from "swr/infinite"
import useSWRMutation, { SWRMutationConfiguration } from "swr/mutation"
import { getUserColor, useFixedColorScheme } from "./colors"
import { getUserColor } from "./colors"
import { ProjectContext } from "./context"

import { useAuth } from "./auth"
import { fetcher } from "./fetcher"
import { useFixedColorScheme } from "./hooks"

type KeyType = string | ((...args: any[]) => string)

Expand Down
49 changes: 49 additions & 0 deletions packages/frontend/utils/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// fixes the stutter effect in dark mode
// needs to be outside the hook as window.computedColorScheme doesn't reflect the update
// but we need to update the ddefault value

import { useColorScheme, useDidUpdate } from "@mantine/hooks"
import { useEffect, useState } from "react"

// TODO FIX: THIS ACTUALLY DOESNT WORK WELL WITH MANTINE CSS VARS
let defaultColorScheme =
typeof window !== "undefined" ? window?.computedColorScheme : null

export function useFixedColorScheme() {
const [scheme, setScheme] = useState(defaultColorScheme)

const mantineScheme = useColorScheme()

useDidUpdate(() => {
defaultColorScheme = mantineScheme
setScheme(mantineScheme)
}, [mantineScheme])

return scheme
}

type Shortcut = [string, () => void]

export function useGlobalShortcut(shortcuts: Shortcut[]) {
useEffect(() => {
const handleKeyDown = (evt: KeyboardEvent) => {
console.log("useGlobalShortcut", evt)
shortcuts.forEach(([keyCombination, action]) => {
const [mod, key] = keyCombination.split("+")
const isModPressed =
mod === "mod" ? evt.ctrlKey || evt.metaKey : evt[`${mod}Key`]
if (isModPressed && evt.key.toLowerCase() === key.toLowerCase()) {
console.log("useGlobalShortcut", keyCombination)
action()
evt.preventDefault()
}
})
}

document.addEventListener("keydown", handleKeyDown)

return () => {
document.removeEventListener("keydown", handleKeyDown)
}
}, [shortcuts])
}

0 comments on commit ec99d5e

Please sign in to comment.