Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
207 lines
5.8 KiB
TypeScript
207 lines
5.8 KiB
TypeScript
import React, { createContext, useCallback, useContext, useMemo } from 'react';
|
|
import type { Host, Snippet, VaultNote } from '../../../types';
|
|
import { useI18n } from '../../../application/i18n/I18nProvider';
|
|
import { toast } from '../../ui/toast';
|
|
import type { VaultSummarySection, VaultToolArtifact } from './vaultToolArtifact';
|
|
|
|
export type VaultArtifactNavSection = Extract<VaultSummarySection, 'notes' | 'hosts' | 'snippets'>;
|
|
|
|
export interface VaultArtifactNavigationActions {
|
|
openVaultNote?: (noteId: string) => void;
|
|
openVaultHost?: (hostId: string) => void;
|
|
openVaultSnippet?: (snippetId: string) => void;
|
|
openVaultSection?: (section: VaultArtifactNavSection) => void;
|
|
}
|
|
|
|
interface CreateVaultArtifactNavigationActionsOptions {
|
|
notes: VaultNote[];
|
|
hosts: Host[];
|
|
snippets: Snippet[];
|
|
t: (key: string) => string;
|
|
onOpenVaultNote?: (noteId: string) => void;
|
|
onOpenVaultHost?: (hostId: string) => void;
|
|
onOpenVaultSnippet?: (snippetId: string) => void;
|
|
onOpenVaultSection?: (section: VaultArtifactNavSection) => void;
|
|
onUnavailable: (message: string, title: string) => void;
|
|
}
|
|
|
|
interface VaultArtifactNavigationProviderProps {
|
|
notes: VaultNote[];
|
|
hosts: Host[];
|
|
snippets?: Snippet[];
|
|
onOpenVaultNote?: (noteId: string) => void;
|
|
onOpenVaultHost?: (hostId: string) => void;
|
|
onOpenVaultSnippet?: (snippetId: string) => void;
|
|
onOpenVaultSection?: (section: VaultArtifactNavSection) => void;
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
const VaultArtifactNavigationContext = createContext<VaultArtifactNavigationActions | null>(null);
|
|
|
|
export function createVaultArtifactNavigationActions({
|
|
notes,
|
|
hosts,
|
|
snippets,
|
|
t,
|
|
onOpenVaultNote,
|
|
onOpenVaultHost,
|
|
onOpenVaultSnippet,
|
|
onOpenVaultSection,
|
|
onUnavailable,
|
|
}: CreateVaultArtifactNavigationActionsOptions): VaultArtifactNavigationActions {
|
|
const actions: VaultArtifactNavigationActions = {};
|
|
|
|
if (onOpenVaultNote) {
|
|
actions.openVaultNote = (noteId: string) => {
|
|
const exists = notes.some((note) => note.id === noteId);
|
|
if (!exists) {
|
|
onUnavailable(t('ai.chat.artifact.noteMissing'), t('ai.chat.artifact.unavailableTitle'));
|
|
return;
|
|
}
|
|
onOpenVaultNote(noteId);
|
|
};
|
|
}
|
|
|
|
if (onOpenVaultHost) {
|
|
actions.openVaultHost = (hostId: string) => {
|
|
const exists = hosts.some((host) => host.id === hostId);
|
|
if (!exists) {
|
|
onUnavailable(t('ai.chat.artifact.hostMissing'), t('ai.chat.artifact.unavailableTitle'));
|
|
return;
|
|
}
|
|
onOpenVaultHost(hostId);
|
|
};
|
|
}
|
|
|
|
if (onOpenVaultSnippet) {
|
|
actions.openVaultSnippet = (snippetId: string) => {
|
|
const exists = snippets.some((snippet) => snippet.id === snippetId);
|
|
if (!exists) {
|
|
onUnavailable(t('ai.chat.artifact.snippetMissing'), t('ai.chat.artifact.unavailableTitle'));
|
|
return;
|
|
}
|
|
onOpenVaultSnippet(snippetId);
|
|
};
|
|
}
|
|
|
|
if (onOpenVaultSection) {
|
|
actions.openVaultSection = onOpenVaultSection;
|
|
}
|
|
|
|
return actions;
|
|
}
|
|
|
|
export function VaultArtifactNavigationProvider({
|
|
notes,
|
|
hosts,
|
|
snippets = [],
|
|
onOpenVaultNote,
|
|
onOpenVaultHost,
|
|
onOpenVaultSnippet,
|
|
onOpenVaultSection,
|
|
children,
|
|
}: VaultArtifactNavigationProviderProps) {
|
|
const { t } = useI18n();
|
|
|
|
const onUnavailable = useCallback((message: string, title: string) => {
|
|
toast.warning(message, title);
|
|
}, []);
|
|
|
|
const value = useMemo<VaultArtifactNavigationActions>(() => createVaultArtifactNavigationActions({
|
|
notes,
|
|
hosts,
|
|
snippets,
|
|
t,
|
|
onOpenVaultNote,
|
|
onOpenVaultHost,
|
|
onOpenVaultSnippet,
|
|
onOpenVaultSection,
|
|
onUnavailable,
|
|
}), [
|
|
hosts,
|
|
notes,
|
|
onOpenVaultHost,
|
|
onOpenVaultNote,
|
|
onOpenVaultSection,
|
|
onOpenVaultSnippet,
|
|
onUnavailable,
|
|
snippets,
|
|
t,
|
|
]);
|
|
|
|
return (
|
|
<VaultArtifactNavigationContext.Provider value={value}>
|
|
{children}
|
|
</VaultArtifactNavigationContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useVaultArtifactNavigation(): VaultArtifactNavigationActions | null {
|
|
return useContext(VaultArtifactNavigationContext);
|
|
}
|
|
|
|
export function navigateVaultArtifact(
|
|
artifact: VaultToolArtifact,
|
|
navigation: VaultArtifactNavigationActions,
|
|
): void {
|
|
switch (artifact.kind) {
|
|
case 'vault.note':
|
|
navigation.openVaultNote?.(artifact.noteId);
|
|
break;
|
|
case 'vault.host':
|
|
navigation.openVaultHost?.(artifact.hostId);
|
|
break;
|
|
case 'vault.hosts.batch':
|
|
navigation.openVaultSection?.('hosts');
|
|
break;
|
|
case 'vault.summary':
|
|
if (artifact.section === 'scripts') {
|
|
navigation.openVaultSection?.('snippets');
|
|
} else {
|
|
navigation.openVaultSection?.(artifact.section);
|
|
}
|
|
break;
|
|
case 'vault.snippet':
|
|
case 'vault.script':
|
|
navigation.openVaultSnippet?.(artifact.kind === 'vault.snippet' ? artifact.snippetId : artifact.scriptId);
|
|
break;
|
|
case 'vault.snippet.run':
|
|
navigation.openVaultSnippet?.(artifact.snippetId);
|
|
break;
|
|
case 'vault.script.run':
|
|
navigation.openVaultSnippet?.(artifact.scriptId);
|
|
break;
|
|
case 'vault.script.reference':
|
|
case 'vault.script.runs':
|
|
navigation.openVaultSection?.('snippets');
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
export function canNavigateVaultArtifact(
|
|
artifact: VaultToolArtifact,
|
|
navigation: VaultArtifactNavigationActions | null,
|
|
): boolean {
|
|
if (!navigation) return false;
|
|
switch (artifact.kind) {
|
|
case 'vault.note':
|
|
return Boolean(navigation.openVaultNote);
|
|
case 'vault.host':
|
|
return Boolean(navigation.openVaultHost);
|
|
case 'vault.hosts.batch':
|
|
case 'vault.summary':
|
|
case 'vault.script.reference':
|
|
case 'vault.script.runs':
|
|
return Boolean(navigation.openVaultSection);
|
|
case 'vault.snippet':
|
|
case 'vault.script':
|
|
case 'vault.snippet.run':
|
|
case 'vault.script.run':
|
|
return Boolean(navigation.openVaultSnippet);
|
|
default:
|
|
return false;
|
|
}
|
|
}
|