Skip to content

Commit

Permalink
extension card cleanup (#252)
Browse files Browse the repository at this point in the history
* [Cleanup] Use enums for extension "InstalledState"

* [Cleanup] Reduce code duplication
  • Loading branch information
schroda authored Mar 3, 2023
1 parent 9625436 commit dcc18bb
Showing 1 changed file with 48 additions and 38 deletions.
86 changes: 48 additions & 38 deletions src/components/ExtensionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,74 @@ interface IProps {
notifyInstall: () => void;
}

enum ExtensionAction {
UPDATE = 'UPDATE',
UNINSTALL = 'UNINSTALL',
INSTALL = 'INSTALL',
}

enum ExtensionState {
OBSOLETE = 'OBSOLETE',
UPDATING = 'UPDATING',
UNINSTALLING = 'UNINSTALLING',
INSTALLING = 'INSTALLING',
}

type InstalledStates = ExtensionAction | ExtensionState;

const InstalledState = { ...ExtensionAction, ...ExtensionState } as const;

const EXTENSION_ACTION_TO_STATE_MAP: { [action in ExtensionAction]: ExtensionState } = {
[ExtensionAction.UPDATE]: ExtensionState.UPDATING,
[ExtensionAction.UNINSTALL]: ExtensionState.UNINSTALLING,
[ExtensionAction.INSTALL]: ExtensionState.INSTALLING,
} as const;

const EXTENSION_ACTION_TO_NEXT_ACTION_MAP: { [action in ExtensionAction]: ExtensionAction } = {
[ExtensionAction.UPDATE]: ExtensionAction.UNINSTALL,
[ExtensionAction.UNINSTALL]: ExtensionAction.INSTALL,
[ExtensionAction.INSTALL]: ExtensionAction.UNINSTALL,
} as const;

export default function ExtensionCard(props: IProps) {
const {
extension: { name, lang, versionName, installed, hasUpdate, obsolete, pkgName, iconUrl, isNsfw },
notifyInstall,
} = props;
const [installedState, setInstalledState] = useState<string>(() => {
const [installedState, setInstalledState] = useState<InstalledStates>(() => {
if (obsolete) {
return 'obsolete';
return InstalledState.OBSOLETE;
}
if (hasUpdate) {
return 'update';
return InstalledState.UPDATE;
}
return installed ? 'uninstall' : 'install';
return installed ? InstalledState.UNINSTALL : InstalledState.INSTALL;
});

const [serverAddress] = useLocalStorage<String>('serverBaseURL', '');
const [useCache] = useLocalStorage<boolean>('useCache', true);

const langPress = lang === 'all' ? 'All' : lang.toUpperCase();

function install() {
setInstalledState('installing');
client.get(`/api/v1/extension/install/${pkgName}`).then(() => {
setInstalledState('uninstall');
notifyInstall();
});
}
const requestExtensionAction = async (action: ExtensionAction): Promise<void> => {
const nextAction = EXTENSION_ACTION_TO_NEXT_ACTION_MAP[action];
const state = EXTENSION_ACTION_TO_STATE_MAP[action];

function update() {
setInstalledState('updating');
client.get(`/api/v1/extension/update/${pkgName}`).then(() => {
setInstalledState('uninstall');
notifyInstall();
});
}

function uninstall() {
setInstalledState('uninstalling');
client.get(`/api/v1/extension/uninstall/${pkgName}`).then(() => {
// setInstalledState('install');
notifyInstall();
});
}
setInstalledState(state);
await client.get(`/api/v1/extension/${action.toLowerCase()}/${pkgName}`);
setInstalledState(nextAction);
notifyInstall();
};

function handleButtonClick() {
switch (installedState) {
case 'install':
install();
break;
case 'update':
update();
break;
case 'obsolete':
uninstall();
setTimeout(() => window.location.reload(), 3000);
case ExtensionAction.INSTALL:
case ExtensionAction.UPDATE:
case ExtensionAction.UNINSTALL:
requestExtensionAction(installedState).catch(() => {});
break;
case 'uninstall':
uninstall();
case ExtensionState.OBSOLETE:
requestExtensionAction(ExtensionAction.UNINSTALL).catch(() => {});
break;
default:
break;
Expand Down Expand Up @@ -124,7 +134,7 @@ export default function ExtensionCard(props: IProps) {

<Button
variant="outlined"
sx={{ color: installedState === 'obsolete' ? 'red' : 'inherit' }}
sx={{ color: installedState === InstalledState.OBSOLETE ? 'red' : 'inherit' }}
onClick={() => handleButtonClick()}
>
{installedState}
Expand Down

0 comments on commit dcc18bb

Please sign in to comment.