misc: add more links

This commit is contained in:
Xylight committed 2024-07-19 15:42:44 -07:00
1 parent 59bb6dde89
commit f38011e0c6
3 files changed
+97 -59

No files matched your search

@@ -0,0 +1,30 @@
<script lang="ts">
import { Button } from 'mono-svelte'
import { Icon, QuestionMarkCircle, type IconSource } from 'svelte-hero-icons'
import Avatar from '../../Avatar.svelte'
export let action: {
name: string
desc?: string
handle?: () => any
href?: string
shortcut?: string
icon: string | IconSource
}
</script>
<Button
href={action.href}
on:click={action.handle}
alignment="left"
color="tertiary"
size="lg"
class="focus:bg-slate-200 focus:dark:bg-zinc-800"
>
{#if typeof action.icon === 'string'}
<Avatar url={action.icon} width={20} />
{:else}
<Icon src={action.icon ?? QuestionMarkCircle} size="20" />
{/if}
{action.name}
</Button>
@@ -9,25 +9,23 @@
} from 'mono-svelte'
import { createEventDispatcher, onMount } from 'svelte'
import {
Bookmark,
Icon,
Inbox,
PencilSquare,
QuestionMarkCircle,
UserCircle,
type IconSource,
} from 'svelte-hero-icons'
import Avatar from '../../Avatar.svelte'
import { t } from '$lib/translations'
import CommandItem from './CommandItem.svelte'
import { browser } from '$app/environment'
import { goto } from '$app/navigation'
let open = true
export let open = false
$: if (open) search = ''
onMount(() => {
window.addEventListener('keydown', (e) => {
if (e.key == '/') {
e.preventDefault()
open = !open
}
})
})
interface Group {
name: string
actions: Action[]
@@ -53,9 +51,30 @@
href: r.url,
})),
},
{
name: $t('profile.profile'),
actions: [
{
href: '/profile/user',
name: $t('profile.profile'),
icon: UserCircle,
},
{
href: '/inbox',
name: $t('profile.inbox'),
icon: Inbox,
},
{
href: '/saved',
name: $t('profile.saved'),
icon: Bookmark,
},
],
},
]
let search = ''
let container: HTMLElement
const dispatch = createEventDispatcher()
let selectedIndex = 0
@@ -63,14 +82,12 @@
group.actions.map((action) => ({ ...action, group: group.name })),
)
$: filteredActions = flattenedActions.filter(
(action) =>
action.name.toLowerCase().includes(search.toLowerCase()) ||
action.group.toLowerCase().includes(search.toLowerCase()),
$: filteredActions = flattenedActions.filter((action) =>
action.name.toLowerCase().includes(search.toLowerCase()),
)
function togglePalette() {
open = !open
open = false
if (open) {
search = ''
selectedIndex = 0
@@ -78,9 +95,9 @@
}
function handleKeydown(event: KeyboardEvent) {
if (event.ctrlKey && event.key === 'k') {
if ((event.ctrlKey && event.key === 'k') || event.key == '/') {
event.preventDefault()
togglePalette()
open = true
}
if (!open) return
@@ -98,8 +115,8 @@
case 'Enter':
event.preventDefault()
if (filteredActions[selectedIndex]) {
handleSelect(filteredActions[selectedIndex])
dispatch('select', filteredActions[selectedIndex])
togglePalette()
}
break
case 'Escape':
@@ -109,56 +126,42 @@
}
}
function handleSelect(action: Action & { group: string }) {
function focusItem(index: number) {
if (!browser) return
const listItems = container?.querySelectorAll('li')
listItems[index % listItems.length].focus()
}
async function handleSelect(action: Action & { group: string }) {
if (action.href) await goto(action.href)
if (action.handle) action.handle()
dispatch('select', action)
togglePalette()
}
</script>
<svelte:window on:keydown={handleKeydown} />
<Modal title="Command Prompt" bind:open>
<TextInput bind:value={search} autofocus />
{#if search == ''}
{#each groups as group}
<div class="space-y-1">
<div class="space-y-1" bind:this={container}>
{#if search == ''}
{#each groups as group}
<span class="text-slate-700 dark:text-zinc-300 font-medium text-sm">
{group.name}
</span>
<ul class="flex flex-col">
{#each group.actions as action}
<Button
href={action.href}
on:click={action.handle}
alignment="left"
color="tertiary"
size="lg"
>
{#if typeof action.icon === 'string'}
<Avatar url={action.icon} width={24} />
{:else}
<Icon src={action.icon ?? QuestionMarkCircle} size="24" mini />
{/if}
{action.name}
</Button>
<ul class="flex flex-col gap-1">
{#each group.actions as action, index}
<CommandItem {action} />
{/each}
</ul>
</div>
{/each}
{:else}
{#each filteredActions as action}
<Button
href={action.href}
on:click={action.handle}
alignment="left"
color="tertiary"
size="lg"
>
{#if typeof action.icon === 'string'}
<Avatar url={action.icon} width={24} />
{:else}
<Icon src={action.icon ?? QuestionMarkCircle} size="24" mini />
{/if}
{action.name}
</Button>
{/each}
{/if}
{/each}
{:else}
<ul class="flex flex-col gap-1">
{#each filteredActions as action, index}
<CommandItem {action} />
{/each}
</ul>
{/if}
</div>
</Modal>