-
Notifications
You must be signed in to change notification settings - Fork 943
feat: create dynamic parameter component #17351
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1d32dbe
feat: create dynamic parameter component
jaaydenh 5b1d5b4
fix: format
jaaydenh 4516af2
chore: cleanup, update validation
jaaydenh 5784127
chore: update for types from typesGenerated
jaaydenh dd5147d
fix: remove filters
jaaydenh 3b8d5a5
chore: remove unused typesParameter.ts
jaaydenh 25c7d3c
fix: use options instead of defaultOptions to set option values
jaaydenh 1976720
fix: updates for PR review
jaaydenh 2931256
fix: format
jaaydenh 07632b8
fix: update to use useDebouncedFunction
jaaydenh b7d0d32
fix: remove websocket code
jaaydenh 2a35fe2
chore: updates for PR review
jaaydenh fa3caf1
Merge branch 'main' into jaaydenh/dynamic-parameter-component
jaaydenh 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
Next
Next commit
feat: create dynamic parameter component
- Loading branch information
commit 1d32dbe8595579be32144e7543e55eaafdab9be2
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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Code generated by 'guts'. DO NOT EDIT. | ||
|
||
// From types/diagnostics.go | ||
export type DiagnosticSeverityString = "error" | "warning"; | ||
|
||
export const DiagnosticSeverityStrings: DiagnosticSeverityString[] = [ | ||
"error", | ||
"warning", | ||
]; | ||
|
||
// From types/diagnostics.go | ||
export type Diagnostics = readonly FriendlyDiagnostic[]; | ||
|
||
// From types/diagnostics.go | ||
export interface FriendlyDiagnostic { | ||
readonly severity: DiagnosticSeverityString; | ||
readonly summary: string; | ||
readonly detail: string; | ||
} | ||
|
||
// From types/value.go | ||
export interface NullHCLString { | ||
readonly value: string; | ||
readonly valid: boolean; | ||
} | ||
|
||
// From types/parameter.go | ||
export interface Parameter extends ParameterData { | ||
readonly value: NullHCLString; | ||
readonly diagnostics: Diagnostics; | ||
} | ||
|
||
// From types/parameter.go | ||
export interface ParameterData { | ||
readonly name: string; | ||
readonly display_name: string; | ||
readonly description: string; | ||
readonly type: ParameterType; | ||
// this is likely an enum in an external package "github.com/coder/terraform-provider-coder/v2/provider.ParameterFormType" | ||
readonly form_type: string; | ||
// empty interface{} type, falling back to unknown | ||
readonly styling: unknown; | ||
readonly mutable: boolean; | ||
readonly default_value: NullHCLString; | ||
readonly icon: string; | ||
readonly options: readonly ParameterOption[]; | ||
readonly validations: readonly ParameterValidation[]; | ||
readonly required: boolean; | ||
readonly order: number; | ||
readonly ephemeral: boolean; | ||
} | ||
|
||
// From types/parameter.go | ||
export interface ParameterOption { | ||
readonly name: string; | ||
readonly description: string; | ||
readonly value: NullHCLString; | ||
readonly icon: string; | ||
} | ||
|
||
// From types/enum.go | ||
export type ParameterType = "bool" | "list(string)" | "number" | "string"; | ||
|
||
export const ParameterTypes: ParameterType[] = [ | ||
"bool", | ||
"list(string)", | ||
"number", | ||
"string", | ||
]; | ||
|
||
// From types/parameter.go | ||
export interface ParameterValidation { | ||
readonly validation_error: string; | ||
readonly validation_regex: string | null; | ||
readonly validation_min: number | null; | ||
readonly validation_max: number | null; | ||
readonly validation_monotonic: string | null; | ||
readonly validation_invalid: boolean | null; | ||
} | ||
|
||
// From web/session.go | ||
export interface Request { | ||
readonly id: number; | ||
readonly inputs: Record<string, string>; | ||
} | ||
|
||
// From web/session.go | ||
export interface Response { | ||
readonly id: number; | ||
readonly diagnostics: Diagnostics; | ||
readonly parameters: readonly Parameter[]; | ||
} | ||
|
||
// From web/session.go | ||
export interface SessionInputs { | ||
readonly PlanPath: string; | ||
readonly User: WorkspaceOwner; | ||
} | ||
|
||
// From types/parameter.go | ||
export const ValidationMonotonicDecreasing = "decreasing"; | ||
|
||
// From types/parameter.go | ||
export const ValidationMonotonicIncreasing = "increasing"; | ||
|
||
// From types/owner.go | ||
export interface WorkspaceOwner { | ||
readonly id: string; | ||
readonly name: string; | ||
readonly full_name: string; | ||
readonly email: string; | ||
readonly ssh_public_key: string; | ||
readonly groups: readonly string[]; | ||
readonly session_token: string; | ||
readonly oidc_access_token: string; | ||
readonly login_type: string; | ||
readonly rbac_roles: readonly WorkspaceOwnerRBACRole[]; | ||
} | ||
|
||
// From types/owner.go | ||
export interface WorkspaceOwnerRBACRole { | ||
readonly name: string; | ||
readonly org_id: string; | ||
} |
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
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
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// This file is temporary until we have a proper websocket implementation for dynamic parameters | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
|
||
export function useWebSocket<T>( | ||
url: string, | ||
testdata: string, | ||
user: string, | ||
plan: string, | ||
) { | ||
B41A | const [message, setMessage] = useState<T | null>(null); | |
const [connectionStatus, setConnectionStatus] = useState< | ||
"connecting" | "connected" | "disconnected" | ||
>("connecting"); | ||
const wsRef = useRef<WebSocket | null>(null); | ||
const urlRef = useRef(url); | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const connectWebSocket = useCallback(() => { | ||
try { | ||
const ws = new WebSocket(urlRef.current); | ||
wsRef.current = ws; | ||
setConnectionStatus("connecting"); | ||
|
||
ws.onopen = () => { | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// console.log("Connected to WebSocket"); | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setConnectionStatus("connected"); | ||
ws.send(JSON.stringify({})); | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
ws.onmessage = (event) => { | ||
try { | ||
const data: T = JSON.parse(event.data); | ||
// console.log("Received message:", data); | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setMessage(data); | ||
} catch (err) { | ||
console.error("Invalid JSON from server: ", event.data); | ||
console.error("Error: ", err); | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}; | ||
|
||
ws.onerror = (event) => { | ||
console.error("WebSocket error:", event); | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
ws.onclose = (event) => { | ||
// console.log( | ||
// `WebSocket closed with code ${event.code}. Reason: ${event.reason}`, | ||
// ); | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setConnectionStatus("disconnected"); | ||
}; | ||
} catch (error) { | ||
console.error("Failed to create WebSocket connection:", error); | ||
setConnectionStatus("disconnected"); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!testdata) { | ||
return; | ||
} | ||
|
||
setMessage(null); | ||
setConnectionStatus("connecting"); | ||
|
||
const createConnection = () => { | ||
urlRef.current = url; | ||
connectWebSocket(); | ||
}; | ||
|
||
if (wsRef.current) { | ||
wsRef.current.close(); | ||
wsRef.current = null; | ||
} | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const timeoutId = setTimeout(createConnection, 100); | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return () => { | ||
clearTimeout(timeoutId); | ||
if (wsRef.current) { | ||
wsRef.current.close(); | ||
wsRef.current = null; | ||
} | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
}, [testdata, connectWebSocket, url]); | ||
|
||
const sendMessage = (data: unknown) => { | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) { | ||
wsRef.current.send(JSON.stringify(data)); | ||
} else { | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.warn("Cannot send message: WebSocket is not connected"); | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}; | ||
|
||
return { message, sendMessage, connectionStatus }; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.