Skip to content

Commit e1e625c

Browse files
committed
Move swr-models to separate package
1 parent dc51b2c commit e1e625c

File tree

12 files changed

+41
-164
lines changed

12 files changed

+41
-164
lines changed

src/client/apps/contwatch-client/app/[lang]/APIModels.ts

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
import { Endpoint } from "@repo/utils/endpoints";
22
import { getApiEndpoint } from "@repo/utils/getApiEndpoint";
3+
import { io } from "socket.io-client";
4+
import { customMutate, SWRModelEndpoint } from "swr-models";
35

4-
import { APIModelEndpoint } from "./APIModels";
6+
import { fetchJson } from "../../src/utils";
57

6-
export const Handlers = new APIModelEndpoint({ key: getApiEndpoint(Endpoint.handlers) });
7-
export const Attributes = new APIModelEndpoint({ key: getApiEndpoint(Endpoint.attributes) });
8-
export const DataStats = new APIModelEndpoint({ key: getApiEndpoint(Endpoint.dataStats) });
8+
export const Handlers = new SWRModelEndpoint({
9+
key: getApiEndpoint(Endpoint.handlers),
10+
serverFetcher: fetchJson,
11+
});
12+
export const Attributes = new SWRModelEndpoint({
13+
key: getApiEndpoint(Endpoint.attributes),
14+
serverFetcher: fetchJson,
15+
});
16+
export const DataStats = new SWRModelEndpoint({
17+
key: getApiEndpoint(Endpoint.dataStats),
18+
serverFetcher: fetchJson,
19+
});
20+
21+
export const socket = io();
22+
23+
socket.on("mutate", (endpoint: string) => customMutate(getApiEndpoint(endpoint)));

src/client/apps/contwatch-client/app/[lang]/handlers/components/AttributeWidget/AttributeWidget.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import { bemClassNames } from "@repo/utils/bemClassNames";
1717
import { useTranslation } from "@repo/utils/useTranslation";
1818
import { useRouter } from "next/navigation";
1919
import { type FC, useState } from "react";
20+
import { useModel } from "swr-models";
2021

2122
import { Attributes, DataStats } from "../../../APIModelsDefinitions";
22-
import { useModel } from "../../../swrUtils";
2323
import styles from "./AttributeWidget.module.scss";
2424

