Add /profile-get command

4fd7828a9bb8cb9aff530d8db5dea03333b9b693

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +59 -16Ignore whitespace
public/scripts/extensions/connection-manager/index.js+59 -16
@@ -75,6 +75,31 @@ const collapseSpaces = (s) => s.replace(/\s+/g, ' ').trim();
75const makeUnique = (s, f, i) => { if (!f(s)) { return s; } else { while (f(`${s} (${i})`)) { i++; } return `${s} (${i})`; } };75const makeUnique = (s, f, i) => { if (!f(s)) { return s; } else { while (f(`${s} (${i})`)) { i++; } return `${s} (${i})`; } };
7676
77/**77/**
78 * Finds the best match for the search value.
79 * @param {string} value Search value
80 * @returns {ConnectionProfile|null} Best match or null
81 */
82function findProfileByName(value) {
83 // Try to find exact match
84 const profile = extension_settings.connectionManager.profiles.find(p => p.name === value);
85
86 if (profile) {
87 return profile;
88 }
89
90 // Try to find fuzzy match
91 const fuse = new Fuse(extension_settings.connectionManager.profiles, { keys: ['name'] });
92 const results = fuse.search(value);
93
94 if (results.length === 0) {
95 return null;
96 }
97
98 const bestMatch = results[0];
99 return bestMatch.item;
100}
101
102/**
78 * Reads the connection profile from the commands.103 * Reads the connection profile from the commands.
79 * @param {string} mode Mode of the connection profile104 * @param {string} mode Mode of the connection profile
80 * @param {ConnectionProfile} profile Connection profile105 * @param {ConnectionProfile} profile Connection profile
@@ -374,27 +399,16 @@ async function renderDetailsContent(details, detailsContent) {
374 return NONE;399 return NONE;
375 }400 }
376401
377 // Try to find exact match402 const profile = findProfileByName(value);
378 const profile = extension_settings.connectionManager.profiles.find(p => p.name === value);
379
380 if (profile) {
381 profiles.selectedIndex = Array.from(profiles.options).findIndex(o => o.value === profile.id);
382 profiles.dispatchEvent(new Event('change'));
383 return profile.name;
384 }
385
386 // Try to find fuzzy match
387 const fuse = new Fuse(extension_settings.connectionManager.profiles, { keys: ['name'] });
388 const results = fuse.search(value);
389403
390 if (results.length === 0) {404 if (!profile) {
391 return '';405 return '';
392 }406 }
393407
394 const bestMatch = results[0];408 profiles.selectedIndex = Array.from(profiles.options).findIndex(o => o.value === profile.id);
395 profiles.value = bestMatch.item.id;
396 profiles.dispatchEvent(new Event('change'));409 profiles.dispatchEvent(new Event('change'));
397 return bestMatch.item.name;410
411 return profiles.name;
398 },412 },
399 }));413 }));
400414
@@ -448,4 +462,33 @@ async function renderDetailsContent(details, detailsContent) {
448 saveSettingsDebounced();462 saveSettingsDebounced();
449 },463 },
450 }));464 }));
465
466 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
467 name: 'profile-get',
468 helpString: 'Get the details of the connection profile. Returns the selected profile if no argument is provided.',
469 returns: 'object of the selected profile',
470 unnamedArgumentList: [
471 SlashCommandArgument.fromProps({
472 description: 'Name of the connection profile',
473 enumProvider: profilesProvider,
474 isRequired: false,
475 }),
476 ],
477 callback: async (_args, value) => {
478 if (!value || typeof value !== 'string') {
479 const selectedProfile = extension_settings.connectionManager.selectedProfile;
480 const profile = extension_settings.connectionManager.profiles.find(p => p.id === selectedProfile);
481 if (!profile) {
482 return '';
483 }
484 return JSON.stringify(profile);
485 }
486
487 const profile = findProfileByName(value);
488 if (!profile) {
489 return '';
490 }
491 return JSON.stringify(profile);
492 },
493 }));
451})();494})();