Persist Global Settings of a confluence app using storage api

Hello,

I m building an app with forge and I want to use the Global Settings. While I m able to persist the values using the storage api I m not able to retrieve them on page load.
Can you please help?

const onSubmit = async (formData) => {

	const options = {}
	options.headers = {
		"Authorization": formData['accessToken']
	}
	console.log(formData)
	const result = await fetch(formData['url'] + "/me", options)
	if (result.ok){
		console.log("Success")
		console.log(result)
		await storage.set("url", formData['url'])
		await storage.set("accessToken", formData['accessToken'])
	} else {
		console.error(result)
		throw Error(result.status + " - " + result.statusText)
	}
};

export const globalConfig = render(
		<GlobalSettings>
			<Form onSubmit={onSubmit}>
				<TextField name={"url"} label={"URL"} defaultValue={???}/>
				<TextField name={"accessToken"} label={"accessToken"} defaultValue={???}/>
			</Form>
		</GlobalSettings>
);

Hi @aalexandrakis ,

Take a look at the UI kit hooks documentation. You should be able to use useState to get the state. Here’s how the Forge getting started guide does it:

const [comments] = useState(async () => await fetchCommentsForContent(context.contentId));

Regards,
Dugald

1 Like

Hi @dmorrow and thanks for your reply,
unfortunately this didn’t work

const [url] = useState(async () => await storage.get("url"));
const [accessToken] = useState(async () => await storage.get("url"));

export const globalConfig = render(
		<GlobalSettings>
			<Form onSubmit={onSubmit}>
				<TextField name={"url"} label={"URL"} defaultValue={url}/>
				<TextField name={"accessToken"} label={"Access Token"} defaultValue={accessToken}/>
			</Form>
		</GlobalSettings>
);

I m getting compilation errors

Error: Error thrown in the snapshot context.
App code snapshot error: cannot use a hook outside of a component
Learn more about the snapshot context at http://go.atlassian.com/forge-snapshot-context.

Should I use something else than storage api to persist global settings values ?