Skip to content

Commit

Permalink
chore: pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Aug 9, 2024
1 parent 68ece0d commit 22c171d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 60 deletions.
91 changes: 31 additions & 60 deletions src/components/multi-stage-output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export type FormattedKeyValue = {
readonly isBold?: boolean
// eslint-disable-next-line react/no-unused-prop-types
readonly label?: string
// -- what's the difference between `string|undefined` and `string?`
readonly value: string | undefined
// eslint-disable-next-line react/no-unused-prop-types
readonly stage?: string
Expand Down Expand Up @@ -152,8 +153,8 @@ function Infos({
keyValuePairs,
stage,
}: {
keyValuePairs: FormattedKeyValue[]
error?: Error
keyValuePairs: FormattedKeyValue[]
stage?: string
}): React.ReactNode {
return (
Expand Down Expand Up @@ -189,12 +190,13 @@ function Infos({
return <StaticKeyValue key={key} {...kv} />
}

return null
return null // what's the difference between returning null and returning undefined?
})
)
}

export function Stages({
// it's more idiomatic for React to put each component in its own file
error,
hasElapsedTime = true,
hasStageTime = true,
Expand All @@ -209,7 +211,7 @@ export function Stages({
<Box flexDirection="column" paddingTop={1} paddingBottom={1}>
<Divider title={title} />

{preStagesBlock && preStagesBlock.length > 0 && (
{preStagesBlock?.length && (
<Box flexDirection="column" marginLeft={1} paddingTop={1}>
<Infos error={error} keyValuePairs={preStagesBlock} />
</Box>
Expand Down Expand Up @@ -249,11 +251,14 @@ export function Stages({
)}
</Box>

{stageSpecificBlock && stageSpecificBlock.length > 0 && status !== 'pending' && status !== 'skipped' && (
<Box flexDirection="column" marginLeft={5}>
<Infos error={error} keyValuePairs={stageSpecificBlock} stage={stage} />
</Box>
)}
{stageSpecificBlock &&
stageSpecificBlock.length > 0 &&
status !== 'pending' &&
status !== 'skipped' && ( // stageSpecificBlock && stageSpecificBlock.length > 0 => stageSpecificBlock?.length
<Box flexDirection="column" marginLeft={5}>
<Infos error={error} keyValuePairs={stageSpecificBlock} stage={stage} />
</Box>
)}
</Box>
))}
</Box>
Expand Down Expand Up @@ -474,18 +479,7 @@ export class MultiStageOutput<T extends Record<string, unknown>> implements Disp
title,
})
} else {
this.inkInstance = render(
<Stages
hasElapsedTime={this.hasElapsedTime}
hasStageTime={this.hasStageTime}
postStagesBlock={this.formatKeyValuePairs(this.postStagesBlock)}
preStagesBlock={this.formatKeyValuePairs(this.preStagesBlock)}
stageSpecificBlock={this.formatKeyValuePairs(this.stageSpecificBlock)}
stageTracker={this.stageTracker}
timerUnit={this.timerUnit}
title={this.title}
/>,
)
this.inkInstance = render(<Stages {...this.generateStagesInput()} />)
}
}

Expand Down Expand Up @@ -521,35 +515,9 @@ export class MultiStageOutput<T extends Record<string, unknown>> implements Disp
return
}

if (error) {
this.inkInstance?.rerender(
<Stages
error={error}
hasElapsedTime={this.hasElapsedTime}
hasStageTime={this.hasStageTime}
postStagesBlock={this.formatKeyValuePairs(this.postStagesBlock)}
preStagesBlock={this.formatKeyValuePairs(this.preStagesBlock)}
stageSpecificBlock={this.formatKeyValuePairs(this.stageSpecificBlock)}
stageTracker={this.stageTracker}
timerUnit={this.timerUnit}
title={this.title}
/>,
)
} else {
this.inkInstance?.rerender(
<Stages
hasElapsedTime={this.hasElapsedTime}
hasStageTime={this.hasStageTime}
postStagesBlock={this.formatKeyValuePairs(this.postStagesBlock)}
preStagesBlock={this.formatKeyValuePairs(this.preStagesBlock)}
stageSpecificBlock={this.formatKeyValuePairs(this.stageSpecificBlock)}
stageTracker={this.stageTracker}
timerUnit={this.timerUnit}
title={this.title}
/>,
)
}
const stagesInput = {...this.generateStagesInput(), ...(error ? {error} : {})}

this.inkInstance?.rerender(<Stages {...stagesInput} />)
this.inkInstance?.unmount()
}

Expand Down Expand Up @@ -580,6 +548,20 @@ export class MultiStageOutput<T extends Record<string, unknown>> implements Disp
)
}

/** shared method to populate everything needed for Stages cmp */
private generateStagesInput(): StagesProps {
return {
hasElapsedTime: this.hasElapsedTime,
hasStageTime: this.hasStageTime,
postStagesBlock: this.formatKeyValuePairs(this.postStagesBlock),
preStagesBlock: this.formatKeyValuePairs(this.preStagesBlock),
stageSpecificBlock: this.formatKeyValuePairs(this.stageSpecificBlock),
stageTracker: this.stageTracker,
timerUnit: this.timerUnit,
title: this.title,
}
}

private update(stage: string, data?: Partial<T>): void {
this.data = {...this.data, ...data} as Partial<T>

Expand All @@ -588,18 +570,7 @@ export class MultiStageOutput<T extends Record<string, unknown>> implements Disp
if (isInCi) {
this.ciInstance?.update(this.stageTracker, this.data)
} else {
this.inkInstance?.rerender(
<Stages
hasElapsedTime={this.hasElapsedTime}
hasStageTime={this.hasStageTime}
postStagesBlock={this.formatKeyValuePairs(this.postStagesBlock)}
preStagesBlock={this.formatKeyValuePairs(this.preStagesBlock)}
stageSpecificBlock={this.formatKeyValuePairs(this.stageSpecificBlock)}
stageTracker={this.stageTracker}
timerUnit={this.timerUnit}
title={this.title}
/>,
)
this.inkInstance?.rerender(<Stages {...this.generateStagesInput()} />)
}
}
}
1 change: 1 addition & 0 deletions src/components/spinner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type UseSpinnerResult = {
frame: string
}

// there's a lot of what look like unnecessary exports.
export function useSpinner({type = 'dots'}: UseSpinnerProps): UseSpinnerResult {
const [frame, setFrame] = useState(0)
const spinner = spinners[type]
Expand Down
2 changes: 2 additions & 0 deletions src/stage-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Performance} from '@oclif/core/performance'

export type StageStatus = 'pending' | 'current' | 'completed' | 'skipped' | 'failed'

// Steve and I have seen some **weird** things with testing where he extended a Map, especially with the constructor.
// other options would be a class that holds the Map (since you're not calling any of the Map methods anyway)
export class StageTracker extends Map<string, StageStatus> {
public current: string | undefined
private markers = new Map<string, ReturnType<typeof Performance.mark>>()
Expand Down

0 comments on commit 22c171d

Please sign in to comment.