-
Notifications
You must be signed in to change notification settings - Fork 2
Instance name and description editing #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Luxxy-GF
wants to merge
9
commits into
hyecompany:yerek
Choose a base branch
from
Luxxy-GF:Instance-Name-and-Description-Editing
base: yerek
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e95f9b1
feat: Implement inline editing for instance name and description, inc…
Luxxy-GF d892a2c
deps update
Luxxy-GF 5cb61ff
Update app/(main)/instance/layout.tsx
Luxxy-GF 47f922a
Refine instance UI constants
Luxxy-GF d6ca470
Merge pull request #1 from Luxxy-GF/codex/fix-issues-in-pull-request-85
Luxxy-GF d800fd1
remove rename toast (event handler sends notification already)
Hye-Dev df7dc6e
Use events socket for operation completion
Luxxy-GF 732ea73
Merge branch 'Instance-Name-and-Description-Editing' into codex/fix-i…
Luxxy-GF 92389ae
Merge pull request #6 from Luxxy-GF/codex/fix-issues-in-pull-request-…
Luxxy-GF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| import { Instance } from '../../instances/_lib/instances.d'; | ||
|
|
||
| const OPERATION_POLL_MAX_ATTEMPTS = 20; | ||
| const OPERATION_POLL_DELAY_MS = 500; | ||
|
|
||
| export type InstanceAction = 'start' | 'stop' | 'restart' | 'freeze'; | ||
|
|
||
| export async function performInstanceAction({ | ||
|
|
@@ -34,3 +37,184 @@ export async function performInstanceAction({ | |
| ); | ||
| } | ||
| } | ||
|
|
||
| export async function updateInstance({ | ||
| name, | ||
| description, | ||
| project, | ||
| }: { | ||
| name: string; | ||
| description: string; | ||
| project?: string; | ||
| }) { | ||
| const projectSuffix = project | ||
| ? `?project=${encodeURIComponent(project)}` | ||
| : ''; | ||
| const res = await fetch( | ||
| `/1.0/instances/${encodeURIComponent(name)}${projectSuffix}`, | ||
| { | ||
| method: 'PATCH', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ | ||
| description, | ||
| }), | ||
| }, | ||
| ); | ||
| if (!res.ok) { | ||
| const payload = await res.json().catch(() => ({ error: res.statusText })); | ||
| throw new Error(payload?.error || `Unable to update instance ${name}`); | ||
| } | ||
| } | ||
|
|
||
| export async function renameInstance({ | ||
| name, | ||
| newName, | ||
| project, | ||
| }: { | ||
| name: string; | ||
| newName: string; | ||
| project?: string; | ||
| }) { | ||
| const projectSuffix = project | ||
| ? `?project=${encodeURIComponent(project)}` | ||
| : ''; | ||
| const res = await fetch( | ||
| `/1.0/instances/${encodeURIComponent(name)}${projectSuffix}`, | ||
| { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ | ||
| name: newName, | ||
| }), | ||
| }, | ||
| ); | ||
| if (!res.ok) { | ||
| const payload = await res.json().catch(() => ({ error: res.statusText })); | ||
| throw new Error(payload?.error || `Unable to rename instance ${name}`); | ||
| } | ||
|
|
||
| const data = await res.json(); | ||
| if (data.type === 'async' && data.operation) { | ||
| await waitForOperation(data.operation); | ||
| } | ||
| } | ||
|
|
||
| async function waitForOperation(operationUrl: string) { | ||
| // Poll the operation URL until it's done | ||
| for (let i = 0; i < OPERATION_POLL_MAX_ATTEMPTS; i++) { | ||
| const res = await fetch(operationUrl); | ||
| if (!res.ok) { | ||
| // If we can't check the operation, assume it failed or network issue | ||
| throw new Error('Failed to check operation status'); | ||
| } | ||
| const data = await res.json(); | ||
| // Operation status: Running, Pending, Success, Failure, Cancelled | ||
| if (data.metadata?.status === 'Success') { | ||
| return; | ||
| } | ||
| if (data.metadata?.status === 'Failure') { | ||
| throw new Error(data.metadata.err || 'Operation failed'); | ||
| } | ||
| if (data.metadata?.status === 'Cancelled') { | ||
| throw new Error('Operation cancelled'); | ||
| } | ||
|
|
||
| // Wait before next poll | ||
| await new Promise((resolve) => setTimeout(resolve, OPERATION_POLL_DELAY_MS)); | ||
| } | ||
|
|
||
| const operationId = operationUrl.split('/').pop() ?? operationUrl; | ||
| const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'; | ||
| const socketUrl = `${protocol}://${window.location.host}/1.0/events?type=operation&operation=${encodeURIComponent( | ||
| operationId, | ||
| )}`; | ||
|
|
||
| await new Promise<void>((resolve, reject) => { | ||
| let settled = false; | ||
| const socket = new WebSocket(socketUrl); | ||
| const timeoutId = window.setTimeout(() => { | ||
| if (settled) return; | ||
| settled = true; | ||
| socket.close(); | ||
| reject(new Error('Operation timed out')); | ||
| }, OPERATION_EVENT_TIMEOUT_MS); | ||
|
|
||
| const cleanup = () => { | ||
| window.clearTimeout(timeoutId); | ||
| socket.onopen = null; | ||
| socket.onmessage = null; | ||
| socket.onerror = null; | ||
| socket.onclose = null; | ||
| }; | ||
|
|
||
| socket.onerror = () => { | ||
| if (settled) return; | ||
| settled = true; | ||
| cleanup(); | ||
| reject(new Error('Failed to check operation status')); | ||
| }; | ||
|
|
||
| socket.onclose = () => { | ||
| if (settled) return; | ||
| settled = true; | ||
| cleanup(); | ||
| reject(new Error('Operation socket closed before completion')); | ||
| }; | ||
|
|
||
| socket.onmessage = (event) => { | ||
| try { | ||
| const data = JSON.parse(event.data) as OperationEvent; | ||
|
||
| if (data.type !== 'operation') { | ||
| return; | ||
| } | ||
|
|
||
| const metadata = data.metadata; | ||
| if (!metadata?.status) { | ||
| return; | ||
| } | ||
|
|
||
| if (metadata.id && metadata.id !== operationId) { | ||
| return; | ||
| } | ||
|
|
||
| if (metadata.status === 'Success') { | ||
| if (settled) return; | ||
| settled = true; | ||
| cleanup(); | ||
| socket.close(); | ||
| resolve(); | ||
| } | ||
|
|
||
| if (metadata.status === 'Failure') { | ||
| if (settled) return; | ||
| settled = true; | ||
| cleanup(); | ||
| socket.close(); | ||
| reject(new Error(metadata.err || 'Operation failed')); | ||
| } | ||
|
|
||
| if (metadata.status === 'Cancelled') { | ||
| if (settled) return; | ||
| settled = true; | ||
| cleanup(); | ||
| socket.close(); | ||
| reject(new Error('Operation cancelled')); | ||
| } | ||
| } catch (error) { | ||
| if (settled) return; | ||
| settled = true; | ||
| cleanup(); | ||
| socket.close(); | ||
| reject( | ||
| error instanceof Error | ||
| ? error | ||
| : new Error('Failed to parse operation status'), | ||
| ); | ||
| } | ||
| }; | ||
| }); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constant
OPERATION_EVENT_TIMEOUT_MSis referenced but not defined anywhere in this file. This will cause a ReferenceError at runtime when the WebSocket fallback is triggered.