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

cmd-k: starting point #1188

Merged
merged 7 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions frontend/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@
"dependency": "Dependency",
"trust": "Trust",
"solutions": "Solutions",
"tpSolutions": "Third-Party solutions",
"solution": "Solution",
"addSolution": "Add solution",
"providerEntity": "Provider entity",
Expand Down
1 change: 1 addition & 0 deletions frontend/messages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@
"dependency": "Dépendance",
"trust": "Confiance",
"solutions": "Solutions",
"tpSolutions": "Solutions tierces",
"solution": "Solution",
"addSolution": "Ajouter une solution",
"providerEntity": "Entité fournisseur",
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@typescript-eslint/parser": "^8.18.0",
"@unovis/svelte": "1.4.3-beta.0",
"@unovis/ts": "1.4.3-beta.0",
"cmdk-sv": "^0.0.18",
"dotenv": "^16.4.7",
"echarts": "^5.5.1",
"eslint-plugin-storybook": "^0.10.2",
Expand Down
74 changes: 74 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions frontend/src/lib/components/CommandPalette/CommandPalette.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<script lang="ts">
import { browser } from '$app/environment';
import { page } from '$app/stores';
import { Command } from 'cmdk-sv';
import { onMount, onDestroy } from 'svelte';
import { writable } from 'svelte/store';
import { safeTranslate } from '$lib/utils/i18n';
import { navigationLinks } from './paletteData.ts';
import { goto } from '$app/navigation';

// Create a store for command palette visibility
export const commandPaletteOpen = writable(false);

// Custom case-insensitive filter
function caseInsensitiveFilter(value: string, search: string) {
return value.toLowerCase().includes(search.toLowerCase()) ? 1 : 0;
}

// Keyboard shortcut handler
function handleKeydown(e: KeyboardEvent) {
if (browser && (e.metaKey || e.ctrlKey) && e.key === 'k') {
e.preventDefault();
commandPaletteOpen.update((current) => !current);
}
}

// Generate navigation commands with automatic close
const navigationCommands = navigationLinks.map((link) => ({
label: safeTranslate(link.label),
value: link.href,
onSelect: () => {
commandPaletteOpen.set(false);
goto(link.href);
}
}));

// Close command palette on route change
$: if ($page.url.pathname) {
commandPaletteOpen.set(false);
}

// Add global event listener
onMount(() => {
if (browser) {
window.addEventListener('keydown', handleKeydown);
}
});

onDestroy(() => {
if (browser) {
window.removeEventListener('keydown', handleKeydown);
}
});
</script>

<Command.Dialog bind:open={$commandPaletteOpen} label="Command Menu">
<Command.Root filter={caseInsensitiveFilter}>
<Command.Input placeholder="Type a command..." />
<Command.List>
<Command.Empty>No results found.</Command.Empty>
<Command.Group heading="Navigation">
{#each navigationCommands as command}
<Command.Item value={command.label} onSelect={command.onSelect}>
{command.label}
</Command.Item>
{/each}
</Command.Group>
<Command.Separator />
</Command.List>
</Command.Root>
</Command.Dialog>

<style lang="postcss">
/* Global styles for the command palette */
:global([data-cmdk-dialog]) {
@apply fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm p-2;
}

:global([data-cmdk-root]) {
@apply w-full max-w-md bg-white rounded-lg shadow-xl border border-gray-200 overflow-hidden;
}

:global([data-cmdk-input]) {
@apply w-full px-4 py-3 border-b border-gray-200 outline-none;
}

:global([data-cmdk-list]) {
@apply max-h-[300px] overflow-y-auto;
}

:global([data-cmdk-item]) {
@apply px-4 py-2 cursor-pointer hover:bg-gray-100
focus:bg-gray-100 focus:outline-none;
}

:global([data-cmdk-item][data-selected='true']) {
@apply bg-gray-100;
}

:global([data-cmdk-group-heading]) {
@apply px-4 py-2 text-xs text-gray-500 uppercase;
}
</style>
65 changes: 65 additions & 0 deletions frontend/src/lib/components/CommandPalette/paletteData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as m from '$paraglide/messages';

export interface NavigationLink {
label: string;
href: string;
}

export const navigationLinks: NavigationLink[] = [
{
label: 'analytics',
href: '/'
},
{
label: 'myAssignments',
href: '/my-assignments'
},
{
label: 'domains',
href: '/folders'
},
{
label: 'assets',
href: '/assets'
},
{
label: 'complianceAssessments',
href: '/compliance-assessments'
},
{
label: 'riskAssessments',
href: '/risk-assessments'
},
{
label: 'riskScenarios',
href: '/risk-scenarios'
},
{
label: 'actionPlan',
href: '/applied-controls'
},
{
label: 'evidences',
href: '/evidences'
},
{
label: 'xRays',
href: '/x-rays'
},
{
label: 'tpSolutions',
href: '/solutions'
},
{
label: 'libraries',
href: '/libraries'
},
{
label: 'myProfile',
href: '/my-profile'
},
{
label: 'settings',
href: '/settings'
}
];
Loading
Loading