Skip to content

Commit 17ed2fb

Browse files
ClemsazertSimonClo
authored andcommitted
♻️ null args handling in grist hooks
1 parent c0b5c0b commit 17ed2fb

File tree

6 files changed

+35
-121
lines changed

6 files changed

+35
-121
lines changed

src/frontend/apps/impress/src/features/docs/doc-editor/components/DatabaseSourceSelector.tsx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,34 @@ type DatabaseSourceSelectorProps = {
88
onSourceSelected: (args: { documentId: string; tableId: string }) => void;
99
};
1010

11+
const TableSelector = ({
12+
documentId,
13+
onSourceSelected,
14+
}: { documentId: number } & DatabaseSourceSelectorProps) => {
15+
const { tables } = useListGristTables(documentId);
16+
return tables ? (
17+
<DropdownMenu
18+
options={tables.map(({ id }) => ({
19+
label: id,
20+
value: id,
21+
callback: () => onSourceSelected({ documentId, tableId: id }),
22+
}))}
23+
showArrow
24+
>
25+
<Text>Sélectionner une table Grist existante</Text>
26+
</DropdownMenu>
27+
) : (
28+
<Box>
29+
<Text>No tables available</Text>
30+
</Box>
31+
);
32+
};
33+
1134
export const DatabaseSourceSelector = ({
1235
onSourceSelected,
1336
}: DatabaseSourceSelectorProps) => {
1437
const [selectedDoc, setSelectedDoc] = useState<Doc>();
1538
const { docs } = useListGristDocs();
16-
const { tables } = useListGristTables(selectedDoc?.id);
1739

1840
return (
1941
<Box>
@@ -27,18 +49,11 @@ export const DatabaseSourceSelector = ({
2749
>
2850
<Text>{selectedDoc?.name ?? 'Sélectionner un document Grist'}</Text>
2951
</DropdownMenu>
30-
{selectedDoc && tables && (
31-
<DropdownMenu
32-
options={tables.map(({ id }) => ({
33-
label: id,
34-
value: id,
35-
callback: () =>
36-
onSourceSelected({ documentId: selectedDoc.id, tableId: id }),
37-
}))}
38-
showArrow
39-
>
40-
<Text>Sélectionner une table Grist existante</Text>
41-
</DropdownMenu>
52+
{selectedDoc && (
53+
<TableSelector
54+
documentId={selectedDoc.id}
55+
onSourceSelected={onSourceSelected}
56+
/>
4257
)}
4358
</Box>
4459
);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './useGristTable';
1+
export * from './useGristTableData';
22
export * from './useListGristDocs';
33
export * from './useListGristTables';

src/frontend/apps/impress/src/features/grist/useGristTable.ts

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/frontend/apps/impress/src/features/grist/useGristTableData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const useGristTableData = ({
1919
const response = await gristFetchApi(url);
2020
if (!response.ok) {
2121
throw new APIError(
22-
'Failed to request ai transform',
22+
'Failed to fetch Grist table data',
2323
await errorCauses(response),
2424
);
2525
}

src/frontend/apps/impress/src/features/grist/useListGristDocs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const useListGristDocs = (): { docs: Doc[] } => {
4343
const response = await gristFetchApi(url);
4444
if (!response.ok) {
4545
throw new APIError(
46-
'Failed to request ai transform',
46+
'Failed to fetch Grist documents',
4747
await errorCauses(response),
4848
);
4949
}

src/frontend/apps/impress/src/features/grist/useListGristTables.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@ export interface Fields {
1717
}
1818

1919
export const useListGristTables = (
20-
documentId?: number,
21-
): { tables: Table[] | null } => {
22-
const [tables, setTables] = useState<Table[] | null>(null);
20+
documentId: number,
21+
): { tables: Table[] | undefined } => {
22+
const [tables, setTables] = useState<Table[]>();
2323

2424
useEffect(() => {
2525
const fetchTables = async () => {
26-
if (!documentId) {
27-
console.warn('Document ID is required to fetch Grist tables');
28-
return;
29-
}
3026
const url = `docs/${documentId}/tables`;
3127
const response = await gristFetchApi(url);
3228
if (!response.ok) {
@@ -37,6 +33,7 @@ export const useListGristTables = (
3733
}
3834
return (await response.json()) as Promise<TableDescription>;
3935
};
36+
4037
fetchTables()
4138
.then((response) => {
4239
if (response) {

0 commit comments

Comments
 (0)