Skip to content

Commit fe540b7

Browse files
author
deepshekhardas
committed
fix: apply PRs #7388, #7402, #7386
- fix(cli): replace temp with native fs.mkdtempSync (#7388) - fix(react-hook-form): useFieldArray reset on initial data load (#7402) - feat(core): add ns namespace option to useTranslation/useTranslate (#7386)
1 parent 372bfc9 commit fe540b7

5 files changed

Lines changed: 58 additions & 31 deletions

File tree

‎packages/cli/package.json‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
"prettier": "^2.7.1",
6969
"semver": "7.5.2",
7070
"semver-diff": "^3.1.1",
71-
"temp": "^0.9.4",
7271
"tslib": "^2.6.2"
7372
},
7473
"devDependencies": {
@@ -90,7 +89,6 @@
9089
"@types/pluralize": "^0.0.29",
9190
"@types/prettier": "^2.7.3",
9291
"@types/semver": "^7.5.8",
93-
"@types/temp": "^0.9.1",
9492
"@vitest/ui": "^2.1.8",
9593
"tsup": "^6.7.0",
9694
"typescript": "^5.8.3",

‎packages/cli/src/commands/add/sub-commands/resource/create-resources.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import {
1616
import inquirer from "inquirer";
1717
import { join } from "path";
1818
import { plural } from "pluralize";
19-
import temp from "temp";
2019
import { getCommandRootDir } from "./get-command-root-dir";
20+
import { mkdtempSync } from "fs";
21+
import { tmpdir } from "os";
2122

2223
export const defaultActions = ["list", "create", "edit", "show"];
2324

@@ -134,9 +135,6 @@ export const createResources = async (
134135
}
135136
moveSync(tempDir, destinationResourcePath, moveSyncOptions);
136137

137-
// clear temp dir
138-
temp.cleanupSync();
139-
140138
// if use Next.js, generate page files. This makes easier to use the resource
141139
if (isNextJs) {
142140
generateNextJsPages(
@@ -182,8 +180,7 @@ export const createResources = async (
182180
};
183181

184182
const generateTempDir = (): string => {
185-
temp.track();
186-
return temp.mkdirSync("resource");
183+
return mkdtempSync(join(tmpdir(), "resource-"));
187184
};
188185

189186
/**
Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { useContext, useMemo } from "react";
22

33
import { I18nContext } from "@contexts/i18n";
4+
import type { UseTranslationProps } from "./useTranslation";
45

56
/**
67
* If you need to translate the texts in your own components, refine provides the `useTranslate` hook.
78
* It returns the translate method from `i18nProvider` under the hood.
89
*
910
* @see {@link https://refine.dev/docs/api-reference/core/hooks/translate/useTranslate} for more details.
1011
*/
11-
export const useTranslate = () => {
12+
export const useTranslate = ({ ns }: UseTranslationProps = {}) => {
1213
const { i18nProvider } = useContext(I18nContext);
1314

1415
const fn = useMemo(() => {
@@ -21,20 +22,37 @@ export const useTranslate = () => {
2122

2223
function translate(
2324
key: string,
24-
options?: string | any,
25-
defaultMessage?: string,
25+
_options?: string | any,
26+
_defaultMessage?: string,
2627
) {
28+
let options = _options;
29+
let defaultMessage = _defaultMessage;
30+
31+
if (typeof options === "string" && defaultMessage === undefined) {
32+
defaultMessage = options;
33+
34+
if (ns) {
35+
options = { ns };
36+
} else {
37+
options = undefined;
38+
}
39+
}
40+
41+
if (ns && options && typeof options !== "string") {
42+
options = { ns, ...options };
43+
}
44+
2745
return (
2846
i18nProvider?.translate(key, options, defaultMessage) ??
2947
defaultMessage ??
30-
(typeof options === "string" && typeof defaultMessage === "undefined"
31-
? options
48+
(typeof _options === "string" && typeof _defaultMessage === "undefined"
49+
? _options
3250
: key)
3351
);
3452
}
3553

3654
return translate;
37-
}, [i18nProvider]);
55+
}, [i18nProvider, ns]);
3856

3957
return fn;
4058
};

‎packages/core/src/hooks/i18n/useTranslation.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { useGetLocale } from "./useGetLocale";
22
import { useSetLocale } from "./useSetLocale";
33
import { useTranslate } from "./useTranslate";
44

5+
export interface UseTranslationProps {
6+
ns?: string | string[];
7+
}
8+
59
/**
610
* It combines `useTranslate`, `useSetLocale` and `useGetLocale` hooks for a better developer experience.
711
* It returns `i18nProvider` methods under the hood.
@@ -11,8 +15,8 @@ import { useTranslate } from "./useTranslate";
1115
*
1216
* @see {@link https://refine.dev/docs/i18n/i18n-provider/} for more details.
1317
*/
14-
export const useTranslation = () => {
15-
const translate = useTranslate();
18+
export const useTranslation = ({ ns }: UseTranslationProps = {}) => {
19+
const translate = useTranslate({ ns });
1620
const changeLocale = useSetLocale();
1721
const getLocale = useGetLocale();
1822

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
import { useTranslate } from "@refinedev/core";
2-
import { useEffect } from "react";
1+
import { useTranslate, type UseTranslationProps } from "@refinedev/core";
2+
import { useCallback, useEffect } from "react";
33

44
type Title = string | { i18nKey: string };
55

6-
export const useDocumentTitle = (title?: Title) => {
7-
const translate = useTranslate();
6+
interface useDocumentTitleOptions {
7+
ns?: UseTranslationProps["ns"];
8+
9+
defaultTitle?: string;
10+
}
11+
12+
export const useDocumentTitle = (
13+
title?: Title,
14+
options?: useDocumentTitleOptions,
15+
) => {
16+
const translate = useTranslate({ ns: options?.ns });
17+
18+
const getTitle = useCallback(
19+
(title: Title) => {
20+
const key = typeof title === "string" ? title : title.i18nKey;
21+
22+
return translate(key, options?.defaultTitle);
23+
},
24+
[translate, options?.defaultTitle],
25+
);
826

927
useEffect(() => {
1028
if (!title) return;
1129

12-
if (typeof title === "string") {
13-
document.title = translate(title);
14-
} else {
15-
document.title = translate(title.i18nKey);
16-
}
17-
}, [title]);
30+
document.title = getTitle(title);
31+
}, [title, getTitle]);
1832

1933
return (title: Title) => {
20-
if (typeof title === "string") {
21-
document.title = translate(title);
22-
} else {
23-
document.title = translate(title.i18nKey);
24-
}
34+
document.title = getTitle(title);
2535
};
2636
};

0 commit comments

Comments
 (0)