Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix see more #464

Merged
merged 1 commit into from
Mar 4, 2024
Merged

fix see more #464

merged 1 commit into from
Mar 4, 2024

Conversation

mruwnik
Copy link
Collaborator

@mruwnik mruwnik commented Mar 1, 2024

No description provided.

Copy link
Collaborator

@jrhender jrhender left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions

// Sometimes string fields are returned as lists. This can happen when there are duplicate entries in Coda
const head = (item: string | string[]) => {
const head = (item: any | any[]) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it would be more type-safe down the line to use unknown here. This change doesn't seem to break anything in the existing code and so should be easy to apply now.

Suggested change
const head = (item: any | any[]) => {
const head = (item: unknown | unknown[]) => {

@@ -132,7 +132,7 @@ export type AnswersRow = CodaRowCommon & {
Banners: '' | Entity[]
'Rich Text': string
Subtitle?: string
Icon?: string
Icon?: Entity | string | Entity[]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type change doesn't seem necessary as far as I can tell. The new extractIcon function returns string | undefined, so I think that is a suitable type here.

Suggested change
Icon?: Entity | string | Entity[]
Icon?: string

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is here mainly to document what Coda returns. Which can be either the raw image (base64 encoded), an url to the image, a reference to a Coda item or a list of references to Coda items... I'm not sure what exactly causes what

Comment on lines +266 to +271
const item = head(val)
if (typeof item === 'string') return extractText(val as string)
if (item) return (item as Entity).url
return undefined
}

Copy link
Collaborator

@jrhender jrhender Mar 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to avoid the val as casts to benefit from type checking of val in the future. I've suggested an isStringOrStringArray type guard to do this, though there might be a more elegant way.

Suggested change
const item = head(val)
if (typeof item === 'string') return extractText(val as string)
if (item) return (item as Entity).url
return undefined
}
if (isStringOrStringArray(val)) return extractText(val)
const item = head(val)
if (item) return item.url
}
const isStringOrStringArray = (x: unknown): x is string | string[] => {
const item = head(x)
return typeof item === 'string'
}

I also think the last return undefined isn't necessary. I think undefined will be returned if none of the if cases are matched, even without this line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the thing is that I know that if it's not a string and not undefined, then it's going to be an Entity at this point. Typescript doesn't know that.

Wouldn't that be void?

Copy link
Collaborator

@jrhender jrhender Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the thing is that I know that if it's not a string and not undefined, then it's going to be an Entity at this point. Typescript doesn't know that.

Ah maybe part of the confusion here is that in my local code where I was trying this out, I also changed the signature of the head function to const head = <T,>(item: T | T[]): T. With this, I'm pretty sure that typescript does know that item is an Entity at that point. You can see in screenshot below that my local compiler is showing the type of item as that point as Entity.

image

The way I'm reasoning about it is that the parameter type is undefined | string | string[] | Entity | Entity[] and then:

  1. if (!val) return val removes the possibility of undefined
  2. if (isStringOrStringArray(val)) return extractText(val) removes the possibility of string | string[]
  3. head (with the signature change above) narrows Entity | Entity[] to Entity

At any rate, I'll make a PR at some point to improve the head function so that it's easy to make future code more type safe 😊 . Probably less important getting the UI redesign out.

Wouldn't that be void?

No, I don't think so. I think void is a typescript type but the actual return type is undefined. I wrote this JS fiddle as a quick test: https://jsfiddle.net/#&togetherjs=M9fHnDlskK

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhh, I like that version of head!

@mruwnik mruwnik merged commit 3c1cf73 into stampy-redesign Mar 4, 2024
1 check passed
@mruwnik mruwnik deleted the see-more branch March 4, 2024 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants