Skip to content

Commit 7ad94d3

Browse files
committed
add getParentsData helper
1 parent 407c0f6 commit 7ad94d3

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

.changeset/ten-apes-rest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/v1-meta": patch
3+
---
4+
5+
Add `getParentsData` helper function

packages/v1-meta/src/index.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {
22
V2_MetaArgs as MetaArgs,
33
V2_MetaDescriptor as MetaDescriptor,
44
} from "@remix-run/react";
5-
import type { LoaderFunction } from "@remix-run/server-runtime";
5+
import type { LoaderFunction, SerializeFrom } from "@remix-run/server-runtime";
66

77
function metaV1<
88
Loader extends LoaderFunction | unknown = unknown,
@@ -90,6 +90,39 @@ function fromMetaData(metaData: V1_MetaDescriptor): MetaDescriptor[] {
9090
return meta;
9191
}
9292

93+
function getParentsData<
94+
Loader extends unknown = unknown,
95+
MatchLoaders extends Record<string, unknown> = Record<string, unknown>
96+
>(args: MetaArgs<Loader, MatchLoaders>) {
97+
let { matches } = args;
98+
return matches.reduce(
99+
(data, match) => {
100+
if (data === args.data) return data;
101+
return {
102+
...data,
103+
[match.id]: match.data,
104+
};
105+
},
106+
{} as {
107+
[K in keyof MatchLoaders]: MatchLoaders[K] extends LoaderFunction
108+
? SerializeFrom<MatchLoaders[K]>
109+
: unknown;
110+
}
111+
);
112+
}
113+
114+
export interface V2_MetaMatch<
115+
RouteId extends string = string,
116+
Loader extends LoaderFunction | unknown = unknown
117+
> {
118+
id: RouteId;
119+
pathname: string;
120+
data: Loader extends LoaderFunction ? SerializeFrom<Loader> : unknown;
121+
handle?: unknown;
122+
params: {};
123+
meta: MetaDescriptor[];
124+
}
125+
93126
function getMetaKey(metaDescriptor: MetaDescriptor) {
94127
if ("title" in metaDescriptor && metaDescriptor.title != null) {
95128
return "title";
@@ -155,5 +188,5 @@ interface V1_MetaDescriptor {
155188
| Array<Record<string, string> | string>;
156189
}
157190

158-
export { fromMetaData, metaV1, mergeMeta };
191+
export { fromMetaData, getParentsData, metaV1, mergeMeta };
159192
export type { V1_MetaDescriptor };

0 commit comments

Comments
 (0)