Skip to content

Commit

Permalink
AI Proofread: Use block id as high level of the reducer structure
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoagds committed Jul 30, 2024
1 parent d9b6056 commit a84f3a5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,24 @@ export function toggleFeature( feature: string, force?: boolean ) {
};
}

export function setBlockMd5( feature: string, blockId: string, md5: string ) {
export function setBlockMd5( blockId: string, md5: string ) {
return {
type: 'SET_BLOCK_MD5',
feature,
blockId,
md5,
};
}

export function invalidateSuggestions( feature: string, blockId: string ) {
export function invalidateSuggestions( blockId: string ) {
return {
type: 'INVALIDATE_SUGGESTIONS',
feature,
blockId,
};
}

export function ignoreSuggestion( feature: string, blockId: string, id: string ) {
export function ignoreSuggestion( blockId: string, id: string ) {
return {
type: 'IGNORE_SUGGESTION',
feature,
blockId,
id,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function suggestions(
action: {
type: string;
id: string;
feature: string;
feature?: string;
blockId: string;
loading: boolean;
md5?: string;
Expand All @@ -119,17 +119,17 @@ export function suggestions(
) {
const { id, feature, blockId } = action ?? {};
const current = { ...state };
const currentBlock = current?.[ feature ]?.[ blockId ] ?? {};
const currentItem = current?.[ feature ]?.[ blockId ]?.[ id ] || {};
const currentBlock = current?.[ blockId ] ?? {};
const currentItem = current?.[ blockId ]?.[ feature ]?.[ id ] || {};

switch ( action.type ) {
case 'SET_SUGGESTIONS_LOADING': {
return {
...current,
[ feature ]: {
...( current[ feature ] ?? {} ),
[ blockId ]: {
...currentBlock,
[ blockId ]: {
...currentBlock,
[ feature ]: {
...( currentBlock[ feature ] ?? {} ),
[ id ]: {
...currentItem,
loading: action.loading,
Expand All @@ -142,10 +142,10 @@ export function suggestions(
case 'SET_SUGGESTIONS': {
return {
...current,
[ feature ]: {
...( current[ feature ] ?? {} ),
[ blockId ]: {
...currentBlock,
[ blockId ]: {
...currentBlock,
[ feature ]: {
...( currentBlock[ feature ] ?? {} ),
[ id ]: {
...currentItem,
loading: false,
Expand All @@ -159,35 +159,26 @@ export function suggestions(
case 'SET_BLOCK_MD5': {
return {
...current,
[ feature ]: {
...( current[ feature ] ?? {} ),
[ blockId ]: {
...currentBlock,
md5: action.md5,
},
[ blockId ]: {
md5: action.md5,
...currentBlock,
},
};
}

case 'INVALIDATE_SUGGESTIONS': {
return {
...current,
[ feature ]: {
...( current[ feature ] ?? {} ),
[ blockId ]: {},
},
[ blockId ]: {},
};
}

case 'IGNORE_SUGGESTION': {
return {
...current,
[ feature ]: {
...( current[ feature ] ?? {} ),
[ blockId ]: {
...currentBlock,
ignored: [ ...( currentBlock.ignored ?? [] ), id ],
},
[ blockId ]: {
...currentBlock,
ignored: [ ...( currentBlock.ignored ?? [] ), id ],
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,24 @@ export function getDisabledFeatures( state: BreveState ) {

// Suggestions

export function getBlockMd5( state: BreveState, feature: string, blockId: string ) {
return state.suggestions?.[ feature ]?.[ blockId ]?.md5 ?? '';
export function getBlockMd5( state: BreveState, blockId: string ) {
return state.suggestions?.[ blockId ]?.md5 ?? '';
}

export function getSuggestionsLoading(
state: BreveState,
{ feature, id, blockId }: { feature: string; id: string; blockId: string }
) {
return state.suggestions?.[ feature ]?.[ blockId ]?.[ id ]?.loading;
return state.suggestions?.[ blockId ]?.[ feature ]?.[ id ]?.loading;
}

export function getSuggestions(
state: BreveState,
{ feature, id, blockId }: { feature: string; id: string; blockId: string }
) {
return state.suggestions?.[ feature ]?.[ blockId ]?.[ id ]?.suggestions;
return state.suggestions?.[ blockId ]?.[ feature ]?.[ id ]?.suggestions;
}

export function getIgnoredSuggestions(
state: BreveState,
{ feature, blockId }: { feature: string; blockId: string }
) {
return state.suggestions?.[ feature ]?.[ blockId ]?.ignored;
export function getIgnoredSuggestions( state: BreveState, { blockId }: { blockId: string } ) {
return state.suggestions?.[ blockId ]?.ignored;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type BreveSelect = {
isProofreadEnabled: () => boolean;
isFeatureEnabled: ( feature: string ) => boolean;
getDisabledFeatures: () => Array< string >;
getBlockMd5: ( feature: string, blockId: string ) => string;
getBlockMd5: ( blockId: string ) => string;
getSuggestionsLoading: ( {
feature,
id,
Expand All @@ -64,13 +64,7 @@ export type BreveSelect = {
html: string;
suggestion: string;
};
getIgnoredSuggestions: ( {
feature,
blockId,
}: {
feature: string;
blockId: string;
} ) => Array< string >;
getIgnoredSuggestions: ( { blockId }: { blockId: string } ) => Array< string >;
};

export type BreveDispatch = {
Expand All @@ -79,9 +73,9 @@ export type BreveDispatch = {
setPopoverAnchor: ( anchor: Anchor ) => void;
toggleProofread: ( force?: boolean ) => void;
toggleFeature: ( feature: string, force?: boolean ) => void;
invalidateSuggestions: ( feature: string, blockId: string ) => void;
ignoreSuggestion: ( feature: string, blockId: string, id: string ) => void;
setBlockMd5: ( feature: string, blockId: string, md5: string ) => void;
invalidateSuggestions: ( blockId: string ) => void;
ignoreSuggestion: ( blockId: string, id: string ) => void;
setBlockMd5: ( blockId: string, md5: string ) => void;
setSuggestions: ( suggestions: {
anchor: Anchor[ 'target' ];
id: string;
Expand Down

0 comments on commit a84f3a5

Please sign in to comment.