@mandujs/mcp 0.13.0 → 0.16.0
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/README.md +6 -7
- package/package.json +3 -2
- package/src/adapters/index.ts +20 -20
- package/src/adapters/monitor-adapter.ts +100 -100
- package/src/adapters/tool-adapter.ts +88 -88
- package/src/executor/error-handler.ts +250 -250
- package/src/executor/index.ts +22 -22
- package/src/executor/tool-executor.ts +148 -148
- package/src/hooks/config-watcher.ts +174 -174
- package/src/hooks/index.ts +23 -23
- package/src/hooks/mcp-hooks.ts +227 -227
- package/src/logging/index.ts +15 -15
- package/src/logging/mcp-transport.ts +134 -134
- package/src/registry/index.ts +13 -13
- package/src/registry/mcp-tool-registry.ts +298 -298
- package/src/resources/skills/guides.ts +1136 -1136
- package/src/resources/skills/index.ts +12 -12
- package/src/resources/skills/loader.ts +218 -218
- package/src/resources/skills/mandu-composition/SKILL.md +91 -91
- package/src/resources/skills/mandu-composition/metadata.json +13 -13
- package/src/resources/skills/mandu-composition/rules/_sections.md +26 -26
- package/src/resources/skills/mandu-composition/rules/_template.md +77 -77
- package/src/resources/skills/mandu-composition/rules/comp-arch-avoid-boolean-props.md +146 -146
- package/src/resources/skills/mandu-composition/rules/comp-arch-compound-components.md +164 -164
- package/src/resources/skills/mandu-composition/rules/comp-island-event.md +161 -161
- package/src/resources/skills/mandu-composition/rules/comp-island-slot-split.md +167 -167
- package/src/resources/skills/mandu-composition/rules/comp-pattern-children.md +149 -149
- package/src/resources/skills/mandu-composition/rules/comp-state-context-interface.md +148 -148
- package/src/resources/skills/mandu-composition/rules/comp-state-lift-state.md +150 -150
- package/src/resources/skills/mandu-deployment/SKILL.md +92 -92
- package/src/resources/skills/mandu-deployment/_sections.md +41 -41
- package/src/resources/skills/mandu-deployment/_template.md +38 -38
- package/src/resources/skills/mandu-deployment/metadata.json +13 -13
- package/src/resources/skills/mandu-deployment/rules/deploy-build-bun.md +109 -109
- package/src/resources/skills/mandu-deployment/rules/deploy-build-output.md +115 -115
- package/src/resources/skills/mandu-deployment/rules/deploy-cicd-github.md +219 -219
- package/src/resources/skills/mandu-deployment/rules/deploy-docker-bun.md +150 -150
- package/src/resources/skills/mandu-deployment/rules/deploy-docker-compose.md +223 -223
- package/src/resources/skills/mandu-deployment/rules/deploy-platform-fly.md +152 -152
- package/src/resources/skills/mandu-deployment/rules/deploy-platform-render.md +179 -179
- package/src/resources/skills/mandu-deployment/rules/deploy-platform-supabase.md +323 -323
- package/src/resources/skills/mandu-deployment/rules/deploy-platform-vercel.md +140 -140
- package/src/resources/skills/mandu-fs-routes/SKILL.md +82 -82
- package/src/resources/skills/mandu-fs-routes/metadata.json +12 -12
- package/src/resources/skills/mandu-fs-routes/rules/_sections.md +36 -36
- package/src/resources/skills/mandu-fs-routes/rules/_template.md +69 -69
- package/src/resources/skills/mandu-fs-routes/rules/routes-api-methods.md +65 -65
- package/src/resources/skills/mandu-fs-routes/rules/routes-dynamic-param.md +93 -93
- package/src/resources/skills/mandu-fs-routes/rules/routes-naming-page.md +55 -55
- package/src/resources/skills/mandu-guard/SKILL.md +129 -129
- package/src/resources/skills/mandu-guard/metadata.json +12 -12
- package/src/resources/skills/mandu-guard/rules/_sections.md +36 -36
- package/src/resources/skills/mandu-guard/rules/_template.md +82 -82
- package/src/resources/skills/mandu-guard/rules/guard-config-rules.md +100 -100
- package/src/resources/skills/mandu-guard/rules/guard-layer-direction.md +76 -76
- package/src/resources/skills/mandu-guard/rules/guard-preset-mandu.md +81 -81
- package/src/resources/skills/mandu-guard/rules/guard-validate-import.md +80 -80
- package/src/resources/skills/mandu-hydration/SKILL.md +91 -91
- package/src/resources/skills/mandu-hydration/metadata.json +12 -12
- package/src/resources/skills/mandu-hydration/rules/_sections.md +31 -31
- package/src/resources/skills/mandu-hydration/rules/_template.md +72 -72
- package/src/resources/skills/mandu-hydration/rules/hydration-data-event.md +109 -109
- package/src/resources/skills/mandu-hydration/rules/hydration-directive-use-client.md +55 -55
- package/src/resources/skills/mandu-hydration/rules/hydration-island-setup.md +113 -113
- package/src/resources/skills/mandu-hydration/rules/hydration-priority-visible.md +68 -68
- package/src/resources/skills/mandu-performance/SKILL.md +85 -85
- package/src/resources/skills/mandu-performance/metadata.json +14 -14
- package/src/resources/skills/mandu-performance/rules/_sections.md +31 -31
- package/src/resources/skills/mandu-performance/rules/_template.md +64 -64
- package/src/resources/skills/mandu-performance/rules/perf-async-defer-await.md +103 -103
- package/src/resources/skills/mandu-performance/rules/perf-async-parallel.md +95 -95
- package/src/resources/skills/mandu-performance/rules/perf-bun-file.md +124 -124
- package/src/resources/skills/mandu-performance/rules/perf-bun-serve.md +125 -125
- package/src/resources/skills/mandu-performance/rules/perf-bundle-imports.md +80 -80
- package/src/resources/skills/mandu-performance/rules/perf-bundle-island-lazy.md +145 -145
- package/src/resources/skills/mandu-performance/rules/perf-cache-react.md +98 -98
- package/src/resources/skills/mandu-performance/rules/perf-render-transitions.md +154 -154
- package/src/resources/skills/mandu-security/SKILL.md +87 -87
- package/src/resources/skills/mandu-security/metadata.json +13 -13
- package/src/resources/skills/mandu-security/rules/_sections.md +31 -31
- package/src/resources/skills/mandu-security/rules/_template.md +74 -74
- package/src/resources/skills/mandu-security/rules/sec-auth-guard.md +127 -127
- package/src/resources/skills/mandu-security/rules/sec-env-management.md +133 -133
- package/src/resources/skills/mandu-security/rules/sec-input-validate.md +148 -148
- package/src/resources/skills/mandu-security/rules/sec-protect-csrf.md +146 -146
- package/src/resources/skills/mandu-security/rules/sec-protect-headers.md +138 -138
- package/src/resources/skills/mandu-slot/SKILL.md +85 -85
- package/src/resources/skills/mandu-slot/metadata.json +12 -12
- package/src/resources/skills/mandu-slot/rules/_sections.md +36 -36
- package/src/resources/skills/mandu-slot/rules/_template.md +63 -63
- package/src/resources/skills/mandu-slot/rules/slot-basic-structure.md +38 -38
- package/src/resources/skills/mandu-slot/rules/slot-ctx-response.md +56 -56
- package/src/resources/skills/mandu-slot/rules/slot-guard-auth.md +59 -59
- package/src/resources/skills/mandu-slot/rules/slot-http-methods.md +64 -64
- package/src/resources/skills/mandu-styling/SKILL.md +154 -154
- package/src/resources/skills/mandu-styling/_sections.md +43 -43
- package/src/resources/skills/mandu-styling/_template.md +32 -32
- package/src/resources/skills/mandu-styling/metadata.json +15 -15
- package/src/resources/skills/mandu-styling/rules/style-component-compound.md +235 -235
- package/src/resources/skills/mandu-styling/rules/style-component-slots.md +255 -255
- package/src/resources/skills/mandu-styling/rules/style-component-tokens.md +205 -205
- package/src/resources/skills/mandu-styling/rules/style-island-animations.md +272 -272
- package/src/resources/skills/mandu-styling/rules/style-island-scoping.md +167 -167
- package/src/resources/skills/mandu-styling/rules/style-island-variants.md +221 -221
- package/src/resources/skills/mandu-styling/rules/style-perf-critical.md +209 -209
- package/src/resources/skills/mandu-styling/rules/style-perf-purge.md +192 -192
- package/src/resources/skills/mandu-styling/rules/style-setup-modules.md +162 -162
- package/src/resources/skills/mandu-styling/rules/style-setup-panda.md +164 -164
- package/src/resources/skills/mandu-styling/rules/style-setup-tailwind.md +170 -170
- package/src/resources/skills/mandu-styling/rules/style-tailwind-v4-gotchas.md +179 -179
- package/src/resources/skills/mandu-styling/rules/style-theme-darkmode.md +229 -229
- package/src/resources/skills/mandu-testing/SKILL.md +99 -99
- package/src/resources/skills/mandu-testing/metadata.json +13 -13
- package/src/resources/skills/mandu-testing/rules/_sections.md +26 -26
- package/src/resources/skills/mandu-testing/rules/_template.md +65 -65
- package/src/resources/skills/mandu-testing/rules/test-component-island.md +195 -195
- package/src/resources/skills/mandu-testing/rules/test-e2e-playwright.md +196 -196
- package/src/resources/skills/mandu-testing/rules/test-mock-fetch.md +219 -219
- package/src/resources/skills/mandu-testing/rules/test-slot-unit.md +192 -192
- package/src/resources/skills/mandu-ui/SKILL.md +117 -117
- package/src/resources/skills/mandu-ui/_sections.md +23 -23
- package/src/resources/skills/mandu-ui/_template.md +32 -32
- package/src/resources/skills/mandu-ui/metadata.json +13 -13
- package/src/resources/skills/mandu-ui/rules/ui-accessibility-aria.md +232 -232
- package/src/resources/skills/mandu-ui/rules/ui-accessibility-focus.md +238 -238
- package/src/resources/skills/mandu-ui/rules/ui-composition-patterns.md +259 -259
- package/src/resources/skills/mandu-ui/rules/ui-island-integration.md +258 -258
- package/src/resources/skills/mandu-ui/rules/ui-radix-patterns.md +213 -213
- package/src/resources/skills/mandu-ui/rules/ui-shadcn-setup.md +209 -209
- package/src/resources/skills/recipes.ts +932 -932
- package/src/tools/ate.ts +129 -0
- package/src/tools/index.ts +4 -1
- package/src/tools/project.ts +334 -334
- package/src/tools/runtime.ts +497 -497
- package/src/tools/seo.ts +417 -417
- package/src/utils/withWarnings.ts +83 -83
|
@@ -1,148 +1,148 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Define Generic Context Interface for Dependency Injection
|
|
3
|
-
impact: HIGH
|
|
4
|
-
impactDescription: Enables dependency-injectable state across use-cases
|
|
5
|
-
tags: composition, context, state, typescript, dependency-injection
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Define Generic Context Interface for Dependency Injection
|
|
9
|
-
|
|
10
|
-
**Impact: HIGH (Enables dependency-injectable state across use-cases)**
|
|
11
|
-
|
|
12
|
-
컴포넌트 context를 위한 제네릭 인터페이스를 `state`, `actions`, `meta` 세 부분으로 정의하세요. 어떤 Provider든 이 인터페이스를 구현할 수 있어, 같은 UI 컴포넌트가 완전히 다른 상태 구현과 작동할 수 있습니다.
|
|
13
|
-
|
|
14
|
-
**핵심 원칙:** Lift state, compose internals, make state dependency-injectable.
|
|
15
|
-
|
|
16
|
-
**Incorrect (특정 상태 구현에 결합):**
|
|
17
|
-
|
|
18
|
-
```tsx
|
|
19
|
-
// ❌ 특정 훅에 강하게 결합
|
|
20
|
-
function ComposerInput() {
|
|
21
|
-
const { input, setInput } = useChannelComposerState();
|
|
22
|
-
return <TextInput value={input} onChangeText={setInput} />;
|
|
23
|
-
}
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
**Correct (제네릭 인터페이스로 의존성 주입):**
|
|
27
|
-
|
|
28
|
-
```tsx
|
|
29
|
-
// ✅ 어떤 Provider든 구현할 수 있는 제네릭 인터페이스
|
|
30
|
-
interface ComposerState {
|
|
31
|
-
input: string;
|
|
32
|
-
attachments: Attachment[];
|
|
33
|
-
isSubmitting: boolean;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
interface ComposerActions {
|
|
37
|
-
update: (updater: (state: ComposerState) => ComposerState) => void;
|
|
38
|
-
submit: () => void;
|
|
39
|
-
addAttachment: (file: File) => void;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface ComposerMeta {
|
|
43
|
-
inputRef: React.RefObject<HTMLTextAreaElement>;
|
|
44
|
-
maxLength?: number;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
interface ComposerContextValue {
|
|
48
|
-
state: ComposerState;
|
|
49
|
-
actions: ComposerActions;
|
|
50
|
-
meta: ComposerMeta;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const ComposerContext = createContext<ComposerContextValue | null>(null);
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
**UI 컴포넌트는 인터페이스만 소비:**
|
|
57
|
-
|
|
58
|
-
```tsx
|
|
59
|
-
function ComposerInput() {
|
|
60
|
-
const {
|
|
61
|
-
state,
|
|
62
|
-
actions: { update },
|
|
63
|
-
meta,
|
|
64
|
-
} = use(ComposerContext)!;
|
|
65
|
-
|
|
66
|
-
// 이 컴포넌트는 인터페이스를 구현한 어떤 Provider와도 작동
|
|
67
|
-
return (
|
|
68
|
-
<textarea
|
|
69
|
-
ref={meta.inputRef}
|
|
70
|
-
value={state.input}
|
|
71
|
-
maxLength={meta.maxLength}
|
|
72
|
-
onChange={(e) => update((s) => ({ ...s, input: e.target.value }))}
|
|
73
|
-
/>
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
**서로 다른 Provider가 같은 인터페이스 구현:**
|
|
79
|
-
|
|
80
|
-
```tsx
|
|
81
|
-
// Provider A: 임시 폼을 위한 로컬 상태
|
|
82
|
-
function ForwardMessageProvider({ children }: { children: React.ReactNode }) {
|
|
83
|
-
const [state, setState] = useState(initialState);
|
|
84
|
-
const inputRef = useRef<HTMLTextAreaElement>(null);
|
|
85
|
-
const submit = useForwardMessage();
|
|
86
|
-
|
|
87
|
-
return (
|
|
88
|
-
<ComposerContext value={{
|
|
89
|
-
state,
|
|
90
|
-
actions: { update: setState, submit, addAttachment: ... },
|
|
91
|
-
meta: { inputRef },
|
|
92
|
-
}}>
|
|
93
|
-
{children}
|
|
94
|
-
</ComposerContext>
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Provider B: 채널을 위한 전역 동기화 상태
|
|
99
|
-
function ChannelProvider({ channelId, children }: Props) {
|
|
100
|
-
const { state, update, submit } = useGlobalChannel(channelId);
|
|
101
|
-
const inputRef = useRef<HTMLTextAreaElement>(null);
|
|
102
|
-
|
|
103
|
-
return (
|
|
104
|
-
<ComposerContext value={{
|
|
105
|
-
state,
|
|
106
|
-
actions: { update, submit, addAttachment: ... },
|
|
107
|
-
meta: { inputRef, maxLength: 2000 },
|
|
108
|
-
}}>
|
|
109
|
-
{children}
|
|
110
|
-
</ComposerContext>
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
**같은 UI가 두 Provider와 모두 작동:**
|
|
116
|
-
|
|
117
|
-
```tsx
|
|
118
|
-
// ForwardMessageProvider와 작동 (로컬 상태)
|
|
119
|
-
<ForwardMessageProvider>
|
|
120
|
-
<Composer.Frame>
|
|
121
|
-
<Composer.Input />
|
|
122
|
-
<Composer.Submit />
|
|
123
|
-
</Composer.Frame>
|
|
124
|
-
</ForwardMessageProvider>
|
|
125
|
-
|
|
126
|
-
// ChannelProvider와 작동 (전역 동기화 상태)
|
|
127
|
-
<ChannelProvider channelId="abc">
|
|
128
|
-
<Composer.Frame>
|
|
129
|
-
<Composer.Input />
|
|
130
|
-
<Composer.Submit />
|
|
131
|
-
</Composer.Frame>
|
|
132
|
-
</ChannelProvider>
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
## Mandu Island에서의 활용
|
|
136
|
-
|
|
137
|
-
```tsx
|
|
138
|
-
// Island 컴포넌트는 인터페이스만 알면 됨
|
|
139
|
-
// slot에서 어떤 Provider를 사용하든 같은 Island 재사용 가능
|
|
140
|
-
|
|
141
|
-
// spec/slots/forward.slot.ts → ForwardMessageProvider 사용
|
|
142
|
-
// spec/slots/channel.slot.ts → ChannelProvider 사용
|
|
143
|
-
// 둘 다 같은 Composer Island 컴포넌트 사용
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
UI는 재사용 가능한 조각들이고, 상태는 Provider가 주입합니다. Provider를 바꾸면 UI는 그대로!
|
|
147
|
-
|
|
148
|
-
Reference: [Context Interface Pattern](https://kentcdodds.com/blog/how-to-use-react-context-effectively)
|
|
1
|
+
---
|
|
2
|
+
title: Define Generic Context Interface for Dependency Injection
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: Enables dependency-injectable state across use-cases
|
|
5
|
+
tags: composition, context, state, typescript, dependency-injection
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Define Generic Context Interface for Dependency Injection
|
|
9
|
+
|
|
10
|
+
**Impact: HIGH (Enables dependency-injectable state across use-cases)**
|
|
11
|
+
|
|
12
|
+
컴포넌트 context를 위한 제네릭 인터페이스를 `state`, `actions`, `meta` 세 부분으로 정의하세요. 어떤 Provider든 이 인터페이스를 구현할 수 있어, 같은 UI 컴포넌트가 완전히 다른 상태 구현과 작동할 수 있습니다.
|
|
13
|
+
|
|
14
|
+
**핵심 원칙:** Lift state, compose internals, make state dependency-injectable.
|
|
15
|
+
|
|
16
|
+
**Incorrect (특정 상태 구현에 결합):**
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
// ❌ 특정 훅에 강하게 결합
|
|
20
|
+
function ComposerInput() {
|
|
21
|
+
const { input, setInput } = useChannelComposerState();
|
|
22
|
+
return <TextInput value={input} onChangeText={setInput} />;
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Correct (제네릭 인터페이스로 의존성 주입):**
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
// ✅ 어떤 Provider든 구현할 수 있는 제네릭 인터페이스
|
|
30
|
+
interface ComposerState {
|
|
31
|
+
input: string;
|
|
32
|
+
attachments: Attachment[];
|
|
33
|
+
isSubmitting: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface ComposerActions {
|
|
37
|
+
update: (updater: (state: ComposerState) => ComposerState) => void;
|
|
38
|
+
submit: () => void;
|
|
39
|
+
addAttachment: (file: File) => void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface ComposerMeta {
|
|
43
|
+
inputRef: React.RefObject<HTMLTextAreaElement>;
|
|
44
|
+
maxLength?: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface ComposerContextValue {
|
|
48
|
+
state: ComposerState;
|
|
49
|
+
actions: ComposerActions;
|
|
50
|
+
meta: ComposerMeta;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const ComposerContext = createContext<ComposerContextValue | null>(null);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**UI 컴포넌트는 인터페이스만 소비:**
|
|
57
|
+
|
|
58
|
+
```tsx
|
|
59
|
+
function ComposerInput() {
|
|
60
|
+
const {
|
|
61
|
+
state,
|
|
62
|
+
actions: { update },
|
|
63
|
+
meta,
|
|
64
|
+
} = use(ComposerContext)!;
|
|
65
|
+
|
|
66
|
+
// 이 컴포넌트는 인터페이스를 구현한 어떤 Provider와도 작동
|
|
67
|
+
return (
|
|
68
|
+
<textarea
|
|
69
|
+
ref={meta.inputRef}
|
|
70
|
+
value={state.input}
|
|
71
|
+
maxLength={meta.maxLength}
|
|
72
|
+
onChange={(e) => update((s) => ({ ...s, input: e.target.value }))}
|
|
73
|
+
/>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**서로 다른 Provider가 같은 인터페이스 구현:**
|
|
79
|
+
|
|
80
|
+
```tsx
|
|
81
|
+
// Provider A: 임시 폼을 위한 로컬 상태
|
|
82
|
+
function ForwardMessageProvider({ children }: { children: React.ReactNode }) {
|
|
83
|
+
const [state, setState] = useState(initialState);
|
|
84
|
+
const inputRef = useRef<HTMLTextAreaElement>(null);
|
|
85
|
+
const submit = useForwardMessage();
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<ComposerContext value={{
|
|
89
|
+
state,
|
|
90
|
+
actions: { update: setState, submit, addAttachment: ... },
|
|
91
|
+
meta: { inputRef },
|
|
92
|
+
}}>
|
|
93
|
+
{children}
|
|
94
|
+
</ComposerContext>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Provider B: 채널을 위한 전역 동기화 상태
|
|
99
|
+
function ChannelProvider({ channelId, children }: Props) {
|
|
100
|
+
const { state, update, submit } = useGlobalChannel(channelId);
|
|
101
|
+
const inputRef = useRef<HTMLTextAreaElement>(null);
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<ComposerContext value={{
|
|
105
|
+
state,
|
|
106
|
+
actions: { update, submit, addAttachment: ... },
|
|
107
|
+
meta: { inputRef, maxLength: 2000 },
|
|
108
|
+
}}>
|
|
109
|
+
{children}
|
|
110
|
+
</ComposerContext>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
**같은 UI가 두 Provider와 모두 작동:**
|
|
116
|
+
|
|
117
|
+
```tsx
|
|
118
|
+
// ForwardMessageProvider와 작동 (로컬 상태)
|
|
119
|
+
<ForwardMessageProvider>
|
|
120
|
+
<Composer.Frame>
|
|
121
|
+
<Composer.Input />
|
|
122
|
+
<Composer.Submit />
|
|
123
|
+
</Composer.Frame>
|
|
124
|
+
</ForwardMessageProvider>
|
|
125
|
+
|
|
126
|
+
// ChannelProvider와 작동 (전역 동기화 상태)
|
|
127
|
+
<ChannelProvider channelId="abc">
|
|
128
|
+
<Composer.Frame>
|
|
129
|
+
<Composer.Input />
|
|
130
|
+
<Composer.Submit />
|
|
131
|
+
</Composer.Frame>
|
|
132
|
+
</ChannelProvider>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Mandu Island에서의 활용
|
|
136
|
+
|
|
137
|
+
```tsx
|
|
138
|
+
// Island 컴포넌트는 인터페이스만 알면 됨
|
|
139
|
+
// slot에서 어떤 Provider를 사용하든 같은 Island 재사용 가능
|
|
140
|
+
|
|
141
|
+
// spec/slots/forward.slot.ts → ForwardMessageProvider 사용
|
|
142
|
+
// spec/slots/channel.slot.ts → ChannelProvider 사용
|
|
143
|
+
// 둘 다 같은 Composer Island 컴포넌트 사용
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
UI는 재사용 가능한 조각들이고, 상태는 Provider가 주입합니다. Provider를 바꾸면 UI는 그대로!
|
|
147
|
+
|
|
148
|
+
Reference: [Context Interface Pattern](https://kentcdodds.com/blog/how-to-use-react-context-effectively)
|
|
@@ -1,150 +1,150 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Lift State into Provider for Sibling Access
|
|
3
|
-
impact: HIGH
|
|
4
|
-
impactDescription: Enables state sharing without prop drilling
|
|
5
|
-
tags: composition, state, provider, lift-state
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Lift State into Provider for Sibling Access
|
|
9
|
-
|
|
10
|
-
**Impact: HIGH (Enables state sharing without prop drilling)**
|
|
11
|
-
|
|
12
|
-
형제 컴포넌트가 상태를 공유해야 할 때, 상태를 Provider로 끌어올리세요. Provider 경계 내의 모든 컴포넌트가 상태에 접근할 수 있습니다.
|
|
13
|
-
|
|
14
|
-
**Incorrect (prop drilling):**
|
|
15
|
-
|
|
16
|
-
```tsx
|
|
17
|
-
// ❌ 상태를 여러 단계로 전달
|
|
18
|
-
function ChatPage() {
|
|
19
|
-
const [message, setMessage] = useState("");
|
|
20
|
-
const [isTyping, setIsTyping] = useState(false);
|
|
21
|
-
|
|
22
|
-
return (
|
|
23
|
-
<div>
|
|
24
|
-
<MessageList
|
|
25
|
-
isTyping={isTyping}
|
|
26
|
-
typingMessage={message} // prop drilling
|
|
27
|
-
/>
|
|
28
|
-
<Composer
|
|
29
|
-
message={message}
|
|
30
|
-
setMessage={setMessage}
|
|
31
|
-
setIsTyping={setIsTyping}
|
|
32
|
-
/>
|
|
33
|
-
<TypingIndicator isTyping={isTyping} /> // prop drilling
|
|
34
|
-
</div>
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
**Correct (Provider로 끌어올리기):**
|
|
40
|
-
|
|
41
|
-
```tsx
|
|
42
|
-
// ✅ Provider가 상태 관리
|
|
43
|
-
interface ChatContextValue {
|
|
44
|
-
state: { message: string; isTyping: boolean };
|
|
45
|
-
actions: { setMessage: (msg: string) => void; setIsTyping: (val: boolean) => void };
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const ChatContext = createContext<ChatContextValue | null>(null);
|
|
49
|
-
|
|
50
|
-
function ChatProvider({ children }: { children: React.ReactNode }) {
|
|
51
|
-
const [message, setMessage] = useState("");
|
|
52
|
-
const [isTyping, setIsTyping] = useState(false);
|
|
53
|
-
|
|
54
|
-
return (
|
|
55
|
-
<ChatContext value={{
|
|
56
|
-
state: { message, isTyping },
|
|
57
|
-
actions: { setMessage, setIsTyping },
|
|
58
|
-
}}>
|
|
59
|
-
{children}
|
|
60
|
-
</ChatContext>
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// 각 컴포넌트가 필요한 것만 가져감
|
|
65
|
-
function MessageList() {
|
|
66
|
-
const { state } = use(ChatContext)!;
|
|
67
|
-
return (
|
|
68
|
-
<div>
|
|
69
|
-
{messages.map(msg => <Message key={msg.id} {...msg} />)}
|
|
70
|
-
{state.isTyping && <TypingPreview text={state.message} />}
|
|
71
|
-
</div>
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function Composer() {
|
|
76
|
-
const { state, actions } = use(ChatContext)!;
|
|
77
|
-
return (
|
|
78
|
-
<textarea
|
|
79
|
-
value={state.message}
|
|
80
|
-
onChange={(e) => {
|
|
81
|
-
actions.setMessage(e.target.value);
|
|
82
|
-
actions.setIsTyping(e.target.value.length > 0);
|
|
83
|
-
}}
|
|
84
|
-
/>
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function TypingIndicator() {
|
|
89
|
-
const { state } = use(ChatContext)!;
|
|
90
|
-
if (!state.isTyping) return null;
|
|
91
|
-
return <span>Someone is typing...</span>;
|
|
92
|
-
}
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
**사용:**
|
|
96
|
-
|
|
97
|
-
```tsx
|
|
98
|
-
function ChatPage() {
|
|
99
|
-
return (
|
|
100
|
-
<ChatProvider>
|
|
101
|
-
<div>
|
|
102
|
-
<MessageList /> {/* context에서 상태 읽음 */}
|
|
103
|
-
<Composer /> {/* context에서 상태 수정 */}
|
|
104
|
-
<TypingIndicator /> {/* context에서 상태 읽음 */}
|
|
105
|
-
</div>
|
|
106
|
-
</ChatProvider>
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
## Provider 경계 이해하기
|
|
112
|
-
|
|
113
|
-
Provider 경계 내라면 시각적 위치와 무관하게 상태 접근 가능:
|
|
114
|
-
|
|
115
|
-
```tsx
|
|
116
|
-
<ChatProvider>
|
|
117
|
-
<Dialog>
|
|
118
|
-
{/* Dialog 내부 */}
|
|
119
|
-
<Composer.Frame>
|
|
120
|
-
<Composer.Input />
|
|
121
|
-
</Composer.Frame>
|
|
122
|
-
|
|
123
|
-
{/* Frame 외부지만 Provider 내부! */}
|
|
124
|
-
<MessagePreview /> {/* ✅ context 접근 가능 */}
|
|
125
|
-
|
|
126
|
-
<DialogActions>
|
|
127
|
-
<SendButton /> {/* ✅ context 접근 가능 */}
|
|
128
|
-
</DialogActions>
|
|
129
|
-
</Dialog>
|
|
130
|
-
</ChatProvider>
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
## Mandu Island에서의 적용
|
|
134
|
-
|
|
135
|
-
```tsx
|
|
136
|
-
// app/chat/page.tsx
|
|
137
|
-
import { ChatIsland } from "./client";
|
|
138
|
-
|
|
139
|
-
export default function ChatPage({ data }) {
|
|
140
|
-
return (
|
|
141
|
-
<div>
|
|
142
|
-
<h1>Chat</h1>
|
|
143
|
-
{/* Island 내부에서 Provider로 상태 관리 */}
|
|
144
|
-
<ChatIsland initialMessages={data.messages} />
|
|
145
|
-
</div>
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
Reference: [Lifting State Up](https://react.dev/learn/sharing-state-between-components)
|
|
1
|
+
---
|
|
2
|
+
title: Lift State into Provider for Sibling Access
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: Enables state sharing without prop drilling
|
|
5
|
+
tags: composition, state, provider, lift-state
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Lift State into Provider for Sibling Access
|
|
9
|
+
|
|
10
|
+
**Impact: HIGH (Enables state sharing without prop drilling)**
|
|
11
|
+
|
|
12
|
+
형제 컴포넌트가 상태를 공유해야 할 때, 상태를 Provider로 끌어올리세요. Provider 경계 내의 모든 컴포넌트가 상태에 접근할 수 있습니다.
|
|
13
|
+
|
|
14
|
+
**Incorrect (prop drilling):**
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
// ❌ 상태를 여러 단계로 전달
|
|
18
|
+
function ChatPage() {
|
|
19
|
+
const [message, setMessage] = useState("");
|
|
20
|
+
const [isTyping, setIsTyping] = useState(false);
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<div>
|
|
24
|
+
<MessageList
|
|
25
|
+
isTyping={isTyping}
|
|
26
|
+
typingMessage={message} // prop drilling
|
|
27
|
+
/>
|
|
28
|
+
<Composer
|
|
29
|
+
message={message}
|
|
30
|
+
setMessage={setMessage}
|
|
31
|
+
setIsTyping={setIsTyping}
|
|
32
|
+
/>
|
|
33
|
+
<TypingIndicator isTyping={isTyping} /> // prop drilling
|
|
34
|
+
</div>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**Correct (Provider로 끌어올리기):**
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
// ✅ Provider가 상태 관리
|
|
43
|
+
interface ChatContextValue {
|
|
44
|
+
state: { message: string; isTyping: boolean };
|
|
45
|
+
actions: { setMessage: (msg: string) => void; setIsTyping: (val: boolean) => void };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const ChatContext = createContext<ChatContextValue | null>(null);
|
|
49
|
+
|
|
50
|
+
function ChatProvider({ children }: { children: React.ReactNode }) {
|
|
51
|
+
const [message, setMessage] = useState("");
|
|
52
|
+
const [isTyping, setIsTyping] = useState(false);
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<ChatContext value={{
|
|
56
|
+
state: { message, isTyping },
|
|
57
|
+
actions: { setMessage, setIsTyping },
|
|
58
|
+
}}>
|
|
59
|
+
{children}
|
|
60
|
+
</ChatContext>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 각 컴포넌트가 필요한 것만 가져감
|
|
65
|
+
function MessageList() {
|
|
66
|
+
const { state } = use(ChatContext)!;
|
|
67
|
+
return (
|
|
68
|
+
<div>
|
|
69
|
+
{messages.map(msg => <Message key={msg.id} {...msg} />)}
|
|
70
|
+
{state.isTyping && <TypingPreview text={state.message} />}
|
|
71
|
+
</div>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function Composer() {
|
|
76
|
+
const { state, actions } = use(ChatContext)!;
|
|
77
|
+
return (
|
|
78
|
+
<textarea
|
|
79
|
+
value={state.message}
|
|
80
|
+
onChange={(e) => {
|
|
81
|
+
actions.setMessage(e.target.value);
|
|
82
|
+
actions.setIsTyping(e.target.value.length > 0);
|
|
83
|
+
}}
|
|
84
|
+
/>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function TypingIndicator() {
|
|
89
|
+
const { state } = use(ChatContext)!;
|
|
90
|
+
if (!state.isTyping) return null;
|
|
91
|
+
return <span>Someone is typing...</span>;
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
**사용:**
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
function ChatPage() {
|
|
99
|
+
return (
|
|
100
|
+
<ChatProvider>
|
|
101
|
+
<div>
|
|
102
|
+
<MessageList /> {/* context에서 상태 읽음 */}
|
|
103
|
+
<Composer /> {/* context에서 상태 수정 */}
|
|
104
|
+
<TypingIndicator /> {/* context에서 상태 읽음 */}
|
|
105
|
+
</div>
|
|
106
|
+
</ChatProvider>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Provider 경계 이해하기
|
|
112
|
+
|
|
113
|
+
Provider 경계 내라면 시각적 위치와 무관하게 상태 접근 가능:
|
|
114
|
+
|
|
115
|
+
```tsx
|
|
116
|
+
<ChatProvider>
|
|
117
|
+
<Dialog>
|
|
118
|
+
{/* Dialog 내부 */}
|
|
119
|
+
<Composer.Frame>
|
|
120
|
+
<Composer.Input />
|
|
121
|
+
</Composer.Frame>
|
|
122
|
+
|
|
123
|
+
{/* Frame 외부지만 Provider 내부! */}
|
|
124
|
+
<MessagePreview /> {/* ✅ context 접근 가능 */}
|
|
125
|
+
|
|
126
|
+
<DialogActions>
|
|
127
|
+
<SendButton /> {/* ✅ context 접근 가능 */}
|
|
128
|
+
</DialogActions>
|
|
129
|
+
</Dialog>
|
|
130
|
+
</ChatProvider>
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Mandu Island에서의 적용
|
|
134
|
+
|
|
135
|
+
```tsx
|
|
136
|
+
// app/chat/page.tsx
|
|
137
|
+
import { ChatIsland } from "./client";
|
|
138
|
+
|
|
139
|
+
export default function ChatPage({ data }) {
|
|
140
|
+
return (
|
|
141
|
+
<div>
|
|
142
|
+
<h1>Chat</h1>
|
|
143
|
+
{/* Island 내부에서 Provider로 상태 관리 */}
|
|
144
|
+
<ChatIsland initialMessages={data.messages} />
|
|
145
|
+
</div>
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Reference: [Lifting State Up](https://react.dev/learn/sharing-state-between-components)
|