Skip to content

Commit

Permalink
type WidgetExampleAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
mishig25 committed Nov 2, 2023
1 parent 2a7c9c9 commit 1b94b66
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
4 changes: 4 additions & 0 deletions js/src/lib/components/InferenceWidget/shared/WidgetExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ export type WidgetExample<TOutput = WidgetExampleOutput> =
| WidgetExampleTableDataInput<TOutput>
| WidgetExampleZeroShotTextInput<TOutput>
| WidgetExampleSentenceSimilarityInput<TOutput>;

type KeysOfUnion<T> = T extends any ? keyof T : never;

export type WidgetExampleAttribute = KeysOfUnion<WidgetExample>;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import type { WidgetProps, ModelLoadInfo, ExampleRunOpts } from "../types";
import type { WidgetExample } from "../WidgetExample";
import type { QueryParam } from "../../shared/helpers";
import type { WidgetExample, WidgetExampleAttribute } from "../WidgetExample";
type TWidgetExample = $$Generic<WidgetExample>;
Expand Down Expand Up @@ -33,7 +32,7 @@
export let outputJson: string;
export let applyInputSample: (sample: TWidgetExample, opts?: ExampleRunOpts) => void = () => {};
export let validateExample: (sample: WidgetExample) => sample is TWidgetExample;
export let exampleQueryParams: QueryParam[] = [];
export let exampleQueryParams: WidgetExampleAttribute[] = [];
let isDisabled = model.inference !== InferenceDisplayability.Yes && model.pipeline_tag !== "reinforcement-learning";
let isMaximized = false;
Expand Down
12 changes: 5 additions & 7 deletions js/src/lib/components/InferenceWidget/shared/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import type { ModelData } from "../../../interfaces/Types";
import { randomItem, parseJSON } from "../../../utils/ViewUtils";
import type { WidgetExample } from "./WidgetExample";
import type { WidgetExample, WidgetExampleAttribute } from "./WidgetExample";
import type { ModelLoadInfo, TableData } from "./types";
import { LoadState } from "./types";

type KeysOfUnion<T> = T extends any ? keyof T : never;
export type QueryParam = KeysOfUnion<WidgetExample>;
const KEYS_TEXT: WidgetExampleAttribute[] = ["text", "context", "candidate_labels"];
const KEYS_TABLE: WidgetExampleAttribute[] = ["table", "structured_data"];
type QueryParamVal = string | null | boolean | (string | number)[][];
const KEYS_TEXT: QueryParam[] = ["text", "context", "candidate_labels"];
const KEYS_TABLE: QueryParam[] = ["table", "structured_data"];

export function getQueryParamVal(key: QueryParam): QueryParamVal {
export function getQueryParamVal(key: WidgetExampleAttribute): QueryParamVal {
const searchParams = new URL(window.location.href).searchParams;
const value = searchParams.get(key);
if (KEYS_TEXT.includes(key)) {
Expand All @@ -35,7 +33,7 @@ export function getWidgetExample<TWidgetExample extends WidgetExample>(
}

// Update current url search params, keeping existing keys intact.
export function updateUrl(obj: Partial<Record<QueryParam, string | undefined>>): void {
export function updateUrl(obj: Partial<Record<WidgetExampleAttribute, string | undefined>>): void {
if (!window) {
return;
}
Expand Down

0 comments on commit 1b94b66

Please sign in to comment.