2525
type AttributeWidgetProps = {

src/client/apps/contwatch-client/app/[lang]/handlers/components/HandlerWidget/HandlerWidget.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import { useTranslation } from "@repo/utils/useTranslation";
2727
import { DateTime } from "luxon";
2828
import Link from "next/link";
2929
import type { FC } from "react";
30+
import { useModel } from "swr-models";
3031

3132
import { Handlers } from "../../../APIModelsDefinitions";
32-
import { useModel } from "../../../swrUtils";
3333
import { AttributeWidget } from "../AttributeWidget/AttributeWidget";
3434
import styles from "./HandlerWidget.module.scss";
3535

src/client/apps/contwatch-client/app/[lang]/handlers/page.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import type { AttributeModel } from "@repo/types/AttributeModel";
22
import type { DataStatModel } from "@repo/types/DataStatModel";
3-
import type { HandlerModel } from "@repo/types/HandlerModel";
43
import type { PageParams } from "@repo/types/PageProps";
54
import { Text } from "@repo/ui/Text";
65
import { ssrTranslation } from "@repo/utils/ssrTranslation";
7-
import { SWRConfig } from "swr";
6+
import { CustomSWRConfig } from "swr-models";
87

98
import { Attributes, DataStats, Handlers } from "../APIModelsDefinitions";
109
import { HandlersWrapper } from "./components/HandlersWrapper/HandlersWrapper";
@@ -17,12 +16,6 @@ export default async function PageHandlers({ params }: PageParams) {
1716
// Fetch all handler ids
1817
const handlerIds: number[] = await Handlers.fetch();
1918

20-
// Fetch all handler objects for fallback
21-
const handlersFallback: Record<string, HandlerModel> = {};
22-
for (const handlerId of handlerIds) {
23-
handlersFallback[Handlers.endpoint({ id: handlerId })] = await Handlers.fetch({ id: handlerId });
24-
}
25-
2619
// Fetch all attribute objects for fallback
2720
const attributesFallback: Record<string, AttributeModel> = {};
2821
for (const attribute of await Attributes.fetch<AttributeModel[]>()) {
@@ -37,10 +30,10 @@ export default async function PageHandlers({ params }: PageParams) {
3730
}
3831

3932
return (
40-
<SWRConfig
33+
<CustomSWRConfig
4134
value={{
4235
fallback: {
43-
...handlersFallback,
36+
...(await Handlers.fetchFallback({ id: handlerIds })),
4437
...attributesFallback,
4538
...dataStatsFallback,
4639
},
@@ -54,6 +47,6 @@ export default async function PageHandlers({ params }: PageParams) {
5447
<HandlerWidget key={handlerId} {...{ handlerId }} />
5548
))}
5649
</HandlersWrapper>
57-
</SWRConfig>
50+
</CustomSWRConfig>
5851
);
5952
}

src/client/apps/contwatch-client/app/[lang]/inspector/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import { Text } from "@repo/ui/Text";
1111
import { useTranslation } from "@repo/utils/useTranslation";
1212
import { useSearchParams } from "next/navigation";
1313
import { useState } from "react";
14+
import { useModel } from "swr-models";
1415

1516
import { Attributes } from "../APIModelsDefinitions";
16-
import { useModel } from "../swrUtils";
1717
import { InspectorChart } from "./components/InspectorChart/InspectorChart";
1818

1919
export default function Inspector() {

src/client/apps/contwatch-client/app/[lang]/swrUtils.ts

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,10 @@
11
"use client";
2-
3-
import type { APIModel } from "@repo/types/APIModel";
42
import type { AttributeChartModel } from "@repo/types/AttributeChartModel";
53
import { getJson } from "@repo/utils/communication";
64
import { Endpoint } from "@repo/utils/endpoints";
75
import { getApiEndpoint } from "@repo/utils/getApiEndpoint";
8-
import { useEffect, useState } from "react";
9-
import useSWR, { mutate } from "swr";
10-
11-
import type { APIModelEndpoint, APIModelEndpointConfigOverride } from "./APIModels";
12-
13-
export const customSWR = <T>(endpoint: string) => {
14-
// eslint-disable-next-line react-hooks/rules-of-hooks
15-
return useSWR<T>(endpoint, getJson, { revalidateOnFocus: false });
16-
};
17-
18-
export const customMutate = mutate;
19-
20-
export const useModel = <T extends APIModel | APIModel[]>(
21-
modelInstance: APIModelEndpoint,
22-
config?: APIModelEndpointConfigOverride,
23-
) => {
24-
const { data: original } = modelInstance.use<T>(config);
25-
const [model, set] = useState<T | undefined>(original);
26-
const [refreshLock, setRefreshLock] = useState(false);
27-
const [commitLock, setCommitLock] = useState(true);
28-
29-
useEffect(() => {
30-
if (!refreshLock) {
31-
set(original);
32-
}
33-
}, [refreshLock, original]);
6+
import useSWR from "swr";
347

35-
useEffect(() => {
36-
if (!commitLock && model) {
37-
modelInstance.update(model, undefined, config);
38-
setCommitLock(true);
39-
}
40-
}, [commitLock, config, model, modelInstance]);
41-
42-
const reset = () => {
43-
set(original);
44-
};
45-
46-
const commit = () => {
47-
setCommitLock(false);
48-
};
49-
50-
const lock = () => {
51-
setRefreshLock(true);
52-
};
53-
54-
const unlock = () => {
55-
setRefreshLock(false);
56-
};
57-
58-
return { original, model, set, reset, commit, lock, unlock };
59-
};
608

619
export const useAttributeChart = (attributeIds: number[], date?: string) => {
6210
return useSWR<AttributeChartModel[]>(
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { fetchJsonFactory } from "@repo/utils/fetchJsonFactory";
1+
import { jsonFetcherFactory } from "swr-models";
22

33
import { HOST, PORT, PROTOCOL } from "./settings.mjs";
44

5-
export const fetchJson = fetchJsonFactory(HOST, PORT, PROTOCOL);
5+
export const fetchJson = jsonFetcherFactory(PROTOCOL, HOST, PORT, "force-cache", 600);

src/client/package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"lodash.debounce": "4.0.8",
1717
"next": "15.1.4",
1818
"sass": "1.83.0",
19+
"swr-models": "1.0.2",
1920
"turbo": "2.3.3",
2021
"typescript": "5.7.2"
2122
},

0 commit comments

Comments
 (0)