Skip to content

Commit cf2e086

Browse files
feat(desktop): Add context settings to toggle docker/git credential (loft-sh#1251)
forwarding behaviour for default context from desktop app
1 parent 6075e9b commit cf2e086

File tree

5 files changed

+303
-223
lines changed

5 files changed

+303
-223
lines changed

desktop/src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ export type TImportWorkspaceConfig = Readonly<{
206206
//#region Context
207207
export type TContextOptions = Record<TContextOptionName, TContextOption>
208208
// See pkg/config/context.go
209-
export type TContextOptionName = "AGENT_URL" | "TELEMETRY"
209+
export type TContextOptionName =
210+
| "AGENT_URL"
211+
| "TELEMETRY"
212+
| "SSH_INJECT_DOCKER_CREDENTIALS"
213+
| "SSH_INJECT_GIT_CREDENTIALS"
210214
export type TContextOption = Readonly<{
211215
name: TContextOptionName
212216
description: string | null | undefined
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { CloseIcon } from "@chakra-ui/icons"
2+
import { IconButton, Input, InputGroup, InputLeftAddon, InputRightElement } from "@chakra-ui/react"
3+
import { FocusEvent, KeyboardEvent, ReactNode, useCallback, useRef, useState } from "react"
4+
5+
type TClearableInputProps = Readonly<{
6+
defaultValue: string
7+
placeholder: string
8+
label?: ReactNode
9+
onChange: (newValue: string) => void
10+
}>
11+
export function ClearableInput({
12+
defaultValue,
13+
placeholder,
14+
label,
15+
onChange,
16+
}: TClearableInputProps) {
17+
const [hasFocus, setHasFocus] = useState(false)
18+
const inputRef = useRef<HTMLInputElement>(null)
19+
20+
const handleBlur = useCallback(
21+
(e: FocusEvent<HTMLInputElement>) => {
22+
const value = e.target.value.trim()
23+
onChange(value)
24+
setHasFocus(false)
25+
},
26+
[onChange]
27+
)
28+
29+
const handleKeyUp = useCallback((e: KeyboardEvent<HTMLInputElement>) => {
30+
if (e.key !== "Enter") return
31+
32+
e.currentTarget.blur()
33+
}, [])
34+
35+
const handleFocus = useCallback(() => {
36+
setHasFocus(true)
37+
}, [])
38+
39+
const handleClearClicked = useCallback(() => {
40+
const el = inputRef.current
41+
if (!el) return
42+
43+
el.value = ""
44+
}, [])
45+
46+
return (
47+
<InputGroup maxWidth="96">
48+
{label && <InputLeftAddon>{label}</InputLeftAddon>}
49+
<Input
50+
ref={inputRef}
51+
spellCheck={false}
52+
placeholder={placeholder}
53+
defaultValue={defaultValue}
54+
onBlur={handleBlur}
55+
onKeyUp={handleKeyUp}
56+
onFocus={handleFocus}
57+
/>
58+
<InputRightElement>
59+
<IconButton
60+
visibility={hasFocus ? "visible" : "hidden"}
61+
size="xs"
62+
borderRadius="full"
63+
icon={<CloseIcon />}
64+
aria-label="clear"
65+
onMouseDown={(e) => {
66+
// needed to prevent losing focus from input
67+
e.stopPropagation()
68+
e.preventDefault()
69+
}}
70+
onClick={handleClearClicked}
71+
/>
72+
</InputRightElement>
73+
</InputGroup>
74+
)
75+
}

desktop/src/views/Settings/Settings.tsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,19 @@ import {
3939
} from "../../lib"
4040
import { useWelcomeModal } from "../../useWelcomeModal"
4141
import {
42-
useDotfilesOption,
43-
useSSHKeySignatureOption,
44-
useCLIFlagsOption,
4542
useAgentURLOption,
43+
useDockerCredentialsForwardingOption,
44+
useGitCredentialsForwardingOption,
4645
useTelemetryOption,
47-
useExtraEnvVarsOption,
48-
useProxyOptions,
4946
} from "./useContextOptions"
5047
import { useIDESettings } from "./useIDESettings"
48+
import {
49+
useCLIFlagsOption,
50+
useDotfilesOption,
51+
useExtraEnvVarsOption,
52+
useProxyOptions,
53+
useSSHKeySignatureOption,
54+
} from "./useSettingsOptions"
5155

5256
const SETTINGS_TABS = [
5357
{ label: "General", component: <GeneralSettings /> },
@@ -159,11 +163,14 @@ function CustomizationSettings() {
159163
const { input: gitSSHSignatureInput } = useSSHKeySignatureOption()
160164
const { settings, set } = useChangeSettings()
161165
const { ides, defaultIDE, updateDefaultIDE } = useIDESettings()
166+
const { input: dockerCredentialForwardingInput, helpText: dockerCredentialForwardingHelpText } =
167+
useDockerCredentialsForwardingOption()
168+
const { input: gitCredentialForwardingInput, helpText: gitCredentialForwardingHelpText } =
169+
useGitCredentialsForwardingOption()
162170

163171
return (
164172
<>
165173
<SettingSection
166-
showDivider={true}
167174
title="IDE"
168175
description="Select the default IDE you're using for workspaces. This will be overridden whenever you create a workspace with a different IDE. You can prevent this by checking the 'Always use this IDE' checkbox">
169176
<>
@@ -185,18 +192,31 @@ function CustomizationSettings() {
185192
</Checkbox>
186193
</>
187194
</SettingSection>
195+
188196
<SettingSection
189-
showDivider={true}
190197
title="Dotfiles"
191198
description="Set the dotfiles git repository to use inside workspaces">
192199
{dotfilesInput}
193200
</SettingSection>
201+
194202
<SettingSection
195-
showDivider={false}
196203
title="SSH Key for Git commit signing"
197204
description="Set path of your SSH key you want to use for signing Git commits">
198205
{gitSSHSignatureInput}
199206
</SettingSection>
207+
208+
<SettingSection
209+
title="Docker credentials forwarding"
210+
description={dockerCredentialForwardingHelpText}>
211+
{dockerCredentialForwardingInput}
212+
</SettingSection>
213+
214+
<SettingSection
215+
showDivider={false}
216+
title="Git credentials forwarding"
217+
description={gitCredentialForwardingHelpText}>
218+
{gitCredentialForwardingInput}
219+
</SettingSection>
200220
</>
201221
)
202222
}

0 commit comments

Comments
 (0)