forked from tangled.org/core
Monorepo for Tangled — https://tangled.org

appview: pages: make allow-pull update as oob-swap

Signed-off-by: Anirudh Oppiliappan <x@icyphox.sh>

Changed files
+49 -25
appview
pages
state
-1
appview/pages/pages.go
···
Tags []*types.TagReference
Base string
Head string
-
AllowPull bool
Active string
}
+4 -11
appview/pages/templates/repo/compare.html
···
{{ end }}
{{ define "repoContent" }}
-
<section>
+
<section id="compare-select">
<h2 class="font-bold text-sm mb-4 uppercase dark:text-white">
Compare changes
</h2>
···
if (baseToUse && headToUse) {
const url = `/{{ .RepoInfo.FullName }}/compare/diff/${baseToUse}/${headToUse}`;
-
htmx.ajax('GET', url, { target: '#compare-diff' });
-
document.title = `comparing ${baseToUse} and ${headToUse}`;
-
const allowPull = `{{ .AllowPull }}`
-
if (allowPull) {
-
htmx.ajax('GET',
-
`/{{ .RepoInfo.FullName }}/compare/allow-pull/${baseToUse}/${headToUse}`,
-
{ target: '#allow-pull'},
-
)
-
}
+
// htmx.ajax('GET', url, { target: '#compare-diff' })
+
document.title = `comparing ${baseToUse} and ${headToUse}`;
}
}
</script>
-
<section id="allow-pull" class="pt-4"></section>
{{ end }}
{{ define "repoAfter" }}
+
<div id="allow-pull"></div>
<div id="compare-diff"></div>
{{ end }}
+5 -1
appview/pages/templates/repo/fragments/compareAllowPull.html
···
{{ define "repo/fragments/compareAllowPull" }}
-
<div class="flex items-baseline justify-normal gap-4">
+
<div
+
class="flex items-baseline justify-normal gap-4"
+
id="allow-pull"
+
hx-oob-swap="true"
+
>
<p>
This comparison can be turned into a pull request to be reviewed and
discussed.
+10 -1
appview/pages/templates/repo/index.html
···
{{ end }}
</optgroup>
</select>
+
<div class="flex items-center gap-2">
{{ $isOwner := and .LoggedInUser .RepoInfo.Roles.IsOwner }}
{{ $isCollaborator := and .LoggedInUser .RepoInfo.Roles.IsCollaborator }}
{{ if and (or $isOwner $isCollaborator) .ForkInfo .ForkInfo.IsFork }}
···
<span>sync</span>
</button>
{{ end }}
-
</div>
+
<a
+
href="/{{ .RepoInfo.FullName }}/compare"
+
class="btn flex items-center gap-2 no-underline hover:no-underline"
+
title="Compare branches or tags"
+
>
+
{{ i "git-compare" "w-4 h-4" }}
+
</a>
+
</div>
+
</div>
{{ end }}
{{ define "fileTree" }}
+30 -11
appview/state/repo.go
···
-
var allowPull bool = false
-
if user != nil {
-
if slices.ContainsFunc(branches.Branches, func(branch types.Branch) bool {
-
return branch.Name == head || branch.Name == base
-
}) {
-
allowPull = true
-
}
-
}
+
repoinfo := f.RepoInfo(s, user)
s.pages.RepoCompare(w, pages.RepoCompareParams{
LoggedInUser: user,
-
RepoInfo: f.RepoInfo(s, user),
+
RepoInfo: repoinfo,
Forks: forks,
Branches: branches.Branches,
Tags: tags.Tags,
Base: base,
Head: head,
-
AllowPull: allowPull,
})
···
diff := patchutil.AsNiceDiff(formatPatch.Patch, base)
+
branches, err := us.Branches(f.OwnerDid(), f.RepoName)
+
if err != nil {
+
s.pages.Notice(w, "compare-error", "Failed to produce comparison. Try again later.")
+
log.Println("failed to fetch branches", err)
+
return
+
}
+
+
repoinfo := f.RepoInfo(s, user)
+
w.Header().Add("Hx-Push-Url", fmt.Sprintf("/%s/compare/%s...%s", f.OwnerSlashRepo(), base, head))
+
w.Header().Add("Content-Type", "text/html")
s.pages.RepoCompareDiff(w, pages.RepoCompareDiffParams{
LoggedInUser: user,
-
RepoInfo: f.RepoInfo(s, user),
+
RepoInfo: repoinfo,
Diff: diff,
})
+
+
// checks if pull is allowed and performs an htmx oob-swap
+
// by writing to the same http.ResponseWriter
+
if user != nil {
+
if slices.ContainsFunc(branches.Branches, func(branch types.Branch) bool {
+
return branch.Name == head || branch.Name == base
+
}) {
+
if repoinfo.Roles.IsPushAllowed() {
+
s.pages.RepoCompareAllowPullFragment(w, pages.RepoCompareAllowPullParams{
+
LoggedInUser: user,
+
RepoInfo: repoinfo,
+
Base: base,
+
Head: head,
+
})
+
}
+
}
+
}