Skip to content

Commit

Permalink
refactor: remove unused ws messages
Browse files Browse the repository at this point in the history
  • Loading branch information
m8vago committed Nov 26, 2024
1 parent 76bc1de commit 2a6dc53
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface DeploymentContainerStatusListProps {
progress: Record<string, ContainerProgress>
}

type ContainerWithInstance = Container & {
type ContainerWithConfigId = Container & {
configId: string
}

Expand All @@ -44,7 +44,7 @@ const DeploymentContainerStatusList = (props: DeploymentContainerStatusListProps

const now = utcNow()

const [containers, setContainers] = useState<ContainerWithInstance[]>(() =>
const [containers, setContainers] = useState<ContainerWithConfigId[]>(() =>
deployment.instances.map(it => ({
configId: it.config.id,
id: {
Expand Down Expand Up @@ -73,7 +73,7 @@ const DeploymentContainerStatusList = (props: DeploymentContainerStatusListProps
} as WatchContainerStatusMessage),
})

const merge = (weak: ContainerWithInstance[], strong: Container[]): ContainerWithInstance[] => {
const merge = (weak: ContainerWithConfigId[], strong: Container[]): ContainerWithConfigId[] => {
if (!strong || strong.length === 0) {
return weak
}
Expand Down Expand Up @@ -121,7 +121,7 @@ const DeploymentContainerStatusList = (props: DeploymentContainerStatusListProps
/>
<DyoColumn
className="w-4/12"
body={(it: ContainerWithInstance) =>
body={(it: ContainerWithConfigId) =>
progress[it.configId]?.progress < 1 ? (
<DyoProgress progress={progress[it.configId].progress} text={`${it.imageName}:${it.imageTag}`} />
) : (
Expand All @@ -136,7 +136,7 @@ const DeploymentContainerStatusList = (props: DeploymentContainerStatusListProps
/>
<DyoColumn
className="w-24 text-center"
body={(it: ContainerWithInstance) => (
body={(it: ContainerWithConfigId) => (
<>
{it.state && (
<div className="inline-block mr-2">
Expand Down
100 changes: 0 additions & 100 deletions web/crux-ui/src/components/deployments/instances/use-instance-state.ts

This file was deleted.

160 changes: 3 additions & 157 deletions web/crux-ui/src/components/deployments/use-deployment-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import usePersistedViewMode from '@app/hooks/use-persisted-view-mode'
import useTeamRoutes from '@app/hooks/use-team-routes'
import useWebSocket from '@app/hooks/use-websocket'
import {
ConcreteContainerConfigData,
DeploymentDetails,
DeploymentInvalidatedSecrets,
deploymentIsCopiable,
deploymentIsDeletable,
deploymentIsDeployable,
Expand All @@ -21,14 +19,12 @@ import {
ImageDeletedMessage,
Instance,
instanceCreatedMessageToInstance,
InstanceMessage,
InstancesAddedMessage,
NodeEventMessage,
ProjectDetails,
VersionDetails,
WebSocketSaveState,
WS_TYPE_IMAGE_DELETED,
WS_TYPE_INSTANCE,
WS_TYPE_INSTANCES_ADDED,
WS_TYPE_NODE_EVENT,
} from '@app/models'
Expand Down Expand Up @@ -68,38 +64,23 @@ export type DeploymentState = {
export type DeploymentActions = {
setEditState: (state: DeploymentEditState) => void
onDeploymentEdited: (editedDeployment: DeploymentDetails) => void
onPatchInstance: (id: string, newConfig: ConcreteContainerConfigData) => void
updateInstanceConfig: (id: string, newConfig: ConcreteContainerConfigData) => void
setViewMode: (viewMode: ViewMode) => void
onInvalidateSecrets: (secrets: DeploymentInvalidatedSecrets[]) => void
onDeploymentTokenCreated: (token: DeploymentToken) => void
onRevokeDeploymentToken: VoidFunction
onInstanceSelected: (id: string, deploy: boolean) => void
onAllInstancesToggled: (deploy: boolean) => void
}

// const mergeInstancePatch = (instance: Instance, message: InstanceUpdatedMessage): Instance => ({
// ...instance,
// config: {
// ...instance.config,
// ...message,
// },
// })

const useDeploymentState = (options: DeploymentStateOptions): [DeploymentState, DeploymentActions] => {
const { t } = useTranslation('deployments')
const routes = useTeamRoutes()

const { deployment: optionDeploy, onWsError, onApiError } = options
const { project, version } = optionDeploy

// const throttle = useThrottling(DEPLOYMENT_EDIT_WS_REQUEST_DELAY)

// const patch = useRef<Partial<ConcreteContainerConfigData>>({})

const [deployment, setDeployment] = useState<DeploymentDetails>(optionDeploy)
const [node, setNode] = useNodeState(optionDeploy.node)
const [saveState] = useState<WebSocketSaveState>(null)
const [saveState, setSaveState] = useState<WebSocketSaveState>('disconnected')
const [editState, setEditState] = useState<DeploymentEditState>('details')
const [instances, setInstances] = useState<Instance[]>(deployment.instances ?? [])
const [viewMode, setViewMode] = usePersistedViewMode({ initialViewMode: 'list', pageName: 'deployments' })
Expand All @@ -126,50 +107,13 @@ const useDeploymentState = (options: DeploymentStateOptions): [DeploymentState,
})

const sock = useWebSocket(routes.deployment.detailsSocket(deployment.id), {
// onOpen: () => setSaveState('connected'),
// onClose: () => setSaveState('disconnected'),
// onSend: message => {
// if ([WS_TYPE_PATCH_INSTANCE, WS_TYPE_PATCH_DEPLOYMENT_ENV].includes(message.type)) {
// setSaveState('saving')
// }
// },
// onReceive: message => {
// if (WS_TYPE_PATCH_RECEIVED === message.type) {
// setSaveState('saved')
// }
// },
onOpen: () => setSaveState('connected'),
onClose: () => setSaveState('disconnected'),
onError: onWsError,
})

const editor = useEditorState(sock)

// sock.on(WS_TYPE_DEPLOYMENT_ENV_UPDATED, (message: DeploymentEnvUpdatedMessage) => {
// setDeployment({
// ...deployment,
// ...message,
// })
// })

// sock.on(WS_TYPE_INSTANCE_UPDATED, (message: InstanceUpdatedMessage) => {
// const index = instances.findIndex(it => it.id === message.instanceId)
// if (index < 0) {
// sock.send(WS_TYPE_GET_INSTANCE, {
// id: message.instanceId,
// } as GetInstanceMessage)
// return
// }

// const oldOne = instances[index]
// const instance = mergeInstancePatch(oldOne, message)

// const newInstances = [...instances]
// newInstances[index] = instance

// setInstances(newInstances)
// })

sock.on(WS_TYPE_INSTANCE, (message: InstanceMessage) => setInstances([...instances, message]))

sock.on(WS_TYPE_INSTANCES_ADDED, (message: InstancesAddedMessage) =>
setInstances([...instances, ...message.map(it => instanceCreatedMessageToInstance(it))]),
)
Expand All @@ -183,101 +127,6 @@ const useDeploymentState = (options: DeploymentStateOptions): [DeploymentState,
setEditState('details')
}

// const onEnvironmentEdited = environment => {
// setSaveState('saving')
// setDeployment({
// ...deployment,
// environment,
// })
// throttle(() => {
// sock.send(WS_TYPE_PATCH_DEPLOYMENT_ENV, {
// environment,
// })
// })
// }

const onInvalidateSecrets = (secrets: DeploymentInvalidatedSecrets[]) => {
const newInstances = instances.map(it => {
const invalidated = secrets.find(sec => sec.instanceId === it.id)
if (!invalidated) {
return it
}

return {
...it,
config: {
...it.config,
secrets: (it.config.secrets ?? []).map(secret => {
if (invalidated.invalid.includes(secret.id)) {
return {
...secret,
encrypted: false,
publicKey: '',
value: '',
}
}

return secret
}),
},
}
})

setInstances(newInstances)
}

const onPatchInstance = (_: string, __: ConcreteContainerConfigData) => {
// const onPatchInstance = (id: string, newConfig: ConcreteContainerConfigData) => {
// const index = instances.findIndex(it => it.id === id)
// if (index < 0) {
// return
// }
// setSaveState('saving')
// const newPatch = {
// ...patch.current,
// ...newConfig,
// }
// patch.current = newPatch
// const newInstances = [...instances]
// const instance = newInstances[index]
// newInstances[index] = {
// ...instance,
// config: {
// ...instance.config,
// ...newConfig,
// },
// }
// setInstances(newInstances)
// throttle(() => {
// sock.send(WS_TYPE_PATCH_INSTANCE, {
// instanceId: id,
// config: patch.current,
// } as PatchInstanceMessage)
// patch.current = {}
// })
}

const updateInstanceConfig = (_: string, __: ConcreteContainerConfigData) => {
// const updateInstanceConfig = (id: string, newConfig: ConcreteContainerConfigData) => {
// const index = instances.findIndex(it => it.id === id)
// if (index < 0) {
// return
// }
// setSaveState('saving')
// const newInstances = [...instances]
// const instance = newInstances[index]
// newInstances[index] = {
// ...instance,
// config: instance.config
// ? {
// ...instance.config,
// ...newConfig,
// }
// : newConfig,
// }
// setInstances(newInstances)
}

const onDeploymentTokenCreated = (token: DeploymentToken) => {
setDeployment({
...deployment,
Expand Down Expand Up @@ -348,13 +197,10 @@ const useDeploymentState = (options: DeploymentStateOptions): [DeploymentState,
setEditState,
onDeploymentEdited,
setViewMode,
onInvalidateSecrets,
onPatchInstance,
onDeploymentTokenCreated,
onRevokeDeploymentToken,
onInstanceSelected,
onAllInstancesToggled,
updateInstanceConfig,
},
]
}
Expand Down
5 changes: 4 additions & 1 deletion web/crux-ui/src/components/nodes/node-containers-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ const NodeContainersList = (props: NodeContainersListProps) => {
sortField={imageNameOfContainer}
sort={sortString}
bodyClassName="truncate"
body={(it: Container) => <span title={imageNameOfContainer(it)}>{imageNameOfContainer(it)}</span>}
body={(it: Container) => {
const name = imageNameOfContainer(it)
return <span title={name}>{name}</span>
}}
/>
<DyoColumn
header={t('common:state')}
Expand Down
Loading

0 comments on commit 2a6dc53

Please sign in to comment.