summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/context/file.tsx
diff options
context:
space:
mode:
authorAdam <[email protected]>2026-02-12 09:49:14 -0600
committerGitHub <[email protected]>2026-02-12 09:49:14 -0600
commitff4414bb152acfddb5c0eb073c38bedc1df4ae14 (patch)
tree78381c67d21ef6f089647f6b19e7aa2976840dbc /packages/app/src/context/file.tsx
parent56ad2db02055955f926fda0e4a89055b22ead6f9 (diff)
downloadopencode-ff4414bb152acfddb5c0eb073c38bedc1df4ae14.tar.gz
opencode-ff4414bb152acfddb5c0eb073c38bedc1df4ae14.zip
chore: refactor packages/app files (#13236)
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Frank <[email protected]>
Diffstat (limited to 'packages/app/src/context/file.tsx')
-rw-r--r--packages/app/src/context/file.tsx101
1 files changed, 57 insertions, 44 deletions
diff --git a/packages/app/src/context/file.tsx b/packages/app/src/context/file.tsx
index 88b70cd41..99c6d2e42 100644
--- a/packages/app/src/context/file.tsx
+++ b/packages/app/src/context/file.tsx
@@ -43,6 +43,12 @@ export {
touchFileContent,
}
+function errorMessage(error: unknown) {
+ if (error instanceof Error && error.message) return error.message
+ if (typeof error === "string" && error) return error
+ return "Unknown error"
+}
+
export const { use: useFile, provider: FileProvider } = createSimpleContext({
name: "File",
gate: false,
@@ -110,6 +116,45 @@ export const { use: useFile, provider: FileProvider } = createSimpleContext({
setStore("file", file, { path: file, name: getFilename(file) })
}
+ const setLoading = (file: string) => {
+ setStore(
+ "file",
+ file,
+ produce((draft) => {
+ draft.loading = true
+ draft.error = undefined
+ }),
+ )
+ }
+
+ const setLoaded = (file: string, content: FileState["content"]) => {
+ setStore(
+ "file",
+ file,
+ produce((draft) => {
+ draft.loaded = true
+ draft.loading = false
+ draft.content = content
+ }),
+ )
+ }
+
+ const setLoadError = (file: string, message: string) => {
+ setStore(
+ "file",
+ file,
+ produce((draft) => {
+ draft.loading = false
+ draft.error = message
+ }),
+ )
+ showToast({
+ variant: "error",
+ title: language.t("toast.file.loadFailed.title"),
+ description: message,
+ })
+ }
+
const load = (input: string, options?: { force?: boolean }) => {
const file = path.normalize(input)
if (!file) return Promise.resolve()
@@ -124,29 +169,14 @@ export const { use: useFile, provider: FileProvider } = createSimpleContext({
const pending = inflight.get(key)
if (pending) return pending
- setStore(
- "file",
- file,
- produce((draft) => {
- draft.loading = true
- draft.error = undefined
- }),
- )
+ setLoading(file)
const promise = sdk.client.file
.read({ path: file })
.then((x) => {
if (scope() !== directory) return
const content = x.data
- setStore(
- "file",
- file,
- produce((draft) => {
- draft.loaded = true
- draft.loading = false
- draft.content = content
- }),
- )
+ setLoaded(file, content)
if (!content) return
touchFileContent(file, approxBytes(content))
@@ -154,19 +184,7 @@ export const { use: useFile, provider: FileProvider } = createSimpleContext({
})
.catch((e) => {
if (scope() !== directory) return
- setStore(
- "file",
- file,
- produce((draft) => {
- draft.loading = false
- draft.error = e.message
- }),
- )
- showToast({
- variant: "error",
- title: language.t("toast.file.loadFailed.title"),
- description: e.message,
- })
+ setLoadError(file, errorMessage(e))
})
.finally(() => {
inflight.delete(key)
@@ -211,21 +229,16 @@ export const { use: useFile, provider: FileProvider } = createSimpleContext({
return state
}
- const scrollTop = (input: string) => view().scrollTop(path.normalize(input))
- const scrollLeft = (input: string) => view().scrollLeft(path.normalize(input))
- const selectedLines = (input: string) => view().selectedLines(path.normalize(input))
-
- const setScrollTop = (input: string, top: number) => {
- view().setScrollTop(path.normalize(input), top)
- }
-
- const setScrollLeft = (input: string, left: number) => {
- view().setScrollLeft(path.normalize(input), left)
- }
-
- const setSelectedLines = (input: string, range: SelectedLineRange | null) => {
- view().setSelectedLines(path.normalize(input), range)
+ function withPath(input: string, action: (file: string) => unknown) {
+ return action(path.normalize(input))
}
+ const scrollTop = (input: string) => withPath(input, (file) => view().scrollTop(file))
+ const scrollLeft = (input: string) => withPath(input, (file) => view().scrollLeft(file))
+ const selectedLines = (input: string) => withPath(input, (file) => view().selectedLines(file))
+ const setScrollTop = (input: string, top: number) => withPath(input, (file) => view().setScrollTop(file, top))
+ const setScrollLeft = (input: string, left: number) => withPath(input, (file) => view().setScrollLeft(file, left))
+ const setSelectedLines = (input: string, range: SelectedLineRange | null) =>
+ withPath(input, (file) => view().setSelectedLines(file, range))
onCleanup(() => {
stop()