go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/frontend/ui/src/components/timestamp_info_bar/timestamp_info_bar.tsx (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import './styles.css';
    16  
    17  import dayjs from 'dayjs';
    18  
    19  import Grid from '@mui/material/Grid';
    20  import Link from '@mui/material/Link';
    21  
    22  interface Props {
    23      createUsername: string | undefined;
    24      createTime: string | undefined;
    25      updateUsername: string | undefined;
    26      updateTime: string | undefined;
    27  }
    28  
    29  interface FormattedUsernameProps {
    30      username: string | undefined;
    31  }
    32  
    33  const FormattedUsername = ({ username }: FormattedUsernameProps) => {
    34    if (!username) {
    35      return <></>;
    36    }
    37    if (username == 'system') {
    38      return <>LUCI Analysis</>;
    39    } else if (username.endsWith('@google.com')) {
    40      const ldap = username.substring(0, username.length - '@google.com'.length);
    41      return <Link target="_blank" href={`http://who/${ldap}`}>{ldap}</Link>;
    42    } else {
    43      return <>{username}</>;
    44    }
    45  };
    46  
    47  const dateFormat = 'LLLL';
    48  
    49  const TimestampInfoBar = ({
    50    createUsername,
    51    createTime,
    52    updateUsername,
    53    updateTime,
    54  }: Props) => {
    55    return (
    56      <Grid container>
    57        <Grid item>
    58          <small
    59            title={dayjs.utc(createTime).local().format(dateFormat)}
    60            data-testid="timestamp-info-bar-create"
    61            className='timestamp-text'>
    62              Created
    63            {
    64              createUsername &&
    65                  <>
    66                    {' '}by {<FormattedUsername username={createUsername} />}
    67                  </>
    68            }
    69            {' '}{dayjs.utc(createTime).local().fromNow()}. |
    70          </small>
    71          <small
    72            title={dayjs.utc(updateTime).local().format(dateFormat)}
    73            data-testid="timestamp-info-bar-update"
    74            className='timestamp-text'>
    75            {' '}Last modified
    76            {
    77              updateUsername &&
    78                <>
    79                  {' '}by {<FormattedUsername username={updateUsername} />}
    80                </>
    81            }
    82            {' '}{dayjs.utc(updateTime).local().fromNow()}.
    83          </small>
    84        </Grid>
    85      </Grid>
    86    );
    87  };
    88  
    89  export default TimestampInfoBar;