diff --git a/src/hooks/useGlobalSearch.ts b/src/hooks/useGlobalSearch.ts index 9d2fd1c..a91f215 100644 --- a/src/hooks/useGlobalSearch.ts +++ b/src/hooks/useGlobalSearch.ts @@ -7,6 +7,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { entryPath, getObjectKind, groupByType, searchEntries } from '@/lib/globalSearch'; +import { isLinkVisible } from '@/lib/layout'; import { useAccountStore } from '@/stores/accountStore'; import { useSchemaStore, type SearchIndexEntry } from '@/stores/schemaStore'; @@ -22,6 +23,8 @@ export function useGlobalSearch(onAfterSelect?: () => void) { const schema = useSchemaStore((s) => s.schema); const searchIndex = useSchemaStore((s) => s.searchIndex); const hasObjectPermission = useAccountStore((s) => s.hasObjectPermission); + const hasPermission = useAccountStore((s) => s.hasPermission); + const edition = useAccountStore((s) => s.edition); const type = useCallback((value: string) => { setQuery(value); @@ -31,11 +34,18 @@ export function useGlobalSearch(onAfterSelect?: () => void) { useEffect(() => () => clearTimeout(timer.current), []); - const groups = useMemo( - () => - groupByType(searchEntries(searchIndex, debouncedQuery, schema, (prefix) => hasObjectPermission(prefix, 'Get'))), - [debouncedQuery, searchIndex, schema, hasObjectPermission], - ); + const groups = useMemo(() => { + const canOpen = (viewName: string) => + !!schema && + isLinkVisible( + schema, + viewName, + edition, + (prefix) => hasObjectPermission(prefix, 'Get'), + (permission) => hasPermission(permission), + ); + return groupByType(searchEntries(searchIndex, debouncedQuery, schema, canOpen)); + }, [debouncedQuery, searchIndex, schema, edition, hasObjectPermission, hasPermission]); const selectEntry = useCallback( (entry: SearchIndexEntry) => { diff --git a/src/i18n/en.json b/src/i18n/en.json index 382537d..9705d97 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -247,7 +247,8 @@ "title": "Search", "typeToSearch": "Type to search the admin panel.", "placeholderActions": "Search pages and settings, or run an action...", - "actions": "Actions" + "actions": "Actions", + "open": "Open" }, "jmapErrors": { "addressBookHasContents": "This address book has contacts. Remove them first.", diff --git a/src/lib/globalSearch.test.ts b/src/lib/globalSearch.test.ts index 5b2605e..1cc3ce6 100644 --- a/src/lib/globalSearch.test.ts +++ b/src/lib/globalSearch.test.ts @@ -5,7 +5,7 @@ */ import { describe, it, expect } from 'vitest'; -import { List, Plus, Settings } from 'lucide-react'; +import { ArrowRight, List, Plus, Settings } from 'lucide-react'; import { MAX_RESULTS, entryPath, @@ -15,6 +15,7 @@ import { groupByType, searchEntries, } from './globalSearch'; +import { isLinkVisible } from './layout'; import type { SearchIndexEntry } from '@/stores/schemaStore'; import type { Schema } from '@/types/schema'; @@ -37,8 +38,15 @@ const index = [ entry('People', 'link', 'x:Account/User', ['users']), entry('Security', 'link', 'x:Security'), entry('Gone', 'link', 'x:Missing'), + entry('Deleted Accounts', 'link', 'CustomComponent/DeletedAccounts'), + entry('Someday', 'link', 'CustomComponent/NotInThisConsole'), ]; -const all = () => true; +// The sidebar's check, as the command bar uses it. +const openFor = + (canGet: (prefix: string) => boolean) => + (viewName: string) => + isLinkVisible(schema, viewName, 'enterprise', canGet, () => true); +const all = openFor(() => true); describe('searchEntries', () => { it('needs every word, in the text or the keywords, ignoring case', () => { @@ -49,14 +57,22 @@ describe('searchEntries', () => { it('puts pages first, then sections, then fields', () => { const results = searchEntries(index, 'd', schema, all); - expect(results.map((e) => e.type)).toEqual(['link', 'form', 'field']); + expect(results.map((e) => e.type)).toEqual(['link', 'link', 'form', 'field']); }); it('leaves out what the account may not read, and unknown views', () => { - expect(searchEntries(index, 'people', schema, (p) => p !== 'sysAccount')).toEqual([]); + expect(searchEntries(index, 'people', schema, openFor((p) => p !== 'sysAccount'))).toEqual([]); expect(searchEntries(index, 'gone', schema, all)).toEqual([]); }); + it("finds the console's own pages when the sidebar would show them (#192)", () => { + expect(searchEntries(index, 'deleted', schema, all).map((e) => e.viewName)).toEqual([ + 'CustomComponent/DeletedAccounts', + ]); + expect(searchEntries(index, 'deleted', schema, openFor((p) => p !== 'sysAccount'))).toEqual([]); + expect(searchEntries(index, 'someday', schema, all)).toEqual([]); + }); + it('finds nothing without a query or a schema', () => { expect(searchEntries(index, ' ', schema, all)).toEqual([]); expect(searchEntries(index, 'domain', null, all)).toEqual([]); @@ -91,6 +107,7 @@ describe('where a result leads', () => { expect(getActionInfo('field', 'singleton', t)).toEqual({ label: 'Settings', Icon: Settings }); expect(getActionInfo('link', 'object', t)).toEqual({ label: 'List', Icon: List }); expect(getActionInfo('form', null, t)).toEqual({ label: 'Create', Icon: Plus }); + expect(getActionInfo('link', null, t)).toEqual({ label: 'Open', Icon: ArrowRight }); }); it('names a view after its last part', () => { diff --git a/src/lib/globalSearch.ts b/src/lib/globalSearch.ts index 9c59cf4..f7cb56d 100644 --- a/src/lib/globalSearch.ts +++ b/src/lib/globalSearch.ts @@ -5,7 +5,7 @@ */ /** Searching the schema's pages, sections and fields, and where a result leads. */ -import { List, Plus, Settings } from 'lucide-react'; +import { ArrowRight, List, Plus, Settings } from 'lucide-react'; import { resolveObject } from '@/lib/schemaResolver'; import type { SearchIndexEntry } from '@/stores/schemaStore'; import type { Schema } from '@/types/schema'; @@ -23,13 +23,15 @@ export function getObjectKind(schema: Schema, viewName: string): ObjectKind { return resolved.objectType.type === 'singleton' ? 'singleton' : 'object'; } -/** What choosing a result does: open settings, open a list, or create a record. */ +/** What choosing a result does: open settings, a list or one of the console's own pages, or create a record. */ export function getActionInfo( entryType: SearchIndexEntry['type'], objectKind: ObjectKind, t: (key: string, fallback: string) => string, ): { label: string; Icon: typeof List } { if (objectKind === 'singleton') return { label: t('globalSearch.settings', 'Settings'), Icon: Settings }; + // A page that isn't a schema object, such as Deleted Accounts. + if (objectKind === null && entryType === 'link') return { label: t('globalSearch.open', 'Open'), Icon: ArrowRight }; return entryType === 'link' ? { label: t('globalSearch.list', 'List'), Icon: List } : { label: t('globalSearch.create', 'Create'), Icon: Plus }; @@ -49,22 +51,22 @@ export function friendlyName(viewName: string): string { /** * The entries holding every word of the query, in their text or keywords, - * that the account may read; pages first, at most fifteen. + * whose page the account may open; pages first, at most fifteen. `canOpen` + * is the sidebar's own check (`isLinkVisible`), so the console's own pages, + * such as Deleted Accounts, are found when the sidebar shows them. */ export function searchEntries( index: SearchIndexEntry[], query: string, schema: Schema | null, - canGet: (permissionPrefix: string) => boolean, + canOpen: (viewName: string) => boolean, ): SearchIndexEntry[] { const words = query.toLowerCase().split(/\s+/).filter(Boolean); if (words.length === 0 || !schema) return []; return index .filter((entry) => { const text = `${entry.text} ${entry.keywords?.join(' ') ?? ''}`.toLowerCase(); - if (!words.every((word) => text.includes(word))) return false; - const resolved = resolveObject(schema, entry.viewName); - return resolved !== null && canGet(resolved.permissionPrefix); + return words.every((word) => text.includes(word)) && canOpen(entry.viewName); }) .sort((a, b) => TYPE_ORDER[a.type] - TYPE_ORDER[b.type]) .slice(0, MAX_RESULTS);