refactor: simplify search integration flow
This commit is contained in:
parent
129012c3ee
commit
56f44dbcaa
4 changed files with 105 additions and 111 deletions
|
|
@ -58,8 +58,6 @@ export function DialogIntegration(
|
|||
) {
|
||||
const data = useData()
|
||||
const dialog = useDialog()
|
||||
const sdk = useSDK()
|
||||
const toast = useToast()
|
||||
const { theme } = useTheme()
|
||||
const options = createMemo(() =>
|
||||
integrationOptions(data.location.integration.list() ?? [])
|
||||
|
|
@ -68,26 +66,28 @@ export function DialogIntegration(
|
|||
const methods = connectMethods(integration)
|
||||
const connected = integration.connections.length > 0
|
||||
const search = integration.capabilities.find((capability) => capability.type === "search")
|
||||
const credentials = credentialConnections(integration)
|
||||
const description = search?.connection === "optional" ? "API key optional" : undefined
|
||||
const category = search ? "Web search" : undefined
|
||||
return {
|
||||
title: integration.name,
|
||||
value: integration.id,
|
||||
description:
|
||||
search?.connection === "optional" ? "API key optional" : methods.length ? undefined : "Environment only",
|
||||
description: description ?? (methods.length === 0 ? "Environment only" : undefined),
|
||||
footer:
|
||||
[connectionSummary(integration), search?.selected ? "Web search default" : undefined]
|
||||
.filter((value) => value !== undefined && value.length > 0)
|
||||
.join(" · ") || undefined,
|
||||
category: search ? "Web search" : integration.id in INTEGRATION_PRIORITY ? "Popular" : "Services",
|
||||
category: category ?? (integration.id in INTEGRATION_PRIORITY ? "Popular" : "Services"),
|
||||
disabled: methods.length === 0 && !search,
|
||||
gutter: connected ? () => <text fg={theme.success}>✓</text> : undefined,
|
||||
onSelect: () => {
|
||||
if (props.connectionOnly) {
|
||||
return credentialConnections(integration).length
|
||||
return credentials.length
|
||||
? manageConnections(integration, methods, dialog, props.onConnected)
|
||||
: selectMethod(integration, methods, dialog, props.onConnected)
|
||||
}
|
||||
if (search) return manageIntegration(integration, methods, search, data, sdk, dialog, toast)
|
||||
return credentialConnections(integration).length
|
||||
if (search) return manageIntegration(integration, methods, search, dialog)
|
||||
return credentials.length
|
||||
? manageConnections(integration, methods, dialog, props.onConnected)
|
||||
: selectMethod(integration, methods, dialog, props.onConnected)
|
||||
},
|
||||
|
|
@ -112,55 +112,58 @@ function manageIntegration(
|
|||
integration: IntegrationInfo,
|
||||
methods: ConnectMethod[],
|
||||
search: IntegrationInfo["capabilities"][number],
|
||||
data: ReturnType<typeof useData>,
|
||||
sdk: ReturnType<typeof useSDK>,
|
||||
dialog: ReturnType<typeof useDialog>,
|
||||
toast: ReturnType<typeof useToast>,
|
||||
) {
|
||||
const connected = integration.connections.length > 0
|
||||
const select = () => {
|
||||
void sdk.api.integration
|
||||
.selectCapability({
|
||||
integrationID: integration.id,
|
||||
capability: "search",
|
||||
location: location(data),
|
||||
})
|
||||
.then(async () => {
|
||||
await data.location.integration.refresh()
|
||||
toast.show({ variant: "success", message: `${integration.name} is now the web search default` })
|
||||
dialog.clear()
|
||||
})
|
||||
.catch(toast.error)
|
||||
}
|
||||
dialog.replace(() => {
|
||||
const data = useData()
|
||||
const sdk = useSDK()
|
||||
const toast = useToast()
|
||||
const credentials = credentialConnections(integration)
|
||||
const selectSearch = () => {
|
||||
void sdk.api.integration
|
||||
.selectCapability({
|
||||
integrationID: integration.id,
|
||||
capability: "search",
|
||||
location: location(data),
|
||||
})
|
||||
.then(async () => {
|
||||
await data.location.integration.refresh()
|
||||
toast.show({ variant: "success", message: `${integration.name} is now the web search default` })
|
||||
dialog.clear()
|
||||
})
|
||||
.catch(toast.error)
|
||||
}
|
||||
|
||||
dialog.replace(() => (
|
||||
<DialogSelect
|
||||
title={integration.name}
|
||||
options={[
|
||||
{
|
||||
title: search.selected ? "Web search default" : "Use for web search",
|
||||
value: "search",
|
||||
disabled: search.selected,
|
||||
onSelect:
|
||||
search.connection === "required" && !connected
|
||||
? () => selectMethod(integration, methods, dialog, select)
|
||||
: select,
|
||||
},
|
||||
...(methods.length
|
||||
? [
|
||||
{
|
||||
title: credentialConnections(integration).length ? "Manage connections" : "Connect",
|
||||
value: "connect",
|
||||
onSelect: () =>
|
||||
credentialConnections(integration).length
|
||||
? manageConnections(integration, methods, dialog)
|
||||
: selectMethod(integration, methods, dialog),
|
||||
},
|
||||
]
|
||||
: []),
|
||||
]}
|
||||
/>
|
||||
))
|
||||
return (
|
||||
<DialogSelect
|
||||
title={integration.name}
|
||||
options={[
|
||||
{
|
||||
title: search.selected ? "Web search default" : "Use for web search",
|
||||
value: "search",
|
||||
disabled: search.selected,
|
||||
onSelect:
|
||||
search.connection === "required" && !connected
|
||||
? () => selectMethod(integration, methods, dialog, selectSearch)
|
||||
: selectSearch,
|
||||
},
|
||||
...(methods.length
|
||||
? [
|
||||
{
|
||||
title: credentials.length ? "Manage connections" : "Connect",
|
||||
value: "connect",
|
||||
onSelect: () =>
|
||||
credentials.length
|
||||
? manageConnections(integration, methods, dialog)
|
||||
: selectMethod(integration, methods, dialog),
|
||||
},
|
||||
]
|
||||
: []),
|
||||
]}
|
||||
/>
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
function manageConnections(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue