@salesforce/ui-bundle-template-app-react-template-b2e 1.117.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +82 -0
- package/README.md +52 -0
- package/dist/.forceignore +15 -0
- package/dist/.husky/pre-commit +4 -0
- package/dist/.prettierignore +11 -0
- package/dist/.prettierrc +17 -0
- package/dist/AGENT.md +193 -0
- package/dist/CHANGELOG.md +2128 -0
- package/dist/README.md +52 -0
- package/dist/config/project-scratch-def.json +13 -0
- package/dist/eslint.config.js +7 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/.forceignore +15 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/.graphqlrc.yml +2 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/.prettierignore +9 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/.prettierrc +11 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/CHANGELOG.md +10 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/README.md +35 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/codegen.yml +95 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/components.json +18 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/e2e/app.spec.ts +17 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/eslint.config.js +169 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/index.html +12 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/package.json +69 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/playwright.config.ts +24 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/reactinternalapp.uibundle-meta.xml +7 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/scripts/get-graphql-schema.mjs +68 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/scripts/rewrite-e2e-assets.mjs +23 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/api/account/accountSearchService.ts +46 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/api/account/query/distinctAccountIndustries.graphql +19 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/api/account/query/distinctAccountTypes.graphql +19 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/api/account/query/getAccountDetail.graphql +121 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/api/account/query/searchAccounts.graphql +51 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/api/graphql-operations-types.ts +11260 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/api/graphqlClient.ts +25 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/app.tsx +17 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/appLayout.tsx +85 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/assets/icons/book.svg +3 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/assets/icons/copy.svg +4 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/assets/icons/rocket.svg +3 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/assets/icons/star.svg +3 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/assets/images/codey-1.png +0 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/assets/images/codey-2.png +0 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/assets/images/codey-3.png +0 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/assets/images/vibe-codey.svg +194 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/AgentforceConversationClient.tsx +168 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/__inherit_AgentforceConversationClient.tsx +3 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/alerts/status-alert.tsx +49 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/layouts/card-layout.tsx +29 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/alert.tsx +76 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/badge.tsx +48 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/breadcrumb.tsx +109 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/button.tsx +67 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/calendar.tsx +232 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/card.tsx +103 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/checkbox.tsx +32 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/collapsible.tsx +33 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/datePicker.tsx +127 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/dialog.tsx +162 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/field.tsx +237 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/index.ts +84 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/input.tsx +19 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/label.tsx +22 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/pagination.tsx +132 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/popover.tsx +89 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/select.tsx +193 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/separator.tsx +26 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/skeleton.tsx +14 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/sonner.tsx +20 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/spinner.tsx +16 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/table.tsx +114 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/components/ui/tabs.tsx +88 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/__examples__/api/accountSearchService.ts +46 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/__examples__/api/query/distinctAccountIndustries.graphql +19 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/__examples__/api/query/distinctAccountTypes.graphql +19 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/__examples__/api/query/getAccountDetail.graphql +121 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/__examples__/api/query/searchAccounts.graphql +51 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/__examples__/pages/AccountObjectDetailPage.tsx +357 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/__examples__/pages/AccountSearch.tsx +312 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/__examples__/pages/Home.tsx +34 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/api/objectSearchService.ts +84 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/ActiveFilters.tsx +89 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/FilterContext.tsx +83 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/ObjectBreadcrumb.tsx +66 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/PaginationControls.tsx +109 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/SearchBar.tsx +41 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/SortControl.tsx +143 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/filters/BooleanFilter.tsx +78 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/filters/DateFilter.tsx +128 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/filters/DateRangeFilter.tsx +70 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/filters/FilterFieldWrapper.tsx +33 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/filters/MultiSelectFilter.tsx +97 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/filters/NumericRangeFilter.tsx +163 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/filters/SearchFilter.tsx +50 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/filters/SelectFilter.tsx +97 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/components/filters/TextFilter.tsx +91 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/hooks/useAsyncData.ts +54 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/hooks/useCachedAsyncData.ts +184 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/hooks/useDebouncedCallback.ts +34 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/hooks/useObjectSearchParams.ts +252 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/utils/debounce.ts +25 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/utils/fieldUtils.ts +29 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/utils/filterUtils.ts +395 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/features/object-search/utils/sortUtils.ts +38 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/index.ts +6 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/lib/utils.ts +6 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/navigationMenu.tsx +80 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/pages/AccountObjectDetailPage.tsx +361 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/pages/AccountSearch.tsx +305 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/pages/Home.tsx +34 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/pages/NotFound.tsx +18 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/router-utils.tsx +35 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/routes.tsx +32 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/styles/global.css +135 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/src/types/conversation.ts +33 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/tsconfig.json +42 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/tsconfig.node.json +13 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/ui-bundle.json +7 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/vite-env.d.ts +1 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/vite.config.ts +106 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/vitest-env.d.ts +2 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/vitest.config.ts +11 -0
- package/dist/force-app/main/default/uiBundles/reactinternalapp/vitest.setup.ts +1 -0
- package/dist/jest.config.js +6 -0
- package/dist/package-lock.json +9995 -0
- package/dist/package.json +40 -0
- package/dist/scripts/apex/hello.apex +10 -0
- package/dist/scripts/graphql-search.sh +191 -0
- package/dist/scripts/prepare-import-unique-fields.js +122 -0
- package/dist/scripts/setup-cli.mjs +563 -0
- package/dist/scripts/sf-project-setup.mjs +66 -0
- package/dist/scripts/soql/account.soql +6 -0
- package/dist/sfdx-project.json +12 -0
- package/package.json +40 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Select,
|
|
3
|
+
SelectContent,
|
|
4
|
+
SelectItem,
|
|
5
|
+
SelectTrigger,
|
|
6
|
+
SelectValue,
|
|
7
|
+
} from "../../../../components/ui/select";
|
|
8
|
+
import { cn } from "../../../../lib/utils";
|
|
9
|
+
import { useFilterField } from "../FilterContext";
|
|
10
|
+
import { FilterFieldWrapper } from "./FilterFieldWrapper";
|
|
11
|
+
import type { ActiveFilterValue } from "../../utils/filterUtils";
|
|
12
|
+
|
|
13
|
+
const ALL_VALUE = "__all__";
|
|
14
|
+
|
|
15
|
+
interface SelectFilterProps extends Omit<React.ComponentProps<"div">, "onChange"> {
|
|
16
|
+
field: string;
|
|
17
|
+
label: string;
|
|
18
|
+
options: Array<{ value: string; label: string }>;
|
|
19
|
+
helpText?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function SelectFilter({
|
|
23
|
+
field,
|
|
24
|
+
label,
|
|
25
|
+
options,
|
|
26
|
+
helpText,
|
|
27
|
+
className,
|
|
28
|
+
...props
|
|
29
|
+
}: SelectFilterProps) {
|
|
30
|
+
const { value, onChange } = useFilterField(field);
|
|
31
|
+
return (
|
|
32
|
+
<FilterFieldWrapper
|
|
33
|
+
label={label}
|
|
34
|
+
htmlFor={`filter-${field}`}
|
|
35
|
+
helpText={helpText}
|
|
36
|
+
className={className}
|
|
37
|
+
{...props}
|
|
38
|
+
>
|
|
39
|
+
<SelectFilterControl
|
|
40
|
+
field={field}
|
|
41
|
+
label={label}
|
|
42
|
+
options={options}
|
|
43
|
+
value={value}
|
|
44
|
+
onChange={onChange}
|
|
45
|
+
/>
|
|
46
|
+
</FilterFieldWrapper>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface SelectFilterControlProps {
|
|
51
|
+
field: string;
|
|
52
|
+
label: string;
|
|
53
|
+
options: Array<{ value: string; label: string }>;
|
|
54
|
+
value: ActiveFilterValue | undefined;
|
|
55
|
+
onChange: (value: ActiveFilterValue | undefined) => void;
|
|
56
|
+
triggerProps?: React.ComponentProps<typeof SelectTrigger>;
|
|
57
|
+
contentProps?: React.ComponentProps<typeof SelectContent>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function SelectFilterControl({
|
|
61
|
+
field,
|
|
62
|
+
label,
|
|
63
|
+
options,
|
|
64
|
+
value,
|
|
65
|
+
onChange,
|
|
66
|
+
triggerProps,
|
|
67
|
+
contentProps,
|
|
68
|
+
}: SelectFilterControlProps) {
|
|
69
|
+
return (
|
|
70
|
+
<Select
|
|
71
|
+
value={value?.value ?? ALL_VALUE}
|
|
72
|
+
onValueChange={(v) => {
|
|
73
|
+
if (v === ALL_VALUE) {
|
|
74
|
+
onChange(undefined);
|
|
75
|
+
} else {
|
|
76
|
+
onChange({ field, label, type: "picklist", value: v });
|
|
77
|
+
}
|
|
78
|
+
}}
|
|
79
|
+
>
|
|
80
|
+
<SelectTrigger
|
|
81
|
+
id={`filter-${field}`}
|
|
82
|
+
{...triggerProps}
|
|
83
|
+
className={cn("w-full", triggerProps?.className)}
|
|
84
|
+
>
|
|
85
|
+
<SelectValue placeholder={`Select ${label.toLowerCase()}`} />
|
|
86
|
+
</SelectTrigger>
|
|
87
|
+
<SelectContent {...contentProps}>
|
|
88
|
+
<SelectItem value={ALL_VALUE}>All</SelectItem>
|
|
89
|
+
{options.map((opt) => (
|
|
90
|
+
<SelectItem key={opt.value} value={opt.value}>
|
|
91
|
+
{opt.label}
|
|
92
|
+
</SelectItem>
|
|
93
|
+
))}
|
|
94
|
+
</SelectContent>
|
|
95
|
+
</Select>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { Input } from "../../../../components/ui/input";
|
|
3
|
+
import { cn } from "../../../../lib/utils";
|
|
4
|
+
import { useFilterField } from "../FilterContext";
|
|
5
|
+
import { FilterFieldWrapper } from "./FilterFieldWrapper";
|
|
6
|
+
import { useDebouncedCallback } from "../../hooks/useDebouncedCallback";
|
|
7
|
+
import type { ActiveFilterValue } from "../../utils/filterUtils";
|
|
8
|
+
|
|
9
|
+
interface TextFilterProps extends Omit<React.ComponentProps<"div">, "onChange"> {
|
|
10
|
+
field: string;
|
|
11
|
+
label: string;
|
|
12
|
+
placeholder?: string;
|
|
13
|
+
helpText?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function TextFilter({
|
|
17
|
+
field,
|
|
18
|
+
label,
|
|
19
|
+
placeholder,
|
|
20
|
+
helpText,
|
|
21
|
+
className,
|
|
22
|
+
...props
|
|
23
|
+
}: TextFilterProps) {
|
|
24
|
+
const { value, onChange } = useFilterField(field);
|
|
25
|
+
return (
|
|
26
|
+
<FilterFieldWrapper
|
|
27
|
+
label={label}
|
|
28
|
+
htmlFor={`filter-${field}`}
|
|
29
|
+
helpText={helpText}
|
|
30
|
+
className={className}
|
|
31
|
+
{...props}
|
|
32
|
+
>
|
|
33
|
+
<TextFilterInput
|
|
34
|
+
field={field}
|
|
35
|
+
label={label}
|
|
36
|
+
placeholder={placeholder}
|
|
37
|
+
value={value}
|
|
38
|
+
onChange={onChange}
|
|
39
|
+
/>
|
|
40
|
+
</FilterFieldWrapper>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface TextFilterInputProps extends Omit<
|
|
45
|
+
React.ComponentProps<typeof Input>,
|
|
46
|
+
"onChange" | "value"
|
|
47
|
+
> {
|
|
48
|
+
field: string;
|
|
49
|
+
label: string;
|
|
50
|
+
value: ActiveFilterValue | undefined;
|
|
51
|
+
onChange: (value: ActiveFilterValue | undefined) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function TextFilterInput({
|
|
55
|
+
field,
|
|
56
|
+
label,
|
|
57
|
+
value,
|
|
58
|
+
onChange,
|
|
59
|
+
className,
|
|
60
|
+
...props
|
|
61
|
+
}: TextFilterInputProps) {
|
|
62
|
+
const [localValue, setLocalValue] = useState(value?.value ?? "");
|
|
63
|
+
|
|
64
|
+
const externalValue = value?.value ?? "";
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
setLocalValue(externalValue);
|
|
67
|
+
}, [externalValue]);
|
|
68
|
+
|
|
69
|
+
const debouncedOnChange = useDebouncedCallback((v: string) => {
|
|
70
|
+
if (v) {
|
|
71
|
+
onChange({ field, label, type: "text", value: v });
|
|
72
|
+
} else {
|
|
73
|
+
onChange(undefined);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<Input
|
|
79
|
+
id={`filter-${field}`}
|
|
80
|
+
type="text"
|
|
81
|
+
placeholder={props.placeholder ?? `Filter by ${label.toLowerCase()}...`}
|
|
82
|
+
value={localValue}
|
|
83
|
+
onChange={(e) => {
|
|
84
|
+
setLocalValue(e.target.value);
|
|
85
|
+
debouncedOnChange(e.target.value);
|
|
86
|
+
}}
|
|
87
|
+
className={cn(className)}
|
|
88
|
+
{...props}
|
|
89
|
+
/>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
interface UseAsyncDataResult<T> {
|
|
4
|
+
data: T | null;
|
|
5
|
+
loading: boolean;
|
|
6
|
+
error: string | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Runs an async fetcher on mount and whenever `deps` change.
|
|
11
|
+
* Returns the loading/error/data state. Does not cache — every call
|
|
12
|
+
* to the fetcher hits the source directly.
|
|
13
|
+
*
|
|
14
|
+
* A cleanup flag prevents state updates if the component unmounts
|
|
15
|
+
* or deps change before the fetch completes (avoids React warnings
|
|
16
|
+
* and stale updates from out-of-order responses).
|
|
17
|
+
*/
|
|
18
|
+
export function useAsyncData<T>(
|
|
19
|
+
fetcher: () => Promise<T>,
|
|
20
|
+
deps: React.DependencyList,
|
|
21
|
+
): UseAsyncDataResult<T> {
|
|
22
|
+
const [data, setData] = useState<T | null>(null);
|
|
23
|
+
const [loading, setLoading] = useState(true);
|
|
24
|
+
const [error, setError] = useState<string | null>(null);
|
|
25
|
+
|
|
26
|
+
// Re-create the fetcher reference only when deps change.
|
|
27
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps --- deps are explicitly managed by the caller
|
|
28
|
+
const memoizedFetcher = useCallback(fetcher, deps);
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
// Guard against setting state after unmount or dep change.
|
|
32
|
+
let cancelled = false;
|
|
33
|
+
setLoading(true);
|
|
34
|
+
setError(null);
|
|
35
|
+
|
|
36
|
+
memoizedFetcher()
|
|
37
|
+
.then((result) => {
|
|
38
|
+
if (!cancelled) setData(result);
|
|
39
|
+
})
|
|
40
|
+
.catch((err) => {
|
|
41
|
+
console.error(err);
|
|
42
|
+
if (!cancelled) setError(err instanceof Error ? err.message : "An error occurred");
|
|
43
|
+
})
|
|
44
|
+
.finally(() => {
|
|
45
|
+
if (!cancelled) setLoading(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return () => {
|
|
49
|
+
cancelled = true;
|
|
50
|
+
};
|
|
51
|
+
}, [memoizedFetcher]);
|
|
52
|
+
|
|
53
|
+
return { data, loading, error };
|
|
54
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
interface UseAsyncDataResult<T> {
|
|
4
|
+
data: T | null;
|
|
5
|
+
loading: boolean;
|
|
6
|
+
error: string | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface CacheOptions {
|
|
10
|
+
/** Unique cache key. Used for lookups and invalidation via `clearCacheEntry`. */
|
|
11
|
+
key: string;
|
|
12
|
+
/** Time-to-live in ms. Default: 30_000 (30s) */
|
|
13
|
+
ttl?: number;
|
|
14
|
+
/** Max entries in the cache. Default: 50. Evicts oldest entry when exceeded. */
|
|
15
|
+
maxSize?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface CacheEntry {
|
|
19
|
+
data: unknown;
|
|
20
|
+
timestamp: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Module-level cache shared across all useCachedAsyncData consumers.
|
|
25
|
+
* Cleared automatically on page reload since it only lives in memory.
|
|
26
|
+
* Keys are caller-provided so different hook call-sites get independent entries.
|
|
27
|
+
*/
|
|
28
|
+
const cache = new Map<string, CacheEntry>();
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns a cached entry if it exists and hasn't exceeded its TTL.
|
|
32
|
+
* Expired entries are deleted lazily here rather than on a timer,
|
|
33
|
+
* so there's no background cleanup overhead.
|
|
34
|
+
*/
|
|
35
|
+
function getValidEntry(key: string, ttl: number): CacheEntry | undefined {
|
|
36
|
+
const entry = cache.get(key);
|
|
37
|
+
if (!entry) return undefined;
|
|
38
|
+
if (Date.now() - entry.timestamp > ttl) {
|
|
39
|
+
cache.delete(key);
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
return entry;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Stores a result in the cache. If the cache is at capacity and the key
|
|
47
|
+
* is new, the oldest entry (first in Map insertion order — FIFO) is evicted.
|
|
48
|
+
*/
|
|
49
|
+
function setEntry(key: string, data: unknown, maxSize: number): void {
|
|
50
|
+
if (!cache.has(key) && cache.size >= maxSize) {
|
|
51
|
+
const firstKey = cache.keys().next().value;
|
|
52
|
+
if (firstKey !== undefined) cache.delete(firstKey);
|
|
53
|
+
}
|
|
54
|
+
cache.set(key, { data, timestamp: Date.now() });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Removes a single cache entry by key. The key must match the
|
|
59
|
+
* `options.key` passed to `useCachedAsyncData`.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* // Hook:
|
|
63
|
+
* useCachedAsyncData(() => fetchAccountDetail(id), [id], { key: `account:${id}` });
|
|
64
|
+
*
|
|
65
|
+
* // Invalidate after a mutation:
|
|
66
|
+
* await updateAccount(id, fields);
|
|
67
|
+
* clearCacheEntry(`account:${id}`);
|
|
68
|
+
*/
|
|
69
|
+
export function clearCacheEntry(key: string): void {
|
|
70
|
+
cache.delete(key);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Removes all cache entries. Useful for global invalidation scenarios
|
|
75
|
+
* like user logout or after a bulk operation.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* async function handleLogout() {
|
|
79
|
+
* await logout();
|
|
80
|
+
* clearCache();
|
|
81
|
+
* navigate("/login");
|
|
82
|
+
* }
|
|
83
|
+
*/
|
|
84
|
+
export function clearCache(): void {
|
|
85
|
+
cache.clear();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Async data hook with in-memory caching. Works like `useAsyncData` but
|
|
90
|
+
* avoids redundant network calls when the same data is requested again
|
|
91
|
+
* (e.g. navigating away from a page and back).
|
|
92
|
+
*
|
|
93
|
+
* Cache behaviour:
|
|
94
|
+
* - **Key**: Provided via `options.key`. Must be unique per logical data source.
|
|
95
|
+
* Use the same key with `clearCacheEntry` to invalidate.
|
|
96
|
+
* - **Hit**: Data is returned synchronously on the initial render —
|
|
97
|
+
* `loading` starts as `false`, so there's no flash of a loading state.
|
|
98
|
+
* - **Miss**: The fetcher runs, and the result is stored for future hits.
|
|
99
|
+
* - **TTL**: Entries expire after `options.ttl` ms (default 30 s).
|
|
100
|
+
* Expiry is checked lazily on read, not on a timer.
|
|
101
|
+
* - **Max size**: Oldest entries are evicted FIFO when the cache exceeds
|
|
102
|
+
* `options.maxSize` (default 50).
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* // Cache picklist options for 5 minutes (data rarely changes)
|
|
106
|
+
* const { data: types } = useCachedAsyncData(fetchDistinctTypes, [], {
|
|
107
|
+
* key: "distinctTypes",
|
|
108
|
+
* ttl: 300_000,
|
|
109
|
+
* });
|
|
110
|
+
*
|
|
111
|
+
* // Cache search results with default 30 s TTL (back-nav returns instantly)
|
|
112
|
+
* const { data } = useCachedAsyncData(
|
|
113
|
+
* () => searchAccounts({ where, orderBy, first, after }),
|
|
114
|
+
* [where, orderBy, first, after],
|
|
115
|
+
* { key: `accounts:${JSON.stringify({ where, orderBy, first, after })}` },
|
|
116
|
+
* );
|
|
117
|
+
*
|
|
118
|
+
* // Invalidate a specific entry after a mutation
|
|
119
|
+
* await updateAccount(id, fields);
|
|
120
|
+
* clearCacheEntry(`account:${id}`);
|
|
121
|
+
*
|
|
122
|
+
* // Or clear everything (e.g. on logout)
|
|
123
|
+
* clearCache();
|
|
124
|
+
*/
|
|
125
|
+
export function useCachedAsyncData<T>(
|
|
126
|
+
fetcher: () => Promise<T>,
|
|
127
|
+
deps: React.DependencyList,
|
|
128
|
+
options: CacheOptions,
|
|
129
|
+
): UseAsyncDataResult<T> {
|
|
130
|
+
const ttl = options.ttl ?? 30_000;
|
|
131
|
+
const maxSize = options.maxSize ?? 50;
|
|
132
|
+
const cacheKey = options.key;
|
|
133
|
+
|
|
134
|
+
// Synchronous cache check during state initialization so a cache hit
|
|
135
|
+
// never triggers a loading → loaded transition (avoids UI flicker).
|
|
136
|
+
const cached = getValidEntry(cacheKey, ttl);
|
|
137
|
+
|
|
138
|
+
const [data, setData] = useState<T | null>((cached?.data as T) ?? null);
|
|
139
|
+
const [loading, setLoading] = useState(!cached);
|
|
140
|
+
const [error, setError] = useState<string | null>(null);
|
|
141
|
+
|
|
142
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps --- deps are explicitly managed by the caller
|
|
143
|
+
const memoizedFetcher = useCallback(fetcher, deps);
|
|
144
|
+
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
// Re-check the cache inside the effect because deps may have changed
|
|
147
|
+
// since the initial render (e.g. StrictMode double-invoke).
|
|
148
|
+
const entry = getValidEntry(cacheKey, ttl);
|
|
149
|
+
if (entry) {
|
|
150
|
+
setData(entry.data as T);
|
|
151
|
+
setLoading(false);
|
|
152
|
+
setError(null);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// No cache hit — fetch from the network.
|
|
157
|
+
let cancelled = false;
|
|
158
|
+
setLoading(true);
|
|
159
|
+
setError(null);
|
|
160
|
+
|
|
161
|
+
memoizedFetcher()
|
|
162
|
+
.then((result) => {
|
|
163
|
+
if (!cancelled) {
|
|
164
|
+
setEntry(cacheKey, result, maxSize);
|
|
165
|
+
setData(result);
|
|
166
|
+
}
|
|
167
|
+
})
|
|
168
|
+
.catch((err) => {
|
|
169
|
+
console.error(err);
|
|
170
|
+
if (!cancelled) setError(err instanceof Error ? err.message : "An error occurred");
|
|
171
|
+
})
|
|
172
|
+
.finally(() => {
|
|
173
|
+
if (!cancelled) setLoading(false);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// Cleanup: if deps change or the component unmounts before the fetch
|
|
177
|
+
// completes, the cancelled flag prevents stale state updates.
|
|
178
|
+
return () => {
|
|
179
|
+
cancelled = true;
|
|
180
|
+
};
|
|
181
|
+
}, [memoizedFetcher, cacheKey, ttl, maxSize]);
|
|
182
|
+
|
|
183
|
+
return { data, loading, error };
|
|
184
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
2
|
+
import { debounce, FILTER_DEBOUNCE_MS } from "../utils/debounce";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Returns a stable debounced wrapper around the provided callback.
|
|
6
|
+
*
|
|
7
|
+
* The wrapper always invokes the *latest* version of `fn` (via a ref),
|
|
8
|
+
* so the debounce timer is never reset when `fn` changes — only when
|
|
9
|
+
* the caller invokes the returned function again.
|
|
10
|
+
*
|
|
11
|
+
* @param fn - The callback to debounce.
|
|
12
|
+
* @param delay - Debounce delay in ms. Defaults to `FILTER_DEBOUNCE_MS`.
|
|
13
|
+
*/
|
|
14
|
+
export function useDebouncedCallback<T extends (...args: any[]) => void>(
|
|
15
|
+
fn: T,
|
|
16
|
+
delay: number = FILTER_DEBOUNCE_MS,
|
|
17
|
+
): (...args: Parameters<T>) => void {
|
|
18
|
+
const fnRef = useRef(fn);
|
|
19
|
+
const debouncedRef = useRef<((...args: any[]) => void) | null>(null);
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
fnRef.current = fn;
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
debouncedRef.current = debounce((...args: any[]) => {
|
|
27
|
+
fnRef.current(...(args as Parameters<T>));
|
|
28
|
+
}, delay);
|
|
29
|
+
}, [delay]);
|
|
30
|
+
|
|
31
|
+
return useCallback((...args: Parameters<T>) => {
|
|
32
|
+
debouncedRef.current?.(...args);
|
|
33
|
+
}, []);
|
|
34
|
+
}
|