-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnav-panel.tsx
310 lines (279 loc) · 12.2 KB
/
nav-panel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { decodeString, lookupENSName } from '@app/helpers';
import { usePlayer, useWallet } from '@app/hooks/use-game-state';
import { useSession } from '@app/hooks/use-session';
import { GlobalUnityContext } from '@app/hooks/use-unity-instance';
import { useWalletProvider } from '@app/hooks/use-wallet-provider';
import { ActionButton, TextButton } from '@app/styles/button.styles';
import { useCallback, useEffect, useMemo, useState } from 'react';
import styled from 'styled-components';
import { Dialog } from '../molecules/dialog';
import { ZoneWithBags } from '@downstream/core';
const g = globalThis as unknown as { __globalUnityContext: GlobalUnityContext };
const AccountButton = styled.button`
display: flex;
justify-content: flex-end;
align-items: center;
border: 0;
background: #050f25;
color: #fff;
padding: 0 1.6rem;
margin: 0;
border-radius: 1rem;
min-width: 62px;
> svg {
margin-left: 0.2rem;
}
> .text {
display: block;
padding-top: 5px;
margin: 0 1rem;
}
`;
const NavContainer = styled.div`
position: relative !important;
align-self: flex-start;
z-index: 99999;
display: flex;
justify-content: flex-start;
height: 5rem;
user-select: none;
margin-bottom: 1.5rem;
pointer-events: none;
gap: 1rem;
> * {
pointer-events: all;
}
`;
export const NavPanel = ({
toggleQuestsActive,
questsActive,
questsCount,
toggleWalletItemsActive,
walletItemsActive,
zone,
className,
style,
}: {
questsCount?: number;
questsActive?: boolean;
toggleQuestsActive?: () => void;
toggleWalletItemsActive?: () => void;
walletItemsActive?: boolean;
zone?: ZoneWithBags;
className?: string;
style?: React.CSSProperties;
}) => {
const { connect, disconnect: forgetProvider, provider } = useWalletProvider();
const { clearSession } = useSession();
const { wallet } = useWallet();
const player = usePlayer();
const [showAccountDialog, setShowAccountDialog] = useState(false);
const [zoneName, setZoneName] = useState(decodeString(zone?.name?.value ?? '') || '');
const [zoneDescription, setZoneDescription] = useState(zone?.description?.value || '');
const [zoneUrl, setZoneUrl] = useState(zone?.url?.value || '');
const [ensNames, setEnsNames] = useState<{ [key: string]: string | null }>({});
const hasConnection = player || wallet;
const address = player?.addr || wallet?.address || '';
useEffect(() => {
if (showAccountDialog) {
if (address) {
lookupENSName(address)
.then((n) =>
setEnsNames((prevNames) => ({
...prevNames,
[address]: n,
}))
)
.catch(console.error);
}
}
}, [address, showAccountDialog]);
const isZoneOwner = address === zone?.owner?.addr;
const closeAccountDialog = useCallback(() => {
setShowAccountDialog(false);
}, []);
const openAccountDialog = useCallback(() => {
setShowAccountDialog(true);
}, []);
const disconnect = useCallback(() => {
if (clearSession) {
clearSession();
}
if (forgetProvider) {
forgetProvider();
}
window.location.reload();
}, [clearSession, forgetProvider]);
const home = useCallback(() => {
window.location.href = '/';
}, []);
const canvasHeight = g.__globalUnityContext?.getCanvasHeight ? g.__globalUnityContext.getCanvasHeight() : -1;
const onChangeQuality = useCallback((e) => {
if (!e) {
return;
}
const newHeight = parseInt(e.target.value, 10);
if (g.__globalUnityContext?.setCanvasHeight) {
g.__globalUnityContext.setCanvasHeight(newHeight);
}
}, []);
const handleZoneNameChange = useCallback((e) => {
setZoneName(e.target.value.slice(0, 31));
}, []);
const handleZoneDescriptionChange = useCallback((e) => {
setZoneDescription(e.target.value.slice(0, 140));
}, []);
const handleZoneUrlChange = useCallback((e) => {
setZoneUrl(e.target.value.slice(0, 140));
}, []);
const applyZoneChanges = useCallback(() => {
if (!player) {
return;
}
const currentZoneName = decodeString(zone?.name?.value ?? '');
const currentZoneDescription = zone?.description?.value;
const currentZoneUrl = zone?.url?.value;
if (zoneName === currentZoneName && zoneDescription === currentZoneDescription && zoneUrl === currentZoneUrl) {
console.log("Can't apply changes, no changes detected.");
return;
}
player
.dispatch(
{ name: 'NAME_OWNED_ENTITY', args: [zone?.id, zoneName] },
{ name: 'DESCRIBE_OWNED_ENTITY', args: [zone?.id, zoneDescription] },
{ name: 'URL_OWNED_ENTITY', args: [zone?.id, zoneUrl] }
)
.catch((err) => console.error('naming failed', err));
}, [
player,
zoneName,
zone?.name?.value,
zone?.description?.value,
zone?.url?.value,
zone?.id,
zoneDescription,
zoneUrl,
]);
// TEMP: allow revealing the burner private key, this is a workaround for
// helping demo ds-cli bits for people without walletconnect
const [burnerKey, setBurnerKey] = useState<string | null>(null);
const [showBurnerKey, setShowBurnerKey] = useState<boolean>(false);
useEffect(() => {
if (!provider) {
setBurnerKey(null);
return;
}
if (provider.method !== 'burner') {
setBurnerKey(null);
return;
}
setBurnerKey(provider.provider.privateKey);
}, [provider]);
useEffect(() => {
setZoneName(decodeString(zone?.name?.value ?? '') || '');
setZoneDescription(zone?.description?.value || '');
setZoneUrl(zone?.url?.value || '');
}, [zone?.name?.value, zone?.description?.value, zone?.url?.value]);
const overrides = useMemo(() => style || {}, [style]);
return (
<NavContainer className={className} style={overrides}>
{showAccountDialog && hasConnection && (
<Dialog onClose={closeAccountDialog} width="304px" height="">
<div style={{ padding: 0 }}>
<h3>SETTINGS</h3>
<p>{ensNames[address] || `${address.slice(0, 9)}...${address.slice(-9)}`}</p>
<br />
{isZoneOwner && (
<fieldset>
<legend>Owner Controls</legend>
<div>
<strong>Zone Name:</strong>
<input type="text" value={zoneName} onChange={handleZoneNameChange} />
</div>
<br />
<div>
<strong>Zone Description:</strong>
<input type="text" value={zoneDescription} onChange={handleZoneDescriptionChange} />
</div>
<br />
<div>
<strong>Zone Image URL:</strong>
<input type="text" value={zoneUrl} onChange={handleZoneUrlChange} />
</div>
<br />
<button onClick={applyZoneChanges} style={{ width: '100%' }}>
Apply
</button>
</fieldset>
)}
<br />
<fieldset>
<legend>Quality</legend>
<select onChange={onChangeQuality} value={canvasHeight}>
<option value="480">Low (480p)</option>
<option value="720">Medium (720p)</option>
<option value="1080">High (1080p)</option>
<option value="-1">
Auto ({Math.min(window.innerHeight, window.innerHeight * window.devicePixelRatio)}p)
</option>
<option value="-2">
Native ({Math.floor(window.innerHeight * window.devicePixelRatio)}p)
</option>
</select>
</fieldset>
<br />
{burnerKey ? (
<>
<fieldset>
<legend>Private Key</legend>
{showBurnerKey ? (
<input type="text" disabled={true} value={burnerKey} />
) : (
<button style={{ width: '100%' }} onClick={() => setShowBurnerKey(true)}>
show
</button>
)}
</fieldset>
<br />
</>
) : undefined}
{zone && <ActionButton onClick={home}>Home</ActionButton>}
<br />
<ActionButton onClick={disconnect}>Disconnect</ActionButton>
</div>
</Dialog>
)}
<AccountButton onClick={player ? openAccountDialog : connect}>
{hasConnection ? (
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="30" viewBox="3 0 20 24" fill="none">
<path
d="m2.344 15.271 2 3.46a1 1 0 0 0 1.366.365l1.396-.806c.58.457 1.221.832 1.895 1.112V21a1 1 0 0 0 1 1h4a1 1 0 0 0 1-1v-1.598a8.094 8.094 0 0 0 1.895-1.112l1.396.806c.477.275 1.091.11 1.366-.365l2-3.46a1.004 1.004 0 0 0-.365-1.366l-1.372-.793a7.683 7.683 0 0 0-.002-2.224l1.372-.793c.476-.275.641-.89.365-1.366l-2-3.46a1 1 0 0 0-1.366-.365l-1.396.806A8.034 8.034 0 0 0 15 4.598V3a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v1.598A8.094 8.094 0 0 0 7.105 5.71L5.71 4.904a.999.999 0 0 0-1.366.365l-2 3.46a1.004 1.004 0 0 0 .365 1.366l1.372.793a7.683 7.683 0 0 0 0 2.224l-1.372.793c-.476.275-.641.89-.365 1.366zM12 8c2.206 0 4 1.794 4 4s-1.794 4-4 4-4-1.794-4-4 1.794-4 4-4z"
fill="#dedede"
/>
</svg>
) : (
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 500 500" fill="none">
<path
d="M461.2 128H80c-8.84 0-16-7.16-16-16s7.16-16 16-16h384c8.84 0 16-7.16 16-16 0-26.51-21.49-48-48-48H64C28.65 32 0 60.65 0 96v320c0 35.35 28.65 64 64 64h397.2c28.02 0 50.8-21.53 50.8-48V176c0-26.47-22.78-48-50.8-48zM416 336c-17.67 0-32-14.33-32-32s14.33-32 32-32 32 14.33 32 32-14.33 32-32 32z"
fill="#dedede"
/>
</svg>
)}
{!player && <span className="text">CONNECT</span>}
</AccountButton>
{address && toggleWalletItemsActive && (
<TextButton onClick={toggleWalletItemsActive} className={`${walletItemsActive ? 'toggleOn' : ''}`}>
WALLET
</TextButton>
)}
{typeof questsActive !== 'undefined' && questsCount && questsCount > 0 ? (
<TextButton onClick={toggleQuestsActive} className={`${questsActive ? 'toggleOn' : ''}`}>
QUESTS
{questsCount && questsCount > 0 ? (
<span style={{ opacity: 0.4, color: '#333' }}> ({questsCount})</span>
) : undefined}
</TextButton>
) : undefined}
</NavContainer>
);
};