Skip to content

Commit 3cc45d6

Browse files
authored
feat: added different document titles if the app is installed (#5)
1 parent d181908 commit 3cc45d6

File tree

9 files changed

+41
-6
lines changed

9 files changed

+41
-6
lines changed

src/components/Home.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Text } from "@mantine/core";
2-
import { useDocumentTitle } from "@mantine/hooks";
2+
3+
import { useCustomDocumentTitle } from "../hooks";
34

45
export default function Home() {
5-
useDocumentTitle("RMG Utils for Stellaris - Home");
6+
useCustomDocumentTitle("Home");
67
return (
78
<Text align="center" size="xl" weight={700}>
89
Welcome to RMG's utils for Stellaris. Use the tabs above to navigate.

src/components/LightLocatorsGeneratorTab.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import {
77
Text,
88
Textarea,
99
} from "@mantine/core";
10-
import { useDocumentTitle, useInputState } from "@mantine/hooks";
10+
import { useInputState } from "@mantine/hooks";
1111
import * as Sentry from "@sentry/browser";
1212
import { NetlifyFunctions } from "../constants";
1313
import { makeLocators } from "../utils/factories";
1414
import { isDNTEnabled, netlifyFunctionInvoke } from "../utils/general";
15+
import { useCustomDocumentTitle } from "../hooks";
1516

1617
const gridColums = 12;
1718

1819
export default function LightLocatorsGeneratorTab() {
1920
const [locators, setLocators] = useInputState<number | "">(10);
2021
const [stateTime, setStateTime] = useInputState<number | "">(5);
2122
const [generatedLocators, setGeneratedLocators] = useInputState("");
22-
useDocumentTitle("RMG Utils for Stellaris - Light Locators Generator");
23+
useCustomDocumentTitle("Light Locators Generator");
2324

2425
const onGenerateClick = async () => {
2526
const result = makeLocators(Number(locators || 0), Number(stateTime || 0));

src/components/TraitsBuilderTab/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import ColorPickerElement from "../shared/ColorPickerElement";
1010
import { Dropzone, IMAGE_MIME_TYPE } from "@mantine/dropzone";
1111
import ImagePreview from "../shared/ImagePreview";
12-
import { useDocumentTitle } from "@mantine/hooks";
1312
import DownloadButton from "../shared/DownloadButton";
1413
import { Stage as StageType } from "konva/lib/Stage";
1514
import TraitIconControls from "./TraitIconControls";
@@ -20,6 +19,7 @@ import {
2019
traitsBuilderReducer,
2120
} from "./traits-builder.utils";
2221
import { State } from "./index.d";
22+
import { useCustomDocumentTitle } from "../../hooks";
2323

2424
const TRAIT_WIDTH = 29;
2525

@@ -37,7 +37,7 @@ const INITIAL_STATE: State = {
3737
};
3838

3939
export default function TraitsBuilderTab() {
40-
useDocumentTitle("RMG Utils for Stellaris - Traits Builder");
40+
useCustomDocumentTitle("Traits Builder");
4141
const [state, dispatch] = useReducer(traitsBuilderReducer, INITIAL_STATE);
4242
const mainStageRef = useRef<StageType>(null);
4343

src/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ export enum NetlifyFunctions {
2525
SAVE_LOCATORS_INTERACTIONS = "save_locators_interaction",
2626
SAVE_TRAITS_INTERACTIONS = "save_traits_interaction",
2727
}
28+
29+
export const APP_TITLE = "RMG Utils for Stellaris";

src/hooks/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { useIsInstalled } from "./useIsInstalled";
2+
export { useCustomDocumentTitle } from "./useCustomDocumentTitle";

src/hooks/useCustomDocumentTitle.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useDocumentTitle } from "@mantine/hooks";
2+
3+
import { APP_TITLE } from "../constants";
4+
import { useIsInstalled } from "./useIsInstalled";
5+
6+
/**
7+
* A custom hook that sets the document title with a given title string.
8+
* If the app is not installed, the app title is prepended to the given title.
9+
* @param title - The title string to set as the document title.
10+
*/
11+
export const useCustomDocumentTitle = (title: string) => {
12+
const isInstalled = useIsInstalled();
13+
const prefix = isInstalled ? "" : `${APP_TITLE} - `;
14+
useDocumentTitle(`${prefix}${title}`);
15+
};

src/hooks/useIsInstalled.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Returns a boolean indicating whether the web app is installed on the user's device.
3+
*
4+
* @returns {boolean} A boolean indicating whether the web app is installed on the user's device.
5+
*/
6+
export const useIsInstalled = () => {
7+
const isInstalled = window.matchMedia("(display-mode: standalone)").matches;
8+
9+
return isInstalled;
10+
};

src/static_pages/OtherTools.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Anchor, Container, List, Text, Title } from "@mantine/core";
2+
import { useCustomDocumentTitle } from "../hooks";
23

34
export default function OtherTools() {
5+
useCustomDocumentTitle("Other Tools");
46
return (
57
<Container>
68
<Title order={3} my="sm">

src/static_pages/Privacy.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Title,
99
} from "@mantine/core";
1010
import { dateFormatter, isDNTEnabled } from "../utils/general";
11+
import { useCustomDocumentTitle } from "../hooks";
1112

1213
const PRIVACY_POLICY_LAST_UPDATE = new Date("2022-09-03");
1314
const CONTACT_EMAIL = "[email protected]";
@@ -16,6 +17,7 @@ const DNTstatus = isDNTEnabled() ? "ENABLED" : "DISABLED";
1617
const DNTstatusColor = isDNTEnabled() ? "green" : "red";
1718

1819
export default function Privacy() {
20+
useCustomDocumentTitle("Privacy Policy");
1921
return (
2022
<Container>
2123
<Title

0 commit comments

Comments
 (0)