Skip to content

Commit

Permalink
Use max depth as a var
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwalo32 committed Jan 15, 2024
1 parent a3b2274 commit bc2ae38
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ListItem from '@material-ui/core/ListItem';
import _ from 'lodash';
import React, { useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import { trackPromise } from 'react-promise-tracker';

import {
Expand Down Expand Up @@ -37,11 +37,6 @@ import { drawerWidth, verticalMovementWidth } from '../../styles/variables';
import { BaseStep, Field, Nullish, Role, Step, StepPathToField, Unsaved, UnsavedField } from '@3dp4me/types';
import { FormOption } from '../../components/Fields/FormOption';

const expandedSidebarWidth = `${
drawerWidth + 3 * verticalMovementWidth
}px`;
const retractedSidebarWidth = drawerWidth;

const SectionTab = () => {
const [stepMetadata, setStepMetadata] = useState<Step[]>([]);
const [originalStepMetadata, setOriginalStepMetadata] = useState<Step[]>([]);
Expand Down Expand Up @@ -159,8 +154,6 @@ const SectionTab = () => {

if (adjStepIndex < 0) return;

console.log("SWAP", updatedMetadata)

// Perform field number swap
swapValuesInArrayByKey(
updatedMetadata,
Expand Down
9 changes: 6 additions & 3 deletions packages/types/src/utils/fieldPath.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { DecreaseDepth } from "./path"
import { DecreaseDepth, MaxRecursionDepth } from "./path"

// We need to make decrease the depth twice as fast since we index twice in PathToField
type DoubleDecrease<T extends number> = DecreaseDepth<DecreaseDepth<T>>

/**
* Gives the path to a field in a step.
*/
export type StepPathToField = PathToField<`fields`>
export type PathToField<T extends string, D extends number = 5> =
export type PathToField<T extends string, D extends number = DoubleDecrease<MaxRecursionDepth>> =
D extends never ?
never
: T |
`${T}[${number}].subFields`
| PathToField<`${T}[${number}].subFields`, DecreaseDepth<D>>
| PathToField<`${T}[${number}].subFields`, DoubleDecrease<D>>
24 changes: 12 additions & 12 deletions packages/types/src/utils/path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Field, Signature, Step } from "src/models";
export type MaxRecursionDepth = 12;

/**
* Thet literal keys and nested keys of T. Nested fields are separated by a dot (.)
*
Expand All @@ -18,26 +19,26 @@ import { Field, Signature, Step } from "src/models";
*
* We have to limit the depth of the recursion to a reasonable amount, so we use the DecreaseDepth type
*/
export type Path<T, D extends number = 6> =
_Path<T, D> extends `.${infer P}` ?
P
: _Path<T, D>

type _Path<T, D extends number = 6> =
export type Path<T, D extends number = MaxRecursionDepth> =
D extends never ?
never
:
T extends (infer A)[] ?
`[${number}]` | `[${number}]${_Path<A, DecreaseDepth<D>>}`
`[${number}]` | `[${number}]${WithSeparator<A, Path<A, DecreaseDepth<D>>>}`
: T extends object
? { [K in keyof T]:
K extends string | number
? `.${K}`
| `.${K}${_Path<T[K], DecreaseDepth<D>>}`
? `${K}`
| `${K}${WithSeparator<T[K], Path<T[K], DecreaseDepth<D>>>}`
: never
}[keyof T]
: never;

type WithSeparator<T, P extends string> =
T extends any[] ?
P
: `.${P}`

/**
* Resolves to all types of the path P in object T.
* This is every possible value that can be stored in T
Expand All @@ -61,10 +62,9 @@ export type PathValue<T, P = Path<T>> =
: never
: never


/**
* This is just a helper type. In the below types, it's impossible to specify all paths for a recursive
* object. So this just acts as a recursion counter for us to limit the depth to a reasonable amount
*/
export type DecreaseDepth<T extends number> =
T extends 1 ? never : T extends 2 ? 1 : T extends 3 ? 2 : T extends 4 ? 3 : T extends 5 ? 4 : T extends 6 ? 5 : never;
T extends 1 ? never : T extends 2 ? 1 : T extends 3 ? 2 : T extends 4 ? 3 : T extends 5 ? 4 : T extends 6 ? 5 : T extends 7 ? 6 : T extends 8 ? 7 : T extends 9 ? 8 : T extends 10 ? 9 : T extends 11 ? 10 : T extends 12 ? 11 : never;

0 comments on commit bc2ae38

Please sign in to comment.