Skip to content

Commit b85fcb4

Browse files
committed
feat: allow setting entity to ask in URL param
1 parent c93184d commit b85fcb4

File tree

5 files changed

+67
-9
lines changed

5 files changed

+67
-9
lines changed

docs/catalog.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Integration with catalog
2+
3+
You can integrate list of questions ans question post form to the catalog page. Create a new component to
4+
packages/app/src/components/catalog/EntityPage/content/QetaContent.tsx:
5+
6+
```ts
7+
import { useEntity } from '@backstage/plugin-catalog-react';
8+
import { Container } from '@material-ui/core';
9+
import { stringifyEntityRef } from '@backstage/catalog-model';
10+
import React from 'react';
11+
import { AskForm, QuestionsContainer } from '@drodil/backstage-plugin-qeta';
12+
13+
export const QetaContent = () => {
14+
const { entity } = useEntity();
15+
16+
return (
17+
<Container>
18+
<QuestionsContainer entity={stringifyEntityRef(entity)} />
19+
<AskForm
20+
entity={stringifyEntityRef(entity)}
21+
onPost={q => console.log(q)}
22+
/>
23+
</Container>
24+
);
25+
};
26+
```
27+
28+
This can then be added to the specific component pages as a new tab, for example:
29+
30+
```ts
31+
<EntityLayout.Route path="/qeta" title="Q&A">
32+
<QetaContent />
33+
</EntityLayout.Route>
34+
```

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
## Usage
99

1010
- [Integration with `@backstage/plugin-search`](search.md)
11+
- [Integration with Backstage catalog](catalog.md)

plugins/qeta/src/components/AskForm/AskForm.tsx

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Alert, Autocomplete } from '@material-ui/lab';
44
import React, { useEffect } from 'react';
55
import { Controller, useForm } from 'react-hook-form';
66
import 'react-mde/lib/styles/css/react-mde-all.css';
7-
import { useNavigate } from 'react-router-dom';
7+
import { useNavigate, useSearchParams } from 'react-router-dom';
88
import {
99
QetaApi,
1010
qetaApiRef,
@@ -78,12 +78,15 @@ export const AskForm = (props: {
7878
}) => {
7979
const { id, entity, onPost } = props;
8080
const navigate = useNavigate();
81+
const [entityRef, setEntityRef] = React.useState(entity);
8182
const [values, setValues] = React.useState(getDefaultValues());
8283
const [error, setError] = React.useState(false);
8384
const [availableTags, setAvailableTags] = React.useState<string[] | null>([]);
85+
const [searchParams, _setSearchParams] = useSearchParams();
8486
const [availableEntities, setAvailableEntities] = React.useState<
8587
Entity[] | null
8688
>([]);
89+
8790
const qetaApi = useApi(qetaApiRef);
8891
const catalogApi = useApi(catalogApiRef);
8992
const configApi = useApi(configApiRef);
@@ -131,6 +134,15 @@ export const AskForm = (props: {
131134
.catch(_e => setError(true));
132135
};
133136

137+
useEffect(() => {
138+
if (!entityRef) {
139+
const e = searchParams.get('entity');
140+
if (e) {
141+
setEntityRef(e);
142+
}
143+
}
144+
}, [entityRef, searchParams]);
145+
134146
useEffect(() => {
135147
if (id) {
136148
getValues(qetaApi, catalogApi, id).then(data => {
@@ -140,14 +152,17 @@ export const AskForm = (props: {
140152
}, [qetaApi, catalogApi, id]);
141153

142154
useEffect(() => {
143-
if (entity) {
144-
catalogApi.getEntityByRef(entity).then(data => {
155+
if (entityRef) {
156+
catalogApi.getEntityByRef(entityRef).then(data => {
145157
if (data) {
146-
setValues({ ...values, entities: [data] });
158+
setValues(v => {
159+
return { ...v, entities: [data] };
160+
});
161+
setAvailableEntities([data]);
147162
}
148163
});
149164
}
150-
}, [catalogApi, entity, values]);
165+
}, [catalogApi, entityRef]);
151166

152167
useEffect(() => {
153168
reset(values);
@@ -165,6 +180,9 @@ export const AskForm = (props: {
165180
}, [qetaApi]);
166181

167182
useEffect(() => {
183+
if (entityRef) {
184+
return;
185+
}
168186
let entityKinds = configApi.getOptionalStringArray('qeta.entityKinds');
169187
if (!entityKinds) {
170188
entityKinds = ['Component'];
@@ -184,7 +202,7 @@ export const AskForm = (props: {
184202
.then(data =>
185203
data ? setAvailableEntities(data.items) : setAvailableEntities(null),
186204
);
187-
}, [catalogApi, configApi]);
205+
}, [catalogApi, entityRef, configApi]);
188206

189207
return (
190208
<form onSubmit={handleSubmit(postQuestion)}>
@@ -254,7 +272,7 @@ export const AskForm = (props: {
254272
render={({ field: { onChange, value } }) => (
255273
<Autocomplete
256274
multiple
257-
hidden={!!entity}
275+
hidden={!!entityRef}
258276
value={value}
259277
id="entities-select"
260278
options={availableEntities}

plugins/qeta/src/components/QuestionsContainer/QuestionList.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ export const QuestionList = (props: {
1212
error: any;
1313
response?: QuestionsResponse;
1414
onPageChange: (page: number) => void;
15+
entity?: string;
1516
}) => {
16-
const { loading, error, response, onPageChange } = props;
17+
const { loading, error, response, onPageChange, entity } = props;
1718
const [page, setPage] = React.useState(1);
1819
const pageSize = 10;
1920
const styles = useStyles();
@@ -47,7 +48,10 @@ export const QuestionList = (props: {
4748
<Typography variant="h6">No questions found</Typography>
4849
</Grid>
4950
<Grid item>
50-
<Button href="/qeta/ask" startIcon={<HelpOutline />}>
51+
<Button
52+
href={entity ? `/qeta/ask?entity=${entity}` : '/qeta/ask'}
53+
startIcon={<HelpOutline />}
54+
>
5155
Go ahead and ask one!
5256
</Button>
5357
</Grid>

plugins/qeta/src/components/QuestionsContainer/QuestionsContainer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export const QuestionsContainer = (props: QuestionsContainerProps) => {
7979
error={error}
8080
response={response}
8181
onPageChange={onPageChange}
82+
entity={entity}
8283
/>
8384
</Box>
8485
);

0 commit comments

Comments
 (0)