Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore refactor grouping option #460

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions apps/test-app/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ export function App() {
console.log(a);
return {
allGroupingOptions: [
{ groupingKey: 'RFOC', dimension: ['Daily', 'Weekly', 'Monthly'], type: ['Forecast', 'Planned'] },
{ groupingKey: 'RFCC', dimension: ['Daily', 'Weekly', 'Monthly'], type: ['Forecast', 'Planned'] },
{
groupingKey: 'RFOC',
timeInterval: ['Daily', 'Weekly', 'Monthly'],
dateVariant: ['Forecast', 'Planned'],
},
{
groupingKey: 'RFCC',
timeInterval: ['Daily', 'Weekly', 'Monthly'],
dateVariant: ['Forecast', 'Planned'],
},
{
groupingKey: 'Some very long keys',
dimension: ['Daily', 'Weekly'],
type: ['Forecast', 'Planned', 'Done'],
timeInterval: ['Daily', 'Weekly'],
dateVariant: ['Forecast', 'Planned', 'Done'],
},
{ groupingKey: 'System', dimension: null, type: null },
{ groupingKey: 'System', timeInterval: null, dateVariant: null },
] as GroupingOption[],
columnCount: 2,
columnStart: 0,
Expand Down Expand Up @@ -64,8 +72,8 @@ export function App() {
throw new Error('');
},
initialGrouping: ['RFOC'],
initialMode: 'Planned',
initialDimension: 'Weekly',
initialDateVariant: 'Planned',
initialTimeInterval: 'Weekly',
}}
filterOptions={{
dataSource: {
Expand Down
16 changes: 8 additions & 8 deletions packages/garden/src/lib/components/Garden.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import { GardenContextProvider } from '../context/gardenContext';

export type GardenMetaRequest = {
groupingKeys: string[];
dimension?: string | null;
type?: string | null;
timeInterval?: string | null;
dateVariant?: string | null;
};

export type GardenDataSource<TContext> = {
Expand All @@ -45,8 +45,8 @@ interface GardenProps<TData extends Record<PropertyKey, unknown>, TContext = und
customViews?: CustomVirtualViews<TData>;
clickEvents?: OnClickEvents<TData>;
groupingKeys: string[];
dimension: string | null;
type: string | null;
timeInterval: string | null;
dateVariant: string | null;
selected?: string | null;
}

Expand All @@ -59,8 +59,8 @@ export function Garden<TData extends Record<PropertyKey, unknown>, TContext = un
getIdentifier,
groupingKeys,
customViews,
dimension,
type,
timeInterval,
dateVariant,
visuals,
clickEvents,
selected = null,
Expand All @@ -72,8 +72,8 @@ export function Garden<TData extends Record<PropertyKey, unknown>, TContext = un
<ErrorBoundary FallbackComponent={GardenError}>
<GardenContextProvider
getIdentifier={getIdentifier}
dimension={dimension}
type={type}
timeInterval={timeInterval}
dateVariant={dateVariant}
initialGrouping={groupingKeys}
selected={selected}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const GardenItemContainer = <TData extends Record<PropertyKey, unknown>,
const isColumnExpanded = !!expand.expandedColumns.find((s) => s === virtualColumn.index);

const {
groupingService: { groupingKeys, dimension, type },
groupingService: { groupingKeys, timeInterval, dateVariant },
} = useGarden();

const queries = useBlockCache<TData[], TContext>({
Expand All @@ -110,8 +110,8 @@ export const GardenItemContainer = <TData extends Record<PropertyKey, unknown>,
{
columnName: group.columnName,
groupingKeys: groupingKeys,
dimension: dimension,
type: type,
timeInterval: timeInterval,
dateVariant: dateVariant,
subgroupName: group.name,
},
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ export const VirtualContainer = <TContext,>({
}: VirtualContainerProps<TContext>): JSX.Element | null => {
const { onClickItem } = useGardenConfig();
const {
groupingService: { groupingKeys, dimension, type },
groupingService: { groupingKeys, timeInterval, dateVariant },
} = useGarden();

const { data, isFetching } = useQuery(['garden', ...groupingKeys, dimension, type, context], {
const { data, isFetching } = useQuery(['garden', ...groupingKeys, timeInterval, dateVariant, context], {
refetchOnWindowFocus: false,
suspense: true,
useErrorBoundary: true,
keepPreviousData: false,
queryFn: ({ signal }) =>
dataSource.getGardenMeta(
{
dimension,
type,
timeInterval,
dateVariant,
groupingKeys,
},
context,
Expand Down
18 changes: 9 additions & 9 deletions packages/garden/src/lib/context/gardenContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type GardenState = {

type GroupingService = {
groupingKeys: string[];
dimension: string | null;
type: string | null;
timeInterval: string | null;
dateVariant: string | null;
};

type SelectionService = {
Expand All @@ -25,12 +25,12 @@ export const GardenContextProvider = <T,>(
getIdentifier: GetIdentifier<T>;
selected: string | null;
initialGrouping: string[];
dimension: string | null;
type: string | null;
timeInterval: string | null;
dateVariant: string | null;
}>
) => {
const selectionService = useSelectionService(props.getIdentifier, props.selected);
const groupingService = useGroupingService(props.initialGrouping, props.dimension, props.type);
const groupingService = useGroupingService(props.initialGrouping, props.timeInterval, props.dateVariant);

return (
<GardenContext.Provider value={{ groupingService, selectionService }}>{props.children}</GardenContext.Provider>
Expand Down Expand Up @@ -60,12 +60,12 @@ const useSelectionService = <T,>(getIdentifier: GetIdentifier<T>, initialSelecte

const useGroupingService = (
initialGrouping: string[],
dimension: string | null,
type: string | null
timeInterval: string | null,
dateVariant: string | null
): GroupingService => {
return {
groupingKeys: initialGrouping,
dimension: dimension,
type: type,
timeInterval: timeInterval,
dateVariant: dateVariant,
};
};
6 changes: 3 additions & 3 deletions packages/garden/src/lib/hooks/useBlockCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export function useBlockCache<T, TContext = undefined>({
hash,
}: UseBlockCacheArgs<T, TContext>) {
const {
groupingService: { groupingKeys, dimension, type },
groupingService: { groupingKeys, timeInterval, dateVariant },
} = useGarden();

const blockCache = useQueries({
queries: blocks.map((block) => ({
/** Unique identifier for blocks, add state here to invalidate query onChange */
queryKey: [...groupingKeys, dimension, type, `x${block.x}`, `y${block.y}`, context, ...hash],
queryKey: [...groupingKeys, timeInterval, dateVariant, `x${block.x}`, `y${block.y}`, context, ...hash],
/** Only fetch if block is in view */
enabled: !!blocksInView.find((s) => s.x === block.x && s.y === block.y),
/** Annoying default in react-query */
Expand All @@ -39,7 +39,7 @@ export function useBlockCache<T, TContext = undefined>({
const coordinates = getBlockIndexes(block, blockSqrt);

return getBlockAsync(
{ ...coordinates, groupingKeys: groupingKeys, dimension: dimension, type: type },
{ ...coordinates, groupingKeys: groupingKeys, timeInterval: timeInterval, dateVariant: dateVariant },
context,
signal
);
Expand Down
4 changes: 2 additions & 2 deletions packages/garden/src/lib/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export type GardenConfig<TData extends Record<PropertyKey, unknown>, TContext =
getDisplayName: GetDisplayName<TData>;
/** The keys used for grouping when the garden loads initially */
initialGrouping: string[];
initialDimension?: string | null;
initialMode?: string | null;
initialTimeInterval?: string | null;
initialDateVariant?: string | null;
/** The available keys to be used for grouping */
// fieldSettings?: FieldSettings<TData, TExtendedFields, TCustomGroupByKeys>;
/** Supply functions for handling clicks in the garden */
Expand Down
14 changes: 7 additions & 7 deletions packages/garden/src/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type GardenMeta = {

export type GetHeaderBlockRequestArgs = Pick<
GetBlockRequestArgs,
'columnStart' | 'columnEnd' | 'groupingKeys' | 'dimension' | 'type'
'columnStart' | 'columnEnd' | 'groupingKeys' | 'timeInterval' | 'dateVariant'
>;

export type GetBlockRequestArgs = {
Expand All @@ -34,17 +34,17 @@ export type GetBlockRequestArgs = {
/** Grouping key */
groupingKeys: string[];

dimension: string | null;
timeInterval: string | null;

type: string | null;
dateVariant: string | null;
};

export type GetSubgroupItemsArgs = {
groupingKeys: string[];
columnName: string;
subgroupName: string;
dimension: string | null;
type: string | null;
timeInterval: string | null;
dateVariant: string | null;
};

export type GardenHeaderGroup = {
Expand All @@ -54,6 +54,6 @@ export type GardenHeaderGroup = {

export type GroupingOption = {
groupingKey: string;
dimension: string[] | null;
type: string[] | null;
timeInterval: string[] | null;
dateVariant: string[] | null;
};
2 changes: 1 addition & 1 deletion packages/workspace-fusion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@equinor/workspace-fusion",
"version": "4.0.4",
"version": "5.0.0",
"type": "module",
"sideEffects": false,
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type GroupingSelectorProps = {
context: FilterState;
setGroupingKeys: (keys: string[]) => void;
groupingKeys: string[];
dimension: string | null;
type: string | null;
onChangeDimension: (dimension: string | null) => void;
onChangeMode: (type: string | null) => void;
timeInterval: string | null;
dateVariant: string | null;
onChangeTimeInterval: (timeInterval: string | null) => void;
onChangeDateVarient: (dateVariant: string | null) => void;
popoverRef: React.MutableRefObject<HTMLDivElement | null>;
iconRef: React.MutableRefObject<HTMLDivElement | null>;
close: VoidFunction;
Expand All @@ -23,23 +23,23 @@ type GroupingSelectorProps = {
export function GroupingSelector({
dataSource,
context,
dimension,
type,
timeInterval,
dateVariant,
setGroupingKeys,
onChangeDimension,
onChangeMode,
onChangeTimeInterval,
onChangeDateVarient,
groupingKeys,
iconRef,
popoverRef,
close,
}: GroupingSelectorProps): JSX.Element | null {
const { data } = useQuery(['garden', ...groupingKeys, dimension, type, context], {
const { data } = useQuery(['garden', ...groupingKeys, timeInterval, dateVariant, context], {
refetchOnWindowFocus: false,
suspense: true,
useErrorBoundary: true,
keepPreviousData: false,
queryFn: ({ signal }) =>
dataSource.getGardenMeta({ groupingKeys, dimension, type }, context, signal ?? new AbortSignal()),
dataSource.getGardenMeta({ groupingKeys, timeInterval, dateVariant }, context, signal ?? new AbortSignal()),
});

const selectorRef = useRef(null);
Expand All @@ -58,12 +58,12 @@ export function GroupingSelector({
throw new Error('Invalid grouping option');
}

if (!foundGroupingOption?.dimension?.includes(dimension ?? '')) {
onChangeDimension(foundGroupingOption.dimension?.at(0) ?? null);
if (!foundGroupingOption?.timeInterval?.includes(timeInterval ?? '')) {
onChangeTimeInterval(foundGroupingOption.timeInterval?.at(0) ?? null);
}

if (!foundGroupingOption?.type?.includes(type ?? '')) {
onChangeMode(foundGroupingOption.type?.at(0) ?? null);
if (!foundGroupingOption?.dateVariant?.includes(dateVariant ?? '')) {
onChangeDateVarient(foundGroupingOption.dateVariant?.at(0) ?? null);
}

setGroupingKeys([key]);
Expand Down Expand Up @@ -104,28 +104,28 @@ export function GroupingSelector({
groupingOption.groupingKey === groupingKeys[0] && (
<RadioWrapper>
<RadioCategoryWrapper>
{groupingOption.dimension &&
groupingOption.dimension.map((dim) => (
{groupingOption.timeInterval &&
groupingOption.timeInterval.map((dim) => (
<Radio
key={dim}
label={dim}
value={dim}
name="Dimension"
checked={dimension?.trim().toLowerCase() === dim.trim().toLowerCase()}
onChange={(e) => onChangeDimension(e.target.value)}
name="timeInterval"
checked={timeInterval?.trim().toLowerCase() === dim.trim().toLowerCase()}
onChange={(e) => onChangeTimeInterval(e.target.value)}
/>
))}
</RadioCategoryWrapper>
<RadioCategoryWrapper>
{groupingOption.type &&
groupingOption.type.map((typ) => (
{groupingOption.dateVariant &&
groupingOption.dateVariant.map((typ) => (
<Radio
key={typ}
label={typ}
value={typ}
name="type"
checked={type?.trim().toLowerCase() === typ.trim().toLowerCase()}
onChange={(e) => onChangeMode(e.target.value)}
name="dateVariant"
checked={dateVariant?.trim().toLowerCase() === typ.trim().toLowerCase()}
onChange={(e) => onChangeDateVarient(e.target.value)}
/>
))}
</RadioCategoryWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ type GardenPopoverItemProps = {
filterState: FilterState;
config: GardenConfig<any, FilterState>;
setGroupingKeys: (keys: string[]) => void;
onChangeDimension: (dimension: string | null) => void;
onChangeMode: (mode: string | null) => void;
onChangeTimeInterval: (timeInterval: string | null) => void;
onChangeDateVariant: (dateVariant: string | null) => void;
};
export const GardenPopoverItem = ({
anchor,
groupingKeys$,
onChangeDimension,
onChangeMode,
onChangeTimeInterval,
onChangeDateVariant,
setGroupingKeys,
config,
filterState,
Expand Down Expand Up @@ -73,10 +73,10 @@ export const GardenPopoverItem = ({
popoverRef={popoverRef}
groupingKeys={groupState.groupingKeys}
setGroupingKeys={setGroupingKeys}
dimension={groupState.dimension}
onChangeDimension={onChangeDimension}
type={groupState.type}
onChangeMode={onChangeMode}
timeInterval={groupState.timeInterval}
onChangeTimeInterval={onChangeTimeInterval}
dateVariant={groupState.dateVariant}
onChangeDateVarient={onChangeDateVariant}
context={filterState}
dataSource={config}
/>
Expand Down
Loading