mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: move cell hook into custom cell component
This commit is contained in:
parent
3635b6b63e
commit
c8569d3c62
@ -3,12 +3,12 @@ import { CellCache } from '../../../stores/effects/database/cell/cell_cache';
|
||||
import { FieldController } from '../../../stores/effects/database/field/field_controller';
|
||||
import { CellControllerBuilder } from '../../../stores/effects/database/cell/controller_builder';
|
||||
import { DateCellDataPB, SelectOptionCellDataPB, URLCellDataPB } from '../../../../services/backend';
|
||||
import { useState } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
export const useCell = (cellIdentifier: CellIdentifier, cellCache: CellCache, fieldController: FieldController) => {
|
||||
const [data, setData] = useState<DateCellDataPB | URLCellDataPB | SelectOptionCellDataPB | string | undefined>();
|
||||
|
||||
const loadCell = async () => {
|
||||
useEffect(() => {
|
||||
const builder = new CellControllerBuilder(cellIdentifier, cellCache, fieldController);
|
||||
const cellController = builder.build();
|
||||
cellController.subscribeChanged({
|
||||
@ -19,10 +19,14 @@ export const useCell = (cellIdentifier: CellIdentifier, cellCache: CellCache, fi
|
||||
|
||||
// ignore the return value, because we are using the subscription
|
||||
void cellController.getCellData();
|
||||
};
|
||||
|
||||
// dispose the cell controller when the component is unmounted
|
||||
return () => {
|
||||
void cellController.dispose();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return {
|
||||
loadCell,
|
||||
data,
|
||||
};
|
||||
};
|
||||
|
@ -1,9 +1,7 @@
|
||||
import { useCell } from '../_shared/database-hooks/useCell';
|
||||
import { CellIdentifier } from '../../stores/effects/database/cell/cell_bd_svc';
|
||||
import { CellCache } from '../../stores/effects/database/cell/cell_cache';
|
||||
import { FieldController } from '../../stores/effects/database/field/field_controller';
|
||||
import { useEffect } from 'react';
|
||||
import { DateCellDataPB, FieldType, SelectOptionCellDataPB } from '../../../services/backend';
|
||||
import { FieldType } from '../../../services/backend';
|
||||
import { BoardOptionsCell } from './BoardOptionsCell';
|
||||
import { BoardDateCell } from './BoardDateCell';
|
||||
import { BoardTextCell } from './BoardTextCell';
|
||||
@ -17,23 +15,28 @@ export const BoardCell = ({
|
||||
cellCache: CellCache;
|
||||
fieldController: FieldController;
|
||||
}) => {
|
||||
const { loadCell, data } = useCell(cellIdentifier, cellCache, fieldController);
|
||||
useEffect(() => {
|
||||
void (async () => {
|
||||
await loadCell();
|
||||
})();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
{cellIdentifier.fieldType === FieldType.SingleSelect ||
|
||||
cellIdentifier.fieldType === FieldType.MultiSelect ||
|
||||
cellIdentifier.fieldType === FieldType.Checklist ? (
|
||||
<BoardOptionsCell value={data as SelectOptionCellDataPB | undefined}></BoardOptionsCell>
|
||||
<BoardOptionsCell
|
||||
cellIdentifier={cellIdentifier}
|
||||
cellCache={cellCache}
|
||||
fieldController={fieldController}
|
||||
></BoardOptionsCell>
|
||||
) : cellIdentifier.fieldType === FieldType.DateTime ? (
|
||||
<BoardDateCell value={data as DateCellDataPB | undefined}></BoardDateCell>
|
||||
<BoardDateCell
|
||||
cellIdentifier={cellIdentifier}
|
||||
cellCache={cellCache}
|
||||
fieldController={fieldController}
|
||||
></BoardDateCell>
|
||||
) : (
|
||||
<BoardTextCell value={data as string | undefined}></BoardTextCell>
|
||||
<BoardTextCell
|
||||
cellIdentifier={cellIdentifier}
|
||||
cellCache={cellCache}
|
||||
fieldController={fieldController}
|
||||
></BoardTextCell>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
|
@ -1,5 +1,18 @@
|
||||
import { DateCellDataPB } from '../../../services/backend';
|
||||
import { useCell } from '../_shared/database-hooks/useCell';
|
||||
import { CellIdentifier } from '../../stores/effects/database/cell/cell_bd_svc';
|
||||
import { CellCache } from '../../stores/effects/database/cell/cell_cache';
|
||||
import { FieldController } from '../../stores/effects/database/field/field_controller';
|
||||
|
||||
export const BoardDateCell = ({ value }: { value: DateCellDataPB | undefined }) => {
|
||||
return <div>{value?.date || ''}</div>;
|
||||
export const BoardDateCell = ({
|
||||
cellIdentifier,
|
||||
cellCache,
|
||||
fieldController,
|
||||
}: {
|
||||
cellIdentifier: CellIdentifier;
|
||||
cellCache: CellCache;
|
||||
fieldController: FieldController;
|
||||
}) => {
|
||||
const { data } = useCell(cellIdentifier, cellCache, fieldController);
|
||||
return <div>{(data as DateCellDataPB | undefined)?.date || ''}</div>;
|
||||
};
|
||||
|
@ -1,5 +1,25 @@
|
||||
import { SelectOptionCellDataPB, SelectOptionColorPB } from '../../../services/backend';
|
||||
import { SelectOptionCellDataPB } from '../../../services/backend';
|
||||
import { useCell } from '../_shared/database-hooks/useCell';
|
||||
import { CellIdentifier } from '../../stores/effects/database/cell/cell_bd_svc';
|
||||
import { CellCache } from '../../stores/effects/database/cell/cell_cache';
|
||||
import { FieldController } from '../../stores/effects/database/field/field_controller';
|
||||
|
||||
export const BoardOptionsCell = ({ value }: { value: SelectOptionCellDataPB | undefined }) => {
|
||||
return <>{value?.select_options?.map((option, index) => <div key={index}>{option?.name || ''}</div>) || ''}</>;
|
||||
export const BoardOptionsCell = ({
|
||||
cellIdentifier,
|
||||
cellCache,
|
||||
fieldController,
|
||||
}: {
|
||||
cellIdentifier: CellIdentifier;
|
||||
cellCache: CellCache;
|
||||
fieldController: FieldController;
|
||||
}) => {
|
||||
const { data } = useCell(cellIdentifier, cellCache, fieldController);
|
||||
|
||||
return (
|
||||
<>
|
||||
{(data as SelectOptionCellDataPB | undefined)?.select_options?.map((option, index) => (
|
||||
<div key={index}>{option?.name || ''}</div>
|
||||
)) || ''}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
@ -1,3 +1,18 @@
|
||||
export const BoardTextCell = ({ value }: { value: string | undefined }) => {
|
||||
return <div>{value || ''}</div>;
|
||||
import { CellIdentifier } from '../../stores/effects/database/cell/cell_bd_svc';
|
||||
import { CellCache } from '../../stores/effects/database/cell/cell_cache';
|
||||
import { FieldController } from '../../stores/effects/database/field/field_controller';
|
||||
import { useCell } from '../_shared/database-hooks/useCell';
|
||||
|
||||
export const BoardTextCell = ({
|
||||
cellIdentifier,
|
||||
cellCache,
|
||||
fieldController,
|
||||
}: {
|
||||
cellIdentifier: CellIdentifier;
|
||||
cellCache: CellCache;
|
||||
fieldController: FieldController;
|
||||
}) => {
|
||||
const { data } = useCell(cellIdentifier, cellCache, fieldController);
|
||||
|
||||
return <div>{(data as string | undefined) || ''}</div>;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user