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

Default ambassador menu open, persist menu state #153

Merged
merged 6 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 2 additions & 3 deletions src/pages/overlay/components/buttons/Buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ButtonsOptions = Readonly<

interface ButtonsProps<T extends ButtonsOptions> {
options: T;
onClick: (key: T[number]["key"] | undefined) => void;
onClick: (key: T[number]["key"] | "") => void;
active?: string;
}

Expand All @@ -32,8 +32,7 @@ export default function Buttons<T extends ButtonsOptions = ButtonsOptions>(
options
.map((option) => ({
...option,
onClick: () =>
onClick(active === option.key ? undefined : option.key),
onClick: () => onClick(active === option.key ? "" : option.key),
active: active === option.key,
}))
.sort((a, b) => {
Expand Down
24 changes: 20 additions & 4 deletions src/pages/overlay/components/overlay/Overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ const overlayOptions = [
},
] as const;

type OverlayKey = (typeof overlayOptions)[number]["key"];
export const isValidOverlayKey = (key: string) =>
key == "" || overlayOptions.some((option) => option.key === key);
trimbe marked this conversation as resolved.
Show resolved Hide resolved

export type OverlayKey = (typeof overlayOptions)[number]["key"] | "";

type ActiveAmbassadorState = {
key?: AmbassadorKey;
Expand All @@ -85,10 +88,22 @@ export default function Overlay() {

const [activeAmbassador, setActiveAmbassador] =
useState<ActiveAmbassadorState>({});
const [visibleOption, setVisibleOption] = useState<OverlayKey>();
const [visibleOption, setVisibleOption] = useState<OverlayKey>(
settings.openedMenu.value,
);
const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined);
const awakingRef = useRef(false);

// update setting when opened menu changes
useEffect(() => {
settings.openedMenu.change(visibleOption);
}, [visibleOption]);

// open saved (or default) menu when mounted
useEffect(() => {
setVisibleOption(settings.openedMenu.value);
}, [settings.openedMenu.value]);

// When a chat command is run, wake the overlay
useChatCommand(
useCallback(
Expand All @@ -104,7 +119,8 @@ export default function Overlay() {
// Dismiss the overlay after a delay
if (timeoutRef.current) clearTimeout(timeoutRef.current);
timeoutRef.current = setTimeout(() => {
setVisibleOption(undefined);
console.log(`setting visible option in timeout`);
trimbe marked this conversation as resolved.
Show resolved Hide resolved
setVisibleOption("");
setActiveAmbassador({});
}, commandTimeout);

Expand Down Expand Up @@ -139,7 +155,7 @@ export default function Overlay() {
// Handle body clicks, dismissing the overlay if the user clicks outside of it
const bodyClick = useCallback((e: MouseEvent) => {
if (!visibleUnderCursor(e)) {
setVisibleOption(undefined);
setVisibleOption("");
}
}, []);

Expand Down
6 changes: 3 additions & 3 deletions src/pages/overlay/components/overlay/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ export default function Settings(props: OverlayOptionProps) {
<Card className={className} title="Extension Settings">
<ul className={styles.settings}>
{typeSafeObjectEntries(settings).map(([key, setting]) => {
if (setting.devOnly && process.env.NODE_ENV !== "development")
if (process.env.NODE_ENV !== "development" && !setting.configurable)
return null;
trimbe marked this conversation as resolved.
Show resolved Hide resolved

return (
<li key={key}>
{setting.type === "boolean" && (
<Toggle
label={setting.title}
value={setting.value}
onChange={setting.change}
value={setting.value as boolean}
onChange={setting.change as (value: boolean) => void}
/>
)}
</li>
Expand Down
15 changes: 12 additions & 3 deletions src/pages/overlay/hooks/useSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@ import {
typeSafeObjectEntries,
typeSafeObjectFromEntries,
} from "../../../utils/helpers";
import { OverlayKey, isValidOverlayKey } from "../components/overlay/Overlay";

const settings = {
disableChatPopup: {
title: "Prevent Mod-triggered Card Popups",
type: "boolean",
process: (value: any) => !!value,
devOnly: false,
configurable: true,
},
disableOverlayHiding: {
title: "(DEV) Prevent app hiding automatically",
type: "boolean",
process: (value: any) => !!value,
devOnly: true,
configurable: false,
},
openedMenu: {
title: "Menu that was last opened",
type: "string",
process: (value: any): OverlayKey => {
return isValidOverlayKey(value) ? value : "ambassadors";
},
configurable: false,
},
};

Expand Down Expand Up @@ -78,7 +87,7 @@ export const SettingsProvider = ({ children }: { children: ReactNode }) => {
change: (value: any) => change(key, value),
},
]),
),
) as Settings,
[stored, change],
);

Expand Down
Loading