Skip to content

Commit

Permalink
wip: fix module adding
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Oct 8, 2024
1 parent cee3706 commit 64aa6b5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 36 deletions.
91 changes: 57 additions & 34 deletions webui/src/Connections/AddConnection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import React, { useContext, useState, useCallback, useRef, forwardRef, useImperativeHandle } from 'react'
import React, {
useContext,
useState,
useCallback,
useRef,
forwardRef,
useImperativeHandle,
useMemo,
useEffect,
} from 'react'
import {
CAlert,
CButton,
Expand Down Expand Up @@ -27,6 +36,7 @@ import {
import { makeLabelSafe } from '@companion-app/shared/Label.js'
import { ClientConnectionConfig } from '@companion-app/shared/Model/Common.js'
import { getModuleVersionInfoForConnection } from './Util.js'
import { DropdownChoiceInt } from '../LocalVariableDefinitions.js'

interface AddConnectionsPanelProps {
showHelp: (moduleId: string, moduleVersion: NewClientModuleVersionInfo2) => void
Expand Down Expand Up @@ -218,7 +228,7 @@ const AddConnectionModal = observer(
setShow(true)
setModuleInfo(info)

// TODO - make sure this is a valid selection
// There is a useEffect below that ensures this is valid
setSelectedVersion({
mode: 'stable',
id: null,
Expand All @@ -245,6 +255,23 @@ const AddConnectionModal = observer(
moduleVersionId: selectedVersion.id,
})

const versionOptions = useMemo(() => moduleInfo && getConnectionVersionSelectOptions(moduleInfo), [moduleInfo])

// Ensure the currently selection version is a valid option
useEffect(() => {
if (!versionOptions) return

setSelectedVersion((value) => {
const valueStr = JSON.stringify(value)

// Check if value is still valid
if (versionOptions.find((v) => v.value === valueStr)) return value

// It is not, so choose the first option
return JSON.parse(versionOptions[0].value)
})
}, [versionOptions])

return (
<CModalExt visible={show} onClose={doClose} onClosed={onClosed} scrollable={true}>
{moduleInfo && (
Expand Down Expand Up @@ -286,7 +313,11 @@ const AddConnectionModal = observer(
value={JSON.stringify(selectedVersion)}
onChange={(e) => setSelectedVersion(JSON.parse(e.currentTarget.value))}
>
<ConnectionVersionSelectOptions moduleInfo={moduleInfo} />
{versionOptions?.map((v) => (
<option key={v.value} value={v.value}>
{v.label}
</option>
))}
</CFormSelect>
</CCol>
</CForm>
Expand Down Expand Up @@ -355,36 +386,28 @@ function findNextConnectionLabel(
return label
}

interface ConnectionVersionSelectOptionsProps {
moduleInfo: NewClientModuleInfo
}
export function ConnectionVersionSelectOptions({ moduleInfo }: ConnectionVersionSelectOptionsProps) {
return (
<>
{moduleInfo.stableVersion && (
<option value={JSON.stringify(moduleInfo.stableVersion.version)}>{moduleInfo.stableVersion.displayName}</option>
)}
{moduleInfo.prereleaseVersion && (
<option value={JSON.stringify(moduleInfo.prereleaseVersion.version)}>
{moduleInfo.prereleaseVersion.displayName}
</option>
)}
export function getConnectionVersionSelectOptions(moduleInfo: NewClientModuleInfo): DropdownChoiceInt[] {
const choices: DropdownChoiceInt[] = []

{moduleInfo.releaseVersions.map((version) => {
return (
<option key={JSON.stringify(version.version)} value={JSON.stringify(version.version)}>
{version.displayName}
</option>
)
})}

{moduleInfo.customVersions.map((version) => {
return (
<option key={JSON.stringify(version.version)} value={JSON.stringify(version.version)}>
{version.displayName}
</option>
)
})}
</>
)
if (moduleInfo.stableVersion)
choices.push({
value: JSON.stringify(moduleInfo.stableVersion.version),
label: moduleInfo.stableVersion.displayName,
})

if (moduleInfo.prereleaseVersion)
choices.push({
value: JSON.stringify(moduleInfo.prereleaseVersion.version),
label: moduleInfo.prereleaseVersion.displayName,
})

for (const version of moduleInfo.releaseVersions) {
choices.push({ value: JSON.stringify(version.version), label: version.displayName })
}

for (const version of moduleInfo.customVersions) {
choices.push({ value: JSON.stringify(version.version), label: version.displayName })
}

return choices
}
8 changes: 6 additions & 2 deletions webui/src/Connections/ConnectionEditPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
NewClientModuleInfo,
NewClientModuleVersionInfo2,
} from '@companion-app/shared/Model/ModuleInfo.js'
import { ConnectionVersionSelectOptions } from './AddConnection.js'
import { getConnectionVersionSelectOptions } from './AddConnection.js'
import { getModuleVersionInfoForConnection } from './Util.js'

interface ConnectionEditPanelProps {
Expand Down Expand Up @@ -263,7 +263,11 @@ const ConnectionEditPanelInner = observer(function ConnectionEditPanelInner({
: 'Select the version of the module to use for this connection'
}
>
<ConnectionVersionSelectOptions moduleInfo={moduleInfo} />
{getConnectionVersionSelectOptions(moduleInfo).map((v) => (
<option key={v.value} value={v.value}>
{v.label}
</option>
))}
</CFormSelect>
</CCol>

Expand Down

0 comments on commit 64aa6b5

Please sign in to comment.