Skip to content

Commit

Permalink
code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Dec 11, 2023
1 parent 1e36a74 commit 01a86bd
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 158 deletions.
2 changes: 1 addition & 1 deletion starsky/starsky/clientapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
"@typescript-eslint/ban-types": "warn",
"no-case-declarations": "warn",
"react/display-name": "off",
"react/prop-types": "warn",
"react/prop-types": "off",
"@typescript-eslint/no-loss-of-precision": "warn",
"react/react-in-jsx-scope": "off"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const ColorClassFilter: React.FunctionComponent<IColorClassProp> = memo(
: "btn colorclass colorclass--" + item
}
>
<label />
<span className="label" />
<span>{colorContent[item]}</span>{" "}
</Link>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const ColorClassSelect: React.FunctionComponent<IColorClassSelectProps> = (
: "btn colorclass colorclass--" + index
}
>
<label />
<span className="label" />
<span>{item}</span>
</button>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const MenuInlineSearch: React.FunctionComponent<IMenuSearchBarProps> = (

return (
<div className="menu-inline-search">
<div
<button
className={!formFocus ? "blur" : ""}
onFocus={() => setFormFocus(true)}
>
Expand Down Expand Up @@ -157,7 +157,7 @@ const MenuInlineSearch: React.FunctionComponent<IMenuSearchBarProps> = (
featuresResult={featuresResult}
/>
</ul>
</div>
</button>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import useGlobalSettings from "../../../../hooks/use-global-settings";
import { IUseLocation } from "../../../../hooks/use-location/interfaces/IUseLocation";
import { IDetailView } from "../../../../interfaces/IDetailView";
import { INavigateState } from "../../../../interfaces/INavigateState";
import localization from "../../../../localization/localization.json";
import { Language } from "../../../../shared/language";
import { UrlQuery } from "../../../../shared/url-query";
import MenuOption from "../../../atoms/menu-option/menu-option";

export interface IGoToParentFolderProps {
isSearchQuery: boolean;
Expand All @@ -15,12 +14,6 @@ export interface IGoToParentFolderProps {
export const GoToParentFolder: React.FunctionComponent<
IGoToParentFolderProps
> = ({ isSearchQuery, history, state }) => {
const settings = useGlobalSettings();
const language = new Language(settings.language);
const MessageGoToParentFolder = language.key(
localization.MessageGoToParentFolder
);

function navigateToParentFolder() {
history.navigate(
new UrlQuery().updateFilePathHash(
Expand All @@ -39,16 +32,12 @@ export const GoToParentFolder: React.FunctionComponent<
return (
<>
{isSearchQuery ? (
<li
className="menu-option"
data-test="go-to-parent-folder"
onClick={() => navigateToParentFolder()}
onKeyDown={(event) => {
event.key === "Enter" && navigateToParentFolder();
}}
>
{MessageGoToParentFolder}
</li>
<MenuOption
isReadOnly={false}
onClickKeydown={() => navigateToParentFolder()}
testName="go-to-parent-folder"
localization={localization.MessageGoToParentFolder}
/>
) : null}
</>
);
Expand Down
135 changes: 83 additions & 52 deletions starsky/starsky/clientapp/src/contexts/archive-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ function updateArchiveReducerTagsDescriptionTitleSet(
if (update.title) state.fileIndexItems[index].title = update.title;
}

function updateArchiveReducerHelper(
action: {
type: "update";
tags?: string | undefined;
colorclass?: number | undefined;
description?: string | undefined;
title?: string | undefined;
append?: boolean | undefined;
select: string[];
fileHash?: string | undefined;
},
state: IArchiveProps
) {
const { select, tags, description, title, append, colorclass, fileHash } =
action;
const update = {
select,
tags,
description,
title,
append,
colorclass,
fileHash
};

return updateArchiveReducer(state, update);
}

function updateArchiveReducer(
state: IArchiveProps,
update: IUpdateArchiveReducer
Expand Down Expand Up @@ -252,6 +280,58 @@ function filterDeletedItems(
return fileIndexItems;
}

function removeReducer(
action: {
type: "remove";
toRemoveFileList: string[];
},
state: IArchiveProps
) {
// files == subpath style not only the name (/dir/file.jpg)
const { toRemoveFileList } = action;

let deletedFilesCount = 0;
const afterFileIndexItems: IFileIndexItem[] = [];

state.fileIndexItems.forEach((item) => {
if (toRemoveFileList.indexOf(item.filePath) === -1) {
afterFileIndexItems.push(item);
} else {
deletedFilesCount++;
}
});

// to update the total results
const collectionsCount = state.collectionsCount - deletedFilesCount;

const newState = {
...state,
fileIndexItems: afterFileIndexItems,
collectionsCount,
lastUpdated: new Date()
};

// when you remove the last item of the directory
if (newState.fileIndexItems.length === 0) {
newState.colorClassUsage = [];
}
return updateCache(newState);
}

function forceResetReducer(action: {
type: "force-reset";
payload: IArchiveProps;
}) {
// also update the cache
const forceResetUpdated = {
...action.payload,
fileIndexItems: sorter(
new ArrayHelper().UniqueResults(action.payload.fileIndexItems, "filePath")
)
};
return updateCache(forceResetUpdated);
}

export function archiveReducer(state: State, action: ArchiveAction): State {
switch (action.type) {
case "remove-folder":
Expand All @@ -263,62 +343,13 @@ export function archiveReducer(state: State, action: ArchiveAction): State {
fileIndexItems: []
});
case "remove":
// files == subpath style not only the name (/dir/file.jpg)
const { toRemoveFileList } = action;

let deletedFilesCount = 0;
const afterFileIndexItems: IFileIndexItem[] = [];

state.fileIndexItems.forEach((item) => {
if (toRemoveFileList.indexOf(item.filePath) === -1) {
afterFileIndexItems.push(item);
} else {
deletedFilesCount++;
}
});

// to update the total results
const collectionsCount = state.collectionsCount - deletedFilesCount;

const newState = {
...state,
fileIndexItems: afterFileIndexItems,
collectionsCount,
lastUpdated: new Date()
};

// when you remove the last item of the directory
if (newState.fileIndexItems.length === 0) {
newState.colorClassUsage = [];
}
return updateCache(newState);
return removeReducer(action, state);
case "update":
const { select, tags, description, title, append, colorclass, fileHash } =
action;
const update = {
select,
tags,
description,
title,
append,
colorclass,
fileHash
};
return updateArchiveReducer(state, update);
return updateArchiveReducerHelper(action, state);
case "set":
return setArchiveReducer(action.payload);
case "force-reset":
// also update the cache
const forceResetUpdated = {
...action.payload,
fileIndexItems: sorter(
new ArrayHelper().UniqueResults(
action.payload.fileIndexItems,
"filePath"
)
)
};
return updateCache(forceResetUpdated);
return forceResetReducer(action);
case "rename-folder":
return updateCache({ ...state, subPath: action.path });
case "add":
Expand Down
125 changes: 75 additions & 50 deletions starsky/starsky/clientapp/src/contexts/detailview-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,80 @@ export type IDetailViewContext = {
state: IDetailView;
dispatch: React.Dispatch<DetailViewAction>;
};

function updateReducer(
action: {
type: "update";
filePath: string;
tags?: string | undefined;
colorclass?: number | undefined;
description?: string | undefined;
title?: string | undefined;
fileHash?: string | undefined;
locationState?: string | undefined;
status?: IExifStatus;
orientation?: Orientation;
lastEdited?: string;
dateTime?: string;
latitude?: number;
longitude?: number;
locationCountry?: string;
locationCountryCode?: string;
locationCity?: string;
},
state: IDetailView
) {
const {
filePath,
tags,
description,
title,
status,
colorclass,
fileHash,
orientation,
lastEdited,
dateTime,
latitude,
longitude,
locationCity,
locationCountry,
locationCountryCode,
locationState
} = action;

if (filePath !== state.fileIndexItem.filePath) {
console.log(
`Error: filePath is not the same ${filePath} != ${state.fileIndexItem.filePath}`
);
return state;
}

if (tags !== undefined) state.fileIndexItem.tags = tags;
if (description !== undefined) state.fileIndexItem.description = description;
if (title !== undefined) state.fileIndexItem.title = title;
if (colorclass !== undefined && colorclass !== -1)
state.fileIndexItem.colorClass = colorclass;
if (status) state.fileIndexItem.status = status;
if (fileHash) state.fileIndexItem.fileHash = fileHash;
if (orientation) state.fileIndexItem.orientation = orientation;
if (lastEdited) state.fileIndexItem.lastEdited = lastEdited;
if (dateTime) state.fileIndexItem.dateTime = dateTime;
if (latitude) state.fileIndexItem.latitude = latitude;
if (longitude) state.fileIndexItem.longitude = longitude;
if (locationCity) state.fileIndexItem.locationCity = locationCity;
if (locationCountry) {
state.fileIndexItem.locationCountry = locationCountry;
}
if (locationCountryCode) {
state.fileIndexItem.locationCountryCode = locationCountryCode;
}
if (locationState) state.fileIndexItem.locationState = locationState;

// Need to update otherwise other events are not triggered
return updateCache({ ...state, lastUpdated: new Date() });
}

export function detailviewReducer(
state: IDetailView,
action: DetailViewAction
Expand All @@ -81,56 +155,7 @@ export function detailviewReducer(
// Need to update otherwise other events are not triggered
return updateCache({ ...state, lastUpdated: new Date() });
case "update":
const {
filePath,
tags,
description,
title,
status,
colorclass,
fileHash,
orientation,
lastEdited,
dateTime,
latitude,
longitude,
locationCity,
locationCountry,
locationCountryCode,
locationState
} = action;

if (filePath !== state.fileIndexItem.filePath) {
console.log(
`Error: filePath is not the same ${filePath} != ${state.fileIndexItem.filePath}`
);
return state;
}

if (tags !== undefined) state.fileIndexItem.tags = tags;
if (description !== undefined)
state.fileIndexItem.description = description;
if (title !== undefined) state.fileIndexItem.title = title;
if (colorclass !== undefined && colorclass !== -1)
state.fileIndexItem.colorClass = colorclass;
if (status) state.fileIndexItem.status = status;
if (fileHash) state.fileIndexItem.fileHash = fileHash;
if (orientation) state.fileIndexItem.orientation = orientation;
if (lastEdited) state.fileIndexItem.lastEdited = lastEdited;
if (dateTime) state.fileIndexItem.dateTime = dateTime;
if (latitude) state.fileIndexItem.latitude = latitude;
if (longitude) state.fileIndexItem.longitude = longitude;
if (locationCity) state.fileIndexItem.locationCity = locationCity;
if (locationCountry) {
state.fileIndexItem.locationCountry = locationCountry;
}
if (locationCountryCode) {
state.fileIndexItem.locationCountryCode = locationCountryCode;
}
if (locationState) state.fileIndexItem.locationState = locationState;

// Need to update otherwise other events are not triggered
return updateCache({ ...state, lastUpdated: new Date() });
return updateReducer(action, state);
case "reset":
// this is triggered a lot when loading a page
return action.payload;
Expand Down
Loading

0 comments on commit 01a86bd

Please sign in to comment.