(
query: ParsedUrlQuery,
existingDraft: Partial<WriteForm> = {},
)
| 34 | const hasValue = (value?: string): boolean => !!value?.trim(); |
| 35 | |
| 36 | export const getSquadsCreatePrefillState = ( |
| 37 | query: ParsedUrlQuery, |
| 38 | existingDraft: Partial<WriteForm> = {}, |
| 39 | ): SquadsCreatePrefillState => { |
| 40 | const title = sanitizeParam(query.title, MAX_CREATE_POST_TITLE_LENGTH); |
| 41 | const body = sanitizeParam(query.body, MAX_CREATE_POST_BODY_LENGTH); |
| 42 | const link = sanitizeParam(query.link); |
| 43 | const hasShareQueryParam = !!getFirstQueryParam(query.share); |
| 44 | const hasPollQueryParam = !!getFirstQueryParam(query.poll); |
| 45 | const hasStandupQueryParam = !!getFirstQueryParam(query.standup); |
| 46 | |
| 47 | const initialShareUrl = link && isValidHttpUrl(link) ? link : undefined; |
| 48 | const shouldOpenShare = !!initialShareUrl || hasShareQueryParam; |
| 49 | let initialDisplay: WriteFormTab | null = null; |
| 50 | |
| 51 | if (hasStandupQueryParam) { |
| 52 | initialDisplay = WriteFormTab.Standup; |
| 53 | } else if (hasPollQueryParam) { |
| 54 | initialDisplay = WriteFormTab.Poll; |
| 55 | } else if (shouldOpenShare) { |
| 56 | initialDisplay = WriteFormTab.Share; |
| 57 | } |
| 58 | |
| 59 | const initialDraft: Partial<WriteForm> = {}; |
| 60 | if (!shouldOpenShare) { |
| 61 | if (title && !hasValue(existingDraft.title)) { |
| 62 | initialDraft.title = title; |
| 63 | } |
| 64 | |
| 65 | if (body && !hasValue(existingDraft.content)) { |
| 66 | initialDraft.content = body; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return { |
| 71 | initialDisplay, |
| 72 | initialDraft, |
| 73 | initialShareUrl, |
| 74 | initialShareCommentary: shouldOpenShare ? body ?? title : undefined, |
| 75 | }; |
| 76 | }; |
no test coverage detected