Skip to content

Commit

Permalink
fix: incorrect key length for binary type
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jul 28, 2024
1 parent b6ac26c commit 53306bc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 48 deletions.
24 changes: 12 additions & 12 deletions src/components/param/array.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ export type ArrayParamProps<T> = ParamProps<T[]> & {
array: ArrayVar;
};

export function ArrayParam<T>(props: ArrayParamProps<T>) {
if (
props.array.value.type === "primitive" &&
props.array.value.value === "u8"
) {
// @ts-expect-error TODO: Improve typing
return <BinaryParam onChangeValue={props.onChangeValue as unknown} />;
}

return <_ArrayParam {...props} />;
}

export function _ArrayParam<T>({
array: arrayVar,
onChangeValue,
Expand Down Expand Up @@ -55,15 +67,3 @@ export function _ArrayParam<T>({
</div>
);
}

export function ArrayParam<T>(props: ArrayParamProps<T>) {
if (
props.array.value.type === "primitive" &&
props.array.value.value === "u8"
) {
// @ts-expect-error TODO: Improve typing
return <BinaryParam onChangeValue={props.onChangeValue as unknown} />;
}

return <_ArrayParam {...props} />;
}
24 changes: 12 additions & 12 deletions src/components/param/sequence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export type SequenceParamProps<T> = ParamProps<T[]> & {
sequence: SequenceVar;
};

export function SequenceParam<T>({
sequence,
onChangeValue,
}: SequenceParamProps<T>) {
if (sequence.value.type === "primitive" && sequence.value.value === "u8") {
// @ts-expect-error TODO: improve typing
return <BinaryParam onChangeValue={onChangeValue} />;
}

return <_SequenceParam sequence={sequence} onChangeValue={onChangeValue} />;
}

function _SequenceParam<T>({ sequence, onChangeValue }: SequenceParamProps<T>) {
const [length, setLength] = useState(1);
const [values, setValues] = useState(
Expand Down Expand Up @@ -77,15 +89,3 @@ function _SequenceParam<T>({ sequence, onChangeValue }: SequenceParamProps<T>) {
</div>
);
}

export function SequenceParam<T>({
sequence,
onChangeValue,
}: SequenceParamProps<T>) {
if (sequence.value.type === "primitive" && sequence.value.value === "u8") {
// @ts-expect-error TODO: improve typing
return <BinaryParam onChangeValue={onChangeValue} />;
}

return <_SequenceParam sequence={sequence} onChangeValue={onChangeValue} />;
}
32 changes: 16 additions & 16 deletions src/components/param/tuple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ export type TupleParamProps<T extends Array<unknown>> = ParamProps<T> & {
tuple: TupleVar;
};

export function TupleParam<T extends Array<unknown>>(
props: TupleParamProps<T>,
) {
if (
props.tuple.value.every(
(lookupEntry) =>
lookupEntry.type === "primitive" && lookupEntry.value === "u8",
)
) {
// @ts-expect-error TODO: Improve typing
return <BinaryParam onChangeValue={props.onChangeValue as unknown} />;
}

return <_TupleParam {...props} />;
}

export function _TupleParam<T extends Array<unknown>>({
tuple: tupleVar,
onChangeValue,
Expand Down Expand Up @@ -48,19 +64,3 @@ export function _TupleParam<T extends Array<unknown>>({
</>
);
}

export function TupleParam<T extends Array<unknown>>(
props: TupleParamProps<T>,
) {
if (
props.tuple.value.every(
(lookupEntry) =>
lookupEntry.type === "primitive" && lookupEntry.value === "u8",
)
) {
// @ts-expect-error TODO: Improve typing
return <BinaryParam onChangeValue={props.onChangeValue as unknown} />;
}

return <_TupleParam {...props} />;
}
27 changes: 19 additions & 8 deletions src/components/storage-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,25 @@ function _StorageKey({ pallet, storage, onAddQuery }: StorageKeyProps) {

const [key, setKey] = useState<ParamInput<unknown>>(INCOMPLETE);

const maxKeyLength =
keyLookup === undefined
? undefined
: keyLookup.type === "tuple"
? keyLookup.value.length
: keyLookup.type === "array"
? keyLookup.len
: 1;
const maxKeyLength = useMemo(() => {
switch (keyLookup?.type) {
case undefined:
return undefined;
case "tuple":
return keyLookup.value.every(
(value) => value.type === "primitive" && value.value === "u8",
)
? 1
: keyLookup.value.length;
case "array":
return keyLookup.value.type === "primitive" &&
keyLookup.value.value === "u8"
? 1
: keyLookup.len;
default:
return 1;
}
}, [keyLookup]);

const [keyLength, setKeyLength] = useState(maxKeyLength ?? 0);

Expand Down

0 comments on commit 53306bc

Please sign in to comment.