Skip to content

Commit

Permalink
[feature/frontend] Add debug sections to settings panel (superserious…
Browse files Browse the repository at this point in the history
…business#2950)

* [feature/frontend] Add debug sections to settings panel

* max-width

* swagger
  • Loading branch information
tsmethurst authored and nyarla committed Jun 19, 2024
1 parent 653c58d commit 7cbe0d7
Show file tree
Hide file tree
Showing 15 changed files with 395 additions and 22 deletions.
8 changes: 8 additions & 0 deletions docs/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,10 @@ definitions:
$ref: '#/definitions/instanceV1Configuration'
contact_account:
$ref: '#/definitions/account'
debug:
description: Whether or not instance is running in DEBUG mode. Omitted if false.
type: boolean
x-go-name: Debug
description:
description: |-
Description of the instance.
Expand Down Expand Up @@ -1571,6 +1575,10 @@ definitions:
$ref: '#/definitions/instanceV2Configuration'
contact:
$ref: '#/definitions/instanceV2Contact'
debug:
description: Whether or not instance is running in DEBUG mode. Omitted if false.
type: boolean
x-go-name: Debug
description:
description: |-
Description of the instance.
Expand Down
2 changes: 2 additions & 0 deletions internal/api/model/instancev1.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type InstanceV1 struct {
//
// example: 0.1.1 cb85f65
Version string `json:"version"`
// Whether or not instance is running in DEBUG mode. Omitted if false.
Debug *bool `json:"debug,omitempty"`
// Primary language of the instance.
// example: ["en"]
Languages []string `json:"languages"`
Expand Down
2 changes: 2 additions & 0 deletions internal/api/model/instancev2.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type InstanceV2 struct {
//
// example: 0.1.1 cb85f65
Version string `json:"version"`
// Whether or not instance is running in DEBUG mode. Omitted if false.
Debug *bool `json:"debug,omitempty"`
// The URL for the source code of the software running on this instance, in keeping with AGPL license requirements.
// example: https://github.com/superseriousbusiness/gotosocial
SourceURL string `json:"source_url"`
Expand Down
9 changes: 9 additions & 0 deletions internal/typeutils/internaltofrontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"time"

"codeberg.org/gruf/go-debug"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
Expand Down Expand Up @@ -1200,6 +1201,10 @@ func (c *Converter) InstanceToAPIV1Instance(ctx context.Context, i *gtsmodel.Ins
instance.Version = toMastodonVersion(instance.Version)
}

if debug.DEBUG {
instance.Debug = util.Ptr(true)
}

// configuration
instance.Configuration.Statuses.MaxCharacters = config.GetStatusesMaxChars()
instance.Configuration.Statuses.MaxMediaAttachments = config.GetStatusesMediaMaxFiles()
Expand Down Expand Up @@ -1307,6 +1312,10 @@ func (c *Converter) InstanceToAPIV2Instance(ctx context.Context, i *gtsmodel.Ins
instance.Version = toMastodonVersion(instance.Version)
}

if debug.DEBUG {
instance.Debug = util.Ptr(true)
}

// thumbnail
thumbnail := apimodel.InstanceV2Thumbnail{}

Expand Down
43 changes: 24 additions & 19 deletions web/source/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { store, persistor } from "./redux/store";
import { Authorization } from "./components/authorization";
import Loading from "./components/loading";
import { Account } from "./lib/types/account";
import { BaseUrlContext, RoleContext } from "./lib/navigation/util";
import { BaseUrlContext, RoleContext, InstanceDebugContext } from "./lib/navigation/util";
import { SidebarMenu } from "./lib/navigation/menu";
import { Redirect, Route, Router } from "wouter";
import AdminMenu from "./views/admin/menu";
Expand All @@ -37,36 +37,41 @@ import UserRouter from "./views/user/router";
import { ErrorBoundary } from "./lib/navigation/error";
import ModerationRouter from "./views/moderation/router";
import AdminRouter from "./views/admin/router";
import { useInstanceV1Query } from "./lib/query/gts-api";

interface AppProps {
account: Account;
}

export function App({ account }: AppProps) {
const roles: string[] = useMemo(() => [ account.role.name ], [account]);
const { data: instance } = useInstanceV1Query();

return (
<RoleContext.Provider value={roles}>
<BaseUrlContext.Provider value={"/settings"}>
<SidebarMenu>
<UserMenu />
<ModerationMenu />
<AdminMenu />
</SidebarMenu>
<section className="with-sidebar">
<Router base="/settings">
<ErrorBoundary>
<UserRouter />
<ModerationRouter />
<AdminRouter />
{/*
<InstanceDebugContext.Provider value={instance?.debug ?? false}>
<BaseUrlContext.Provider value={"/settings"}>
<SidebarMenu>
<UserMenu />
<ModerationMenu />
<AdminMenu />
</SidebarMenu>
<section className="with-sidebar">
<Router base="/settings">
<ErrorBoundary>
<UserRouter />
<ModerationRouter />
<AdminRouter />
{/*
Ensure user ends up somewhere
if they just open /settings.
*/}
<Route path="/"><Redirect to="/user" /></Route>
</ErrorBoundary>
</Router>
</section>
</BaseUrlContext.Provider>
<Route path="/"><Redirect to="/user" /></Route>
</ErrorBoundary>
</Router>
</section>
</BaseUrlContext.Provider>
</InstanceDebugContext.Provider>
</RoleContext.Provider>
);
}
Expand Down
7 changes: 7 additions & 0 deletions web/source/settings/lib/navigation/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { createContext, useContext } from "react";
const RoleContext = createContext<string[]>([]);
const BaseUrlContext = createContext<string>("");
const MenuLevelContext = createContext<number>(0);
const InstanceDebugContext = createContext<boolean>(false);

function urlSafe(str: string) {
return str.toLowerCase().replace(/[\s/]+/g, "-");
Expand Down Expand Up @@ -67,6 +68,10 @@ function useMenuLevel() {
return useContext(MenuLevelContext);
}

function useInstanceDebug() {
return useContext(InstanceDebugContext);
}

export {
urlSafe,
RoleContext,
Expand All @@ -76,4 +81,6 @@ export {
useBaseUrl,
MenuLevelContext,
useMenuLevel,
InstanceDebugContext,
useInstanceDebug,
};
59 changes: 59 additions & 0 deletions web/source/settings/lib/query/admin/debug/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
GoToSocial
Copyright (C) GoToSocial Authors [email protected]
SPDX-License-Identifier: AGPL-3.0-or-later
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { ApURLResponse } from "../../../types/debug";
import { gtsApi } from "../../gts-api";

const extended = gtsApi.injectEndpoints({
endpoints: (build) => ({
ApURL: build.query<ApURLResponse, string>({
query: (url) => {
// Get the url in a SearchParam
// so we can escape it.
const urlParam = new URLSearchParams();
urlParam.set("url", url);

return {
url: `/api/v1/admin/debug/apurl?${urlParam.toString()}`,
};
}
}),
ClearCaches: build.mutation<{}, void>({
query: () => ({
method: "POST",
url: `/api/v1/admin/debug/caches/clear`
})
}),
}),
});

/**
* Lazy GET to /api/v1/admin/debug/apurl.
*/
const useLazyApURLQuery = extended.useLazyApURLQuery;

/**
* POST to /api/v1/admin/debug/caches/clear to empty in-memory caches.
*/
const useClearCachesMutation = extended.useClearCachesMutation;

export {
useLazyApURLQuery,
useClearCachesMutation,
};
26 changes: 26 additions & 0 deletions web/source/settings/lib/types/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
GoToSocial
Copyright (C) GoToSocial Authors [email protected]
SPDX-License-Identifier: AGPL-3.0-or-later
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

export interface ApURLResponse {
request_url: string;
request_headers: string;
response_headers: string;
response_code: number;
response_body: string;
}
1 change: 1 addition & 0 deletions web/source/settings/lib/types/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface InstanceV1 {
short_description_text?: string;
email: string;
version: string;
debug?: boolean;
languages: any[]; // TODO: define this
registrations: boolean;
approval_required: boolean;
Expand Down
17 changes: 17 additions & 0 deletions web/source/settings/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,23 @@ button.with-padding {
}
}

.admin-debug-apurl {
width: 100%;

.prism-highlighted {
max-width: 40rem;

/*
Normally we'd want to use a scrollbar for pre
and code, but it actually looks a bit better
to wrap here because there are many long lines.
*/
pre, code {
white-space: pre-wrap;
}
}
}

@media screen and (orientation: portrait) {
.reports .report .byline {
grid-template-columns: 1fr;
Expand Down
Loading

0 comments on commit 7cbe0d7

Please sign in to comment.