-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28faacf
commit 36f6d69
Showing
5 changed files
with
182 additions
and
5 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
app/components/selections/FrameworkTagSelectInput/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import React, { useMemo, useCallback } from 'react'; | ||
import { | ||
Button, | ||
BadgeInput, | ||
BadgeInputProps, | ||
} from '@the-deep/deep-ui'; | ||
import { useQuery, gql } from '@apollo/client'; | ||
|
||
import { | ||
FrameworkTagOptionsQuery, | ||
FrameworkTagOptionsQueryVariables, | ||
} from '#generated/types'; | ||
|
||
import styles from './styles.css'; | ||
|
||
const FRAMEWORK_TAGS = gql` | ||
query FrameworkTagOptions( | ||
$page: Int, | ||
$pageSize: Int, | ||
) { | ||
analysisFrameworkTags( | ||
page: $page, | ||
pageSize: $pageSize, | ||
) { | ||
page | ||
pageSize | ||
results { | ||
description | ||
id | ||
title | ||
icon { | ||
name | ||
url | ||
} | ||
} | ||
totalCount | ||
} | ||
} | ||
`; | ||
|
||
const PAGE_SIZE = 10; | ||
|
||
type BasicFrameworkTag = NonNullable<NonNullable<NonNullable<FrameworkTagOptionsQuery>['analysisFrameworkTags']>['results']>[number]; | ||
const keySelector = (item: BasicFrameworkTag) => item.id; | ||
const labelSelector = (item: BasicFrameworkTag) => item.title; | ||
const titleSelector = (item: BasicFrameworkTag) => item.description; | ||
function iconSelector(item: BasicFrameworkTag) { | ||
if (!item.icon?.url) { | ||
return undefined; | ||
} | ||
return ( | ||
<img | ||
className={styles.icon} | ||
src={item.icon.url} | ||
alt={item.icon.url} | ||
/> | ||
); | ||
} | ||
|
||
type Props<N extends string> = Omit< | ||
BadgeInputProps<BasicFrameworkTag, N, string>, | ||
'options' | 'keySelector' | 'labelSelector' | ||
>; | ||
|
||
function FrameworkTagSelectInput<N extends string>( | ||
props: Props<N>, | ||
) { | ||
const variables = useMemo(() => ({ | ||
page: 1, | ||
pageSize: PAGE_SIZE, | ||
}), []); | ||
|
||
const { | ||
data, | ||
fetchMore, | ||
} = useQuery<FrameworkTagOptionsQuery, FrameworkTagOptionsQueryVariables>( | ||
FRAMEWORK_TAGS, | ||
{ | ||
variables, | ||
}, | ||
); | ||
|
||
const handleShowMoreClick = useCallback(() => { | ||
fetchMore({ | ||
variables: { | ||
...variables, | ||
page: (data?.analysisFrameworkTags?.page ?? 1) + 1, | ||
}, | ||
updateQuery: (previousResult, { fetchMoreResult }) => { | ||
if (!previousResult.analysisFrameworkTags) { | ||
return previousResult; | ||
} | ||
|
||
const oldFrameworkTags = previousResult.analysisFrameworkTags; | ||
const newFrameworkTags = fetchMoreResult?.analysisFrameworkTags; | ||
|
||
if (!newFrameworkTags) { | ||
return previousResult; | ||
} | ||
|
||
return ({ | ||
...previousResult, | ||
analysisFrameworkTags: { | ||
...newFrameworkTags, | ||
results: [ | ||
...(oldFrameworkTags.results ?? []), | ||
...(newFrameworkTags.results ?? []), | ||
], | ||
}, | ||
}); | ||
}, | ||
}); | ||
}, [ | ||
data?.analysisFrameworkTags?.page, | ||
fetchMore, | ||
variables, | ||
]); | ||
|
||
return ( | ||
<> | ||
<BadgeInput | ||
{...props} | ||
keySelector={keySelector} | ||
labelSelector={labelSelector} | ||
titleSelector={titleSelector} | ||
iconSelector={iconSelector} | ||
options={data?.analysisFrameworkTags?.results ?? undefined} | ||
smallButtons | ||
/> | ||
{(data?.analysisFrameworkTags?.totalCount ?? 0) | ||
> (data?.analysisFrameworkTags?.results ?? []).length && ( | ||
<Button | ||
onClick={handleShowMoreClick} | ||
name={undefined} | ||
spacing="compact" | ||
variant="transparent" | ||
> | ||
Show more tags | ||
</Button> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export default FrameworkTagSelectInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.icon { | ||
width: auto; | ||
height: 1rem; | ||
object-fit: contain; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters