|
1 | 1 | import { goto } from '$app/navigation'; |
2 | 2 | import { organization } from '$lib/stores/organization'; |
3 | | -import { sdk } from '$lib/stores/sdk'; |
| 3 | +import { sdk, getApiEndpoint } from '$lib/stores/sdk'; |
4 | 4 | import { Query } from '@appwrite.io/console'; |
5 | 5 | import { get } from 'svelte/store'; |
6 | 6 | import type { Searcher } from '../commands'; |
| 7 | +import { project } from '$routes/(console)/project-[region]-[project]/store'; |
7 | 8 | import { base } from '$app/paths'; |
8 | 9 |
|
9 | 10 | export const projectsSearcher = (async (query: string) => { |
| 11 | + const q = query.toLowerCase().trim(); |
| 12 | + const wantsCredentials = |
| 13 | + q.includes('endpoint') || |
| 14 | + q.includes('api key') || |
| 15 | + q.includes('api-key') || |
| 16 | + q.includes('apikey') || |
| 17 | + q.includes('project id') || |
| 18 | + q.includes('project-id') || |
| 19 | + q === 'id' || |
| 20 | + q.endsWith(' id') || |
| 21 | + q.startsWith('end') || |
| 22 | + q.includes('api end') || |
| 23 | + (q.includes('api') && q.includes('end')); |
| 24 | + |
| 25 | + if (wantsCredentials) { |
| 26 | + const curr = get(project); |
| 27 | + if (curr?.$id) { |
| 28 | + return [ |
| 29 | + { |
| 30 | + label: 'Go to Settings', |
| 31 | + callback: () => { |
| 32 | + goto(`${base}/project-${curr.region}-${curr.$id}/settings`); |
| 33 | + }, |
| 34 | + group: 'navigation' |
| 35 | + } |
| 36 | + ]; |
| 37 | + } |
| 38 | + return []; |
| 39 | + } |
| 40 | + |
10 | 41 | const { projects } = await sdk.forConsole.projects.list({ |
11 | 42 | queries: [Query.equal('teamId', get(organization).$id), Query.orderDesc('')] |
12 | 43 | }); |
13 | 44 |
|
14 | 45 | return projects |
15 | | - .filter((project) => project.name.toLowerCase().includes(query.toLowerCase())) |
16 | | - .map((project) => { |
17 | | - return { |
18 | | - label: project.name, |
19 | | - callback: () => { |
20 | | - goto(`${base}/project-${project.region}-${project.$id}`); |
21 | | - }, |
22 | | - group: 'projects' |
23 | | - } as const; |
| 46 | + .filter((project) => { |
| 47 | + const endpoint = getApiEndpoint(project.region); |
| 48 | + const searchable = [project.name, project.$id, project.region, endpoint] |
| 49 | + .filter(Boolean) |
| 50 | + .join(' ') |
| 51 | + .toLowerCase(); |
| 52 | + |
| 53 | + const words = q.split(/\s+/).filter(Boolean); |
| 54 | + return words.every((w) => searchable.includes(w)); |
| 55 | + }) |
| 56 | + .flatMap((project) => { |
| 57 | + const href = `${base}/project-${project.region}-${project.$id}`; |
| 58 | + |
| 59 | + const label = project.name; |
| 60 | + |
| 61 | + return [ |
| 62 | + { |
| 63 | + label, |
| 64 | + callback: () => { |
| 65 | + goto(href); |
| 66 | + }, |
| 67 | + group: 'projects' |
| 68 | + } |
| 69 | + ]; |
24 | 70 | }); |
25 | 71 | }) satisfies Searcher; |
0 commit comments