-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Implement instance configuration page #88
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
3
commits into
hyecompany:yerek
Choose a base branch
from
Luxxy-GF:Instance-Configuration-Page
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
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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,11 +1,144 @@ | ||
| 'use client'; | ||
|
|
||
| import React from 'react'; | ||
| import React, { useEffect, useState } from 'react'; | ||
| import { SaveIcon } from 'lucide-react'; | ||
| import GeneralConfiguration from '@/app/(main)/instances/_components/general-configuration'; | ||
| import { Button } from 'ui-web/components/button'; | ||
| import { Spinner } from 'ui-web/components/spinner'; | ||
| import { toast } from 'sonner'; | ||
| import { updateInstance } from '../_lib/instance'; | ||
| import { useInstanceContext } from '../_context/instance'; | ||
|
|
||
| function hasConfigChanged( | ||
| current: Record<string, string>, | ||
| initial: Record<string, string>, | ||
| ) { | ||
| const keys = new Set([...Object.keys(current), ...Object.keys(initial)]); | ||
|
|
||
| for (const key of keys) { | ||
| const hasCurrent = Object.prototype.hasOwnProperty.call(current, key); | ||
| const hasInitial = Object.prototype.hasOwnProperty.call(initial, key); | ||
|
|
||
| if (hasCurrent !== hasInitial) { | ||
| return true; | ||
| } | ||
|
|
||
| if (hasCurrent && hasInitial && current[key] !== initial[key]) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| function buildConfigChanges( | ||
| current: Record<string, string>, | ||
| initial: Record<string, string>, | ||
| ) { | ||
| const changes: Record<string, string | null> = {}; | ||
| const keys = new Set([...Object.keys(current), ...Object.keys(initial)]); | ||
|
|
||
| for (const key of keys) { | ||
| const hasCurrent = Object.prototype.hasOwnProperty.call(current, key); | ||
| const hasInitial = Object.prototype.hasOwnProperty.call(initial, key); | ||
|
|
||
| if (hasCurrent && (!hasInitial || current[key] !== initial[key])) { | ||
| changes[key] = current[key]; | ||
| continue; | ||
| } | ||
|
|
||
| if (!hasCurrent && hasInitial) { | ||
| changes[key] = null; | ||
| } | ||
| } | ||
|
|
||
| return changes; | ||
| } | ||
|
|
||
| export default function ConfigurationPage() { | ||
| const { instance, isLoading, isError, mutate } = useInstanceContext(); | ||
| const [config, setConfig] = useState<Record<string, string>>({}); | ||
| const [initialConfig, setInitialConfig] = useState<Record<string, string>>( | ||
| {}, | ||
| ); | ||
| const [isSaving, setIsSaving] = useState(false); | ||
| const [isDirty, setIsDirty] = useState(false); | ||
|
|
||
| // Initialize config from instance data | ||
| useEffect(() => { | ||
| const nextConfig = { ...(instance?.config ?? {}) }; | ||
| setConfig(nextConfig); | ||
| setInitialConfig({ ...nextConfig }); | ||
| setIsDirty(false); | ||
| }, [instance]); | ||
|
|
||
| const handleConfigChange = (newConfig: Record<string, string>) => { | ||
| setConfig(newConfig); | ||
| setIsDirty(hasConfigChanged(newConfig, initialConfig)); | ||
| }; | ||
|
|
||
| const handleSave = async () => { | ||
| if (!instance) return; | ||
|
|
||
| const changes = buildConfigChanges(config, initialConfig); | ||
| if (Object.keys(changes).length === 0) { | ||
| setIsDirty(false); | ||
| return; | ||
| } | ||
|
|
||
| setIsSaving(true); | ||
| try { | ||
| await updateInstance(instance.name, changes, instance.project ?? null); | ||
| toast.success('Instance configuration updated'); | ||
| setInitialConfig({ ...config }); | ||
| setIsDirty(false); | ||
| // Revalidate instance data | ||
| mutate(); | ||
| } catch (error) { | ||
| toast.error( | ||
| error instanceof Error ? error.message : 'Failed to update configuration', | ||
| ); | ||
| } finally { | ||
| setIsSaving(false); | ||
| } | ||
| }; | ||
|
|
||
| if (isLoading) { | ||
| return ( | ||
| <div className="flex justify-center p-8"> | ||
| <Spinner /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| if (isError || !instance) { | ||
| return ( | ||
| <div className="p-4 text-center text-destructive"> | ||
| Failed to load instance configuration. | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="rounded-lg border border-dashed p-8 text-center text-muted-foreground"> | ||
| Instance Configuration Under Construction | ||
| <div className="h-full flex flex-col space-y-4"> | ||
| <div className="flex justify-end"> | ||
| <Button onClick={handleSave} disabled={!isDirty || isSaving}> | ||
| {isSaving ? ( | ||
| <Spinner className="mr-2 h-4 w-4" /> | ||
| ) : ( | ||
| <SaveIcon className="mr-2 h-4 w-4" /> | ||
| )} | ||
| Save Changes | ||
| </Button> | ||
| </div> | ||
| <div className="flex-1 border rounded-md overflow-hidden bg-background"> | ||
| <GeneralConfiguration | ||
| config={config} | ||
| expandedConfig={instance.expanded_config} | ||
| onConfigChange={handleConfigChange} | ||
| instanceType={instance.type as 'container' | 'virtual-machine'} | ||
| /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
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
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 type assertion
instance.type as 'container' | 'virtual-machine'may fail silently if the instance type is an unexpected value. While the Instance interface allowstype: 'container' | 'virtual-machine' | string, the GeneralConfiguration component expects only the two specific types.Consider adding validation or a fallback:
instanceType={(instance.type === 'virtual-machine' ? 'virtual-machine' : 'container')}