| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ReactNode} from 'react'; |
| b69ab31 | | | 9 | import type {CommitInfo} from '../types'; |
| b69ab31 | | | 10 | import type {CommitMessageFields, FieldConfig, FieldsBeingEdited, TypeaheadKind} from './types'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {Subtle} from 'isl-components/Subtle'; |
| b69ab31 | | | 13 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 14 | import type {TypeaheadResult} from 'isl-components/Types'; |
| b69ab31 | | | 15 | import {randomId} from 'shared/utils'; |
| b69ab31 | | | 16 | import serverApi from '../ClientToServerAPI'; |
| b69ab31 | | | 17 | import {InlineBadge} from '../InlineBadge'; |
| b69ab31 | | | 18 | import {YouAreHereLabel} from '../YouAreHereLabel'; |
| b69ab31 | | | 19 | import {t, T} from '../i18n'; |
| b69ab31 | | | 20 | import platform from '../platform'; |
| b69ab31 | | | 21 | import {RelativeDate} from '../relativeDate'; |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | export function CommitTitleByline({commit}: {commit: CommitInfo}) { |
| b69ab31 | | | 24 | const createdByInfo = ( |
| b69ab31 | | | 25 | // TODO: determine if you're the author to say "you" |
| b69ab31 | | | 26 | <T replace={{$author: commit.author}}>Created by $author</T> |
| b69ab31 | | | 27 | ); |
| b69ab31 | | | 28 | return ( |
| b69ab31 | | | 29 | <Subtle className="commit-info-title-byline"> |
| b69ab31 | | | 30 | {commit.isDot ? <YouAreHereLabel /> : null} |
| b69ab31 | | | 31 | {commit.phase === 'public' ? <PublicCommitBadge /> : null} |
| b69ab31 | | | 32 | <OverflowEllipsis shrink> |
| b69ab31 | | | 33 | <Tooltip trigger="hover" component={() => createdByInfo}> |
| b69ab31 | | | 34 | {createdByInfo} |
| b69ab31 | | | 35 | </Tooltip> |
| b69ab31 | | | 36 | </OverflowEllipsis> |
| b69ab31 | | | 37 | <OverflowEllipsis> |
| b69ab31 | | | 38 | <Tooltip trigger="hover" title={commit.date.toLocaleString()}> |
| b69ab31 | | | 39 | <RelativeDate date={commit.date} /> |
| b69ab31 | | | 40 | </Tooltip> |
| b69ab31 | | | 41 | </OverflowEllipsis> |
| b69ab31 | | | 42 | </Subtle> |
| b69ab31 | | | 43 | ); |
| b69ab31 | | | 44 | } |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | function PublicCommitBadge() { |
| b69ab31 | | | 47 | return ( |
| b69ab31 | | | 48 | <Tooltip |
| b69ab31 | | | 49 | placement="bottom" |
| b69ab31 | | | 50 | title={t( |
| b69ab31 | | | 51 | "This commit has already been pushed to an append-only remote branch and can't be modified locally.", |
| b69ab31 | | | 52 | )}> |
| b69ab31 | | | 53 | <InlineBadge> |
| b69ab31 | | | 54 | <T>Public</T> |
| b69ab31 | | | 55 | </InlineBadge> |
| b69ab31 | | | 56 | </Tooltip> |
| b69ab31 | | | 57 | ); |
| b69ab31 | | | 58 | } |
| b69ab31 | | | 59 | |
| b69ab31 | | | 60 | export function OverflowEllipsis({children, shrink}: {children: ReactNode; shrink?: boolean}) { |
| b69ab31 | | | 61 | return <div className={`overflow-ellipsis${shrink ? ' overflow-shrink' : ''}`}>{children}</div>; |
| b69ab31 | | | 62 | } |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | export function SmallCapsTitle({children}: {children: ReactNode}) { |
| b69ab31 | | | 65 | return <div className="commit-info-small-title">{children}</div>; |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | export function Section({ |
| b69ab31 | | | 69 | children, |
| b69ab31 | | | 70 | className, |
| b69ab31 | | | 71 | ...rest |
| b69ab31 | | | 72 | }: React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>) { |
| b69ab31 | | | 73 | return ( |
| b69ab31 | | | 74 | <section {...rest} className={'commit-info-section' + (className ? ' ' + className : '')}> |
| b69ab31 | | | 75 | {children} |
| b69ab31 | | | 76 | </section> |
| b69ab31 | | | 77 | ); |
| b69ab31 | | | 78 | } |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | export function getFieldToAutofocus( |
| b69ab31 | | | 81 | fields: Array<FieldConfig>, |
| b69ab31 | | | 82 | fieldsBeingEdited: FieldsBeingEdited, |
| b69ab31 | | | 83 | lastFieldsBeingEdited: FieldsBeingEdited | undefined, |
| b69ab31 | | | 84 | ): keyof CommitMessageFields | undefined { |
| b69ab31 | | | 85 | for (const field of fields) { |
| b69ab31 | | | 86 | const isNewlyBeingEdited = |
| b69ab31 | | | 87 | fieldsBeingEdited[field.key] && |
| b69ab31 | | | 88 | (lastFieldsBeingEdited == null || !lastFieldsBeingEdited[field.key]); |
| b69ab31 | | | 89 | if (isNewlyBeingEdited) { |
| b69ab31 | | | 90 | return field.key; |
| b69ab31 | | | 91 | } |
| b69ab31 | | | 92 | } |
| b69ab31 | | | 93 | return undefined; |
| b69ab31 | | | 94 | } |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | export function getOnClickToken( |
| b69ab31 | | | 97 | field: FieldConfig & {type: 'field'}, |
| b69ab31 | | | 98 | ): ((token: string) => unknown) | undefined { |
| b69ab31 | | | 99 | if (field.getUrl == null) { |
| b69ab31 | | | 100 | return undefined; |
| b69ab31 | | | 101 | } |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | return token => { |
| b69ab31 | | | 104 | const url = field.getUrl?.(token); |
| b69ab31 | | | 105 | url && platform.openExternalLink(url); |
| b69ab31 | | | 106 | }; |
| b69ab31 | | | 107 | } |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | export function convertFieldNameToKey(fieldName: string): string { |
| b69ab31 | | | 110 | return fieldName.toLowerCase().replace(/\s/g, '-'); |
| b69ab31 | | | 111 | } |
| b69ab31 | | | 112 | |
| b69ab31 | | | 113 | export async function fetchNewSuggestions( |
| b69ab31 | | | 114 | kind: TypeaheadKind, |
| b69ab31 | | | 115 | text: string, |
| b69ab31 | | | 116 | ): Promise<{values: Array<TypeaheadResult>; fetchStartTimestamp: number}> { |
| b69ab31 | | | 117 | const now = Date.now(); |
| b69ab31 | | | 118 | if (text.trim().length < 2) { |
| b69ab31 | | | 119 | // no need to do a fetch on zero- or one-char input... |
| b69ab31 | | | 120 | // it's slow and doesn't give good suggestions anyway |
| b69ab31 | | | 121 | return {values: [], fetchStartTimestamp: now}; |
| b69ab31 | | | 122 | } |
| b69ab31 | | | 123 | const id = randomId(); |
| b69ab31 | | | 124 | serverApi.postMessage({type: 'typeahead', kind, id, query: text}); |
| b69ab31 | | | 125 | const values = await serverApi.nextMessageMatching( |
| b69ab31 | | | 126 | 'typeaheadResult', |
| b69ab31 | | | 127 | message => message.id === id, |
| b69ab31 | | | 128 | ); |
| b69ab31 | | | 129 | return {values: values.result, fetchStartTimestamp: now}; |
| b69ab31 | | | 130 | } |