Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/ui/components/layouts/Page/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ interface PageProps {
children: React.ReactNode;
header?: string | React.ReactNode;
className?: string;
id?: string;
}

export const Page = ({children, header, className}: PageProps) => {
export const Page = ({children, header, className, id}: PageProps) => {
return (
<Flex direction="column" gap={4} className={classNames(styles.page, className)}>
<Flex direction="column" gap={4} className={classNames(styles.page, className)} id={id}>
{header && <Text variant="header-1">{header}</Text>}
{children}
</Flex>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@
.output {
margin: 0;
}

.scrollToUpButton {
position: fixed;
bottom: 60px;
right: var(--g-spacing-4);
color: var(--g-color-text-misc);
}
42 changes: 40 additions & 2 deletions src/ui/containers/Instance/pages/BuildLogs/BuildLogs.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';

import {idle, useQueryData} from '@gravity-ui/data-source';
import {Flex, Text, sp} from '@gravity-ui/uikit';
import {ArrowUp} from '@gravity-ui/icons';
import {Button, Flex, Icon, Text, Tooltip, sp} from '@gravity-ui/uikit';
import {useParams} from 'react-router-dom';

import type {ListLogsResponse} from '../../../../../shared/api/listLogs';
Expand All @@ -14,6 +15,8 @@ import {i18nInstance} from '../../../../i18n-common/i18nInstance';
import {generateInstanceHref} from '../../../../utils/common';
import {InstanceLayout} from '../../layouts/InstanceLayout';

import {BUILD_LOGS_PAGE_ID} from './constants';
import {useAutoscrollingBehavior} from './hooks';
import {i18n} from './i18n';

import * as styles from './BuildLogs.module.scss';
Expand Down Expand Up @@ -41,6 +44,11 @@ interface LogsContentProps {
}

const LogsContent = ({instance, listLogs}: LogsContentProps) => {
const logsContainerRef = React.useRef<HTMLDivElement>(null);
const LogsBottomRef = React.useRef<HTMLDivElement>(null);

const {isScrollTopButtonVisible} = useAutoscrollingBehavior(listLogs, LogsBottomRef);

const renderLog = (item: Output, index: number) => {
const {command, duration, stdout, stderr} = item;

Expand Down Expand Up @@ -111,10 +119,39 @@ const LogsContent = ({instance, listLogs}: LogsContentProps) => {
);
};

const renderScrollToTopButton = () => {
const handleScrollToTop = () => {
logsContainerRef.current?.scrollIntoView({
behavior: 'smooth',
block: 'start',
});
};

if (!isScrollTopButtonVisible) {
return null;
}

return (
<Tooltip content={i18n('return-to-start-of-logs')} placement="left" openDelay={0}>
<Button
view="raised"
pin="circle-circle"
size="xl"
className={styles.scrollToUpButton}
onClick={handleScrollToTop}
>
<Icon data={ArrowUp} size={24} />
</Button>
</Tooltip>
);
};

return (
<div>
<div ref={logsContainerRef} id="logs">
{listLogs?.logs?.map(renderLog)}
{renderInstanceLink()}
{renderScrollToTopButton()}
<div ref={LogsBottomRef} />
</div>
);
};
Expand All @@ -135,6 +172,7 @@ export const InstanceBuildLogsPage = () => {
listLogs: listLogsQuery.data,
})}
className={styles.buildLogs}
id={BUILD_LOGS_PAGE_ID}
>
<DataLoader
status={instanceQuery.status}
Expand Down
5 changes: 5 additions & 0 deletions src/ui/containers/Instance/pages/BuildLogs/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const BUILD_LOGS_PAGE_ID = 'build-logs-page';

export const SCROLL_TO_TOP_THRESHOLD = 400;

export const SCROLL_BOTTOM_TOLERANCE = 20;
44 changes: 44 additions & 0 deletions src/ui/containers/Instance/pages/BuildLogs/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';

import type {ListLogsResponse} from '../../../../../shared/api/listLogs';

import {BUILD_LOGS_PAGE_ID, SCROLL_BOTTOM_TOLERANCE, SCROLL_TO_TOP_THRESHOLD} from './constants';

export const useAutoscrollingBehavior = (
listLogs: ListLogsResponse | undefined,
logsBottomRef: React.RefObject<HTMLDivElement>,
) => {
const [isScrollTopButtonVisible, setIsScrollTopButtonVisible] = React.useState(false);
const [shouldAutoscroll, setShouldAutoscroll] = React.useState(true);

// Auto-scroll to the end of logs when new ones appear
React.useEffect(() => {
if (logsBottomRef.current && shouldAutoscroll) {
logsBottomRef.current.scrollIntoView({behavior: 'auto', block: 'center'});
}
}, [listLogs?.logs, shouldAutoscroll, logsBottomRef]);

React.useEffect(() => {
const page = document.getElementById(BUILD_LOGS_PAGE_ID);
if (!page) return;

const handleScroll = () => {
const {scrollTop, scrollHeight, clientHeight} = page;
const logsPageHeight = scrollHeight - clientHeight;
// check if user is at the bottom of the page with SCROLL_BOTTOM_TOLERANCE tolerance
const isAtBottom = logsPageHeight - scrollTop < SCROLL_BOTTOM_TOLERANCE;

// show scroll to top button if user is not at the very beginning of the page
setIsScrollTopButtonVisible(scrollTop > SCROLL_TO_TOP_THRESHOLD);
setShouldAutoscroll(isAtBottom);
};

page.addEventListener('scroll', handleScroll);

return () => page.removeEventListener('scroll', handleScroll);
}, []);

return {
isScrollTopButtonVisible,
};
};
3 changes: 2 additions & 1 deletion src/ui/containers/Instance/pages/BuildLogs/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"title": "Build logs",
"command-finished": "Command finished in {{duration}}s"
"command-finished": "Command finished in {{duration}}s",
"return-to-start-of-logs": "Return to start of logs"
}
3 changes: 2 additions & 1 deletion src/ui/containers/Instance/pages/BuildLogs/i18n/ru.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"title": "Логи сборки",
"command-finished": "Команда завершена за {{duration}}с"
"command-finished": "Команда завершена за {{duration}}с",
"return-to-start-of-logs": "Вернуться к началу логов"
}
2 changes: 1 addition & 1 deletion test-docker-app/test-app.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM crccheck/hello-world

RUN sleep 60
RUN echo "1" && sleep 4 && echo "2" && sleep 4 && echo "3" && sleep 4 && echo "4" && sleep 4 && echo "5" && sleep 4 && echo "6" && sleep 4 && echo "7" && sleep 4 && echo "8" && sleep 4 && echo "9" && sleep 4 && echo "10"

ENV PORT=80

Expand Down