Skip to content

Commit

Permalink
feat(CE): added refresh source catalog support during model creation
Browse files Browse the repository at this point in the history
Co-authored-by: Tushar Selvakumar <54372016+macintushar@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and macintushar authored Sep 18, 2024
1 parent 9aabc50 commit 1bb15f9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { format } from 'sql-formatter';
import { autocompleteEntries } from './autocomplete';
import ModelQueryResults from '../ModelQueryResults';
import { useAPIErrorsToast, useErrorToast } from '@/hooks/useErrorToast';
import RefreshModelCatalog from '../RefreshModelCatalog';

const DefineSQL = ({
hasPrefilledValues = false,
Expand Down Expand Up @@ -207,6 +208,7 @@ const DefineSQL = ({
</Flex>
<Spacer />
<HStack spacing={3}>
<RefreshModelCatalog source_id={connector_id} />
<Button
variant='shell'
onClick={getPreview}
Expand All @@ -233,6 +235,7 @@ const DefineSQL = ({
fontSize='12px'
height='32px'
paddingX={3}
isDisabled={!runQuery}
onClick={() => setUserQuery(format(userQuery))}
>
<Image src={StarsImage} w={6} mr={2} /> Beautify
Expand Down
81 changes: 81 additions & 0 deletions ui/src/views/Models/ModelsForm/DefineModel/RefreshModelCatalog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { CustomToastStatus } from '@/components/Toast';
import useCustomToast from '@/hooks/useCustomToast';
import { useAPIErrorsToast, useErrorToast } from '@/hooks/useErrorToast';
import { getCatalog } from '@/services/syncs';
import { Button, Tooltip } from '@chakra-ui/react';
import { useState } from 'react';
import { FiLoader, FiRefreshCw } from 'react-icons/fi';

type RefreshModelCatalogProps = {
source_id: string;
};

const RefreshModelCatalog = ({ source_id }: RefreshModelCatalogProps) => {
const [isRefreshing, setIsRefreshing] = useState(false);

const toast = useCustomToast();
const errorToast = useErrorToast();
const apiErrorsToast = useAPIErrorsToast();

const tooltipLabel = isRefreshing
? 'Source schema is refreshing...'
: 'Refresh the source schema if any tables are missing.';

const handleRefresh = async () => {
setIsRefreshing(true);
try {
const data = await getCatalog(source_id, true);
if (data?.errors) {
apiErrorsToast(data.errors);
}
if (data?.data) {
toast({
title: 'Success!',
description: 'Source schema refreshed successfully',
status: CustomToastStatus.Success,
isClosable: true,
position: 'bottom-right',
});
}
} catch (error) {
errorToast('An error occurred while refreshing the source catalog.', true, null, true);
} finally {
setIsRefreshing(false);
}
};

return (
<Tooltip
hasArrow
label={tooltipLabel}
fontSize='xs'
placement='top'
backgroundColor='black.500'
color='gray.100'
borderRadius='6px'
padding='8px'
>
<Button
variant='shell'
minWidth='0'
width='auto'
fontSize='12px'
height='32px'
paddingX={3}
borderWidth={1}
borderStyle='solid'
borderColor='gray.500'
onClick={handleRefresh}
isDisabled={isRefreshing}
>
{isRefreshing ? (
<FiLoader height='14px' width='14px' />
) : (
<FiRefreshCw height='14px' width='14px' />
)}
</Button>
</Tooltip>
);
};

export default RefreshModelCatalog;
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { QueryType } from '@/views/Models/types';
import ViewSQLModal from './ViewSQLModal';
import SearchBar from '@/components/SearchBar/SearchBar';
import { useAPIErrorsToast, useErrorToast } from '@/hooks/useErrorToast';
import RefreshModelCatalog from '../RefreshModelCatalog';

const generateQuery = (table: string) => `SELECT * FROM ${table}`;

Expand Down Expand Up @@ -203,6 +204,7 @@ const TableSelector = ({
/>
</Box>
<HStack spacing={3} flex={1} justifyContent='flex-end'>
<RefreshModelCatalog source_id={connector_id} />
<Button
variant='shell'
onClick={getPreview}
Expand Down

0 comments on commit 1bb15f9

Please sign in to comment.