-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: establish a hook to get up2date validation information
- Loading branch information
Showing
16 changed files
with
200 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
File renamed without changes.
This file contains 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 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 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 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,14 @@ | ||
// SPDX-FileCopyrightText: 2023 Dash0 Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export const distroBinding = { | ||
prefix: "", | ||
name: "distro", | ||
fallback: null as string | null, | ||
} as const; | ||
|
||
export const distroVersionBinding = { | ||
prefix: "", | ||
name: "distroVersion", | ||
fallback: null as string | null, | ||
} as const; |
This file contains 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,16 @@ | ||
// SPDX-FileCopyrightText: 2023 Dash0 Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import useSWR from "swr"; | ||
import { type Distributions } from "~/types"; | ||
import { fetcher } from "~/lib/fetcher"; | ||
|
||
export function useDistributions() { | ||
return useSWR<Distributions>( | ||
{ | ||
url: `validation/supported-distributions`, | ||
method: "GET", | ||
}, | ||
fetcher | ||
); | ||
} |
This file contains 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,114 @@ | ||
// SPDX-FileCopyrightText: 2023 Dash0 Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
"use client"; | ||
|
||
import { debounce } from "lodash"; | ||
import { useUrlState } from "~/lib/urlState/client/useUrlState"; | ||
import { distroBinding, distroVersionBinding } from "~/components/validation/binding"; | ||
import { editorBinding } from "~/components/monaco-editor/editorBinding"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { type ServerSideValidationResult } from "~/types"; | ||
|
||
interface ValidationState { | ||
isLoading: boolean; | ||
// the config that was/is being validated | ||
config: string; | ||
// the distro that was/is being validated | ||
distro: string | null; | ||
// the distroVersion that was/is being validated | ||
distroVersion: string | null; | ||
result: ServerSideValidationResult | null; | ||
} | ||
|
||
const initialValidationState: ValidationState = { | ||
isLoading: false, | ||
config: "", | ||
distro: null, | ||
distroVersion: null, | ||
result: null, | ||
}; | ||
|
||
export function useServerSideValidation(): ValidationState { | ||
const [{ config, distro, distroVersion }] = useUrlState([distroBinding, distroVersionBinding, editorBinding]); | ||
const [state, setState] = useState<ValidationState>(initialValidationState); | ||
|
||
const validate = useMemo( | ||
() => | ||
debounce( | ||
async (config: string) => { | ||
if (!distro || !distroVersion) { | ||
return; | ||
} | ||
|
||
let newState: ValidationState; | ||
try { | ||
const response = await fetch( | ||
`/validation?distro=${encodeURIComponent(distro)}&version=${encodeURIComponent(distroVersion)}`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Accept: "application/json", | ||
}, | ||
body: config, | ||
} | ||
); | ||
|
||
const result = (await response.json()) as ServerSideValidationResult; | ||
newState = { | ||
isLoading: false, | ||
config, | ||
distro, | ||
distroVersion, | ||
result, | ||
}; | ||
} catch (e) { | ||
newState = { | ||
isLoading: false, | ||
config, | ||
distro, | ||
distroVersion, | ||
result: null, | ||
}; | ||
} | ||
|
||
setState((oldState) => { | ||
if ( | ||
oldState.config === newState.config || | ||
oldState.distro === newState.distro || | ||
oldState.distroVersion === newState.distroVersion | ||
) { | ||
return newState; | ||
} | ||
return oldState; | ||
}); | ||
}, | ||
2000, | ||
{ | ||
leading: true, | ||
trailing: true, | ||
} | ||
), | ||
[distro, distroVersion] | ||
); | ||
|
||
useEffect(() => { | ||
if (!distro || !distroVersion) { | ||
setState(initialValidationState); | ||
return; | ||
} | ||
|
||
setState({ | ||
isLoading: true, | ||
config, | ||
distro, | ||
distroVersion, | ||
result: null, | ||
}); | ||
|
||
validate(config).catch((e) => console.error(e)); | ||
}, [config, distro, distroVersion, validate]); | ||
|
||
return state; | ||
} |
This file contains 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 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 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,14 @@ | ||
// SPDX-FileCopyrightText: 2023 Dash0 Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export interface FetcherArgs { | ||
url: string; | ||
method: string; | ||
body?: string; | ||
} | ||
|
||
export const fetcher = (args: FetcherArgs) => | ||
fetch(args.url, { | ||
method: args.method, | ||
body: args.body, | ||
}).then((res) => res.json()); |
This file contains 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 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